// Token: 0x06006EAF RID: 28335 RVA: 0x001FC9DC File Offset: 0x001FABDC
 private static void TryFlushTextToBamlNode(BamlTreeNode parent, StringBuilder textContent)
 {
     if (textContent.Length > 0)
     {
         BamlTreeNode child = new BamlTextNode(textContent.ToString());
         parent.AddChild(child);
         textContent.Length = 0;
     }
 }
Esempio n. 2
0
 private static void TryFlushTextToBamlNode(BamlTreeNode parent, StringBuilder textContent) 
 { 
     if (textContent.Length > 0)
     { 
         BamlTreeNode textNode  = new BamlTextNode(textContent.ToString());
         parent.AddChild(textNode);
         textContent.Length = 0;
     } 
 }
Esempio n. 3
0
        private static void MergeChildrenList( 
            BamlLocalizableResourceKey  key, 
            BamlTreeUpdateMap           treeMap,
            BamlTreeNode                parent, 
            IList<BamlTreeNode>         newChildren
            )
        {
            if (newChildren == null) return; 

            List<BamlTreeNode> oldChildren = parent.Children; 
 
            int nodeIndex = 0;
            StringBuilder textBuffer = new StringBuilder(); 
            if (oldChildren != null)
            {
                Hashtable uidSubstitutions = new Hashtable(newChildren.Count);
                foreach (BamlTreeNode node in newChildren) 
                {
                    if (node.NodeType == BamlNodeType.StartElement) 
                    { 
                        BamlStartElementNode element = (BamlStartElementNode) node;
 
                        // element's Uid can be null if it is a formatting tag.
                        if (element.Uid != null)
                        {
                            if (uidSubstitutions.ContainsKey(element.Uid)) 
                            {
                                treeMap.Resolver.RaiseErrorNotifyEvent( 
                                    new BamlLocalizerErrorNotifyEventArgs( 
                                        key,
                                        BamlLocalizerError.DuplicateElement 
                                        )
                                    );
                                return; // the substitution contains duplicate elements.
                            } 

                            uidSubstitutions[element.Uid] = null;  // stored in Hashtable 
                        } 
                    }
                } 

                parent.Children = null; // start re-adding child element to parent

                // The last node is EndStartElement node and must remain to be at the end, 
                // so it won't be rearranged.
                for (int i = 0; i < oldChildren.Count - 1; i++) 
                { 
                    BamlTreeNode child = oldChildren[i];
                    switch (child.NodeType) 
                    {
                        case BamlNodeType.StartElement:
                        {
                            BamlStartElementNode element = (BamlStartElementNode) child; 

                            if (element.Uid != null) 
                            { 
                                if (!uidSubstitutions.ContainsKey(element.Uid))
                                { 
                                    // cannot apply uid susbstitution because the susbstituition doesn't
                                    // contain all the existing uids.
                                    parent.Children = oldChildren; // reset to old children and exit
                                    treeMap.Resolver.RaiseErrorNotifyEvent( 
                                        new BamlLocalizerErrorNotifyEventArgs(
                                            key, 
                                            BamlLocalizerError.MismatchedElements 
                                            )
                                    ); 

                                    return;
                                }
 
                                // Each Uid can only appear once.
                                uidSubstitutions.Remove(element.Uid); 
                            } 

                            // Append all the contents till the matching element. 
                            while (nodeIndex < newChildren.Count)
                            {
                                BamlTreeNode newNode = newChildren[nodeIndex++];
                                Invariant.Assert(newNode != null); 

                                if (newNode.NodeType == BamlNodeType.Text) 
                                { 
                                    textBuffer.Append(((BamlTextNode) newNode).Content); // Collect all text into the buffer
                                } 
                                else
                                {
                                    TryFlushTextToBamlNode(parent, textBuffer);
                                    parent.AddChild(newNode); 

                                    if (newNode.NodeType == BamlNodeType.StartElement) 
                                        break; 
                                }
                            } 

                            break;
                        }
                        case BamlNodeType.Text: 
                        {
                            // Skip original text node. New text node will be created from 
                            // text tokens in translation 
                            break;
                        } 
                        default:
                        {
                            parent.AddChild(child);
                            break; 
                        }
                    } 
                } 
            }
 
            // finish the rest of the nodes
            for (; nodeIndex < newChildren.Count; nodeIndex++)
            {
                BamlTreeNode newNode = newChildren[nodeIndex]; 
                Invariant.Assert(newNode != null);
 
                if (newNode.NodeType == BamlNodeType.Text) 
                {
                    textBuffer.Append(((BamlTextNode) newNode).Content); // Collect all text into the buffer 
                }
                else
                {
                    TryFlushTextToBamlNode(parent, textBuffer); 
                    parent.AddChild(newNode);
                } 
            } 

            TryFlushTextToBamlNode(parent, textBuffer); 

            // Always terminate the list with EndElementNode;
            parent.AddChild(new BamlEndElementNode());
        } 
        // 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: 0x06006EAE RID: 28334 RVA: 0x001FC7C0 File Offset: 0x001FA9C0
        private static void MergeChildrenList(BamlLocalizableResourceKey key, BamlTreeUpdater.BamlTreeUpdateMap treeMap, BamlTreeNode parent, IList <BamlTreeNode> newChildren)
        {
            if (newChildren == null)
            {
                return;
            }
            List <BamlTreeNode> children = parent.Children;
            int           i             = 0;
            StringBuilder stringBuilder = new StringBuilder();

            if (children != null)
            {
                Hashtable hashtable = new Hashtable(newChildren.Count);
                foreach (BamlTreeNode bamlTreeNode in newChildren)
                {
                    if (bamlTreeNode.NodeType == BamlNodeType.StartElement)
                    {
                        BamlStartElementNode bamlStartElementNode = (BamlStartElementNode)bamlTreeNode;
                        if (bamlStartElementNode.Uid != null)
                        {
                            if (hashtable.ContainsKey(bamlStartElementNode.Uid))
                            {
                                treeMap.Resolver.RaiseErrorNotifyEvent(new BamlLocalizerErrorNotifyEventArgs(key, BamlLocalizerError.DuplicateElement));
                                return;
                            }
                            hashtable[bamlStartElementNode.Uid] = null;
                        }
                    }
                }
                parent.Children = null;
                for (int j = 0; j < children.Count - 1; j++)
                {
                    BamlTreeNode bamlTreeNode2 = children[j];
                    BamlNodeType nodeType      = bamlTreeNode2.NodeType;
                    if (nodeType != BamlNodeType.StartElement)
                    {
                        if (nodeType != BamlNodeType.Text)
                        {
                            parent.AddChild(bamlTreeNode2);
                        }
                    }
                    else
                    {
                        BamlStartElementNode bamlStartElementNode2 = (BamlStartElementNode)bamlTreeNode2;
                        if (bamlStartElementNode2.Uid != null)
                        {
                            if (!hashtable.ContainsKey(bamlStartElementNode2.Uid))
                            {
                                parent.Children = children;
                                treeMap.Resolver.RaiseErrorNotifyEvent(new BamlLocalizerErrorNotifyEventArgs(key, BamlLocalizerError.MismatchedElements));
                                return;
                            }
                            hashtable.Remove(bamlStartElementNode2.Uid);
                        }
                        while (i < newChildren.Count)
                        {
                            BamlTreeNode bamlTreeNode3 = newChildren[i++];
                            Invariant.Assert(bamlTreeNode3 != null);
                            if (bamlTreeNode3.NodeType == BamlNodeType.Text)
                            {
                                stringBuilder.Append(((BamlTextNode)bamlTreeNode3).Content);
                            }
                            else
                            {
                                BamlTreeUpdater.TryFlushTextToBamlNode(parent, stringBuilder);
                                parent.AddChild(bamlTreeNode3);
                                if (bamlTreeNode3.NodeType == BamlNodeType.StartElement)
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            while (i < newChildren.Count)
            {
                BamlTreeNode bamlTreeNode4 = newChildren[i];
                Invariant.Assert(bamlTreeNode4 != null);
                if (bamlTreeNode4.NodeType == BamlNodeType.Text)
                {
                    stringBuilder.Append(((BamlTextNode)bamlTreeNode4).Content);
                }
                else
                {
                    BamlTreeUpdater.TryFlushTextToBamlNode(parent, stringBuilder);
                    parent.AddChild(bamlTreeNode4);
                }
                i++;
            }
            BamlTreeUpdater.TryFlushTextToBamlNode(parent, stringBuilder);
            parent.AddChild(new BamlEndElementNode());
        }
        private static void MergeChildrenList(
            BamlLocalizableResourceKey key,
            BamlTreeUpdateMap treeMap,
            BamlTreeNode parent,
            IList <BamlTreeNode> newChildren
            )
        {
            if (newChildren == null)
            {
                return;
            }

            List <BamlTreeNode> oldChildren = parent.Children;

            int           nodeIndex  = 0;
            StringBuilder textBuffer = new StringBuilder();

            if (oldChildren != null)
            {
                Hashtable uidSubstitutions = new Hashtable(newChildren.Count);
                foreach (BamlTreeNode node in newChildren)
                {
                    if (node.NodeType == BamlNodeType.StartElement)
                    {
                        BamlStartElementNode element = (BamlStartElementNode)node;

                        // element's Uid can be null if it is a formatting tag.
                        if (element.Uid != null)
                        {
                            if (uidSubstitutions.ContainsKey(element.Uid))
                            {
                                treeMap.Resolver.RaiseErrorNotifyEvent(
                                    new BamlLocalizerErrorNotifyEventArgs(
                                        key,
                                        BamlLocalizerError.DuplicateElement
                                        )
                                    );
                                return; // the substitution contains duplicate elements.
                            }

                            uidSubstitutions[element.Uid] = null;  // stored in Hashtable
                        }
                    }
                }

                parent.Children = null; // start re-adding child element to parent

                // The last node is EndStartElement node and must remain to be at the end,
                // so it won't be rearranged.
                for (int i = 0; i < oldChildren.Count - 1; i++)
                {
                    BamlTreeNode child = oldChildren[i];
                    switch (child.NodeType)
                    {
                    case BamlNodeType.StartElement:
                    {
                        BamlStartElementNode element = (BamlStartElementNode)child;

                        if (element.Uid != null)
                        {
                            if (!uidSubstitutions.ContainsKey(element.Uid))
                            {
                                // cannot apply uid susbstitution because the susbstituition doesn't
                                // contain all the existing uids.
                                parent.Children = oldChildren;     // reset to old children and exit
                                treeMap.Resolver.RaiseErrorNotifyEvent(
                                    new BamlLocalizerErrorNotifyEventArgs(
                                        key,
                                        BamlLocalizerError.MismatchedElements
                                        )
                                    );

                                return;
                            }

                            // Each Uid can only appear once.
                            uidSubstitutions.Remove(element.Uid);
                        }

                        // Append all the contents till the matching element.
                        while (nodeIndex < newChildren.Count)
                        {
                            BamlTreeNode newNode = newChildren[nodeIndex++];
                            Invariant.Assert(newNode != null);

                            if (newNode.NodeType == BamlNodeType.Text)
                            {
                                textBuffer.Append(((BamlTextNode)newNode).Content);      // Collect all text into the buffer
                            }
                            else
                            {
                                TryFlushTextToBamlNode(parent, textBuffer);
                                parent.AddChild(newNode);

                                if (newNode.NodeType == BamlNodeType.StartElement)
                                {
                                    break;
                                }
                            }
                        }

                        break;
                    }

                    case BamlNodeType.Text:
                    {
                        // Skip original text node. New text node will be created from
                        // text tokens in translation
                        break;
                    }

                    default:
                    {
                        parent.AddChild(child);
                        break;
                    }
                    }
                }
            }

            // finish the rest of the nodes
            for (; nodeIndex < newChildren.Count; nodeIndex++)
            {
                BamlTreeNode newNode = newChildren[nodeIndex];
                Invariant.Assert(newNode != null);

                if (newNode.NodeType == BamlNodeType.Text)
                {
                    textBuffer.Append(((BamlTextNode)newNode).Content);  // Collect all text into the buffer
                }
                else
                {
                    TryFlushTextToBamlNode(parent, textBuffer);
                    parent.AddChild(newNode);
                }
            }

            TryFlushTextToBamlNode(parent, textBuffer);

            // Always terminate the list with EndElementNode;
            parent.AddChild(new BamlEndElementNode());
        }
        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);
        }