/// <summary> /// Emulates the behaviour of a SAX LocatorImpl object. /// </summary> /// <param name="locator">The 'XmlSaxLocatorImpl' instance to assing the document location.</param> /// <param name="textReader">The XML document instance to be used.</param> private void UpdateLocatorData(XmlSaxLocatorImpl locator, System.Xml.XmlTextReader textReader) { if ((locator != null) && (textReader != null)) { locator.setColumnNumber(textReader.LinePosition); locator.setLineNumber(textReader.LineNumber); locator.setSystemId(parserFileName); } }
/// <summary> /// Emulates the behavior of a SAX parser, it realizes the callback events of the parser. /// </summary> private void DoParsing() { System.Collections.Hashtable prefixes = new System.Collections.Hashtable(); System.Collections.Stack stackNameSpace = new System.Collections.Stack(); locator = new XmlSaxLocatorImpl(); try { UpdateLocatorData(this.locator, (System.Xml.XmlTextReader) (this.reader.Reader)); if (this.callBackHandler != null) this.callBackHandler.setDocumentLocator(locator); if (this.callBackHandler != null) this.callBackHandler.startDocument(); while (this.reader.Read()) { UpdateLocatorData(this.locator, (System.Xml.XmlTextReader) (this.reader.Reader)); switch (this.reader.NodeType) { case System.Xml.XmlNodeType.Element: bool Empty = reader.IsEmptyElement; System.String namespaceURI = ""; System.String localName = ""; if (this.namespaceAllowed) { namespaceURI = reader.NamespaceURI; localName = reader.LocalName; } System.String name = reader.Name; SaxAttributesSupport attributes = new SaxAttributesSupport(); if (reader.HasAttributes) { for (int i = 0; i < reader.AttributeCount; i++) { reader.MoveToAttribute(i); System.String prefixName = (reader.Name.IndexOf(":") > 0) ? reader.Name.Substring(reader.Name.IndexOf(":") + 1, reader.Name.Length - reader.Name.IndexOf(":") - 1) : ""; System.String prefix = (reader.Name.IndexOf(":") > 0) ? reader.Name.Substring(0, reader.Name.IndexOf(":")) : reader.Name; bool IsXmlns = prefix.ToLower().Equals("xmlns"); if (this.namespaceAllowed) { if (!IsXmlns) attributes.Add(reader.NamespaceURI, reader.LocalName, reader.Name, "" + reader.NodeType, reader.Value); } else attributes.Add("", "", reader.Name, "" + reader.NodeType, reader.Value); if (IsXmlns) { System.String namespaceTemp = ""; namespaceTemp = (namespaceURI.Length == 0) ? reader.Value : namespaceURI; if (this.namespaceAllowed && !prefixes.ContainsKey(namespaceTemp) && namespaceTemp.Length > 0 ) { stackNameSpace.Push(name); System.Collections.Stack namespaceStack = new System.Collections.Stack(); namespaceStack.Push(prefixName); prefixes.Add(namespaceURI, namespaceStack); if (this.callBackHandler != null) ((XmlSaxContentHandler) this.callBackHandler).startPrefixMapping(prefixName, namespaceTemp); } else { if (this.namespaceAllowed && namespaceTemp.Length > 0 && !((System.Collections.Stack) prefixes[namespaceTemp]).Contains(reader.Name)) { ((System.Collections.Stack) prefixes[namespaceURI]).Push(prefixName); if (this.callBackHandler != null) ((XmlSaxContentHandler) this.callBackHandler).startPrefixMapping(prefixName, reader.Value); } } } } } if (this.callBackHandler != null) this.callBackHandler.startElement(namespaceURI, localName, name, attributes); if (Empty) { if (this.NamespaceAllowed) { if (this.callBackHandler != null) this.callBackHandler.endElement(namespaceURI, localName, name); } else if (this.callBackHandler != null) this.callBackHandler.endElement("", "", name); } break; case System.Xml.XmlNodeType.EndElement: if (this.namespaceAllowed) { if (this.callBackHandler != null) this.callBackHandler.endElement(reader.NamespaceURI, reader.LocalName, reader.Name); } else if (this.callBackHandler != null) this.callBackHandler.endElement("", "", reader.Name); if (this.namespaceAllowed && prefixes.ContainsKey(reader.NamespaceURI) && ((System.Collections.Stack) stackNameSpace).Contains(reader.Name)) { stackNameSpace.Pop(); System.Collections.Stack namespaceStack = (System.Collections.Stack) prefixes[reader.NamespaceURI]; while (namespaceStack.Count > 0) { System.String tempString = (System.String) namespaceStack.Pop(); if (this.callBackHandler != null ) ((XmlSaxContentHandler) this.callBackHandler).endPrefixMapping(tempString); } prefixes.Remove(reader.NamespaceURI); } break; case System.Xml.XmlNodeType.Text: if (this.callBackHandler != null) this.callBackHandler.characters(reader.Value.ToCharArray(), 0, reader.Value.Length); break; case System.Xml.XmlNodeType.Whitespace: if (this.callBackHandler != null) this.callBackHandler.ignorableWhitespace(reader.Value.ToCharArray(), 0, reader.Value.Length); break; case System.Xml.XmlNodeType.ProcessingInstruction: if (this.callBackHandler != null) this.callBackHandler.processingInstruction(reader.Name, reader.Value); break; case System.Xml.XmlNodeType.Comment: if (this.lexical != null) this.lexical.comment(reader.Value.ToCharArray(), 0, reader.Value.Length); break; case System.Xml.XmlNodeType.CDATA: if (this.lexical != null) { lexical.startCDATA(); if (this.callBackHandler != null) this.callBackHandler.characters(this.reader.Value.ToCharArray(), 0, this.reader.Value.ToCharArray().Length); lexical.endCDATA(); } break; case System.Xml.XmlNodeType.DocumentType: if (this.lexical != null) { System.String lname = this.reader.Name; System.String systemId = null; if (this.reader.Reader.AttributeCount > 0) systemId = this.reader.Reader.GetAttribute(0); this.lexical.startDTD(lname, null, systemId); this.lexical.startEntity("[dtd]"); this.lexical.endEntity("[dtd]"); this.lexical.endDTD(); } break; } } if (this.callBackHandler != null) this.callBackHandler.endDocument(); } catch (System.Xml.XmlException e) { throw e; } }
/// <summary> /// Public constructor for the class. /// </summary> public XmlSAXDocumentManager() { isValidating = false; namespaceAllowed = false; reader = null; callBackHandler = null; errorHandler = null; locator = null; lexical = null; entityResolver = null; parserFileName = ""; }