예제 #1
0
        XamlTextNode getPropertyValueNode()
        {
            XamlTextNode n = new XamlTextNode(
                reader.LineNumber,
                reader.LinePosition,
                getDepth(),
                reader.Value);

            if (n.TextContent.StartsWith("{StaticResource "))
            {
                n.setkeyText(n.TextContent.Remove(0, "{StaticResource ".Length).TrimEnd('}'));
            }
            return(n);
        }
예제 #2
0
        public void TagWithValue(string xamlText, int endLineTag, int endPosTag, int startLineText, int startPosText, int endLineText, int endPosText)
        {
            XamlParser parser = new XamlParser(Errors);
            //                                                         12345678901234
            XamlMainObjectNode mainNode = parser.ParseFromString(xamlText);

            Assert.IsNotNull(mainNode, "It must be parse result");
            Assert.AreEqual(
                XamlNodeBase.EState.Closed | XamlNodeBase.EState.EndTagPresent | XamlNodeBase.EState.TextNodePresent,
                mainNode.State,
                "Node state");
            Assert.AreEqual("tag", mainNode.Name, "Wrong tag name");
            Assert.AreEqual(0, mainNode.Attributes.Count, "Wrong number of attributes");
            Assert.AreEqual(1, mainNode.Children.Count, "Wrong number of children");
            CheckTextRange("Tag:", 1, endLineTag, 1, endPosTag, mainNode);

            XamlTextNode textNode = mainNode.Children[0] as XamlTextNode;

            Assert.IsNotNull(textNode, "It must be text node as first child");
            Assert.AreEqual("123", textNode.Value, "Not expected text value");
            CheckTextRange("Text:", startLineText, endLineText, startPosText, endPosText, textNode);
        }
예제 #3
0
        /// <summary> 
        /// Write Text node and do template related error checking 
        /// </summary>
        public override void WriteText(XamlTextNode xamlTextNode) 
        {
            StyleMode mode = _styleModeStack.Mode;

            // Text is only valid within certain locations in the Template section. 
            // Check for all the valid locations and write out the text record.  For
            // all other locations, see if the text is non-blank and throw an error 
            // if it is.  Ignore any whitespace, since this is not considered 
            // significant in Style cases.
            if (mode == StyleMode.TargetTypeProperty) 
            {
                // Remember the TargetType so that the setter parsing could use it
                // to resolve non-qualified property names
                if (xamlTextNode.Text != null) 
                {
                    _templateTargetTypeType = XamlTypeMapper.GetTypeFromBaseString(xamlTextNode.Text, 
                                                                                   ParserContext, 
                                                                                   true);
                } 
            }

#if PBTCOMPILER
            if (mode == StyleMode.DataTypeProperty && 
                InDeferLoadedSection &&
                !_defNameFound) 
            { 
                // We have to treat DataType="{x:Type SomeType}" as a key in a
                // resource dictionary, if one is present.  This means generating 
                // a series of baml records to use as the key for the defer loaded
                // body of the Style in addition to generating the records to set
                // the TargetType value.
                base.WriteText(xamlTextNode); 
            }
#endif 
            // Text is only valid within certain locations in the <Template> section. 
            // Check for all the valid locations and write out the text record.  For
            // all other locations, see if the text is non-blank and throw an error 
            // if it is.  Ignore any whitespace, since this is not considered
            // significant in Template cases.
            if (mode != StyleMode.TriggerBase ||
                _inSetterDepth >= 0 || 
                _triggerComplexPropertyDepth >= 0)
            { 
                base.WriteText(xamlTextNode); 
            }
            else 
            {
                for (int i = 0; i< xamlTextNode.Text.Length; i++)
                {
                    if (!XamlReaderHelper.IsWhiteSpace(xamlTextNode.Text[i])) 
                    {
                        ThrowException(SRID.TemplateTextNotSupported, xamlTextNode.Text, 
                               xamlTextNode.LineNumber, xamlTextNode.LinePosition); 
                    }
                } 
            }
        }
예제 #4
0
		XamlTextNode getPropertyValueNode()
		{
			XamlTextNode n = new XamlTextNode(
						reader.LineNumber,
						reader.LinePosition,
						getDepth(),
						reader.Value);
			if (n.TextContent.StartsWith("{StaticResource ")) {
				n.setkeyText(n.TextContent.Remove(0, "{StaticResource ".Length).TrimEnd('}'));
			}
			return n;
		}
예제 #5
0
파일: XamlParser.cs 프로젝트: JianwenSun/cc
 /// <summary>
 /// Write out Text, currently don't keep track if originally CData or Text
 /// </summary>
 public virtual void WriteText(XamlTextNode xamlTextNode)
 {
     if (BamlRecordWriter != null)
     {
         BamlRecordWriter.WriteText(xamlTextNode);
     }
 }
        // Write text content to baml
        internal void WriteText(XamlTextNode xamlTextNode)
        {
            BamlTextRecord bamlText;
            if (xamlTextNode.ConverterType == null)
            {
                if (!InStaticResourceSection && !InDynamicResourceSection)
                {
                    bamlText = (BamlTextRecord) BamlRecordManager.GetWriteRecord(BamlRecordType.Text);
                }
                else
                {
                    bamlText = (BamlTextWithIdRecord) BamlRecordManager.GetWriteRecord(BamlRecordType.TextWithId);

                    // Store the string value in the string table, since these records
                    // are often used as the key for a [Static/Dynamic]Resource.
                    short valueId;
                    if (!MapTable.GetStringInfoId(xamlTextNode.Text,
                                                  out valueId))
                    {
                        valueId = MapTable.AddStringInfoMap(BinaryWriter,
                                                             xamlTextNode.Text);
                    }

                    ((BamlTextWithIdRecord)bamlText).ValueId = valueId;
                }
            }
            else
            {
                bamlText = (BamlTextWithConverterRecord)BamlRecordManager.GetWriteRecord(BamlRecordType.TextWithConverter);

                short typeId;
                string converterAssemblyFullName = xamlTextNode.ConverterType.Assembly.FullName;
                string converterTypeFullName = xamlTextNode.ConverterType.FullName;

                // If we do not already have a type record for the type of this converter,
                // then add a new TypeInfo record to the map table.
                if (!MapTable.GetTypeInfoId(BinaryWriter, converterAssemblyFullName,
                                            converterTypeFullName,
                                            out typeId))
                {
                    typeId = MapTable.AddTypeInfoMap(BinaryWriter,
                                                     converterAssemblyFullName,
                                                     converterTypeFullName,
                                                     xamlTextNode.ConverterType,
                                                     string.Empty,
                                                     string.Empty);
                }
                ((BamlTextWithConverterRecord)bamlText).ConverterTypeId = typeId;
            }

             bamlText.Value = xamlTextNode.Text;

             // up the parent node count, wait to update until endElement.

             // add text to the Tree.
            WriteAndReleaseRecord(bamlText, xamlTextNode);
        }
예제 #7
0
        public XamlMainObjectNode Parse(StreamReader fileStream)
        {
            if (fileStream == null)
            {
                throw new ArgumentNullException(nameof(fileStream));
            }

            Stack <XamlObjectNode> objStack    = new Stack <XamlObjectNode>();
            XamlObjectNode         currentNode = null;
            XamlMainObjectNode     mainObject  = null;

            SymbolScanner scanner = new SymbolScanner(fileStream);

            int lineNumberStart   = 0;
            int linePositionStart = 0;
            //long newLineStreamPosition = fileStream.BaseStream.Position;
            string ident;

            //bool tagBracketClosed = false;
            while (!fileStream.EndOfStream)
            {
                scanner.SkipSpaces();
                char ch = scanner.GetSymbol();
                if (ch == '\0')
                {
                    break;
                }

                if (ch == SymbolScanner.SymbolStartTag)
                {
                    //tagBracketClosed = false;
                    lineNumberStart = scanner.LineNumber;
                    //open tag symbol must be start not the next symbol
                    linePositionStart = scanner.CharPosition - 1;
                    char chNext = scanner.NextSymbol;
                    if (chNext == SymbolScanner.SymbolCloseTag)
                    {
                        scanner.SkipSymbol();
                        ident = scanner.GetName();
                        if (DoEndTag(ident, scanner))
                        {
                            CloseTag(ident, scanner, objStack);
                        }
                    }
                    else if (chNext == SymbolScanner.SymbolStartComment)
                    {
                        string comment = scanner.ReadComment();
                        Trace.WriteLine($"{lineNumberStart + 1}:{linePositionStart + 1} Comment:{comment}");
                        XamlCommentNode commentNode = new XamlCommentNode
                        {
                            LineNumberStart   = lineNumberStart + 1,
                            LinePositionStart = linePositionStart + 1,
                            Comment           = comment
                        };
                        commentNode.LineNumberEnd = scanner.LineNumber + 1;
                        //end position is outside comment tag
                        commentNode.LinePositionEnd = scanner.CharPosition;
                        objStack.Push(commentNode);
                    }
                    else
                    {
                        char breakSymbol;
                        ident       = scanner.GetName();
                        breakSymbol = scanner.NextSymbol;
                        //ident = GetIdent(fileStream, out breakSymbol);
                        Trace.WriteLine($"{lineNumberStart + 1}:{linePositionStart + 1} Start Tag:{ident}");
                        if (mainObject == null)
                        {
                            mainObject = new XamlMainObjectNode {
                                LineNumberStart = lineNumberStart + 1, LinePositionStart = linePositionStart + 1
                            };
                            mainObject.Name = ident;
                            currentNode     = mainObject;
                            //root.MainObject = mainObject;
                            objStack.Push(mainObject);
                        }
                        else
                        {
                            currentNode = new XamlObjectNode {
                                LineNumberStart = lineNumberStart + 1, LinePositionStart = linePositionStart + 1
                            };
                            currentNode.Name = ident;
                            objStack.Push(currentNode);
                        }

                        if (breakSymbol != SymbolScanner.SymbolEndTag)
                        {
                            scanner.SkipSpaces();
                            breakSymbol = scanner.NextSymbol;
                        }

                        if (breakSymbol == SymbolScanner.SymbolCloseTag)
                        {
                            //end of tag
                            if (DoEndTag(ident, scanner))
                            {
                                CloseTag(ident, scanner, objStack);
                            }
                        }
                        else if (breakSymbol != SymbolScanner.SymbolEndTag)
                        {
                            XamlObjectNode objectNode = null;
                            if (objStack.Count > 0)
                            {
                                objectNode = objStack.Peek();
                            }

                            bool isClosed = ReadAttributes(objectNode, scanner, breakSymbol);
                            if (isClosed)
                            {
                                //tagBracketClosed = true;
                                if (objectNode != null)
                                {
                                    objectNode.SetState(XamlNodeBase.EState.EndTagPresent);
                                    //objectNode.IsTagBracketClosed = true;
                                }

                                Trace.WriteLine("Tag end symbol for:" + ident);
                            }

                            if (scanner.NextSymbol == SymbolScanner.SymbolCloseTag)
                            {
                                //end of tag
                                if (DoEndTag(ident, scanner))
                                {
                                    CloseTag(ident, scanner, objStack);
                                }
                            }
                        }
                        else
                        {
                            // skip '>'
                            scanner.SkipSymbol();

                            XamlObjectNode objectNode = objStack.Peek();
                            objectNode.SetState(XamlNodeBase.EState.EndTagPresent);
                            Trace.WriteLine("Tag end symbol for:" + ident);
                        }

                        //chNext = scanner.NextSymbol;
                    }
                }
                else
                {
                    if (objStack.Count > 0)
                    {
                        XamlObjectNode objectNode = objStack.Peek();
                        if (objectNode.IsState(XamlNodeBase.EState.EndTagPresent))
                        {
                            lineNumberStart = scanner.LineNumber;
                            //text symbol must be current not the next symbol
                            linePositionStart = scanner.CharPosition - 1;

                            string text = scanner.ReadText(ch);
                            Trace.WriteLine("Text node:" + text);
                            XamlTextNode textNode = new XamlTextNode(text)
                            {
                                LineNumberStart = lineNumberStart + 1, LinePositionStart = linePositionStart + 1
                            };
                            textNode.LineNumberEnd = scanner.LastTextLineNumberEnd + 1;
                            // it must be the next symbol
                            textNode.LinePositionEnd = scanner.LastTextCharPositionEnd;
                            objectNode.SetState(XamlNodeBase.EState.TextNodePresent);
                            objectNode.AddChild(textNode);
                        }
                        else
                        {
                            //Trace.Write(ch);
                            _errorHandler?.Error($"Not expected symbol :'{ch}'", scanner);
                        }
                    }
                    else
                    {
                        //Trace.Write(ch);
                        _errorHandler?.Error($"Not expected symbol, no tag found :'{ch}'", scanner);
                    }
                }

                char chNext1 = scanner.CheckEndOfLine(scanner.NextSymbol);
            }

            if (objStack.Count > 0)
            {
                //ignore exception by non closed main state. Temporary
                if (mainObject != null && objStack.Count == 1)
                {
                    if (!mainObject.IsState(XamlNodeBase.EState.Closed))
                    {
                        return(mainObject);
                    }
                }
                _errorHandler?.Error($"unused {objStack.Count} nodes", scanner);
            }

            return(mainObject);
        }