/// <summary>parses an xml stream to create an AtomCategory object</summary>
        /// <param name="reader">correctly positioned xmlreader</param>
        /// <param name="owner">the object containing the person</param>
        /// <returns> the created AtomCategory object</returns>
        protected AtomCategory ParseCategory(XmlReader reader, AtomBase owner)
        {
            Tracing.TraceCall();
            Tracing.Assert(reader != null, "reader should not be null");
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }
            if (owner == null)
            {
                throw new ArgumentNullException("owner");
            }

            AtomCategory category = owner.CreateAtomSubElement(reader, this) as AtomCategory;

            if (category != null)
            {
                bool noChildren = reader.IsEmptyElement;
                if (reader.HasAttributes)
                {
                    while (reader.MoveToNextAttribute())
                    {
                        object localname = reader.LocalName;
                        if (localname.Equals(this.nameTable.Term))
                        {
                            category.Term = Utilities.DecodedValue(reader.Value);
                        }
                        else if (localname.Equals(this.nameTable.Scheme))
                        {
                            category.Scheme = new AtomUri(reader.Value);
                        }
                        else if (localname.Equals(this.nameTable.Label))
                        {
                            category.Label = Utilities.DecodedValue(reader.Value);
                        }
                        else
                        {
                            ParseBaseAttributes(reader, category);
                        }
                    }
                }

                if (!noChildren)
                {
                    reader.MoveToElement();
                    int lvl = -1;
                    while (NextChildElement(reader, ref lvl))
                    {
                        ParseExtensionElements(reader, category);
                    }
                }
            }
            return(category);
        }
Exemple #2
0
        /////////////////////////////////////////////////////////////////////////////


        //////////////////////////////////////////////////////////////////////
        /// <summary>parses an AtomGenerator</summary>
        /// <param name="reader">the xmlreader correctly positioned at the generator </param>
        /// <param name="owner">the container element</param>
        /// <returns> </returns>
        //////////////////////////////////////////////////////////////////////
        protected AtomGenerator ParseGenerator(XmlReader reader, AtomBase owner)
        {
            //    atomGenerator = element atom:generator {
            //    atomCommonAttributes,
            //    attribute url { atomUri }?,
            //    attribute version { text }?,
            //    text
            //     }

            Tracing.Assert(reader != null, "reader should not be null");
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            if (owner == null)
            {
                throw new ArgumentNullException("owner");
            }


            Tracing.TraceCall();
            AtomGenerator generator = owner.CreateAtomSubElement(reader, this) as AtomGenerator;

            if (generator != null)
            {
                generator.Text = Utilities.DecodedValue(reader.ReadString());
                if (reader.HasAttributes)
                {
                    while (reader.MoveToNextAttribute())
                    {
                        object attributeName = reader.LocalName;

                        if (attributeName.Equals(this.nameTable.Uri))
                        {
                            generator.Uri = new AtomUri(reader.Value);
                        }
                        else if (attributeName.Equals(this.nameTable.Version))
                        {
                            generator.Version = Utilities.DecodedValue(reader.Value);
                        }
                        else
                        {
                            ParseBaseAttributes(reader, generator);
                        }
                    }
                }
            }

            return(generator);
        }
        /// <summary>parses an AtomTextConstruct</summary>
        /// <param name="reader">the xmlreader correctly positioned at the construct </param>
        /// <param name="owner">the container element</param>
        /// <returns>the new text construct </returns>
        protected AtomTextConstruct ParseTextConstruct(XmlReader reader, AtomBase owner)
        {
            Tracing.Assert(reader != null, "reader should not be null");
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }
            if (owner == null)
            {
                throw new System.ArgumentNullException("owner");
            }

            Tracing.TraceCall("Parsing atomTextConstruct");
            AtomTextConstruct construct = owner.CreateAtomSubElement(reader, this) as AtomTextConstruct;

            if (reader.NodeType == XmlNodeType.Element)
            {
                if (reader.HasAttributes)
                {
                    while (reader.MoveToNextAttribute())
                    {
                        object attributeName = reader.LocalName;

                        if (attributeName.Equals(this.nameTable.Type))
                        {
                            construct.Type = (AtomTextConstructType)Enum.Parse(
                                typeof(AtomTextConstructType), Utilities.DecodedValue(reader.Value), true);
                        }
                        else
                        {
                            ParseBaseAttributes(reader, construct);
                        }
                    }
                }
                reader.MoveToElement();
                switch (construct.Type)
                {
                case AtomTextConstructType.html:
                case AtomTextConstructType.text:
                    construct.Text = Utilities.DecodedValue(reader.ReadString());
                    break;

                case AtomTextConstructType.xhtml:
                default:
                    construct.Text = reader.ReadInnerXml();
                    break;
                }
            }
            return(construct);
        }
        /// <summary>parses an author/person object</summary>
        /// <param name="reader"> an XmlReader positioned at the start of the author</param>
        /// <param name="owner">the object containing the person</param>
        /// <returns> the created author object</returns>
        protected AtomPerson ParsePerson(XmlReader reader, AtomBase owner)
        {
            Tracing.Assert(reader != null, "reader should not be null");
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }
            Tracing.Assert(owner != null, "owner should not be null");
            if (owner == null)
            {
                throw new ArgumentNullException("owner");
            }

            Tracing.TraceCall();
            object     localname = null;
            AtomPerson author    = owner.CreateAtomSubElement(reader, this) as AtomPerson;

            ParseBasicAttributes(reader, author);

            int lvl = -1;

            while (NextChildElement(reader, ref lvl))
            {
                localname = reader.LocalName;

                if (localname.Equals(this.nameTable.Name))
                {
                    // author.Name = Utilities.DecodeString(Utilities.DecodedValue(reader.ReadString()));
                    author.Name = Utilities.DecodedValue(reader.ReadString());
                    reader.Read();
                }
                else if (localname.Equals(this.nameTable.Uri))
                {
                    author.Uri = new AtomUri(Utilities.DecodedValue(reader.ReadString()));
                    reader.Read();
                }
                else if (localname.Equals(this.nameTable.Email))
                {
                    author.Email = Utilities.DecodedValue(reader.ReadString());
                    reader.Read();
                }
                else
                {
                    // default extension parsing.
                    ParseExtensionElements(reader, author);
                }
            }
            return(author);
        }
        /// <summary>creates an AtomContent object by parsing an xml stream</summary> 
        /// <param name="reader">a XMLReader positioned correctly </param>
        /// <param name="owner">the container element</param>
        /// <returns> null or an AtomContent object</returns>
        protected AtomContent ParseContent(XmlReader reader, AtomBase owner) {
            Tracing.Assert(reader != null, "reader should not be null");
            if (reader == null) {
                throw new ArgumentNullException("reader");
            }

            if (owner == null) {
                throw new ArgumentNullException("owner");
            }

            AtomContent content = owner.CreateAtomSubElement(reader, this) as AtomContent;
            Tracing.TraceCall();
            if (content != null) {
                if (reader.HasAttributes) {
                    while (reader.MoveToNextAttribute()) {
                        object localname = reader.LocalName;
                        if (localname.Equals(this.nameTable.Type)) {
                            content.Type = Utilities.DecodedValue(reader.Value);
                        } else if (localname.Equals(this.nameTable.Src)) {
                            content.Src = new AtomUri(reader.Value);
                        } else {
                            ParseBaseAttributes(reader, content);
                        }
                    }
                }

                if (MoveToStartElement(reader)) {
                    if (content.Type.Equals("text") ||
                        content.Type.Equals("html") ||
                        content.Type.StartsWith("text/")) {
                        // if it's text it get's just the string treatment. No 
                        // subelements are allowed here
                        content.Content = Utilities.DecodedValue(reader.ReadString());
                    } else if (content.Type.Equals("xhtml") ||
                        content.Type.Contains("/xml") ||
                        content.Type.Contains("+xml")) {
                        // do not get childlists if the element is empty. That would skip to the next element
                        if (!reader.IsEmptyElement) {
                            // everything else will be nodes in the extension element list
                            // different media type. Create extension elements
                            int lvl = -1;
                            while (NextChildElement(reader, ref lvl)) {
                                ParseExtensionElements(reader, content);
                            }
                        }
                    } else {
                        // everything else SHOULD be base 64 encoded, so one big string
                        // i know the if statement could be combined with the text handling
                        // but i consider it clearer to make a 3 cases statement than combine them
                        content.Content = Utilities.DecodedValue(reader.ReadString());
                    }
                }
            }
            return content;
        }
        /// <summary>parses an AtomGenerator</summary> 
        /// <param name="reader">the xmlreader correctly positioned at the generator </param>
        /// <param name="owner">the container element</param>
        /// <returns> </returns>
        protected AtomGenerator ParseGenerator(XmlReader reader, AtomBase owner) {
            Tracing.Assert(reader != null, "reader should not be null");
            if (reader == null) {
                throw new ArgumentNullException("reader");
            }

            if (owner == null) {
                throw new ArgumentNullException("owner");
            }

            Tracing.TraceCall();
            AtomGenerator generator = owner.CreateAtomSubElement(reader, this) as AtomGenerator;
            if (generator != null) {
                generator.Text = Utilities.DecodedValue(reader.ReadString());
                if (reader.HasAttributes) {
                    while (reader.MoveToNextAttribute()) {
                        object attributeName = reader.LocalName;

                        if (attributeName.Equals(this.nameTable.Uri)) {
                            generator.Uri = new AtomUri(reader.Value);
                        } else if (attributeName.Equals(this.nameTable.Version)) {
                            generator.Version = Utilities.DecodedValue(reader.Value);
                        } else {
                            ParseBaseAttributes(reader, generator);
                        }
                    }
                }
            }

            return generator;
        }
        /// <summary>parses an AtomTextConstruct</summary> 
        /// <param name="reader">the xmlreader correctly positioned at the construct </param>
        /// <param name="owner">the container element</param>
        /// <returns>the new text construct </returns>
        protected AtomTextConstruct ParseTextConstruct(XmlReader reader, AtomBase owner) {
            Tracing.Assert(reader != null, "reader should not be null");
            if (reader == null) {
                throw new ArgumentNullException("reader");
            }
            if (owner == null) {
                throw new System.ArgumentNullException("owner");
            }

            Tracing.TraceCall("Parsing atomTextConstruct");
            AtomTextConstruct construct = owner.CreateAtomSubElement(reader, this) as AtomTextConstruct;

            if (reader.NodeType == XmlNodeType.Element) {
                if (reader.HasAttributes) {
                    while (reader.MoveToNextAttribute()) {
                        object attributeName = reader.LocalName;

                        if (attributeName.Equals(this.nameTable.Type)) {
                            construct.Type = (AtomTextConstructType)Enum.Parse(
                               typeof(AtomTextConstructType), Utilities.DecodedValue(reader.Value), true);
                        } else {
                            ParseBaseAttributes(reader, construct);
                        }
                    }
                }
                reader.MoveToElement();
                switch (construct.Type) {
                    case AtomTextConstructType.html:
                    case AtomTextConstructType.text:
                        construct.Text = Utilities.DecodedValue(reader.ReadString());
                        break;

                    case AtomTextConstructType.xhtml:
                    default:
                        construct.Text = reader.ReadInnerXml();
                        break;
                }
            }
            return construct;
        }
        /// <summary>creates an atomlink object</summary>
        /// <param name="reader">correctly positioned xmlreader</param>
        /// <param name="owner">the object containing the person</param>
        /// <returns> the created AtomLink object</returns>
        protected AtomLink ParseLink(XmlReader reader, AtomBase owner) {
            Tracing.Assert(reader != null, "reader should not be null");
            if (reader == null) {
                throw new ArgumentNullException("reader");
            }

            if (owner == null) {
                throw new ArgumentNullException("owner");
            }

            bool noChildren = reader.IsEmptyElement;

            Tracing.TraceCall();

            AtomLink link = owner.CreateAtomSubElement(reader, this) as AtomLink;
            object localname = null;

            if (reader.HasAttributes) {
                while (reader.MoveToNextAttribute()) {
                    localname = reader.LocalName;
                    if (localname.Equals(this.nameTable.HRef)) {
                        link.HRef = new AtomUri(reader.Value);
                    } else if (localname.Equals(this.nameTable.Rel)) {
                        link.Rel = Utilities.DecodedValue(reader.Value);
                    } else if (localname.Equals(this.nameTable.Type)) {
                        link.Type = Utilities.DecodedValue(reader.Value);
                    } else if (localname.Equals(this.nameTable.HRefLang)) {
                        link.HRefLang = Utilities.DecodedValue(reader.Value);
                    } else if (localname.Equals(this.nameTable.Title)) {
                        link.Title = Utilities.DecodedValue(reader.Value);
                    } else if (localname.Equals(this.nameTable.Length)) {
                        link.Length = int.Parse(Utilities.DecodedValue(reader.Value), CultureInfo.InvariantCulture);
                    } else {
                        ParseBaseAttributes(reader, link);
                    }
                }
            }
            if (!noChildren) {
                reader.MoveToElement();
                int lvl = -1;
                while (NextChildElement(reader, ref lvl)) {
                    ParseExtensionElements(reader, link);
                }
            }
            return link;
        }
        /// <summary>parses an xml stream to create an AtomCategory object</summary> 
        /// <param name="reader">correctly positioned xmlreader</param>
        /// <param name="owner">the object containing the person</param>
        /// <returns> the created AtomCategory object</returns>
        protected AtomCategory ParseCategory(XmlReader reader, AtomBase owner) {
            Tracing.TraceCall();
            Tracing.Assert(reader != null, "reader should not be null");
            if (reader == null) {
                throw new ArgumentNullException("reader");
            }
            if (owner == null) {
                throw new ArgumentNullException("owner");
            }

            AtomCategory category = owner.CreateAtomSubElement(reader, this) as AtomCategory;

            if (category != null) {
                bool noChildren = reader.IsEmptyElement;
                if (reader.HasAttributes) {
                    while (reader.MoveToNextAttribute()) {
                        object localname = reader.LocalName;
                        if (localname.Equals(this.nameTable.Term)) {
                            category.Term = Utilities.DecodedValue(reader.Value);
                        } else if (localname.Equals(this.nameTable.Scheme)) {
                            category.Scheme = new AtomUri(reader.Value);
                        } else if (localname.Equals(this.nameTable.Label)) {
                            category.Label = Utilities.DecodedValue(reader.Value);
                        } else {
                            ParseBaseAttributes(reader, category);
                        }
                    }
                }

                if (!noChildren) {
                    reader.MoveToElement();
                    int lvl = -1;
                    while (NextChildElement(reader, ref lvl)) {
                        ParseExtensionElements(reader, category);
                    }
                }

            }
            return category;
        }
        /// <summary>parses an author/person object</summary> 
        /// <param name="reader"> an XmlReader positioned at the start of the author</param>
        /// <param name="owner">the object containing the person</param>
        /// <returns> the created author object</returns>
        protected AtomPerson ParsePerson(XmlReader reader, AtomBase owner) {
            Tracing.Assert(reader != null, "reader should not be null");
            if (reader == null) {
                throw new ArgumentNullException("reader");
            }
            Tracing.Assert(owner != null, "owner should not be null");
            if (owner == null) {
                throw new ArgumentNullException("owner");
            }

            Tracing.TraceCall();
            object localname = null;
            AtomPerson author = owner.CreateAtomSubElement(reader, this) as AtomPerson;

            ParseBasicAttributes(reader, author);

            int lvl = -1;
            while (NextChildElement(reader, ref lvl)) {
                localname = reader.LocalName;

                if (localname.Equals(this.nameTable.Name)) {
                    // author.Name = Utilities.DecodeString(Utilities.DecodedValue(reader.ReadString()));
                    author.Name = Utilities.DecodedValue(reader.ReadString());
                    reader.Read();
                } else if (localname.Equals(this.nameTable.Uri)) {
                    author.Uri = new AtomUri(Utilities.DecodedValue(reader.ReadString()));
                    reader.Read();
                } else if (localname.Equals(this.nameTable.Email)) {
                    author.Email = Utilities.DecodedValue(reader.ReadString());
                    reader.Read();
                } else {
                    // default extension parsing.
                    ParseExtensionElements(reader, author);
                }
            }
            return author;
        }
Exemple #11
0
        /////////////////////////////////////////////////////////////////////////////



        //////////////////////////////////////////////////////////////////////
        /// <summary>creates an AtomContent object by parsing an xml stream</summary> 
        /// <param name="reader">a XMLReader positioned correctly </param>
        /// <param name="owner">the container element</param>
       /// <param name="skipNode">a boolen indicating if the node needs to be skipped, or not</param>
        /// <returns> null or an AtomContent object</returns>
        //////////////////////////////////////////////////////////////////////
        protected AtomContent ParseContent(XmlReader reader, AtomBase owner, out bool skipNode)
        {
            Tracing.Assert(reader != null, "reader should not be null");
            if (reader == null)
            {
                throw new ArgumentNullException("reader"); 
            }

            // by default, skip to the next node after this routine
            skipNode = true; 

            AtomContent content = owner.CreateAtomSubElement(reader, this) as AtomContent;
            Tracing.TraceCall();
            if (content != null)
            {
                if (reader.HasAttributes)
                {
                    while (reader.MoveToNextAttribute())
                    {
                        object localname = reader.LocalName;
                        if (localname.Equals(this.nameTable.Type))
                        {
                            content.Type = Utilities.DecodedValue(reader.Value);
                        }
                        else if (localname.Equals(this.nameTable.Src))
                        {
                            content.Src = new AtomUri(Utilities.DecodedValue(reader.Value));
                        }
                        else
                        {
                            ParseBaseAttributes(reader, content); 
                        }
                    }
                }

                if (MoveToStartElement(reader) == true)
                {
                    // using readInnerXml has disadvantages, even for HTML/XHTML. in .NET 1.1
                    // decoding will happen and text like "this & that" will come back
                    // as "this &amp; that" 
                    if (content.Type.Equals("xhtml"))
                    {
                        // ReadInnerXml moves to the next node
                        skipNode = false; 
                        content.Content = reader.ReadInnerXml(); 
                    } 
                    else
                    {
                        // anything NOT xhtml get's just the string treatment. No 
                        // subelements are allowed here
                        content.Content = Utilities.DecodedValue(reader.ReadString());
                    }
                }
            }
            return content;
        }
        /// <summary>creates an AtomContent object by parsing an xml stream</summary>
        /// <param name="reader">a XMLReader positioned correctly </param>
        /// <param name="owner">the container element</param>
        /// <returns> null or an AtomContent object</returns>
        protected AtomContent ParseContent(XmlReader reader, AtomBase owner)
        {
            Tracing.Assert(reader != null, "reader should not be null");
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            if (owner == null)
            {
                throw new ArgumentNullException("owner");
            }

            AtomContent content = owner.CreateAtomSubElement(reader, this) as AtomContent;

            Tracing.TraceCall();
            if (content != null)
            {
                if (reader.HasAttributes)
                {
                    while (reader.MoveToNextAttribute())
                    {
                        object localname = reader.LocalName;
                        if (localname.Equals(this.nameTable.Type))
                        {
                            content.Type = Utilities.DecodedValue(reader.Value);
                        }
                        else if (localname.Equals(this.nameTable.Src))
                        {
                            content.Src = new AtomUri(reader.Value);
                        }
                        else
                        {
                            ParseBaseAttributes(reader, content);
                        }
                    }
                }

                if (MoveToStartElement(reader))
                {
                    if (content.Type.Equals("text") ||
                        content.Type.Equals("html") ||
                        content.Type.StartsWith("text/"))
                    {
                        // if it's text it get's just the string treatment. No
                        // subelements are allowed here
                        content.Content = Utilities.DecodedValue(reader.ReadString());
                    }
                    else if (content.Type.Equals("xhtml") ||
                             content.Type.Contains("/xml") ||
                             content.Type.Contains("+xml"))
                    {
                        // do not get childlists if the element is empty. That would skip to the next element
                        if (!reader.IsEmptyElement)
                        {
                            // everything else will be nodes in the extension element list
                            // different media type. Create extension elements
                            int lvl = -1;
                            while (NextChildElement(reader, ref lvl))
                            {
                                ParseExtensionElements(reader, content);
                            }
                        }
                    }
                    else
                    {
                        // everything else SHOULD be base 64 encoded, so one big string
                        // i know the if statement could be combined with the text handling
                        // but i consider it clearer to make a 3 cases statement than combine them
                        content.Content = Utilities.DecodedValue(reader.ReadString());
                    }
                }
            }
            return(content);
        }
        /// <summary>creates an atomlink object</summary>
        /// <param name="reader">correctly positioned xmlreader</param>
        /// <param name="owner">the object containing the person</param>
        /// <returns> the created AtomLink object</returns>
        protected AtomLink ParseLink(XmlReader reader, AtomBase owner)
        {
            Tracing.Assert(reader != null, "reader should not be null");
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            if (owner == null)
            {
                throw new ArgumentNullException("owner");
            }

            bool noChildren = reader.IsEmptyElement;

            Tracing.TraceCall();

            AtomLink link      = owner.CreateAtomSubElement(reader, this) as AtomLink;
            object   localname = null;

            if (reader.HasAttributes)
            {
                while (reader.MoveToNextAttribute())
                {
                    localname = reader.LocalName;
                    if (localname.Equals(this.nameTable.HRef))
                    {
                        link.HRef = new AtomUri(reader.Value);
                    }
                    else if (localname.Equals(this.nameTable.Rel))
                    {
                        link.Rel = Utilities.DecodedValue(reader.Value);
                    }
                    else if (localname.Equals(this.nameTable.Type))
                    {
                        link.Type = Utilities.DecodedValue(reader.Value);
                    }
                    else if (localname.Equals(this.nameTable.HRefLang))
                    {
                        link.HRefLang = Utilities.DecodedValue(reader.Value);
                    }
                    else if (localname.Equals(this.nameTable.Title))
                    {
                        link.Title = Utilities.DecodedValue(reader.Value);
                    }
                    else if (localname.Equals(this.nameTable.Length))
                    {
                        link.Length = int.Parse(Utilities.DecodedValue(reader.Value), CultureInfo.InvariantCulture);
                    }
                    else
                    {
                        ParseBaseAttributes(reader, link);
                    }
                }
            }
            if (!noChildren)
            {
                reader.MoveToElement();
                int lvl = -1;
                while (NextChildElement(reader, ref lvl))
                {
                    ParseExtensionElements(reader, link);
                }
            }
            return(link);
        }