BamlLocalizerError _error;          // The error code.

        internal BamlLocalizerErrorNotifyEventArgs(BamlLocalizableResourceKey key, BamlLocalizerError error)
        {
            _key = key;

            //
            _error = error;
        }
        BamlLocalizerError       _error;    // The error code.

        internal BamlLocalizerErrorNotifyEventArgs(BamlLocalizableResourceKey key, BamlLocalizerError error)
        { 
            _key = key;
 
            // 
            _error = error;
        } 
Beispiel #3
0
 internal static string ResourceKeyToString(BamlLocalizableResourceKey key)
 {
     return string.Format(
         CultureInfo.InvariantCulture,
         "{0}:{1}.{2}",
         key.Uid,
         key.ClassName,
         key.PropertyName
         );
 }
 public BamlLocalizableResource this [BamlLocalizableResourceKey key]
 {
     get
     {
         return(default(BamlLocalizableResource));
     }
     set
     {
     }
 }
        /// <summary>
        /// Compare two BamlLocalizableResourceKey objects
        /// </summary>
        /// <param name="other">The other BamlLocalizableResourceKey object to be compared against</param>
        /// <returns>True if they are equal. False otherwise</returns>
        public bool Equals(BamlLocalizableResourceKey other)
        {
            if (other == null)
            {
                return(false);
            }

            return(_uid == other._uid &&
                   _className == other._className &&
                   _propertyName == other._propertyName);
        }
 /// <summary>
 /// Gets or sets a localizable resource by the key
 /// </summary>
 /// <param name="key">BamlLocalizableResourceKey key</param>
 /// <returns>BamlLocalizableResource object identified by the key</returns>
 public BamlLocalizableResource this[BamlLocalizableResourceKey key]
 {
     get
     {
         CheckNonNullParam(key, "key");
         return(_dictionary[key]);
     }
     set
     {
         CheckNonNullParam(key, "key");
         _dictionary[key] = value;
     }
 }
 public bool Contains(BamlLocalizableResourceKey key)
 {
   return default(bool);
 }
Beispiel #8
0
        private static IList<BamlTreeNode> SplitXmlContent( 
            BamlLocalizableResourceKey  key,
            string                      content, 
            BamlTreeUpdateMap           bamlTreeMap
            )
        {
            // process each translation as a piece of xml content because of potential formatting tag inside 
            StringBuilder xmlContent = new StringBuilder();
            xmlContent.Append("<ROOT>"); 
            xmlContent.Append(content); 
            xmlContent.Append("</ROOT>");
 
            IList<BamlTreeNode> list = new List<BamlTreeNode>(4);
            XmlDocument doc = new XmlDocument();

            bool succeed = true; 

            try 
            { 
                doc.LoadXml(xmlContent.ToString());
                XmlElement root = doc.FirstChild as XmlElement; 
                if (root != null && root.HasChildNodes)
                {
                    for (int i = 0; i < root.ChildNodes.Count && succeed; i++)
                    { 
                        succeed = GetBamlTreeNodeFromXmlNode(
                            key, 
                            root.ChildNodes[i], 
                            bamlTreeMap,
                            list 
                            );
                    }
                }
            } 
            catch (XmlException)
            { 
                // The content can't be parse as Xml. 
                bamlTreeMap.Resolver.RaiseErrorNotifyEvent(
                    new BamlLocalizerErrorNotifyEventArgs( 
                        key,
                        BamlLocalizerError.SubstitutionAsPlaintext
                        )
                ); 

                // Apply the substitution as plain text 
                succeed = GetBamlTreeNodeFromText( 
                    key,
                    content, 
                    bamlTreeMap,
                    list
                    );
            } 

            return (succeed ? list : null); 
        } 
Beispiel #9
0
        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; 
        }
        /// <summary>
        /// build a localizable resource from a baml tree node
        /// </summary>        
        internal BamlLocalizableResource BuildFromNode(BamlLocalizableResourceKey key, BamlTreeNode node)
        {          
            if (node.Formatted)
            {
                // the content of the node has been formatted to be part of 
                // parents' content, so no need to create a seperate entry for the
                // element
                return null;
            }            
            
            BamlLocalizableResource resource = null;
            LocalizabilityAttribute localizability = null; 
            string                  formattingTag;

            // 
            // variable controling what comments gets applied
            //
            BamlStartElementNode    commentNode       = null;  // node containing comment
            string                  commentTargetName = null;  // the target of the comment, e.g. $Content, FontSize, etc.            

            //
            // step 1: Get the localizability attribute from the source files
            //        
            switch(node.NodeType)
            {                
                case BamlNodeType.StartElement:                                     
                {
                    // For element
                    commentNode = (BamlStartElementNode) node;
                    GetLocalizabilityForElementNode(commentNode, out localizability, out formattingTag);                    
                    commentTargetName = BamlConst.ContentSuffix;
                    break;
                }
                case BamlNodeType.LiteralContent:
                {
                    // For literal content, get the attribute from parent element
                    GetLocalizabilityForElementNode((BamlStartElementNode) node.Parent, out localizability, out formattingTag);

                    commentNode       = (BamlStartElementNode) node.Parent;
                    commentTargetName = BamlConst.ContentSuffix;
                    break;
                }

                case BamlNodeType.Property:
                {

                    BamlStartComplexPropertyNode propertyNode = (BamlStartComplexPropertyNode) node;
                    if (  LocComments.IsLocCommentsProperty(propertyNode.OwnerTypeFullName, propertyNode.PropertyName)
                       || LocComments.IsLocLocalizabilityProperty(propertyNode.OwnerTypeFullName, propertyNode.PropertyName)
                       )
                    {
                        // skip Localization.Comments and Localization.Attributes properties. They aren't localizable
                        return null;
                    }
                    
                    // For property                    
                    GetLocalizabilityForPropertyNode(propertyNode, out localizability);                                                                

                    commentTargetName = propertyNode.PropertyName;                    
                    commentNode       = (BamlStartElementNode) node.Parent;
                    break;
                }
                default:                        
                {                           
                    Invariant.Assert(false); // no localizable resource for such node
                    break;
                }
            }

            //
            // Step 2: Find out the inheritance value
            //

            // The node participates in localizability inheritance
            // let's fill things in
            localizability = CombineAndPropagateInheritanceValues(
                node as ILocalizabilityInheritable, 
                localizability
                );                

            //
            // Step 3: We finalized the localizability values. We now apply.
            //            
            string content = null;     
            
            if (localizability.Category != LocalizationCategory.NeverLocalize 
             && localizability.Category != LocalizationCategory.Ignore 
             && TryGetContent(key, node, out content))                
            {   
                // we only create one if it is localizable
                resource                = new BamlLocalizableResource();
                resource.Readable       = (localizability.Readability   == Readability.Readable);               
                resource.Modifiable     = (localizability.Modifiability == Modifiability.Modifiable);
                resource.Category       = localizability.Category;
                // continue to fill in content.
                resource.Content        = content;
                resource.Comments       = _resolver.GetStringComment(commentNode, commentTargetName);
            }
            
            // return the resource
            return resource;  
        }
Beispiel #11
0
        private static void ReArrangeChildren( 
            BamlLocalizableResourceKey  key,
            BamlTreeNode                node, 
            string                      translation, 
            BamlTreeUpdateMap           treeMap
            ) 
        {
            //
            // Split the translation into a list of BamlNodes.
            // 
            IList<BamlTreeNode> nodes = SplitXmlContent(
                key, 
                translation, 
                treeMap
                ); 

            // merge the nodes from translation with the source nodes
            MergeChildrenList(key, treeMap, node, nodes);
        } 
 /// <summary>
 /// Compare two BamlLocalizableResourceKey objects
 /// </summary>
 /// <param name="other">The other BamlLocalizableResourceKey object to be compared against</param>
 /// <returns>True if they are equal. False otherwise</returns>        
 public bool Equals(BamlLocalizableResourceKey other)
 {
     if (other == null)
     {
         return false;
     }
 
     return _uid == other._uid
         && _className == other._className
         && _propertyName == other._propertyName; 
 }
        /// <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;
            }
        }
 /// <summary>
 /// determines whether the dictionary contains the localizable resource 
 /// with the specified key
 /// </summary>
 /// <param name="key"></param>
 /// <returns></returns>
 public bool Contains(BamlLocalizableResourceKey key)
 {
     CheckNonNullParam(key, "key");
     return _dictionary.ContainsKey(key);
 }
 internal void SetRootElementKey(BamlLocalizableResourceKey key)
 {
     _rootElementKey = key;
 }
 /// <summary>
 /// Adds a localizable resource with the provided key
 /// </summary>
 /// <param name="key">the BamlLocalizableResourceKey key</param>
 /// <param name="value">the BamlLocalizableResource</param>
 public void Add(BamlLocalizableResourceKey key, BamlLocalizableResource value)
 {
     CheckNonNullParam(key, "key");
     _dictionary.Add(key, value);
 }
 /// <summary>
 /// removes the localizable resource with the specified key
 /// </summary>
 /// <param name="key">the key</param>
 public void Remove(BamlLocalizableResourceKey key)
 {
     _dictionary.Remove(key);
 }
 /// <summary>
 /// Gets or sets a localizable resource by the key
 /// </summary>
 /// <param name="key">BamlLocalizableResourceKey key</param>
 /// <returns>BamlLocalizableResource object identified by the key</returns>
 public BamlLocalizableResource this[BamlLocalizableResourceKey key]
 {
     get 
     { 
         CheckNonNullParam(key, "key");
         return _dictionary[key];
     }
     set
     {
         CheckNonNullParam(key, "key");
         _dictionary[key] = value;                
     }
 }
Beispiel #19
0
 /// <summary>Compares two instances of <see cref="T:System.Windows.Markup.Localizer.BamlLocalizableResourceKey" /> for equality.</summary>
 /// <param name="other">The other instance of <see cref="T:System.Windows.Markup.Localizer.BamlLocalizableResourceKey" /> to compare for equality.</param>
 /// <returns>
 ///     <see langword="true" /> if the two instances are equal; otherwise, <see langword="false" />.</returns>
 // Token: 0x060024E0 RID: 9440 RVA: 0x000B297E File Offset: 0x000B0B7E
 public bool Equals(BamlLocalizableResourceKey other)
 {
     return(other != null && (this._uid == other._uid && this._className == other._className) && this._propertyName == other._propertyName);
 }
 public void Remove(BamlLocalizableResourceKey key)
 {
 }
 internal void SetRootElementKey(BamlLocalizableResourceKey key)
 {
     _rootElementKey = key;
 }
        //-------------------------------------------------
        // Internal static
        //-------------------------------------------------

        /// <summary>
        /// Return the localizable resource key for this baml tree node. 
        /// If this node shouldn't be localized, the key returned will be null.
        /// </summary>
        internal static BamlLocalizableResourceKey GetKey(BamlTreeNode node)
        {
            BamlLocalizableResourceKey key = null;

            switch (node.NodeType)
            {
                case BamlNodeType.StartElement:
                {                 
                    BamlStartElementNode elementNode = (BamlStartElementNode) node;
                    if (elementNode.Uid != null)
                    {
                        key = new BamlLocalizableResourceKey(
                            elementNode.Uid, 
                            elementNode.TypeFullName,
                            BamlConst.ContentSuffix,
                            elementNode.AssemblyName
                            );                            
                    }
                    break;
                }
                case BamlNodeType.Property:
                {
                    BamlPropertyNode propertyNode = (BamlPropertyNode) node;
                    BamlStartElementNode parent   = (BamlStartElementNode) propertyNode.Parent;

                    if (parent.Uid != null)
                    {
                        string uid; 
                        if (propertyNode.Index <= 0)
                        {                            
                            uid = parent.Uid;
                        }
                        else
                        {
                            // This node is auto-numbered. This has to do with the fact that 
                            // the compiler may compile duplicated properties into Baml under the same element. 
                            uid = string.Format(
                                TypeConverterHelper.InvariantEnglishUS,  
                                "{0}.{1}_{2}",
                                parent.Uid, 
                                propertyNode.PropertyName, 
                                propertyNode.Index
                                );
                        }

                        key = new BamlLocalizableResourceKey(
                            uid,
                            propertyNode.OwnerTypeFullName,
                            propertyNode.PropertyName,
                            propertyNode.AssemblyName
                            );
                    }       
                    break;
                }
                case BamlNodeType.LiteralContent:
                {
                    BamlLiteralContentNode literalNode = (BamlLiteralContentNode) node;
                    BamlStartElementNode parent = (BamlStartElementNode) node.Parent;

                    if (parent.Uid != null)
                    {
                        key = new BamlLocalizableResourceKey(
                            parent.Uid,
                            parent.TypeFullName,
                            BamlConst.LiteralContentSuffix,
                            parent.AssemblyName
                            );
                    }         
                    break;
                }
            }

            return key;
        }
 public void Remove(BamlLocalizableResourceKey key)
 {
 }
 /// <summary>
 /// Maps a key to a baml tree node in the given tree
 /// </summary>        
 internal BamlTreeNode MapKeyToBamlTreeNode(BamlLocalizableResourceKey key, BamlTree tree)
 {
     if (_keyToBamlNodeIndexMap.Contains(key))
     {
         return tree[(int)_keyToBamlNodeIndexMap[key]];
     }
     
     return null;
 }
 public bool Contains(BamlLocalizableResourceKey key)
 {
     return(default(bool));
 }
 public BamlLocalizableResource this [BamlLocalizableResourceKey key]
 {
   get
   {
     return default(BamlLocalizableResource);
   }
   set
   {
   }
 }
        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;
            }            
        }        
Beispiel #28
0
 internal bool IsNewBamlTreeNode(BamlLocalizableResourceKey key)
 {
     return _keyToNewBamlNodeIndexMap.Contains(key); 
 }
Beispiel #29
0
        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;
        }
 public void Add(BamlLocalizableResourceKey key, BamlLocalizableResource value)
 {
 }
Beispiel #31
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());
        } 
 /// <summary>
 /// removes the localizable resource with the specified key
 /// </summary>
 /// <param name="key">the key</param>
 public void Remove(BamlLocalizableResourceKey key)
 {
     _dictionary.Remove(key);
 }
Beispiel #33
0
        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 
        }
 /// <summary>
 /// determines whether the dictionary contains the localizable resource
 /// with the specified key
 /// </summary>
 /// <param name="key"></param>
 /// <returns></returns>
 public bool Contains(BamlLocalizableResourceKey key)
 {
     CheckNonNullParam(key, "key");
     return(_dictionary.ContainsKey(key));
 }
Beispiel #35
0
            internal BamlTreeNode MapKeyToBamlTreeNode(BamlLocalizableResourceKey key) 
            {
                BamlTreeNode node = _originalMap.MapKeyToBamlTreeNode(key, _tree); 
 
                if (node == null)
                { 
                    // find it in the new nodes
                    if (_keyToNewBamlNodeIndexMap.Contains(key))
                    {
                        node = _tree[(int)_keyToNewBamlNodeIndexMap[key]]; 
                    }
                } 
 
                return node;
            } 
 public void Add(BamlLocalizableResourceKey key, BamlLocalizableResource value)
 {
 }
Beispiel #37
0
            internal void AddBamlTreeNode(
                string                      uid, 
                BamlLocalizableResourceKey  key,
                BamlTreeNode                node 
                ) 
            {
                // add to node 
                _tree.AddTreeNode(node);

                // remember the tree node index
                if (uid != null) 
                {
                    _uidToNewBamlNodeIndexMap[uid] = _tree.Size - 1; 
                } 

                _keyToNewBamlNodeIndexMap[key] = _tree.Size - 1; 
            }
 /// <summary>
 /// Adds a localizable resource with the provided key
 /// </summary>
 /// <param name="key">the BamlLocalizableResourceKey key</param>
 /// <param name="value">the BamlLocalizableResource</param>
 public void Add(BamlLocalizableResourceKey key, BamlLocalizableResource value)
 {
     CheckNonNullParam(key, "key");
     _dictionary.Add(key, value);
 }