📄 jdomsource.java
字号:
* Sets the SAX InputSource to be used for the Source. * <p> * As this implementation only supports JDOM document as data * source, this method always throws an * {@link UnsupportedOperationException}. * </p> * * @param inputSource a valid InputSource reference. * * @throws UnsupportedOperationException always! */ public void setInputSource(InputSource inputSource) throws UnsupportedOperationException { throw new UnsupportedOperationException(); } /** * Set the XMLReader to be used for the Source. * <p> * As this implementation only supports JDOM document as data * source, this method throws an * {@link UnsupportedOperationException} if the provided reader * object does not implement the SAX {@link XMLFilter} * interface. Otherwise, the JDOM document reader will be * attached as parent of the filter chain.</p> * * @param reader a valid XMLReader or XMLFilter reference. * * @throws UnsupportedOperationException if <code>reader</code> * is not a SAX * {@link XMLFilter}. * @see #getXMLReader */ public void setXMLReader(XMLReader reader) throws UnsupportedOperationException { if (reader instanceof XMLFilter) { // Connect the filter chain to a document reader. XMLFilter filter = (XMLFilter)reader; while (filter.getParent() instanceof XMLFilter) { filter = (XMLFilter)(filter.getParent()); } filter.setParent(buildDocumentReader()); // Read XML data from filter chain. this.xmlReader = reader; } else { throw new UnsupportedOperationException(); } } /** * Returns the XMLReader to be used for the Source. * <p> * This implementation returns a specific XMLReader reading * the XML data from the source JDOM document. * </p> * * @return an XMLReader reading the XML data from the source * JDOM document. */ public XMLReader getXMLReader() { if (this.xmlReader == null) { this.xmlReader = buildDocumentReader(); } return this.xmlReader; } /** * Build an XMLReader to be used for the source. This will * create a new instance of DocumentReader with an * EntityResolver instance if available. * * @return XMLReader reading the XML data from the source * JDOM document with an optional EntityResolver */ private XMLReader buildDocumentReader() { DocumentReader reader = new DocumentReader(); if (resolver != null) reader.setEntityResolver(resolver); return reader; } //========================================================================= // JDOMInputSource nested class //========================================================================= /** * A subclass of the SAX InputSource interface that wraps a JDOM * Document. * <p> * This class is nested in JDOMSource as it is not intented to * be used independently of its friend: DocumentReader. * </p> * * @see org.jdom.Document */ private static class JDOMInputSource extends InputSource { /** * The source as a JDOM document or a list of JDOM nodes. */ private Object source = null; /** * Builds a InputSource wrapping the specified JDOM Document. * * @param document the source document. */ public JDOMInputSource(Document document) { this.source = document; } /** * Builds a InputSource wrapping a list of JDOM nodes. * * @param nodes the source JDOM nodes. */ public JDOMInputSource(List nodes) { this.source = nodes; } /** * Returns the source. * * @return the source as a JDOM document or a list of JDOM nodes. */ public Object getSource() { return source; } //------------------------------------------------------------------------- // InputSource overwritten methods //------------------------------------------------------------------------- /** * Sets the character stream for this input source. * <p> * This implementation always throws an * {@link UnsupportedOperationException} as the only source * stream supported is the source JDOM document. * </p> * * @param characterStream a character stream containing * an XML document. * * @throws UnsupportedOperationException always! */ public void setCharacterStream(Reader characterStream) throws UnsupportedOperationException { throw new UnsupportedOperationException(); } /** * Gets the character stream for this input source. * <p> * Note that this method is only provided to make this * InputSource implementation acceptable by any XML * parser. As it generates an in-memory string representation * of the JDOM document, it is quite inefficient from both * speed and memory consumption points of view. * </p> * * @return a Reader to a string representation of the * source JDOM document. */ public Reader getCharacterStream() { Object src = this.getSource(); Reader reader = null; if (src instanceof Document) { // Get an in-memory string representation of the document // and return a reader on it. reader = new StringReader( new XMLOutputter().outputString((Document)src)); } else { if (src instanceof List) { reader = new StringReader( new XMLOutputter().outputString((List)src)); } // Else: No source, no reader! } return reader; } } //========================================================================= // DocumentReader nested class //========================================================================= /** * An implementation of the SAX2 XMLReader interface that presents * a SAX view of a JDOM Document. The actual generation of the * SAX events is delegated to JDOM's SAXOutputter. * * @see org.jdom.Document * @see org.jdom.output.SAXOutputter */ private static class DocumentReader extends SAXOutputter implements XMLReader { /** * Public default constructor. */ public DocumentReader() { super(); } //---------------------------------------------------------------------- // SAX XMLReader interface support //---------------------------------------------------------------------- /** * Parses an XML document from a system identifier (URI). * <p> * This implementation does not support reading XML data from * system identifiers, only from JDOM documents. Hence, * this method always throws a {@link SAXNotSupportedException}. * </p> * * @param systemId the system identifier (URI). * * @throws SAXNotSupportedException always! */ public void parse(String systemId) throws SAXNotSupportedException { throw new SAXNotSupportedException( "Only JDOM Documents are supported as input"); } /** * Parses an XML document. * <p> * The methods accepts only <code>JDOMInputSource</code>s * instances as input sources. * </p> * * @param input the input source for the top-level of the * XML document. * * @throws SAXException any SAX exception, * possibly wrapping * another exception. * @throws SAXNotSupportedException if the input source does * not wrap a JDOM document. */ public void parse(InputSource input) throws SAXException { if (input instanceof JDOMInputSource) { try { Object source = ((JDOMInputSource)input).getSource(); if (source instanceof Document) { this.output((Document)source); } else { this.output((List)source); } } catch (JDOMException e) { throw new SAXException(e.getMessage(), e); } } else { throw new SAXNotSupportedException( "Only JDOM Documents are supported as input"); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -