// Token: 0x06006EB7 RID: 28343 RVA: 0x001FD064 File Offset: 0x001FB264 private bool TryFormatElementContent(BamlLocalizableResourceKey key, BamlStartElementNode node, out string content) { content = string.Empty; LocalizabilityAttribute localizabilityAttribute; string text; this.GetLocalizabilityForElementNode(node, out localizabilityAttribute, out text); localizabilityAttribute = this.CombineAndPropagateInheritanceValues(node, localizabilityAttribute); if (text != null && localizabilityAttribute.Category != LocalizationCategory.NeverLocalize && localizabilityAttribute.Category != LocalizationCategory.Ignore && localizabilityAttribute.Modifiability == Modifiability.Modifiable && localizabilityAttribute.Readability == Readability.Readable) { StringBuilder stringBuilder = new StringBuilder(); if (node.Uid != null) { stringBuilder.AppendFormat(TypeConverterHelper.InvariantEnglishUS, "<{0} {1}=\"{2}\">", new object[] { text, "Uid", BamlResourceContentUtil.EscapeString(node.Uid) }); } else { stringBuilder.AppendFormat(TypeConverterHelper.InvariantEnglishUS, "<{0}>", new object[] { text }); } string value; bool flag = this.TryGetContent(key, node, out value); if (flag) { stringBuilder.Append(value); stringBuilder.AppendFormat(TypeConverterHelper.InvariantEnglishUS, "</{0}>", new object[] { text }); node.Formatted = true; content = stringBuilder.ToString(); } return(flag); } bool result = true; if (node.Uid != null) { content = string.Format(TypeConverterHelper.InvariantEnglishUS, "{0}{1}{2}", new object[] { '#', BamlResourceContentUtil.EscapeString(node.Uid), ';' }); } else { this._resolver.RaiseErrorNotifyEvent(new BamlLocalizerErrorNotifyEventArgs(key, BamlLocalizerError.UidMissingOnChildElement)); result = false; } return(result); }
// Token: 0x06006EB6 RID: 28342 RVA: 0x001FCF00 File Offset: 0x001FB100 internal bool TryGetContent(BamlLocalizableResourceKey key, BamlTreeNode currentNode, out string content) { content = string.Empty; BamlNodeType nodeType = currentNode.NodeType; if (nodeType == BamlNodeType.StartElement) { BamlStartElementNode bamlStartElementNode = (BamlStartElementNode)currentNode; if (bamlStartElementNode.Content == null) { StringBuilder stringBuilder = new StringBuilder(); foreach (BamlTreeNode bamlTreeNode in bamlStartElementNode.Children) { nodeType = bamlTreeNode.NodeType; if (nodeType != BamlNodeType.StartElement) { if (nodeType == BamlNodeType.Text) { stringBuilder.Append(BamlResourceContentUtil.EscapeString(((BamlTextNode)bamlTreeNode).Content)); } } else { string value; if (!this.TryFormatElementContent(key, (BamlStartElementNode)bamlTreeNode, out value)) { return(false); } stringBuilder.Append(value); } } bamlStartElementNode.Content = stringBuilder.ToString(); } content = bamlStartElementNode.Content; return(true); } if (nodeType == BamlNodeType.Property) { bool result = true; BamlPropertyNode bamlPropertyNode = (BamlPropertyNode)currentNode; content = BamlResourceContentUtil.EscapeString(bamlPropertyNode.Value); string text = content; string text2; string text3; if (MarkupExtensionParser.GetMarkupExtensionTypeAndArgs(ref text, out text2, out text3)) { LocalizabilityGroup localizabilityComment = this._resolver.GetLocalizabilityComment(bamlPropertyNode.Parent as BamlStartElementNode, bamlPropertyNode.PropertyName); result = (localizabilityComment != null && localizabilityComment.Readability == Readability.Readable); } return(result); } if (nodeType != BamlNodeType.LiteralContent) { return(true); } content = BamlResourceContentUtil.EscapeString(((BamlLiteralContentNode)currentNode).Content); return(true); }
// Token: 0x06006E0B RID: 28171 RVA: 0x001FA728 File Offset: 0x001F8928 internal static BamlStringToken[] ParseChildPlaceholder(string input) { if (input == null) { return(null); } List <BamlStringToken> list = new List <BamlStringToken>(8); int num = 0; bool flag = false; for (int i = 0; i < input.Length; i++) { if (input[i] == '#') { if (i == 0 || input[i - 1] != '\\') { if (flag) { return(null); } flag = true; if (num < i) { list.Add(new BamlStringToken(BamlStringToken.TokenType.Text, BamlResourceContentUtil.UnescapeString(input.Substring(num, i - num)))); num = i; } } } else if (input[i] == ';' && (i > 0 && input[i - 1] != '\\' && flag)) { list.Add(new BamlStringToken(BamlStringToken.TokenType.ChildPlaceHolder, BamlResourceContentUtil.UnescapeString(input.Substring(num + 1, i - num - 1)))); num = i + 1; flag = false; } } if (flag) { return(null); } if (num < input.Length) { list.Add(new BamlStringToken(BamlStringToken.TokenType.Text, BamlResourceContentUtil.UnescapeString(input.Substring(num)))); } return(list.ToArray()); }
// Token: 0x06006EB2 RID: 28338 RVA: 0x001FCC7C File Offset: 0x001FAE7C private static bool GetBamlTreeNodeFromText(BamlLocalizableResourceKey key, string content, BamlTreeUpdater.BamlTreeUpdateMap bamlTreeMap, IList <BamlTreeNode> newChildrenList) { BamlStringToken[] array = BamlResourceContentUtil.ParseChildPlaceholder(content); if (array == null) { bamlTreeMap.Resolver.RaiseErrorNotifyEvent(new BamlLocalizerErrorNotifyEventArgs(key, BamlLocalizerError.IncompleteElementPlaceholder)); return(false); } bool result = true; for (int i = 0; i < array.Length; i++) { BamlStringToken.TokenType type = array[i].Type; if (type != BamlStringToken.TokenType.Text) { if (type == BamlStringToken.TokenType.ChildPlaceHolder) { BamlTreeNode bamlTreeNode = bamlTreeMap.MapUidToBamlTreeElementNode(array[i].Value); if (bamlTreeNode != null) { newChildrenList.Add(bamlTreeNode); } else { bamlTreeMap.Resolver.RaiseErrorNotifyEvent(new BamlLocalizerErrorNotifyEventArgs(new BamlLocalizableResourceKey(array[i].Value, string.Empty, string.Empty), BamlLocalizerError.InvalidUid)); result = false; } } } else { BamlTreeNode item = new BamlTextNode(array[i].Value); newChildrenList.Add(item); } } return(result); }
private bool TryFormatElementContent( BamlLocalizableResourceKey key, BamlStartElementNode node, out string content ) { content = string.Empty; string formattingTag; LocalizabilityAttribute attribute; GetLocalizabilityForElementNode(node, out attribute, out formattingTag); attribute = CombineAndPropagateInheritanceValues(node, attribute); if (formattingTag != null && attribute.Category != LocalizationCategory.NeverLocalize && attribute.Category != LocalizationCategory.Ignore && attribute.Modifiability == Modifiability.Modifiable && attribute.Readability == Readability.Readable ) { // this node should be formatted inline StringBuilder contentBuilder = new StringBuilder(); // write opening tag if (node.Uid != null) { contentBuilder.AppendFormat( TypeConverterHelper.InvariantEnglishUS, "<{0} {1}=\"{2}\">", formattingTag, XamlReaderHelper.DefinitionUid, BamlResourceContentUtil.EscapeString(node.Uid) ); } else { contentBuilder.AppendFormat(TypeConverterHelper.InvariantEnglishUS, "<{0}>", formattingTag); } // recurisively call down to format the content string childContent; bool succeed = TryGetContent(key, node, out childContent); if (succeed) { contentBuilder.Append(childContent); // write closing tag contentBuilder.AppendFormat(TypeConverterHelper.InvariantEnglishUS, "</{0}>", formattingTag); // remeber that we format this element so that we don't format the value again. // e.g. <Text x:Uid="t"> <Bold x:Uid="b"> ... </Bold> </Text> // if <Bold> is already inlined in Text element's contennt, we don't need to // have a seperate entry for <Bold> anymore node.Formatted = true; content = contentBuilder.ToString(); } return(succeed); } else { // this node should be represented by place holder. bool succeed = true; if (node.Uid != null) { content = string.Format( TypeConverterHelper.InvariantEnglishUS, "{0}{1}{2}", BamlConst.ChildStart, BamlResourceContentUtil.EscapeString(node.Uid), BamlConst.ChildEnd ); } else { // we want to enforce the rule that all children element // must have UID defined. _resolver.RaiseErrorNotifyEvent( new BamlLocalizerErrorNotifyEventArgs( key, BamlLocalizerError.UidMissingOnChildElement ) ); succeed = false; // failed } return(succeed); } }
/// <summary> /// This builds the localizable string from the baml tree node /// </summary> /// <return> /// return true when the node has valid localizable content, false otherwise. /// </return> internal bool TryGetContent(BamlLocalizableResourceKey key, BamlTreeNode currentNode, out string content) { content = string.Empty; switch (currentNode.NodeType) { case BamlNodeType.Property: { bool isValidContent = true; BamlPropertyNode propertyNode = (BamlPropertyNode)currentNode; content = BamlResourceContentUtil.EscapeString(propertyNode.Value); // // Markup extensions are not localizable values, e.g. {x:Type SolidColorBrush}. // So if the string can be parsed as Markup extensions, we will exclude it unless // the user sets localization comments explicitly to localize this value. // string typeName, args; string tempContent = content; if (MarkupExtensionParser.GetMarkupExtensionTypeAndArgs(ref tempContent, out typeName, out args)) { // See if this value has been marked as localizable explicitly in comments LocalizabilityGroup localizability = _resolver.GetLocalizabilityComment( propertyNode.Parent as BamlStartElementNode, propertyNode.PropertyName ); isValidContent = (localizability != null && localizability.Readability == Readability.Readable); } return(isValidContent); } case BamlNodeType.LiteralContent: { content = BamlResourceContentUtil.EscapeString( ((BamlLiteralContentNode)currentNode).Content ); return(true); // succeed } case BamlNodeType.StartElement: { BamlStartElementNode elementNode = (BamlStartElementNode)currentNode; if (elementNode.Content == null) { StringBuilder contentBuilder = new StringBuilder(); foreach (BamlTreeNode child in elementNode.Children) { // we only format element and text inline // other nodes like property node we don't put them into the content of the element switch (child.NodeType) { case BamlNodeType.StartElement: { string childContent; if (TryFormatElementContent(key, (BamlStartElementNode)child, out childContent)) { contentBuilder.Append(childContent); } else { return(false); // failed to get content for children element } break; } case BamlNodeType.Text: { contentBuilder.Append(BamlResourceContentUtil.EscapeString( ((BamlTextNode)child).Content) ); break; } } } elementNode.Content = contentBuilder.ToString(); } content = elementNode.Content; return(true); } default: return(true); } }
// Token: 0x06006EAC RID: 28332 RVA: 0x001FC674 File Offset: 0x001FA874 private static bool ApplyChangeToBamlTree(BamlLocalizableResourceKey key, BamlLocalizableResource resource, BamlTreeUpdater.BamlTreeUpdateMap treeMap) { if (resource == null || resource.Content == null || !resource.Modifiable) { return(true); } if (!treeMap.LocalizationDictionary.Contains(key) && !treeMap.IsNewBamlTreeNode(key)) { return(true); } BamlTreeNode bamlTreeNode = treeMap.MapKeyToBamlTreeNode(key); Invariant.Assert(bamlTreeNode != null); BamlNodeType nodeType = bamlTreeNode.NodeType; if (nodeType != BamlNodeType.StartElement) { if (nodeType != BamlNodeType.Property) { if (nodeType == BamlNodeType.LiteralContent) { BamlLiteralContentNode bamlLiteralContentNode = (BamlLiteralContentNode)bamlTreeNode; bamlLiteralContentNode.Content = BamlResourceContentUtil.UnescapeString(resource.Content); if (bamlLiteralContentNode.Parent == null) { BamlTreeNode bamlTreeNode2 = treeMap.MapUidToBamlTreeElementNode(key.Uid); if (bamlTreeNode2 == null) { return(false); } bamlTreeNode2.AddChild(bamlLiteralContentNode); } } } else { BamlPropertyNode bamlPropertyNode = (BamlPropertyNode)bamlTreeNode; bamlPropertyNode.Value = BamlResourceContentUtil.UnescapeString(resource.Content); if (bamlPropertyNode.Parent == null) { BamlStartElementNode bamlStartElementNode = treeMap.MapUidToBamlTreeElementNode(key.Uid); if (bamlStartElementNode == null) { return(false); } bamlStartElementNode.InsertProperty(bamlTreeNode); } } } else { string b = null; if (treeMap.LocalizationDictionary.Contains(key)) { b = treeMap.LocalizationDictionary[key].Content; } if (resource.Content != b) { BamlTreeUpdater.ReArrangeChildren(key, bamlTreeNode, resource.Content, treeMap); } } return(true); }
// Token: 0x06006EB1 RID: 28337 RVA: 0x001FCAE4 File Offset: 0x001FACE4 private static bool GetBamlTreeNodeFromXmlNode(BamlLocalizableResourceKey key, XmlNode node, BamlTreeUpdater.BamlTreeUpdateMap bamlTreeMap, IList <BamlTreeNode> newChildrenList) { if (node.NodeType == XmlNodeType.Text) { return(BamlTreeUpdater.GetBamlTreeNodeFromText(key, node.Value, bamlTreeMap, newChildrenList)); } if (node.NodeType != XmlNodeType.Element) { return(true); } XmlElement xmlElement = node as XmlElement; string text = bamlTreeMap.Resolver.ResolveFormattingTagToClass(xmlElement.Name); bool flag = string.IsNullOrEmpty(text); string text2 = null; if (!flag) { text2 = bamlTreeMap.Resolver.ResolveAssemblyFromClass(text); flag = string.IsNullOrEmpty(text2); } if (flag) { bamlTreeMap.Resolver.RaiseErrorNotifyEvent(new BamlLocalizerErrorNotifyEventArgs(key, BamlLocalizerError.UnknownFormattingTag)); return(false); } string text3 = null; if (xmlElement.HasAttributes) { text3 = xmlElement.GetAttribute("Uid"); if (!string.IsNullOrEmpty(text3)) { text3 = BamlResourceContentUtil.UnescapeString(text3); } } BamlStartElementNode bamlStartElementNode = null; if (text3 != null) { bamlStartElementNode = bamlTreeMap.MapUidToBamlTreeElementNode(text3); } if (bamlStartElementNode == null) { bamlStartElementNode = new BamlStartElementNode(text2, text, false, false); if (text3 != null) { bamlTreeMap.AddBamlTreeNode(text3, new BamlLocalizableResourceKey(text3, text, "$Content", text2), bamlStartElementNode); bamlStartElementNode.AddChild(new BamlDefAttributeNode("Uid", text3)); } BamlTreeUpdater.TryAddContentPropertyToNewElement(bamlTreeMap, bamlStartElementNode); bamlStartElementNode.AddChild(new BamlEndElementNode()); } else if (bamlStartElementNode.TypeFullName != text) { bamlTreeMap.Resolver.RaiseErrorNotifyEvent(new BamlLocalizerErrorNotifyEventArgs(key, BamlLocalizerError.DuplicateUid)); return(false); } newChildrenList.Add(bamlStartElementNode); bool flag2 = true; if (xmlElement.HasChildNodes) { IList <BamlTreeNode> list = new List <BamlTreeNode>(); int num = 0; while (num < xmlElement.ChildNodes.Count && flag2) { flag2 = BamlTreeUpdater.GetBamlTreeNodeFromXmlNode(key, xmlElement.ChildNodes[num], bamlTreeMap, list); num++; } if (flag2) { BamlTreeUpdater.MergeChildrenList(key, bamlTreeMap, bamlStartElementNode, list); } } return(flag2); }
private static bool GetBamlTreeNodeFromText( BamlLocalizableResourceKey key, string content, // xml node to construct BamlTreeNode from BamlTreeUpdateMap bamlTreeMap, IList <BamlTreeNode> newChildrenList // list of new children ) { BamlStringToken[] tokens = BamlResourceContentUtil.ParseChildPlaceholder(content); if (tokens == null) { bamlTreeMap.Resolver.RaiseErrorNotifyEvent( new BamlLocalizerErrorNotifyEventArgs( key, BamlLocalizerError.IncompleteElementPlaceholder ) ); return(false); } bool succeed = true; for (int i = 0; i < tokens.Length; i++) { switch (tokens[i].Type) { case BamlStringToken.TokenType.Text: { BamlTreeNode node = new BamlTextNode(tokens[i].Value); newChildrenList.Add(node); break; } case BamlStringToken.TokenType.ChildPlaceHolder: { BamlTreeNode node = bamlTreeMap.MapUidToBamlTreeElementNode(tokens[i].Value); // The value will be null if there is no uid-matching node in the tree. if (node != null) { newChildrenList.Add(node); } else { bamlTreeMap.Resolver.RaiseErrorNotifyEvent( new BamlLocalizerErrorNotifyEventArgs( new BamlLocalizableResourceKey( tokens[i].Value, string.Empty, string.Empty ), BamlLocalizerError.InvalidUid ) ); succeed = false; } break; } } } return(succeed); }
private static bool GetBamlTreeNodeFromXmlNode( BamlLocalizableResourceKey key, XmlNode node, // xml node to construct BamlTreeNode from BamlTreeUpdateMap bamlTreeMap, // Baml tree update map IList <BamlTreeNode> newChildrenList // list of new children ) { if (node.NodeType == XmlNodeType.Text) { // construct a Text tree node from the xml content return(GetBamlTreeNodeFromText( key, node.Value, bamlTreeMap, newChildrenList )); } else if (node.NodeType == XmlNodeType.Element) { XmlElement child = node as XmlElement; string className = bamlTreeMap.Resolver.ResolveFormattingTagToClass(child.Name); bool invalidResult = string.IsNullOrEmpty(className); string assemblyName = null; if (!invalidResult) { assemblyName = bamlTreeMap.Resolver.ResolveAssemblyFromClass(className); invalidResult = string.IsNullOrEmpty(assemblyName); } if (invalidResult) { bamlTreeMap.Resolver.RaiseErrorNotifyEvent( new BamlLocalizerErrorNotifyEventArgs( key, BamlLocalizerError.UnknownFormattingTag ) ); return(false); } // get the uid for this formatting tag string tagUid = null; if (child.HasAttributes) { tagUid = child.GetAttribute(XamlReaderHelper.DefinitionUid); if (!string.IsNullOrEmpty(tagUid)) { tagUid = BamlResourceContentUtil.UnescapeString(tagUid); } } BamlStartElementNode bamlNode = null; if (tagUid != null) { bamlNode = bamlTreeMap.MapUidToBamlTreeElementNode(tagUid); } if (bamlNode == null) { bamlNode = new BamlStartElementNode( assemblyName, className, false, /*isInjected*/ false /*CreateUsingTypeConverter*/ ); if (tagUid != null) { // store the new node created bamlTreeMap.AddBamlTreeNode( tagUid, new BamlLocalizableResourceKey(tagUid, className, BamlConst.ContentSuffix, assemblyName), bamlNode ); // Add the x:Uid node to the element bamlNode.AddChild( new BamlDefAttributeNode( XamlReaderHelper.DefinitionUid, tagUid ) ); } TryAddContentPropertyToNewElement(bamlTreeMap, bamlNode); // terminate the child by a end element node bamlNode.AddChild(new BamlEndElementNode()); } else { if (bamlNode.TypeFullName != className) { // This can happen if the localizer adds a new element with an id // that is also been added to the newer version of source baml bamlTreeMap.Resolver.RaiseErrorNotifyEvent( new BamlLocalizerErrorNotifyEventArgs( key, BamlLocalizerError.DuplicateUid ) ); return(false); } } newChildrenList.Add(bamlNode); bool succeed = true; if (child.HasChildNodes) { // recursively go down IList <BamlTreeNode> list = new List <BamlTreeNode>(); for (int i = 0; i < child.ChildNodes.Count && succeed; i++) { succeed = GetBamlTreeNodeFromXmlNode( key, child.ChildNodes[i], bamlTreeMap, list ); } if (succeed) { // merging the formatting translation with exisiting nodes. // formatting translation doesn't contain properties. MergeChildrenList(key, bamlTreeMap, bamlNode, list); } } return(succeed); } return(true); // other than text and element nodes }
private static bool ApplyChangeToBamlTree( BamlLocalizableResourceKey key, BamlLocalizableResource resource, BamlTreeUpdateMap treeMap ) { if (resource == null || resource.Content == null || !resource.Modifiable) { // Invalid translation or the resource is marked as non-modifiable. return(true); } if (!treeMap.LocalizationDictionary.Contains(key) && !treeMap.IsNewBamlTreeNode(key)) { // A localizable node is either in the localization dicationary extracted // from the source or it is a new node created by the localizer. // Otherwise, we cannot modify it. return(true); } // get the node, at this point, all the missing nodes are created BamlTreeNode node = treeMap.MapKeyToBamlTreeNode(key); Invariant.Assert(node != null); // apply translations switch (node.NodeType) { case BamlNodeType.LiteralContent: { BamlLiteralContentNode literalNode = (BamlLiteralContentNode)node; // set the content to the node. literalNode.Content = BamlResourceContentUtil.UnescapeString(resource.Content); // now try to link this node into the parent. if (literalNode.Parent == null) { BamlTreeNode parent = treeMap.MapUidToBamlTreeElementNode(key.Uid); if (parent != null) { // link it up with the parent parent.AddChild(literalNode); } else { return(false); // can't resolve the parent yet } } break; } case BamlNodeType.Property: { BamlPropertyNode propertyNode = (BamlPropertyNode)node; // set the translation into the property propertyNode.Value = BamlResourceContentUtil.UnescapeString(resource.Content); // now try to link this node into the parent if (propertyNode.Parent == null) { BamlStartElementNode parent = (BamlStartElementNode)treeMap.MapUidToBamlTreeElementNode(key.Uid); if (parent != null) { // insert property node to the parent parent.InsertProperty(node); } else { return(false); } } break; } case BamlNodeType.StartElement: { string source = null; if (treeMap.LocalizationDictionary.Contains(key)) { source = ((BamlLocalizableResource)treeMap.LocalizationDictionary[key]).Content; } if (resource.Content != source) { // only rearrange the value if source and update are different ReArrangeChildren(key, node, resource.Content, treeMap); } break; } default: break; } return(true); }