public virtual void EndDocument() { if (_contentHandler != null) { _contentHandler.EndDocument(); } }
/* * <b>Sax1, Sax2</b>: Auxiliary API to parse an Xml document, used mostly * when no URI is available. * If you want anything useful to happen, you should set * at least one type of handler. * @param source The Xml input source. Don't set 'encoding' unless * you know for a fact that it's correct. * @see #setEntityResolver * @see #setDtdHandler * @see #setContentHandler * @see #setErrorHandler * @exception SaxException The handlers may throw any SaxException, * and the parser normally throws SaxParseException objects. * @exception IOException IOExceptions are normally through through * the parser if there are problems reading the source document. */ public void Parse(InputSource source) { //FIXME!! synchronized(base) { parser = new XmlParser(); if (namespaces) { namespaceSupport = new NamespaceSupport(); namespaceSupport.NamespaceDeclUris = true; } parser.setHandler(this); try { // .NET status = XmlReaderStatus.Parsing; Stream stream = null; TextReader reader = null; if (source is InputSource <Stream> ) { stream = ((InputSource <Stream>)source).Source; } if (source is InputSource <TextReader> ) { reader = ((InputSource <TextReader>)source).Source; } if (source.SystemId == null && stream != null) { if (stream is FileStream) { source.SystemId = ((FileStream)stream).Name; } } parser.doParse(source.SystemId, source.PublicId, reader, stream, source.Encoding); } catch (SaxException e) { throw e; } catch (Exception e) { throw new SaxParseException(new ParseErrorImpl(e.Message, this, e)); } finally { contentHandler.EndDocument(); entityStack.Clear(); parser = null; namespaceSupport = null; status = XmlReaderStatus.Ready; } }
/// <summary> /// Auxiliary API to parse an XML document, used mostly /// when no URI is available. /// If you want anything useful to happen, you should set /// at least one type of handler. /// </summary> /// <param name="source"> /// The XML input source. Don't set 'encoding' unless /// you know for a fact that it's correct. /// </param> /// <seealso cref="EntityResolver" /> /// <seealso cref="DTDHandler" /> /// <seealso cref="ContentHandler" /> /// <seealso cref="ErrorHandler" /> /// <exception cref="SAXException"> /// The handlers may throw any SAXException, /// and the parser normally throws SAXParseException objects. /// </exception> /// <exception cref="IOException"> /// IOExceptions are normally through through /// the parser if there are problems reading the source document. /// </exception> public virtual void Parse(InputSource source) { lock (_base) { _parser = new XmlParser(); _parser.setHandler(this); try { string systemId = source.SystemId; // MHK addition. SAX2 says the systemId supplied must be absolute. // But often it isn't. This code tries, if necessary, to expand it // relative to the current working directory systemId = TryToExpand(systemId); // duplicate first entry, in case startDocument handler // needs to use Locator.getSystemId(), before entities // start to get reported by the parser //if (systemId != null) _entityStack.Push(systemId); //else // can't happen after tryToExpand() // entityStack.push ("illegal:unknown system ID"); _parser.DoParse(systemId, source.PublicId, source.Reader, source.Stream, source.Encoding); } catch (SAXException e) { throw; } catch (IOException e) { throw; } catch (Exception e) { throw new SAXException(e.Message, e); } finally { _contentHandler.EndDocument(); _entityStack.Clear(); } } }
public virtual void Parse(TextReader text) { Stack nsstack = new Stack(); Locator locator = new Locator(); SAXParseException saxException = new SAXParseException(); Attributes atts = new Attributes(); XmlTextReader reader = null; try { reader = new XmlTextReader(text); object nsuri = reader.NameTable.Add( "http://www.w3.org/2000/xmlns/"); handler.StartDocument(); while (reader.Read()) { string prefix = ""; locator.LineNumber = reader.LineNumber; locator.ColumnNumber = reader.LinePosition; handler.SetDocumentLocator(locator); switch (reader.NodeType) { case XmlNodeType.Element: nsstack.Push(null); //marker atts = new Attributes(); while (reader.MoveToNextAttribute()) { if (reader.NamespaceURI.Equals(nsuri)) { prefix = ""; if (reader.Prefix == "xmlns") { prefix = reader.LocalName; } nsstack.Push(prefix); handler.StartPrefixMapping(prefix, reader.Value); } else { atts.AddAttribute(reader.NamespaceURI, reader.Name, reader.Name, reader.GetType().ToString(), reader.Value); } } reader.MoveToElement(); Handler.StartElement(reader.NamespaceURI, reader.LocalName, reader.Name, atts); if (reader.IsEmptyElement) { handler.EndElement(reader.NamespaceURI, reader.LocalName, reader.Name); } break; case XmlNodeType.EndElement: handler.EndElement(reader.NamespaceURI, reader.LocalName, reader.Name); while (prefix != null) { handler.EndPrefixMapping(prefix); prefix = (string)nsstack.Pop(); } break; case XmlNodeType.Text: handler.Characters(reader.Value.ToCharArray(), 0, reader.Value.Length); break; case XmlNodeType.ProcessingInstruction: handler.ProcessingInstruction(reader.Name, reader.Value); break; case XmlNodeType.Whitespace: char[] whiteSpace = reader.Value.ToCharArray(); handler.IgnorableWhitespace(whiteSpace, 0, 1); break; case XmlNodeType.Entity: handler.SkippedEntity(reader.Name); break; } } //While handler.EndDocument(); } //try catch (Exception exception) { saxException.LineNumber = reader.LineNumber; saxException.SystemID = ""; saxException.setMessage(exception.GetBaseException().ToString()); errorHandler.Error(saxException); } finally { if (reader.ReadState != ReadState.Closed) { reader.Close(); } } } //parse()