public void startDocument() throws SAXException { if (locator == null) { LocatorImpl tem = new LocatorImpl(); tem.setLineNumber(-1); tem.setColumnNumber(-1); locator = tem; } openElements.push(new OpenElement("", "#root")); try { constraint.activate(this); } catch (WrappedSAXException e) { throw e.exception; } }
/** * parse * * @param inputSource * @exception org.xml.sax.SAXException * @exception java.io.IOException */ public void parse(InputSource inputSource) throws SAXException, IOException { // parse document try { XMLInputSource xmlInputSource = new XMLInputSource(inputSource.getPublicId(), inputSource.getSystemId(), null); xmlInputSource.setByteStream(inputSource.getByteStream()); xmlInputSource.setCharacterStream(inputSource.getCharacterStream()); xmlInputSource.setEncoding(inputSource.getEncoding()); parse(xmlInputSource); } // wrap XNI exceptions as SAX exceptions catch (XMLParseException e) { Exception ex = e.getException(); if (ex == null) { // must be a parser exception; mine it for locator info and throw // a SAXParseException LocatorImpl locatorImpl = new LocatorImpl(); locatorImpl.setPublicId(e.getPublicId()); locatorImpl.setSystemId(e.getExpandedSystemId()); locatorImpl.setLineNumber(e.getLineNumber()); locatorImpl.setColumnNumber(e.getColumnNumber()); throw new SAXParseException(e.getMessage(), locatorImpl); } if (ex instanceof SAXException) { // why did we create an XMLParseException? throw (SAXException) ex; } if (ex instanceof IOException) { throw (IOException) ex; } throw new SAXException(ex); } catch (XNIException e) { Exception ex = e.getException(); if (ex == null) { throw new SAXException(e.getMessage()); } if (ex instanceof SAXException) { throw (SAXException) ex; } if (ex instanceof IOException) { throw (IOException) ex; } throw new SAXException(ex); } } // parse(InputSource)
/** * Parses the input source specified by the given system identifier. * * <p>This method is equivalent to the following: * * <pre> * parse(new InputSource(systemId)); * </pre> * * @param systemId The system identifier (URI). * @exception org.xml.sax.SAXException Throws exception on SAX error. * @exception java.io.IOException Throws exception on i/o error. */ public void parse(String systemId) throws SAXException, IOException { // parse document XMLInputSource source = new XMLInputSource(null, systemId, null); try { parse(source); } // wrap XNI exceptions as SAX exceptions catch (XMLParseException e) { Exception ex = e.getException(); if (ex == null) { // must be a parser exception; mine it for locator info and throw // a SAXParseException LocatorImpl locatorImpl = new LocatorImpl(); locatorImpl.setPublicId(e.getPublicId()); locatorImpl.setSystemId(e.getExpandedSystemId()); locatorImpl.setLineNumber(e.getLineNumber()); locatorImpl.setColumnNumber(e.getColumnNumber()); throw new SAXParseException(e.getMessage(), locatorImpl); } if (ex instanceof SAXException) { // why did we create an XMLParseException? throw (SAXException) ex; } if (ex instanceof IOException) { throw (IOException) ex; } throw new SAXException(ex); } catch (XNIException e) { e.printStackTrace(); Exception ex = e.getException(); if (ex == null) { throw new SAXException(e.getMessage()); } if (ex instanceof SAXException) { throw (SAXException) ex; } if (ex instanceof IOException) { throw (IOException) ex; } throw new SAXException(ex); } } // parse(String)
/* 406: */ /* 407: */ protected void documentLocator(Document document) /* 408: */ throws SAXException /* 409: */ { /* 410:661 */ LocatorImpl locator = new LocatorImpl(); /* 411: */ /* 412:663 */ String publicID = null; /* 413:664 */ String systemID = null; /* 414:665 */ DocumentType docType = document.getDocType(); /* 415:667 */ if (docType != null) /* 416: */ { /* 417:668 */ publicID = docType.getPublicID(); /* 418:669 */ systemID = docType.getSystemID(); /* 419: */ } /* 420:672 */ if (publicID != null) { /* 421:673 */ locator.setPublicId(publicID); /* 422: */ } /* 423:676 */ if (systemID != null) { /* 424:677 */ locator.setSystemId(systemID); /* 425: */ } /* 426:680 */ locator.setLineNumber(-1); /* 427:681 */ locator.setColumnNumber(-1); /* 428: */ /* 429:683 */ this.contentHandler.setDocumentLocator(locator); /* 430: */ }
/** * Generate XML data. * * @throws IOException * @throws ProcessingException * @throws SAXException */ public void generate() throws IOException, SAXException, ProcessingException { InputStreamReader in = null; try { final InputStream sis = this.inputSource.getInputStream(); if (sis == null) { throw new ProcessingException("Source '" + this.inputSource.getURI() + "' not found"); } if (encoding != null) { in = new InputStreamReader(sis, encoding); } else { in = new InputStreamReader(sis); } } catch (SourceException se) { throw new ProcessingException("Error during resolving of '" + this.source + "'.", se); } LocatorImpl locator = new LocatorImpl(); locator.setSystemId(this.inputSource.getURI()); locator.setLineNumber(1); locator.setColumnNumber(1); contentHandler.setDocumentLocator(locator); contentHandler.startDocument(); contentHandler.startPrefixMapping("", URI); AttributesImpl atts = new AttributesImpl(); if (localizable) { atts.addAttribute("", "source", "source", "CDATA", locator.getSystemId()); atts.addAttribute("", "line", "line", "CDATA", String.valueOf(locator.getLineNumber())); atts.addAttribute("", "column", "column", "CDATA", String.valueOf(locator.getColumnNumber())); } contentHandler.startElement(URI, "text", "text", atts); LineNumberReader reader = new LineNumberReader(in); String line; String newline = null; while (true) { if (newline == null) { line = convertNonXmlChars(reader.readLine()); } else { line = newline; } if (line == null) { break; } newline = convertNonXmlChars(reader.readLine()); if (newline != null) { line += SystemUtils.LINE_SEPARATOR; } locator.setLineNumber(reader.getLineNumber()); locator.setColumnNumber(1); contentHandler.characters(line.toCharArray(), 0, line.length()); if (newline == null) { break; } } reader.close(); contentHandler.endElement(URI, "text", "text"); contentHandler.endPrefixMapping(""); contentHandler.endDocument(); }
static { DUMMY_LOCATOR.setLineNumber(-1); DUMMY_LOCATOR.setColumnNumber(-1); }
/** * Start processing given node * * @param node Node to process * @throws org.xml.sax.SAXException */ protected void startNode(Node node) throws org.xml.sax.SAXException { // TODO: <REVIEW> // A Serializer implements ContentHandler, but not NodeConsumer // so drop this reference to NodeConsumer which would otherwise // pull in all sorts of things // if (m_contentHandler instanceof NodeConsumer) // { // ((NodeConsumer) m_contentHandler).setOriginatingNode(node); // } // TODO: </REVIEW> if (node instanceof Locator) { Locator loc = (Locator) node; m_locator.setColumnNumber(loc.getColumnNumber()); m_locator.setLineNumber(loc.getLineNumber()); m_locator.setPublicId(loc.getPublicId()); m_locator.setSystemId(loc.getSystemId()); } else { m_locator.setColumnNumber(0); m_locator.setLineNumber(0); } switch (node.getNodeType()) { case Node.COMMENT_NODE: { String data = ((Comment) node).getData(); if (m_contentHandler instanceof LexicalHandler) { LexicalHandler lh = ((LexicalHandler) this.m_contentHandler); lh.comment(data.toCharArray(), 0, data.length()); } } break; case Node.DOCUMENT_FRAGMENT_NODE: // ??; break; case Node.DOCUMENT_NODE: break; case Node.ELEMENT_NODE: Element elem_node = (Element) node; { // Make sure the namespace node // for the element itself is declared // to the ContentHandler String uri = elem_node.getNamespaceURI(); if (uri != null) { String prefix = elem_node.getPrefix(); if (prefix == null) prefix = ""; this.m_contentHandler.startPrefixMapping(prefix, uri); } } NamedNodeMap atts = elem_node.getAttributes(); int nAttrs = atts.getLength(); // System.out.println("TreeWalker#startNode: "+node.getNodeName()); // Make sure the namespace node of // each attribute is declared to the ContentHandler for (int i = 0; i < nAttrs; i++) { final Node attr = atts.item(i); final String attrName = attr.getNodeName(); final int colon = attrName.indexOf(':'); final String prefix; // System.out.println("TreeWalker#startNode: attr["+i+"] = "+attrName+", // "+attr.getNodeValue()); if (attrName.equals("xmlns") || attrName.startsWith("xmlns:")) { // Use "" instead of null, as Xerces likes "" for the // name of the default namespace. Fix attributed // to "Steven Murray" <*****@*****.**>. if (colon < 0) prefix = ""; else prefix = attrName.substring(colon + 1); this.m_contentHandler.startPrefixMapping(prefix, attr.getNodeValue()); } else if (colon > 0) { prefix = attrName.substring(0, colon); String uri = attr.getNamespaceURI(); if (uri != null) this.m_contentHandler.startPrefixMapping(prefix, uri); } } String ns = m_dh.getNamespaceOfNode(node); if (null == ns) ns = ""; this.m_contentHandler.startElement( ns, m_dh.getLocalNameOfNode(node), node.getNodeName(), new AttList(atts, m_dh)); break; case Node.PROCESSING_INSTRUCTION_NODE: { ProcessingInstruction pi = (ProcessingInstruction) node; String name = pi.getNodeName(); // String data = pi.getData(); if (name.equals("xslt-next-is-raw")) { nextIsRaw = true; } else { this.m_contentHandler.processingInstruction(pi.getNodeName(), pi.getData()); } } break; case Node.CDATA_SECTION_NODE: { boolean isLexH = (m_contentHandler instanceof LexicalHandler); LexicalHandler lh = isLexH ? ((LexicalHandler) this.m_contentHandler) : null; if (isLexH) { lh.startCDATA(); } dispatachChars(node); { if (isLexH) { lh.endCDATA(); } } } break; case Node.TEXT_NODE: { // String data = ((Text) node).getData(); if (nextIsRaw) { nextIsRaw = false; m_contentHandler.processingInstruction( javax.xml.transform.Result.PI_DISABLE_OUTPUT_ESCAPING, ""); dispatachChars(node); m_contentHandler.processingInstruction( javax.xml.transform.Result.PI_ENABLE_OUTPUT_ESCAPING, ""); } else { dispatachChars(node); } } break; case Node.ENTITY_REFERENCE_NODE: { EntityReference eref = (EntityReference) node; if (m_contentHandler instanceof LexicalHandler) { ((LexicalHandler) this.m_contentHandler).startEntity(eref.getNodeName()); } else { // warning("Can not output entity to a pure SAX ContentHandler"); } } break; default: } }