Exemple #1
0
        internal PositionXmlElement(string prefix, string localName, string namespaceURI, PositionXmlDocument doc,
                                    IXmlLineInfo lineInfo)
            : base(prefix, localName, namespaceURI, doc)
        {
            if (lineInfo != null)
            {
                xamlElementLineInfo = new XamlElementLineInfo(lineInfo.LineNumber, lineInfo.LinePosition);
            }

            this.positionXmlDocument = doc;
        }
Exemple #2
0
        static void WalkElement(XElement e, Dictionary <XamlElementLineInfo, XamlElementLineInfo> mappingOldNewLineInfo)
        {
            var info        = (IXmlLineInfo)e;
            var newLineInfo = new XamlElementLineInfo(lineNumber, currentColumn);

            mappingOldNewLineInfo.Add(new XamlElementLineInfo(info.LineNumber, info.LinePosition), newLineInfo);

            Console.WriteLine(info.LinePosition);
            NewLine();
            string prefix1 = e.GetPrefixOfNamespace(e.Name.Namespace);
            string name1   = prefix1 == null ? e.Name.LocalName : prefix1 + ":" + e.Name.LocalName;

            newLineInfo.Position = sb.Length;
            Append("<" + name1);

            List <AttributeString> list = new List <AttributeString>();
            int length = name1.Length;

            foreach (var a in e.Attributes())
            {
                string prefix2 = e.GetPrefixOfNamespace(a.Name.Namespace);
                var    g       = new AttributeString()
                {
                    Name = a.Name, Prefix = prefix2, Value = a.Value
                };
                list.Add(g);
                length += g.FinalString.Length;
            }

            list.Sort(AttributeComparrer.Instance);

            if (length > LengthBeforeNewLine)
            {
                nextColumn = currentColumn + 1;
                for (int i = 0; i < list.Count; i++)
                {
                    if (i > 0)
                    {
                        NewLine();
                    }
                    else
                    {
                        Append(" ");
                    }
                    Append(list[i].FinalString);
                }
                nextColumn -= name1.Length + 2;
            }
            else
            {
                foreach (var a in list)
                {
                    Append(" " + a.FinalString);
                }
            }

            if (e.Nodes().Count() > 0)
            {
                Append(">");
                nextColumn += Indenation;

                WalkContainer(e, mappingOldNewLineInfo);

                nextColumn -= Indenation;
                NewLine();
                Append("</" + name1 + ">");
            }
            else
            {
                Append(" />");
            }

            newLineInfo.Length = sb.Length - newLineInfo.Position;
        }
Exemple #3
0
        public override void WriteTo(XmlWriter w)
        {
            if (positionXmlDocument.oldWriter != w)
            {
                try
                {
                    positionXmlDocument.oldWriter            = w;
                    positionXmlDocument.lineCnt              = 0;
                    positionXmlDocument.linePositionPrevious = 0;

                    var xmlWriterField = w.GetType()
                                         .GetField("xmlWriter", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
                    if (xmlWriterField != null)
                    {
                        var xmlwriter   = xmlWriterField.GetValue(w);
                        var rawTextWPrp = xmlwriter.GetType()
                                          .GetProperty("InnerWriter",
                                                       BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
                        var rawTextW      = rawTextWPrp.GetValue(xmlwriter, null);
                        var bufCharsField = rawTextW.GetType()
                                            .GetField("bufChars", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
                        var contentPosField = rawTextW.GetType()
                                              .GetField("contentPos",
                                                        BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
                        var buffPosField = rawTextW.GetType()
                                           .GetField("bufPos", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
                        var ioTextWriterField = rawTextW.GetType()
                                                .GetField("writer", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
                        var ioTextWriter = ioTextWriterField.GetValue(rawTextW);
                        var sbField      = ioTextWriter.GetType()
                                           .GetField("_sb", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
                        positionXmlDocument.writerSb = sbField.GetValue(ioTextWriter) as StringBuilder;

                        positionXmlDocument.bufGetter =
                            Expression
                            .Lambda <Func <char[]> >(Expression.Field(Expression.Constant(rawTextW), bufCharsField))
                            .Compile();
                        positionXmlDocument.contentPosFieldGetter =
                            Expression
                            .Lambda <Func <int> >(Expression.Field(Expression.Constant(rawTextW), contentPosField))
                            .Compile();
                        positionXmlDocument.buffPosGetter =
                            Expression.Lambda <Func <int> >(Expression.Field(Expression.Constant(rawTextW), buffPosField))
                            .Compile();
                    }
                }
                catch (Exception)
                {
                }
            }

            if (positionXmlDocument.bufGetter != null && positionXmlDocument.buffPosGetter != null &&
                positionXmlDocument.writerSb != null)
            {
                try
                {
                    var buff = positionXmlDocument.bufGetter();
                    var pos  = positionXmlDocument.buffPosGetter();
                    for (int n = pos; n >= positionXmlDocument.lastCharacterPos; n--)
                    {
                        if (buff[n] == '\n')
                        {
                            positionXmlDocument.lineCnt++;
                        }
                    }

                    this.xamlElementLineInfo = new XamlElementLineInfo(positionXmlDocument.lineCnt + 1,
                                                                       pos + 1 + positionXmlDocument.writerSb.Length);

                    if (buff[pos - 1] != '>')
                    {
                        this.xamlElementLineInfo.LinePosition++;
                    }

                    this.xamlElementLineInfo.Position = pos + positionXmlDocument.writerSb.Length;
                }
                catch (Exception)
                {
                }
            }

            base.WriteTo(w);

            if (positionXmlDocument.bufGetter != null && positionXmlDocument.buffPosGetter != null &&
                positionXmlDocument.writerSb != null)
            {
                try
                {
                    var pos = positionXmlDocument.buffPosGetter();
                    this.xamlElementLineInfo.Length = pos + positionXmlDocument.writerSb.Length -
                                                      this.xamlElementLineInfo.Position;
                }
                catch (Exception)
                {
                }
            }
        }