Exemple #1
0
        private void PopStack()
        {
            BamlTreeNode node = _bamlTreeStack.Pop();

            if (node.Children != null)
            {
                // pop properties from property inheritance stack as well
                foreach (BamlTreeNode child in node.Children)
                {
                    BamlStartComplexPropertyNode propertyNode = child as BamlStartComplexPropertyNode;
                    if (propertyNode != null)
                    {
                        PopPropertyFromStack(propertyNode.PropertyName);
                    }
                }
            }

            if (_bamlTreeStack.Count > 0)
            {
                _currentParent = _bamlTreeStack.Peek();
            }
            else
            {
                // stack is empty. Set CurrentParent to null
                _currentParent = null;
            }
        }
        // Token: 0x06006EB9 RID: 28345 RVA: 0x001FD22C File Offset: 0x001FB42C
        private void GetLocalizabilityForPropertyNode(BamlStartComplexPropertyNode node, out LocalizabilityAttribute localizability)
        {
            localizability = null;
            string assemblyName      = node.AssemblyName;
            string ownerTypeFullName = node.OwnerTypeFullName;
            string propertyName      = node.PropertyName;

            if (ownerTypeFullName == null || ownerTypeFullName.Length == 0)
            {
                string text;
                this.GetLocalizabilityForElementNode((BamlStartElementNode)node.Parent, out localizability, out text);
                return;
            }
            LocalizabilityGroup localizabilityComment = this._resolver.GetLocalizabilityComment((BamlStartElementNode)node.Parent, node.PropertyName);

            localizability = this._resolver.GetPropertyLocalizability(assemblyName, ownerTypeFullName, propertyName);
            if (localizabilityComment != null)
            {
                localizability = localizabilityComment.Override(localizability);
            }
        }
Exemple #3
0
        // Token: 0x06006E13 RID: 28179 RVA: 0x001FAF18 File Offset: 0x001F9118
        private void PopStack()
        {
            BamlTreeNode bamlTreeNode = this._bamlTreeStack.Pop();

            if (bamlTreeNode.Children != null)
            {
                foreach (BamlTreeNode bamlTreeNode2 in bamlTreeNode.Children)
                {
                    BamlStartComplexPropertyNode bamlStartComplexPropertyNode = bamlTreeNode2 as BamlStartComplexPropertyNode;
                    if (bamlStartComplexPropertyNode != null)
                    {
                        this.PopPropertyFromStack(bamlStartComplexPropertyNode.PropertyName);
                    }
                }
            }
            if (this._bamlTreeStack.Count > 0)
            {
                this._currentParent = this._bamlTreeStack.Peek();
                return;
            }
            this._currentParent = null;
        }
        private void GetLocalizabilityForPropertyNode(
            BamlStartComplexPropertyNode node,
            out LocalizabilityAttribute localizability
            )
        {
            localizability = null;

            string assemblyName      = node.AssemblyName;
            string className         = node.OwnerTypeFullName;
            string propertyLocalName = node.PropertyName;

            if (className == null || className.Length == 0)
            {
                // class name can be empty or null. For example, <Set PropertyPath="...">
                // We will use the parent node's value.
                string formattingTag;
                GetLocalizabilityForElementNode((BamlStartElementNode)node.Parent, out localizability, out formattingTag);
                return;
            }

            LocalizabilityGroup comment = _resolver.GetLocalizabilityComment(
                (BamlStartElementNode)node.Parent,
                node.PropertyName
                );

            localizability = _resolver.GetPropertyLocalizability(
                assemblyName,
                className,
                propertyLocalName
                );

            if (comment != null)
            {
                localizability = comment.Override(localizability);
            }
        }
        private void GetLocalizabilityForPropertyNode(
            BamlStartComplexPropertyNode node,
            out LocalizabilityAttribute  localizability
            )
        {
            localizability = null;           
            
            string assemblyName      = node.AssemblyName;
            string className         = node.OwnerTypeFullName; 
            string propertyLocalName = node.PropertyName;
          
            if (className == null || className.Length == 0)
            {
                // class name can be empty or null. For example, <Set PropertyPath="...">
                // We will use the parent node's value.  
                string formattingTag;                
                GetLocalizabilityForElementNode((BamlStartElementNode)node.Parent, out localizability, out formattingTag);
                return;
            }

            LocalizabilityGroup comment = _resolver.GetLocalizabilityComment(
                    (BamlStartElementNode) node.Parent, 
                    node.PropertyName
                    );            

            localizability = _resolver.GetPropertyLocalizability(
                    assemblyName, 
                    className, 
                    propertyLocalName
                    );

            if (comment != null)
            {
                localizability = comment.Override(localizability);
            }
        }
        /// <summary>
        /// Combine inheritable attributes, and propegate it down the tree.
        /// </summary>
        /// <param name="node">current node</param>
        /// <param name="localizabilityFromSource">localizability defined in source code</param>
        /// <returns>
        /// The LocalizabilityAttribute to used for this node. It is not the same as the
        /// inheritable attributes of the node when the node is set to Ignore.
        /// </returns>
        /// <remarks>We always walk the baml tree in depth-first order</remarks>
        private LocalizabilityAttribute CombineAndPropagateInheritanceValues(
            ILocalizabilityInheritable node,
            LocalizabilityAttribute localizabilityFromSource
            )
        {
            if (node == null)
            {
                return(localizabilityFromSource);
            }

            // If this node's inheritable localizability has been constructed, we can skip it
            // This can happen when recursively format the content
            if (node.InheritableAttribute != null)
            {
                return((!node.IsIgnored) ? node.InheritableAttribute : LocalizabilityIgnore);
            }

            // To test wether the current node needs to inherit values from parents.
            // It inherits values if:
            // o This node is set to Ignore, in which case it propagates parent values.
            // o Some of its attributes set to Inherit.
            if (localizabilityFromSource.Category != LocalizationCategory.Ignore &&
                localizabilityFromSource.Category != LocalizationCategory.Inherit &&
                localizabilityFromSource.Readability != Readability.Inherit &&
                localizabilityFromSource.Modifiability != Modifiability.Inherit)
            {
                // just return the same one because no value is inherited
                node.InheritableAttribute = localizabilityFromSource;
                return(node.InheritableAttribute);
            }

            // find the ancestor to inherit values now.
            ILocalizabilityInheritable ancestor = node.LocalizabilityAncestor;

            // find out the attribute that is inheritable from above
            LocalizabilityAttribute inheritableAttribute = ancestor.InheritableAttribute;

            if (inheritableAttribute == null)
            {
                // if ancestor's inheritable value isn't resolved yet, we recursively
                // resolve it here.
                BamlStartElementNode elementNode = ancestor as BamlStartElementNode;
                if (elementNode != null)
                {
                    string formattingTag;
                    GetLocalizabilityForElementNode(elementNode, out inheritableAttribute, out formattingTag);
                }
                else
                {
                    BamlStartComplexPropertyNode propertyNode = ancestor as BamlStartComplexPropertyNode;
                    GetLocalizabilityForPropertyNode(propertyNode, out inheritableAttribute);
                }

                CombineAndPropagateInheritanceValues(ancestor, inheritableAttribute);

                inheritableAttribute = ancestor.InheritableAttribute;
                Debug.Assert(inheritableAttribute != null);
            }

            // if this item is set to ignore
            if (localizabilityFromSource.Category == LocalizationCategory.Ignore)
            {
                // It propagates ancestor's inheritable localizability, but it will use
                // its own value declared in source.
                // We also mark this node as being "Ignored" in the inheritance tree to signal that
                // this node is not using the inheritance value.
                node.InheritableAttribute = inheritableAttribute;
                node.IsIgnored            = true;
                return(LocalizabilityIgnore);
            }

            // the item is not set to ignore, so we process the inheritable values
            BamlTreeNode treeNode = (BamlTreeNode)node;

            switch (treeNode.NodeType)
            {
            case BamlNodeType.StartElement:
            case BamlNodeType.LiteralContent:
            {
                // if everything set to inherit, we just return the inheritable localizability
                if (localizabilityFromSource.Category == LocalizationCategory.Inherit &&
                    localizabilityFromSource.Readability == Readability.Inherit &&
                    localizabilityFromSource.Modifiability == Modifiability.Inherit)
                {
                    // just propagate the ancestor's localizability.
                    node.InheritableAttribute = inheritableAttribute;
                }
                else
                {
                    // set new inherited values
                    node.InheritableAttribute = CreateInheritedLocalizability(
                        localizabilityFromSource,
                        inheritableAttribute
                        );
                }
                break;
            }

            case BamlNodeType.Property:
            case BamlNodeType.StartComplexProperty:
            {
                ILocalizabilityInheritable parent = (ILocalizabilityInheritable)treeNode.Parent;

                // Find the mininum localizability of the containing class and
                // parent property. Parent property means the proeprty from parent node that
                // has the same name.
                LocalizabilityAttribute inheritedAttribute = CombineMinimumLocalizability(
                    inheritableAttribute,
                    parent.InheritableAttribute
                    );

                node.InheritableAttribute = CreateInheritedLocalizability(
                    localizabilityFromSource,
                    inheritedAttribute
                    );

                if (parent.IsIgnored && localizabilityFromSource.Category == LocalizationCategory.Inherit)
                {
                    // If the parent node is Ignore and this property is set to inherit, then
                    // this property node is to be ignore as well. We set the the "Ignore" flag so that
                    // the node will always be ignored without looking at the source localizability again.
                    node.IsIgnored = true;
                    return(LocalizabilityIgnore);
                }
                break;
            }

            default:
            {
                Debug.Assert(false, "Can't process localizability attribute on nodes other than Element, Property and LiteralContent.");
                break;
            }
            }

            return(node.InheritableAttribute);
        }
        /// <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);
        }
Exemple #8
0
        //-----------------------------
        // private  methods
        //-----------------------------
        /// <summary>
        /// build the baml element tree as well as the localizability inheritance tree
        /// </summary>
        /// <param name="bamlSteam">input baml stream.</param>
        /// <returns>the tree constructed.</returns>
        private BamlTree LoadBamlImp(Stream bamlSteam)
        {
            _reader = new BamlReader(bamlSteam);
            _reader.Read();

            if (_reader.NodeType != BamlNodeType.StartDocument)
            {
                throw new XamlParseException(SR.Get(SRID.InvalidStartOfBaml));
            }

            // create root element.
            _root = new BamlStartDocumentNode();
            PushNodeToStack(_root);

            // A hash table used to handle duplicate properties within an element
            Hashtable propertyOccurrences = new Hashtable(8);

            // create the tree by depth first traversal.
            while (_bamlTreeStack.Count > 0 && _reader.Read())
            {
                switch (_reader.NodeType)
                {
                case BamlNodeType.StartElement:
                {
                    BamlTreeNode bamlNode = new BamlStartElementNode(
                        _reader.AssemblyName,
                        _reader.Name,
                        _reader.IsInjected,
                        _reader.CreateUsingTypeConverter
                        );
                    PushNodeToStack(bamlNode);
                    break;
                }

                case BamlNodeType.EndElement:
                {
                    BamlTreeNode bamlNode = new BamlEndElementNode();
                    AddChildToCurrentParent(bamlNode);
                    PopStack();
                    break;
                }

                case BamlNodeType.StartComplexProperty:
                {
                    BamlStartComplexPropertyNode bamlNode = new BamlStartComplexPropertyNode(
                        _reader.AssemblyName,
                        _reader.Name.Substring(0, _reader.Name.LastIndexOf('.')),
                        _reader.LocalName
                        );

                    bamlNode.LocalizabilityAncestor = PeekPropertyStack(bamlNode.PropertyName);
                    PushPropertyToStack(bamlNode.PropertyName, (ILocalizabilityInheritable)bamlNode);
                    PushNodeToStack(bamlNode);
                    break;
                }

                case BamlNodeType.EndComplexProperty:
                {
                    BamlTreeNode bamlNode = new BamlEndComplexPropertyNode();
                    AddChildToCurrentParent(bamlNode);
                    PopStack();
                    break;
                }

                case BamlNodeType.Event:
                {
                    BamlTreeNode bamlNode = new BamlEventNode(_reader.Name, _reader.Value);
                    AddChildToCurrentParent(bamlNode);
                    break;
                }

                case BamlNodeType.RoutedEvent:
                {
                    BamlTreeNode bamlNode = new BamlRoutedEventNode(
                        _reader.AssemblyName,
                        _reader.Name.Substring(0, _reader.Name.LastIndexOf('.')),
                        _reader.LocalName,
                        _reader.Value
                        );
                    AddChildToCurrentParent(bamlNode);
                    break;
                }

                case BamlNodeType.PIMapping:
                {
                    BamlTreeNode bamlNode = new BamlPIMappingNode(
                        _reader.XmlNamespace,
                        _reader.ClrNamespace,
                        _reader.AssemblyName
                        );
                    AddChildToCurrentParent(bamlNode);
                    break;
                }

                case BamlNodeType.LiteralContent:
                {
                    BamlTreeNode bamlNode = new BamlLiteralContentNode(_reader.Value);
                    AddChildToCurrentParent(bamlNode);
                    break;
                }

                case BamlNodeType.Text:
                {
                    BamlTreeNode bamlNode = new BamlTextNode(
                        _reader.Value,
                        _reader.TypeConverterAssemblyName,
                        _reader.TypeConverterName
                        );

                    AddChildToCurrentParent(bamlNode);
                    break;
                }

                case BamlNodeType.StartConstructor:
                {
                    BamlTreeNode bamlNode = new BamlStartConstructorNode();
                    AddChildToCurrentParent(bamlNode);
                    break;
                }

                case BamlNodeType.EndConstructor:
                {
                    BamlTreeNode bamlNode = new BamlEndConstructorNode();
                    AddChildToCurrentParent(bamlNode);
                    break;
                }

                case BamlNodeType.EndDocument:
                {
                    BamlTreeNode bamlNode = new BamlEndDocumentNode();
                    AddChildToCurrentParent(bamlNode);
                    PopStack();
                    break;
                }

                default:
                {
                    throw new XamlParseException(SR.Get(SRID.UnRecognizedBamlNodeType, _reader.NodeType));
                }
                }

                // read properties if it has any.
                if (_reader.HasProperties)
                {
                    // The Hashtable is used to auto-number repeated properties. The usage is as the following:
                    // When encountering the 1st occurrence of the property, it stores a reference to the property's node (BamlTreeNode).
                    // When encountering the property the 2nd time, we start auto-numbering the property including the 1st occurrence
                    // and store the index count (int) in the slot from that point onwards.
                    propertyOccurrences.Clear();

                    _reader.MoveToFirstProperty();
                    do
                    {
                        switch (_reader.NodeType)
                        {
                        case BamlNodeType.ConnectionId:
                        {
                            BamlTreeNode bamlNode = new BamlConnectionIdNode(_reader.ConnectionId);
                            AddChildToCurrentParent(bamlNode);
                            break;
                        }

                        case BamlNodeType.Property:
                        {
                            BamlPropertyNode bamlNode = new BamlPropertyNode(
                                _reader.AssemblyName,
                                _reader.Name.Substring(0, _reader.Name.LastIndexOf('.')),
                                _reader.LocalName,
                                _reader.Value,
                                _reader.AttributeUsage
                                );

                            bamlNode.LocalizabilityAncestor = PeekPropertyStack(bamlNode.PropertyName);
                            PushPropertyToStack(bamlNode.PropertyName, (ILocalizabilityInheritable)bamlNode);

                            AddChildToCurrentParent(bamlNode);

                            if (propertyOccurrences.Contains(_reader.Name))
                            {
                                // we autonumber properties that have occurrences larger than 1
                                object occurrence = propertyOccurrences[_reader.Name];
                                int    index      = 2;
                                if (occurrence is BamlPropertyNode)
                                {
                                    // start numbering this property as the 2nd occurrence is encountered
                                    // the value stores the 1st occurrence of the property at this point
                                    ((BamlPropertyNode)occurrence).Index = 1;
                                }
                                else
                                {
                                    // For the 3rd or more occurrences, the value stores the next index
                                    // to assign to the property
                                    index = (int)occurrence;
                                }

                                // auto-number the current property node
                                ((BamlPropertyNode)bamlNode).Index = index;
                                propertyOccurrences[_reader.Name]  = ++index;
                            }
                            else
                            {
                                // store the first occurrence of the property
                                propertyOccurrences[_reader.Name] = bamlNode;
                            }

                            break;
                        }

                        case BamlNodeType.DefAttribute:
                        {
                            if (_reader.Name == XamlReaderHelper.DefinitionUid)
                            {
                                // set the Uid proeprty when we see it.
                                ((BamlStartElementNode)_currentParent).Uid = _reader.Value;
                            }

                            BamlTreeNode bamlNode = new BamlDefAttributeNode(
                                _reader.Name,
                                _reader.Value
                                );
                            AddChildToCurrentParent(bamlNode);
                            break;
                        }

                        case BamlNodeType.XmlnsProperty:
                        {
                            BamlTreeNode bamlNode = new BamlXmlnsPropertyNode(
                                _reader.LocalName,
                                _reader.Value
                                );
                            AddChildToCurrentParent(bamlNode);
                            break;
                        }

                        case BamlNodeType.ContentProperty:
                        {
                            BamlTreeNode bamlNode = new BamlContentPropertyNode(
                                _reader.AssemblyName,
                                _reader.Name.Substring(0, _reader.Name.LastIndexOf('.')),
                                _reader.LocalName
                                );

                            AddChildToCurrentParent(bamlNode);
                            break;
                        }

                        case BamlNodeType.PresentationOptionsAttribute:
                        {
                            BamlTreeNode bamlNode = new BamlPresentationOptionsAttributeNode(
                                _reader.Name,
                                _reader.Value
                                );
                            AddChildToCurrentParent(bamlNode);
                            break;
                        }

                        default:
                        {
                            throw new XamlParseException(SR.Get(SRID.UnRecognizedBamlNodeType, _reader.NodeType));
                        }
                        }
                    } while (_reader.MoveToNextProperty());
                }
            }

            // At this point, the baml tree stack should be completely unwinded and also nothing more to read.
            if (_reader.Read() || _bamlTreeStack.Count > 0)
            {
                throw new XamlParseException(SR.Get(SRID.InvalidEndOfBaml));
            }

            return(new BamlTree(_root, _nodeCount));
            //notice that we don't close the input stream because we don't own it.
        }
        //-----------------------------
        // private  methods
        //-----------------------------
        /// <summary>
        /// build the baml element tree as well as the localizability inheritance tree
        /// </summary>
        /// <param name="bamlSteam">input baml stream.</param>
        /// <returns>the tree constructed.</returns>
        private BamlTree LoadBamlImp(Stream bamlSteam)
        {            
            _reader = new BamlReader(bamlSteam);
            _reader.Read();
            
            if (_reader.NodeType != BamlNodeType.StartDocument)
            {
                throw new XamlParseException(SR.Get(SRID.InvalidStartOfBaml));
            }

            // create root element.
            _root = new BamlStartDocumentNode();                       
            PushNodeToStack(_root);

            // A hash table used to handle duplicate properties within an element
            Hashtable propertyOccurrences = new Hashtable(8);                    

            // create the tree by depth first traversal.
            while (_bamlTreeStack.Count > 0 && _reader.Read())
            {                   
                switch (_reader.NodeType)
                {
                    case BamlNodeType.StartElement :
                    {
                        BamlTreeNode bamlNode = new BamlStartElementNode(
                            _reader.AssemblyName,
                            _reader.Name,
                            _reader.IsInjected,
                            _reader.CreateUsingTypeConverter
                            );
                        PushNodeToStack(bamlNode);
                        break;
                    }
                    case BamlNodeType.EndElement :
                    {
                        BamlTreeNode bamlNode = new BamlEndElementNode();
                        AddChildToCurrentParent(bamlNode);
                        PopStack();
                        break;                    
                    }
                    case BamlNodeType.StartComplexProperty :
                    {
                        BamlStartComplexPropertyNode bamlNode = new BamlStartComplexPropertyNode(
                            _reader.AssemblyName, 
                            _reader.Name.Substring(0, _reader.Name.LastIndexOf('.')),
                            _reader.LocalName
                            );
                        
                        bamlNode.LocalizabilityAncestor = PeekPropertyStack(bamlNode.PropertyName);
                        PushPropertyToStack(bamlNode.PropertyName, (ILocalizabilityInheritable) bamlNode);
                        PushNodeToStack(bamlNode);
                        break;
                    }
                    case BamlNodeType.EndComplexProperty :
                    {
                        BamlTreeNode bamlNode = new BamlEndComplexPropertyNode();
                        AddChildToCurrentParent(bamlNode);
                        PopStack();
                        break;
                    }
                    case BamlNodeType.Event :
                    {
                        BamlTreeNode bamlNode = new BamlEventNode(_reader.Name, _reader.Value);
                        AddChildToCurrentParent(bamlNode);
                        break;
                    }
                    case BamlNodeType.RoutedEvent :
                    {
                        BamlTreeNode bamlNode = new BamlRoutedEventNode(
                            _reader.AssemblyName, 
                            _reader.Name.Substring(0, _reader.Name.LastIndexOf('.')),
                            _reader.LocalName, 
                            _reader.Value
                            );                    
                        AddChildToCurrentParent(bamlNode);
                        break;
                    }
                    case BamlNodeType.PIMapping :
                    {
                        BamlTreeNode bamlNode = new BamlPIMappingNode(
                            _reader.XmlNamespace,
                            _reader.ClrNamespace,
                            _reader.AssemblyName
                            );
                        AddChildToCurrentParent(bamlNode);
                        break;
                    }
                    case BamlNodeType.LiteralContent :
                    {
                        BamlTreeNode bamlNode = new BamlLiteralContentNode(_reader.Value);
                        AddChildToCurrentParent(bamlNode);
                        break;
                    }
                    case BamlNodeType.Text :
                    {
                        BamlTreeNode bamlNode = new BamlTextNode(
                            _reader.Value, 
                            _reader.TypeConverterAssemblyName,
                            _reader.TypeConverterName
                            );
                            
                        AddChildToCurrentParent(bamlNode);
                        break;
                    }
                    case BamlNodeType.StartConstructor :                                    
                    {
                        BamlTreeNode bamlNode = new BamlStartConstructorNode();
                        AddChildToCurrentParent(bamlNode);
                        break;
                    }
                    case BamlNodeType.EndConstructor :            
                    {
                        BamlTreeNode bamlNode = new BamlEndConstructorNode();
                        AddChildToCurrentParent(bamlNode); 
                        break;
                    }
                    case BamlNodeType.EndDocument :
                    {
                        BamlTreeNode bamlNode = new BamlEndDocumentNode();
                        AddChildToCurrentParent(bamlNode);   
                        PopStack();
                        break;
                    }
                    default :
                    {
                        throw new XamlParseException(SR.Get(SRID.UnRecognizedBamlNodeType, _reader.NodeType));   
                    }                        
                }

                // read properties if it has any.
                if (_reader.HasProperties)
                {                           
                    // The Hashtable is used to auto-number repeated properties. The usage is as the following:
                    // When encountering the 1st occurrence of the property, it stores a reference to the property's node (BamlTreeNode).
                    // When encountering the property the 2nd time, we start auto-numbering the property including the 1st occurrence
                    // and store the index count (int) in the slot from that point onwards.                                         
                    propertyOccurrences.Clear();
                    
                    _reader.MoveToFirstProperty();
                    do
                    {    
                        switch (_reader.NodeType)
                        {
                            case BamlNodeType.ConnectionId:
                            {
                                BamlTreeNode bamlNode = new BamlConnectionIdNode(_reader.ConnectionId);
                                AddChildToCurrentParent(bamlNode);
                                break;
                            }
                            case BamlNodeType.Property :
                            {         
                                BamlPropertyNode bamlNode = new BamlPropertyNode(
                                    _reader.AssemblyName,
                                    _reader.Name.Substring(0, _reader.Name.LastIndexOf('.')),
                                    _reader.LocalName,
                                    _reader.Value,
                                    _reader.AttributeUsage                                    
                                    );

                                bamlNode.LocalizabilityAncestor = PeekPropertyStack(bamlNode.PropertyName);
                                PushPropertyToStack(bamlNode.PropertyName, (ILocalizabilityInheritable) bamlNode);
                                
                                AddChildToCurrentParent(bamlNode);

                                if (propertyOccurrences.Contains(_reader.Name))
                                {                 
                                    // we autonumber properties that have occurrences larger than 1
                                    object occurrence = propertyOccurrences[_reader.Name];
                                    int index = 2;
                                    if (occurrence is BamlPropertyNode)
                                    {                                    
                                        // start numbering this property as the 2nd occurrence is encountered
                                        // the value stores the 1st occurrence of the property at this point
                                        ((BamlPropertyNode) occurrence).Index = 1;
                                    }else
                                    {
                                        // For the 3rd or more occurrences, the value stores the next index 
                                        // to assign to the property
                                        index = (int) occurrence;
                                    }

                                    // auto-number the current property node
                                    ((BamlPropertyNode)bamlNode).Index = index;
                                    propertyOccurrences[_reader.Name] = ++index;                                    
                                }
                                else
                                {
                                    // store the first occurrence of the property
                                    propertyOccurrences[_reader.Name] = bamlNode;
                                }
                                
                                break;
                            }
                            case BamlNodeType.DefAttribute :
                            {
                                if (_reader.Name == XamlReaderHelper.DefinitionUid)
                                {            
                                    // set the Uid proeprty when we see it.
                                    ((BamlStartElementNode)_currentParent).Uid = _reader.Value;                                    
                                }                                  

                                BamlTreeNode bamlNode = new BamlDefAttributeNode(
                                    _reader.Name,
                                    _reader.Value
                                    );
                                AddChildToCurrentParent(bamlNode);                                                        
                                break;
                            }
                            case BamlNodeType.XmlnsProperty :
                            {             
                                BamlTreeNode bamlNode = new BamlXmlnsPropertyNode(
                                    _reader.LocalName,
                                    _reader.Value
                                    );
                                AddChildToCurrentParent(bamlNode);
                                break;                                
                            }                
                            case BamlNodeType.ContentProperty :
                            {
                                BamlTreeNode bamlNode = new BamlContentPropertyNode(
                                    _reader.AssemblyName, 
                                    _reader.Name.Substring(0, _reader.Name.LastIndexOf('.')),
                                    _reader.LocalName
                                    );

                                AddChildToCurrentParent(bamlNode);        
                                break;
                            }
                            case BamlNodeType.PresentationOptionsAttribute:
                            {
                                BamlTreeNode bamlNode = new BamlPresentationOptionsAttributeNode(
                                    _reader.Name, 
                                    _reader.Value
                                    );
                                AddChildToCurrentParent(bamlNode);                                            
                                break;
                            }
                            default :
                            {
                                throw new XamlParseException(SR.Get(SRID.UnRecognizedBamlNodeType, _reader.NodeType));
                            }
                        }
                    } while (_reader.MoveToNextProperty());                                    
                }                
            }

            // At this point, the baml tree stack should be completely unwinded and also nothing more to read.
            if (_reader.Read() || _bamlTreeStack.Count > 0)
            {
                throw new XamlParseException(SR.Get(SRID.InvalidEndOfBaml));
            }
            
            return new BamlTree(_root, _nodeCount);
            //notice that we don't close the input stream because we don't own it.
        }        
Exemple #10
0
        // Token: 0x06006E10 RID: 28176 RVA: 0x001FA87C File Offset: 0x001F8A7C
        private BamlTree LoadBamlImp(Stream bamlSteam)
        {
            this._reader = new BamlReader(bamlSteam);
            this._reader.Read();
            if (this._reader.NodeType != BamlNodeType.StartDocument)
            {
                throw new XamlParseException(SR.Get("InvalidStartOfBaml"));
            }
            this._root = new BamlStartDocumentNode();
            this.PushNodeToStack(this._root);
            Hashtable hashtable = new Hashtable(8);

IL_5C8:
            while (this._bamlTreeStack.Count > 0 && this._reader.Read())
            {
                switch (this._reader.NodeType)
                {
                case BamlNodeType.EndDocument:
                {
                    BamlTreeNode node = new BamlEndDocumentNode();
                    this.AddChildToCurrentParent(node);
                    this.PopStack();
                    break;
                }

                case BamlNodeType.ConnectionId:
                case BamlNodeType.Property:
                case BamlNodeType.ContentProperty:
                case BamlNodeType.XmlnsProperty:
                case BamlNodeType.IncludeReference:
                case BamlNodeType.DefAttribute:
                case BamlNodeType.PresentationOptionsAttribute:
                    goto IL_2DF;

                case BamlNodeType.StartElement:
                {
                    BamlTreeNode node2 = new BamlStartElementNode(this._reader.AssemblyName, this._reader.Name, this._reader.IsInjected, this._reader.CreateUsingTypeConverter);
                    this.PushNodeToStack(node2);
                    break;
                }

                case BamlNodeType.EndElement:
                {
                    BamlTreeNode node3 = new BamlEndElementNode();
                    this.AddChildToCurrentParent(node3);
                    this.PopStack();
                    break;
                }

                case BamlNodeType.StartComplexProperty:
                {
                    BamlStartComplexPropertyNode bamlStartComplexPropertyNode = new BamlStartComplexPropertyNode(this._reader.AssemblyName, this._reader.Name.Substring(0, this._reader.Name.LastIndexOf('.')), this._reader.LocalName);
                    bamlStartComplexPropertyNode.LocalizabilityAncestor = this.PeekPropertyStack(bamlStartComplexPropertyNode.PropertyName);
                    this.PushPropertyToStack(bamlStartComplexPropertyNode.PropertyName, bamlStartComplexPropertyNode);
                    this.PushNodeToStack(bamlStartComplexPropertyNode);
                    break;
                }

                case BamlNodeType.EndComplexProperty:
                {
                    BamlTreeNode node4 = new BamlEndComplexPropertyNode();
                    this.AddChildToCurrentParent(node4);
                    this.PopStack();
                    break;
                }

                case BamlNodeType.LiteralContent:
                {
                    BamlTreeNode node5 = new BamlLiteralContentNode(this._reader.Value);
                    this.AddChildToCurrentParent(node5);
                    break;
                }

                case BamlNodeType.Text:
                {
                    BamlTreeNode node6 = new BamlTextNode(this._reader.Value, this._reader.TypeConverterAssemblyName, this._reader.TypeConverterName);
                    this.AddChildToCurrentParent(node6);
                    break;
                }

                case BamlNodeType.RoutedEvent:
                {
                    BamlTreeNode node7 = new BamlRoutedEventNode(this._reader.AssemblyName, this._reader.Name.Substring(0, this._reader.Name.LastIndexOf('.')), this._reader.LocalName, this._reader.Value);
                    this.AddChildToCurrentParent(node7);
                    break;
                }

                case BamlNodeType.Event:
                {
                    BamlTreeNode node8 = new BamlEventNode(this._reader.Name, this._reader.Value);
                    this.AddChildToCurrentParent(node8);
                    break;
                }

                case BamlNodeType.PIMapping:
                {
                    BamlTreeNode node9 = new BamlPIMappingNode(this._reader.XmlNamespace, this._reader.ClrNamespace, this._reader.AssemblyName);
                    this.AddChildToCurrentParent(node9);
                    break;
                }

                case BamlNodeType.StartConstructor:
                {
                    BamlTreeNode node10 = new BamlStartConstructorNode();
                    this.AddChildToCurrentParent(node10);
                    break;
                }

                case BamlNodeType.EndConstructor:
                {
                    BamlTreeNode node11 = new BamlEndConstructorNode();
                    this.AddChildToCurrentParent(node11);
                    break;
                }

                default:
                    goto IL_2DF;
                }
                if (this._reader.HasProperties)
                {
                    hashtable.Clear();
                    this._reader.MoveToFirstProperty();
                    for (;;)
                    {
                        BamlNodeType nodeType = this._reader.NodeType;
                        switch (nodeType)
                        {
                        case BamlNodeType.ConnectionId:
                        {
                            BamlTreeNode node12 = new BamlConnectionIdNode(this._reader.ConnectionId);
                            this.AddChildToCurrentParent(node12);
                            break;
                        }

                        case BamlNodeType.StartElement:
                        case BamlNodeType.EndElement:
                            goto IL_58F;

                        case BamlNodeType.Property:
                        {
                            BamlPropertyNode bamlPropertyNode = new BamlPropertyNode(this._reader.AssemblyName, this._reader.Name.Substring(0, this._reader.Name.LastIndexOf('.')), this._reader.LocalName, this._reader.Value, this._reader.AttributeUsage);
                            bamlPropertyNode.LocalizabilityAncestor = this.PeekPropertyStack(bamlPropertyNode.PropertyName);
                            this.PushPropertyToStack(bamlPropertyNode.PropertyName, bamlPropertyNode);
                            this.AddChildToCurrentParent(bamlPropertyNode);
                            if (hashtable.Contains(this._reader.Name))
                            {
                                object obj = hashtable[this._reader.Name];
                                int    num = 2;
                                if (obj is BamlPropertyNode)
                                {
                                    ((BamlPropertyNode)obj).Index = 1;
                                }
                                else
                                {
                                    num = (int)obj;
                                }
                                bamlPropertyNode.Index       = num;
                                hashtable[this._reader.Name] = num + 1;
                            }
                            else
                            {
                                hashtable[this._reader.Name] = bamlPropertyNode;
                            }
                            break;
                        }

                        case BamlNodeType.ContentProperty:
                        {
                            BamlTreeNode node13 = new BamlContentPropertyNode(this._reader.AssemblyName, this._reader.Name.Substring(0, this._reader.Name.LastIndexOf('.')), this._reader.LocalName);
                            this.AddChildToCurrentParent(node13);
                            break;
                        }

                        case BamlNodeType.XmlnsProperty:
                        {
                            BamlTreeNode node14 = new BamlXmlnsPropertyNode(this._reader.LocalName, this._reader.Value);
                            this.AddChildToCurrentParent(node14);
                            break;
                        }

                        default:
                            if (nodeType != BamlNodeType.DefAttribute)
                            {
                                if (nodeType != BamlNodeType.PresentationOptionsAttribute)
                                {
                                    goto Block_6;
                                }
                                BamlTreeNode node15 = new BamlPresentationOptionsAttributeNode(this._reader.Name, this._reader.Value);
                                this.AddChildToCurrentParent(node15);
                            }
                            else
                            {
                                if (this._reader.Name == "Uid")
                                {
                                    ((BamlStartElementNode)this._currentParent).Uid = this._reader.Value;
                                }
                                BamlTreeNode node16 = new BamlDefAttributeNode(this._reader.Name, this._reader.Value);
                                this.AddChildToCurrentParent(node16);
                            }
                            break;
                        }
                        if (!this._reader.MoveToNextProperty())
                        {
                            goto IL_5C8;
                        }
                    }
Block_6:
IL_58F:
                    throw new XamlParseException(SR.Get("UnRecognizedBamlNodeType", new object[]
                    {
                        this._reader.NodeType
                    }));
                }
                continue;
IL_2DF:
                throw new XamlParseException(SR.Get("UnRecognizedBamlNodeType", new object[]
                {
                    this._reader.NodeType
                }));
            }
            if (this._reader.Read() || this._bamlTreeStack.Count > 0)
            {
                throw new XamlParseException(SR.Get("InvalidEndOfBaml"));
            }
            return(new BamlTree(this._root, this._nodeCount));
        }
        // Token: 0x06006EBA RID: 28346 RVA: 0x001FD2AC File Offset: 0x001FB4AC
        private LocalizabilityAttribute CombineAndPropagateInheritanceValues(ILocalizabilityInheritable node, LocalizabilityAttribute localizabilityFromSource)
        {
            if (node == null)
            {
                return(localizabilityFromSource);
            }
            if (node.InheritableAttribute != null)
            {
                if (node.IsIgnored)
                {
                    return(this.LocalizabilityIgnore);
                }
                return(node.InheritableAttribute);
            }
            else
            {
                if (localizabilityFromSource.Category != LocalizationCategory.Ignore && localizabilityFromSource.Category != LocalizationCategory.Inherit && localizabilityFromSource.Readability != Readability.Inherit && localizabilityFromSource.Modifiability != Modifiability.Inherit)
                {
                    node.InheritableAttribute = localizabilityFromSource;
                    return(node.InheritableAttribute);
                }
                ILocalizabilityInheritable localizabilityAncestor = node.LocalizabilityAncestor;
                LocalizabilityAttribute    inheritableAttribute   = localizabilityAncestor.InheritableAttribute;
                if (inheritableAttribute == null)
                {
                    BamlStartElementNode bamlStartElementNode = localizabilityAncestor as BamlStartElementNode;
                    if (bamlStartElementNode != null)
                    {
                        string text;
                        this.GetLocalizabilityForElementNode(bamlStartElementNode, out inheritableAttribute, out text);
                    }
                    else
                    {
                        BamlStartComplexPropertyNode node2 = localizabilityAncestor as BamlStartComplexPropertyNode;
                        this.GetLocalizabilityForPropertyNode(node2, out inheritableAttribute);
                    }
                    this.CombineAndPropagateInheritanceValues(localizabilityAncestor, inheritableAttribute);
                    inheritableAttribute = localizabilityAncestor.InheritableAttribute;
                }
                if (localizabilityFromSource.Category == LocalizationCategory.Ignore)
                {
                    node.InheritableAttribute = inheritableAttribute;
                    node.IsIgnored            = true;
                    return(this.LocalizabilityIgnore);
                }
                BamlTreeNode bamlTreeNode = (BamlTreeNode)node;
                BamlNodeType nodeType     = bamlTreeNode.NodeType;
                if (nodeType <= BamlNodeType.Property)
                {
                    if (nodeType != BamlNodeType.StartElement)
                    {
                        if (nodeType != BamlNodeType.Property)
                        {
                            goto IL_174;
                        }
                        goto IL_127;
                    }
                }
                else
                {
                    if (nodeType == BamlNodeType.StartComplexProperty)
                    {
                        goto IL_127;
                    }
                    if (nodeType != BamlNodeType.LiteralContent)
                    {
                        goto IL_174;
                    }
                }
                if (localizabilityFromSource.Category == LocalizationCategory.Inherit && localizabilityFromSource.Readability == Readability.Inherit && localizabilityFromSource.Modifiability == Modifiability.Inherit)
                {
                    node.InheritableAttribute = inheritableAttribute;
                    goto IL_174;
                }
                node.InheritableAttribute = this.CreateInheritedLocalizability(localizabilityFromSource, inheritableAttribute);
                goto IL_174;
IL_127:
                ILocalizabilityInheritable localizabilityInheritable = (ILocalizabilityInheritable)bamlTreeNode.Parent;
                LocalizabilityAttribute inheritable = this.CombineMinimumLocalizability(inheritableAttribute, localizabilityInheritable.InheritableAttribute);
                node.InheritableAttribute = this.CreateInheritedLocalizability(localizabilityFromSource, inheritable);
                if (localizabilityInheritable.IsIgnored && localizabilityFromSource.Category == LocalizationCategory.Inherit)
                {
                    node.IsIgnored = true;
                    return(this.LocalizabilityIgnore);
                }
IL_174:
                return(node.InheritableAttribute);
            }
        }
        // Token: 0x06006EB5 RID: 28341 RVA: 0x001FCDA0 File Offset: 0x001FAFA0
        internal BamlLocalizableResource BuildFromNode(BamlLocalizableResourceKey key, BamlTreeNode node)
        {
            if (node.Formatted)
            {
                return(null);
            }
            BamlLocalizableResource bamlLocalizableResource = null;
            LocalizabilityAttribute localizabilityAttribute = null;
            BamlStartElementNode    node2 = null;
            string       localName        = null;
            BamlNodeType nodeType         = node.NodeType;

            if (nodeType != BamlNodeType.StartElement)
            {
                if (nodeType != BamlNodeType.Property)
                {
                    if (nodeType != BamlNodeType.LiteralContent)
                    {
                        Invariant.Assert(false);
                    }
                    else
                    {
                        string text;
                        this.GetLocalizabilityForElementNode((BamlStartElementNode)node.Parent, out localizabilityAttribute, out text);
                        node2     = (BamlStartElementNode)node.Parent;
                        localName = "$Content";
                    }
                }
                else
                {
                    BamlStartComplexPropertyNode bamlStartComplexPropertyNode = (BamlStartComplexPropertyNode)node;
                    if (LocComments.IsLocCommentsProperty(bamlStartComplexPropertyNode.OwnerTypeFullName, bamlStartComplexPropertyNode.PropertyName) || LocComments.IsLocLocalizabilityProperty(bamlStartComplexPropertyNode.OwnerTypeFullName, bamlStartComplexPropertyNode.PropertyName))
                    {
                        return(null);
                    }
                    this.GetLocalizabilityForPropertyNode(bamlStartComplexPropertyNode, out localizabilityAttribute);
                    localName = bamlStartComplexPropertyNode.PropertyName;
                    node2     = (BamlStartElementNode)node.Parent;
                }
            }
            else
            {
                node2 = (BamlStartElementNode)node;
                string text;
                this.GetLocalizabilityForElementNode(node2, out localizabilityAttribute, out text);
                localName = "$Content";
            }
            localizabilityAttribute = this.CombineAndPropagateInheritanceValues(node as ILocalizabilityInheritable, localizabilityAttribute);
            string content = null;

            if (localizabilityAttribute.Category != LocalizationCategory.NeverLocalize && localizabilityAttribute.Category != LocalizationCategory.Ignore && this.TryGetContent(key, node, out content))
            {
                bamlLocalizableResource            = new BamlLocalizableResource();
                bamlLocalizableResource.Readable   = (localizabilityAttribute.Readability == Readability.Readable);
                bamlLocalizableResource.Modifiable = (localizabilityAttribute.Modifiability == Modifiability.Modifiable);
                bamlLocalizableResource.Category   = localizabilityAttribute.Category;
                bamlLocalizableResource.Content    = content;
                bamlLocalizableResource.Comments   = this._resolver.GetStringComment(node2, localName);
            }
            return(bamlLocalizableResource);
        }