Ejemplo n.º 1
0
        public static void Serialize(TextBuffer buffer, TextIter start, TextIter end, XmlTextWriter xmlWriter)
        {
            TextIter    currentIter    = start;
            TextIter    nextIter       = start;
            bool        readingValue   = false;
            bool        readingText    = false;
            string      attributeName  = "";
            string      attributeValue = "";
            string      elementText    = "";
            DocumentTag docTag;

            nextIter.ForwardChar();

            // We iterate over all the buffer.
            while (!currentIter.Equal(end))
            {
                ArrayList beginTags, endTags;
                beginTags = new ArrayList();
                endTags   = new ArrayList();
                GetArrays(currentIter, nextIter, beginTags, endTags);

                        #if DEBUG
                Console.WriteLine("Offset: {0} Char: {1}", currentIter.Offset, currentIter.Char);
                        #endif

                if (readingValue)
                {
                    attributeValue += currentIter.Char;
                }

                if (readingText)
                {
                    elementText += currentIter.Char;
                }

                foreach (TextTag tag in beginTags)
                {
                                #if DEBUG
                    Console.WriteLine("Begin Tags: {0} Begins: {1} Ends: {2}", tag.Name, currentIter.BeginsTag(tag) ? "True" : "False", DocumentUtils.TagEndsHere(tag, currentIter, nextIter)? "True" : "False");
                                #endif

                    docTag = tag as DocumentTag;

                    if (docTag.IsElement)
                    {
                        string elementName = docTag.Name.Split('#')[0];
                        xmlWriter.WriteStartElement(null, elementName, null);

                                        #if DEBUG
                        Console.WriteLine("Wrote Start Element: " + elementName);
                                        #endif
                    }
                    else if (docTag.IsAttribute)
                    {
                        attributeName = docTag.Name.Split(':')[1].Split('#')[0];
                        xmlWriter.WriteStartAttribute(null, attributeName, null);

                                        #if DEBUG
                        Console.WriteLine("Wrote Start Attribute: {0}", attributeName);
                                        #endif

                        readingValue   = true;
                        attributeValue = currentIter.Char;
                    }
                    else if (docTag.IsSerializableText)
                    {
                        readingText = true;
                        elementText = currentIter.Char;
                    }
                }

                foreach (TextTag tag in endTags)
                {
                                #if DEBUG
                    Console.WriteLine("End Tags: {0} Begins: {1} Ends: {2}", tag.Name, currentIter.BeginsTag(tag) ? "True" : "False", DocumentUtils.TagEndsHere(tag, currentIter, nextIter)? "True" : "False");
                                #endif

                    docTag = tag as DocumentTag;
                    if (docTag.IsElement)
                    {
                        xmlWriter.WriteEndElement();

                                        #if DEBUG
                        Console.WriteLine("Wrote End Element: " + docTag.Name);
                                        #endif
                    }
                    else if (docTag.IsAttribute)
                    {
                        xmlWriter.WriteString(attributeValue);
                        xmlWriter.WriteEndAttribute();

                                        #if DEBUG
                        Console.WriteLine("Wrote End Attribute: {0} Value: {1}", attributeName, attributeValue);
                                        #endif

                        readingValue   = false;
                        attributeValue = attributeName = String.Empty;
                    }
                    else if (docTag.IsSerializableText)
                    {
                        if (docTag.Name.IndexOf("significant-whitespace") != -1)
                        {
                            xmlWriter.WriteString(DocumentUtils.Unescape(elementText));
                        }
                        else
                        {
                            xmlWriter.WriteString(elementText);
                        }

                        elementText = String.Empty;
                        readingText = false;
                    }
                }

                currentIter.ForwardChar();
                nextIter.ForwardChar();

                        #if DEBUG
                Console.WriteLine("State: {0} Char: {1} \n", xmlWriter.WriteState.ToString(), currentIter.Char);
                        #endif

//			while (Application.EventsPending ())
//				Application.RunIteration ();
            }
        }