#region Copyright 2010-2014 by Roger Knapp, Licensed under the Apache License, Version 2.0 /* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #endregion using System; using System.Collections.Generic; namespace CSharpTest.Net.Html { /// The quote used with an attribute value public enum XmlQuoteStyle : byte { /// The value was not defined, no '=' sign None, /// The value was not quoted, name=value Missing, /// The value was not quoted, name='value' Single, /// The value was not quoted, name="value" Double, } /// /// Represents a single attribute on an xml element /// public struct XmlLightAttribute { private const string Space = " "; /// A static empty list of attributes internal static readonly XmlLightAttribute[] EmptyList = new XmlLightAttribute[0]; /// XmlLightAttribute public XmlLightAttribute(string name) { Quote = XmlQuoteStyle.Double; Name = name; Value = null; Before = Space; Ordinal = 0; } /// The offset of the attribute in the list public int Ordinal; /// The full name of the attribute public string Name; /// The original encoded text value of the attribute public string Value; /// The character used to quote the original value public XmlQuoteStyle Quote; /// The white-space characters preceeding the attribute name public string Before; /// /// Returns the namespace or empty string /// public string Namespace { get { int ix = Name.IndexOf(':'); return ix < 0 ? String.Empty : Name.Substring(0, ix); } } /// /// Returns the namespace or null /// public string NamespaceOrNull { get { int ix = Name.IndexOf(':'); return ix < 0 ? null : Name.Substring(0, ix); } } /// /// Returns the name without the namespace prefix /// public string LocalName { get { int ix = Name.IndexOf(':'); return ix < 0 ? Name : Name.Substring(ix + 1); } } } /// /// Wraps up the information about a tag start while parsing /// public struct XmlTagInfo { /// XmlTagInfo public XmlTagInfo(string name, bool closed) { FullName = name; SelfClosed = closed; EndingWhitespace = String.Empty; UnparsedTag = null; Attributes = XmlLightAttribute.EmptyList; } /// The full name token of the element 'ns:name' public string FullName; /// True if the tag is self-closing/empty: <empty/> public bool SelfClosed; /// THe space preceeding the tag close '>' public string EndingWhitespace; /// The complete tag in raw/unparsed form public string UnparsedTag; /// The name/value pair attributes public IEnumerable Attributes; } /// /// Provides a means by which the XmlLightParser can inform you of the document /// elements encountered. /// public interface IXmlLightReader { /// Begins the processing of an xml input void StartDocument(); /// Begins the processing of an xml tag void StartTag(XmlTagInfo tag); /// Ends the processing of an xml tag void EndTag(XmlTagInfo tag); /// Encountered text or whitespace in the document void AddText(string content); /// Encountered comment in the document void AddComment(string comment); /// Encountered cdata section in the document void AddCData(string cdata); /// Encountered control information <! ... > in the document void AddControl(string cdata); /// Encountered processing instruction <? ... ?> in the document void AddInstruction(string instruction); /// Ends the processing of an xml input void EndDocument(); } }