//----------------------------- // 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. }
// 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)); }