Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / Orcas / QFE / wpf / src / Framework / MS / Internal / Globalization / BamlResourceContent.cs / 1 / BamlResourceContent.cs
//----------------------------------------------------------------------------
//
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//
// Description: Utility that handles parsing Baml Resource Content
//
// History: 11/29/2003 garyyang Rewrote
//
//---------------------------------------------------------------------------
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Diagnostics;
using System.Collections.Generic;
using System.Windows;
namespace MS.Internal.Globalization
{
internal static class BamlResourceContentUtil
{
//-------------------------------------
// Internal methods
//-------------------------------------
///
/// Escape a string
///
internal static string EscapeString(string content)
{
if (content == null) return null;
StringBuilder builder = new StringBuilder();
for (int i = 0; i < content.Length; i++)
{
switch (content[i])
{
case BamlConst.ChildStart :
case BamlConst.ChildEnd :
case BamlConst.EscapeChar :
{
builder.Append(BamlConst.EscapeChar);
builder.Append(content[i]);
break;
}
case '&' :
{
builder.Append("&");
break;
}
case '<' :
{
builder.Append("<");
break;
}
case '>' :
{
builder.Append(">");
break;
}
case '\'':
{
builder.Append("'");
break;
}
case '\"':
{
builder.Append(""");
break;
}
default :
{
builder.Append(content[i]);
break;
}
}
}
return builder.ToString();
}
///
/// Unescape a string. Note:
/// Backslash following any character will become that character.
/// Backslash by itself will be skipped.
///
internal static string UnescapeString(string content)
{
return UnescapePattern.Replace(
content,
UnescapeMatchEvaluator
);
}
// Regular expression
// need to use 4 backslash here because it is escaped by compiler and regular expressions
private static Regex UnescapePattern = new Regex("(\\\\.?|<|>|"|'|&)", RegexOptions.CultureInvariant | RegexOptions.Compiled);
// delegates to escape and unesacpe a matched pattern
private static MatchEvaluator UnescapeMatchEvaluator = new MatchEvaluator(UnescapeMatch);
///
/// the delegate to Unescape the matched pattern
///
private static string UnescapeMatch(Match match)
{
switch (match.Value)
{
case "<" : return "<";
case ">" : return ">";
case "&" : return "&";
case "'": return "'";
case """: return "\"";
default:
{
// this is a '\' followed by 0 or 1 character
Debug.Assert(match.Value.Length > 0 && match.Value[0] == BamlConst.EscapeChar);
if (match.Value.Length == 2)
{
return match.Value[1].ToString();
}
else
{
return string.Empty;
}
}
}
}
///
/// Parse the input string into an array of text/child-placeholder tokens.
/// Element placeholders start with '#' and end with ';'.
/// In case of error, a null array is returned.
///
internal static BamlStringToken[] ParseChildPlaceholder(string input)
{
if (input == null) return null;
List tokens = new List(8);
int tokenStart = 0; bool inPlaceHolder = false;
for (int i = 0; i < input.Length; i++)
{
if (input[i] == BamlConst.ChildStart)
{
if (i == 0 || input[i - 1]!= BamlConst.EscapeChar)
{
if (inPlaceHolder)
{
// All # needs to be escaped in a child place holder
return null; // error
}
inPlaceHolder = true;
if (tokenStart < i)
{
tokens.Add(
new BamlStringToken(
BamlStringToken.TokenType.Text,
UnescapeString(input.Substring(tokenStart, i - tokenStart))
)
);
tokenStart = i;
}
}
}
else if (input[i] == BamlConst.ChildEnd)
{
if ( i > 0
&& input[i - 1] != BamlConst.EscapeChar
&& inPlaceHolder)
{
// It is a valid child placeholder end
tokens.Add(
new BamlStringToken(
BamlStringToken.TokenType.ChildPlaceHolder,
UnescapeString(input.Substring(tokenStart + 1, i - tokenStart - 1))
)
);
// Advance the token index
tokenStart = i + 1;
inPlaceHolder = false;
}
}
}
if (inPlaceHolder)
{
// at the end of the string, all child placeholder must be closed
return null; // error
}
if (tokenStart < input.Length)
{
tokens.Add(
new BamlStringToken(
BamlStringToken.TokenType.Text,
UnescapeString(input.Substring(tokenStart))
)
);
}
return tokens.ToArray();
}
}
internal struct BamlStringToken
{
internal readonly TokenType Type;
internal readonly string Value;
internal BamlStringToken(TokenType type, string value)
{
Type = type;
Value = value;
}
internal enum TokenType
{
Text,
ChildPlaceHolder,
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------------------
//
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//
// Description: Utility that handles parsing Baml Resource Content
//
// History: 11/29/2003 garyyang Rewrote
//
//---------------------------------------------------------------------------
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Diagnostics;
using System.Collections.Generic;
using System.Windows;
namespace MS.Internal.Globalization
{
internal static class BamlResourceContentUtil
{
//-------------------------------------
// Internal methods
//-------------------------------------
///
/// Escape a string
///
internal static string EscapeString(string content)
{
if (content == null) return null;
StringBuilder builder = new StringBuilder();
for (int i = 0; i < content.Length; i++)
{
switch (content[i])
{
case BamlConst.ChildStart :
case BamlConst.ChildEnd :
case BamlConst.EscapeChar :
{
builder.Append(BamlConst.EscapeChar);
builder.Append(content[i]);
break;
}
case '&' :
{
builder.Append("&");
break;
}
case '<' :
{
builder.Append("<");
break;
}
case '>' :
{
builder.Append(">");
break;
}
case '\'':
{
builder.Append("'");
break;
}
case '\"':
{
builder.Append(""");
break;
}
default :
{
builder.Append(content[i]);
break;
}
}
}
return builder.ToString();
}
///
/// Unescape a string. Note:
/// Backslash following any character will become that character.
/// Backslash by itself will be skipped.
///
internal static string UnescapeString(string content)
{
return UnescapePattern.Replace(
content,
UnescapeMatchEvaluator
);
}
// Regular expression
// need to use 4 backslash here because it is escaped by compiler and regular expressions
private static Regex UnescapePattern = new Regex("(\\\\.?|<|>|"|'|&)", RegexOptions.CultureInvariant | RegexOptions.Compiled);
// delegates to escape and unesacpe a matched pattern
private static MatchEvaluator UnescapeMatchEvaluator = new MatchEvaluator(UnescapeMatch);
///
/// the delegate to Unescape the matched pattern
///
private static string UnescapeMatch(Match match)
{
switch (match.Value)
{
case "<" : return "<";
case ">" : return ">";
case "&" : return "&";
case "'": return "'";
case """: return "\"";
default:
{
// this is a '\' followed by 0 or 1 character
Debug.Assert(match.Value.Length > 0 && match.Value[0] == BamlConst.EscapeChar);
if (match.Value.Length == 2)
{
return match.Value[1].ToString();
}
else
{
return string.Empty;
}
}
}
}
///
/// Parse the input string into an array of text/child-placeholder tokens.
/// Element placeholders start with '#' and end with ';'.
/// In case of error, a null array is returned.
///
internal static BamlStringToken[] ParseChildPlaceholder(string input)
{
if (input == null) return null;
List tokens = new List(8);
int tokenStart = 0; bool inPlaceHolder = false;
for (int i = 0; i < input.Length; i++)
{
if (input[i] == BamlConst.ChildStart)
{
if (i == 0 || input[i - 1]!= BamlConst.EscapeChar)
{
if (inPlaceHolder)
{
// All # needs to be escaped in a child place holder
return null; // error
}
inPlaceHolder = true;
if (tokenStart < i)
{
tokens.Add(
new BamlStringToken(
BamlStringToken.TokenType.Text,
UnescapeString(input.Substring(tokenStart, i - tokenStart))
)
);
tokenStart = i;
}
}
}
else if (input[i] == BamlConst.ChildEnd)
{
if ( i > 0
&& input[i - 1] != BamlConst.EscapeChar
&& inPlaceHolder)
{
// It is a valid child placeholder end
tokens.Add(
new BamlStringToken(
BamlStringToken.TokenType.ChildPlaceHolder,
UnescapeString(input.Substring(tokenStart + 1, i - tokenStart - 1))
)
);
// Advance the token index
tokenStart = i + 1;
inPlaceHolder = false;
}
}
}
if (inPlaceHolder)
{
// at the end of the string, all child placeholder must be closed
return null; // error
}
if (tokenStart < input.Length)
{
tokens.Add(
new BamlStringToken(
BamlStringToken.TokenType.Text,
UnescapeString(input.Substring(tokenStart))
)
);
}
return tokens.ToArray();
}
}
internal struct BamlStringToken
{
internal readonly TokenType Type;
internal readonly string Value;
internal BamlStringToken(TokenType type, string value)
{
Type = type;
Value = value;
}
internal enum TokenType
{
Text,
ChildPlaceHolder,
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- Assert.cs
- PagerStyle.cs
- Debug.cs
- XsltSettings.cs
- InvalidCardException.cs
- TransformedBitmap.cs
- ZoomPercentageConverter.cs
- SkinBuilder.cs
- MsmqActivation.cs
- TimeoutHelper.cs
- TableCellAutomationPeer.cs
- WebPartRestoreVerb.cs
- SizeConverter.cs
- FileDataSourceCache.cs
- HashJoinQueryOperatorEnumerator.cs
- GrowingArray.cs
- FixedSOMSemanticBox.cs
- OneWayBindingElementImporter.cs
- ToolStripLocationCancelEventArgs.cs
- ReflectionHelper.cs
- RequestCache.cs
- StructuralCache.cs
- CopyOfAction.cs
- DataGridViewDataErrorEventArgs.cs
- BufferedGraphicsManager.cs
- MSAAWinEventWrap.cs
- QueryCursorEventArgs.cs
- AbandonedMutexException.cs
- TextElementEnumerator.cs
- BindingOperations.cs
- SessionSymmetricTransportSecurityProtocolFactory.cs
- BatchStream.cs
- FeedUtils.cs
- BooleanAnimationUsingKeyFrames.cs
- RealProxy.cs
- DrawTreeNodeEventArgs.cs
- SharedStatics.cs
- MemberAccessException.cs
- QilGeneratorEnv.cs
- DataRecordInfo.cs
- DynamicDataManager.cs
- CachedTypeface.cs
- WaitForChangedResult.cs
- QilInvokeLateBound.cs
- WebPartConnectionsEventArgs.cs
- ClientBuildManagerCallback.cs
- BaseDataList.cs
- SByteStorage.cs
- InvalidBodyAccessException.cs
- SecureStringHasher.cs
- SelectedGridItemChangedEvent.cs
- XsltArgumentList.cs
- TypedElement.cs
- nulltextnavigator.cs
- HostedTcpTransportManager.cs
- WebPartUtil.cs
- SapiRecoContext.cs
- ListViewUpdatedEventArgs.cs
- IconHelper.cs
- TraceSection.cs
- Grant.cs
- adornercollection.cs
- NullReferenceException.cs
- WebPartCloseVerb.cs
- UriTemplateCompoundPathSegment.cs
- sqlser.cs
- EditorPartCollection.cs
- SingleTagSectionHandler.cs
- FacetEnabledSchemaElement.cs
- XsltConvert.cs
- FormViewInsertedEventArgs.cs
- BuildResult.cs
- EntityTransaction.cs
- ProviderUtil.cs
- JavascriptCallbackMessageInspector.cs
- COM2PropertyPageUITypeConverter.cs
- DataGridHelper.cs
- DnsPermission.cs
- Solver.cs
- DeviceContexts.cs
- SqlConnectionPoolProviderInfo.cs
- CodeGotoStatement.cs
- FormsAuthenticationEventArgs.cs
- PostBackOptions.cs
- Command.cs
- StringArrayEditor.cs
- PropertyChangedEventManager.cs
- BulletedList.cs
- PropertyKey.cs
- ScrollBarRenderer.cs
- WebPartTransformerCollection.cs
- SystemInformation.cs
- UncommonField.cs
- GACIdentityPermission.cs
- XmlSchemaCompilationSettings.cs
- DoubleCollection.cs
- OleDbTransaction.cs
- EntityViewContainer.cs
- XmlSerializer.cs
- sortedlist.cs