Esempio n. 1
0
        //-----------------------------
        // internal methods
        //----------------------------- 

        internal static void UpdateTree( 
            BamlTree                    tree, 
            BamlTreeMap                 treeMap,
            BamlLocalizationDictionary  dictionary 
            )
        {

            Debug.Assert(tree != null && tree.Root != null, "Empty Tree!"); 
            Debug.Assert(treeMap != null, "Empty map!");
            Debug.Assert(dictionary != null, "Empty dictionary"); 
 
            // no changes to do to the tree.
            if (dictionary.Count <= 0) 
                return;

            // create a tree map to be used for update
            BamlTreeUpdateMap updateMap = new BamlTreeUpdateMap(treeMap, tree); 

            // 
            // a) Create baml tree nodes for missing child place holders and properties. 
            //    Translations may require new nodes to be constructed. For example
            //    translation contains new child place holders 
            //
            CreateMissingBamlTreeNode(dictionary, updateMap);

 
            //
            // b) Look through each translation and make modification to the tree 
            //    At this step, new nodes are linked to the tree if applicable. 
            //
            BamlLocalizationDictionaryEnumerator enumerator = dictionary.GetEnumerator(); 
            ArrayList deferredResources = new ArrayList();
            while (enumerator.MoveNext())
            {
                if (!ApplyChangeToBamlTree(enumerator.Key, enumerator.Value, updateMap)) 
                {
                    deferredResources.Add(enumerator.Entry); 
                } 
            }
 
            //
            // c) Hook up the property nodes that aren't hooked up yet
            //    Formatting tags inserted in the translation will only be created the
            //    previous step. Hook up properties to those nodes now if applicable 
            //
            for(int i = 0; i < deferredResources.Count; i++) 
            { 
                DictionaryEntry entry = (DictionaryEntry) deferredResources[i];
                ApplyChangeToBamlTree( 
                    (BamlLocalizableResourceKey) entry.Key,
                    (BamlLocalizableResource) entry.Value,
                    updateMap
                    ); 
            }
        } 
Esempio n. 2
0
        private static void CreateMissingBamlTreeNode(
            BamlLocalizationDictionary dictionary, 
            BamlTreeUpdateMap          treeMap
            )
        {
            BamlLocalizationDictionaryEnumerator enumerator = dictionary.GetEnumerator(); 
            while (enumerator.MoveNext())
            { 
                BamlLocalizableResourceKey key   = enumerator.Key; 
                BamlLocalizableResource resource = enumerator.Value;
 
                // get the baml tree node from the tree
                BamlTreeNode node = treeMap.MapKeyToBamlTreeNode(key);

                if (node == null) 
                {
                    if (key.PropertyName == BamlConst.ContentSuffix) 
                    { 
                        // see if there is already a Baml node with the Uid. If so
                        // ignore this entry 
                        node = treeMap.MapUidToBamlTreeElementNode(key.Uid);
                        if (node == null)
                        {
                            // create new Baml element node 
                            BamlStartElementNode newNode    = new BamlStartElementNode(
                                treeMap.Resolver.ResolveAssemblyFromClass(key.ClassName), 
                                key.ClassName, 
                                false, /*isInjected*/
                                false /*CreateUsingTypeConverter*/ 
                                );

                            // create new x:Uid node for this element node
                            newNode.AddChild( 
                                new BamlDefAttributeNode(
                                    XamlReaderHelper.DefinitionUid, 
                                    key.Uid 
                                    )
                                ); 

                            TryAddContentPropertyToNewElement(treeMap, newNode);

                            // terminate the node with EndElementNode 
                            newNode.AddChild(new BamlEndElementNode());
 
                            // store this new node into the map so that it can be found 
                            // when other translations reference it as a childplace holder, or property owner
                            treeMap.AddBamlTreeNode(key.Uid, key, newNode); 
                        }
                    }
                    else
                    { 
                        BamlTreeNode newNode;
                        if (key.PropertyName == BamlConst.LiteralContentSuffix) 
                        { 
                            // create a LiterContent node
                            newNode = new BamlLiteralContentNode(resource.Content); 
                        }
                        else
                        {
                            newNode = new BamlPropertyNode( 
                                treeMap.Resolver.ResolveAssemblyFromClass(key.ClassName),
                                key.ClassName, 
                                key.PropertyName, 
                                resource.Content,
                                BamlAttributeUsage.Default 
                                );
                        }

                        // add to the map 
                        treeMap.AddBamlTreeNode(null, key, newNode);
                    } 
                } 
            }
 
        }