📄 saxwriter.java
字号:
* @param handler the DTD handler. * @throws NullPointerException if the handler argument is * <code>null</code>. * @see #getDTDHandler */ public void setDTDHandler(DTDHandler handler) { if (handler == null) { throw new NullPointerException("handler"); } this.dtdHandler = handler; return; } /** * Returns the current DTD handler. * * @return the current DTD handler, or <code>null</code> if none * has been registered. * @see #setDTDHandler */ public DTDHandler getDTDHandler() { return this.dtdHandler; } /** * Allows an application to register a content event handler. * <p/> * If the application does not register a content handler, all * content events reported by the SAX parser will be silently * ignored.</p> * <p/> * Applications may register a new or different handler in the * middle of a parse, and the SAX parser must begin using the new * handler immediately.</p> * * @param handler the content handler. * @throws NullPointerException if the handler argument is * <code>null</code>. * @see #getContentHandler */ public void setContentHandler(ContentHandler handler) { if (handler == null) { throw new NullPointerException("handler"); } this.contentHandler = handler; return; } /** * Returns the current content handler. * * @return the current content handler, or <code>null</code> if none * has been registered. * @see #setContentHandler */ public ContentHandler getContentHandler() { return this.contentHandler; } /** * Allows an application to register an error event handler. * <p/> * If the application does not register an error handler, all * error events reported by the SAX parser will be silently * ignored; however, normal processing may not continue. It is * highly recommended that all SAX applications implement an * error handler to avoid unexpected bugs.</p> * <p/> * Applications may register a new or different handler in the * middle of a parse, and the SAX parser must begin using the new * handler immediately.</p> * * @param handler the error handler. * @throws NullPointerException if the handler argument is * <code>null</code>. * @see #getErrorHandler */ public void setErrorHandler(ErrorHandler handler) { if (handler == null) { throw new NullPointerException("handler"); } this.errorHandler = handler; return; } /** * Returns the current error handler. * * @return the current error handler, or <code>null</code> if none * has been registered. * @see #setErrorHandler */ public ErrorHandler getErrorHandler() { return this.errorHandler; } //--------------------------------------------------------------------- // Parsing //--------------------------------------------------------------------- /** * Parses an XML document from a system identifier (URI). * <p/> * This method is a shortcut for the common case of reading a * document from a system identifier. It is the exact * equivalent of the following:</p> * <blockquote> * <pre> * parse(new InputSource(systemId)); * </pre> * </blockquote> * <p/> * If the system identifier is a URL, it must be fully resolved * by the application before it is passed to the parser.</p> * <p/> * <strong>Note</strong>: As a custom SAX parser, this class * ignores the <code>systemId</code> argument of this method * and relies on the proprietary SAX property * {@link #SOURCE_OBJECT_LIST_PROPERTY}) to define the list of * objects to serialize.</p> * * @param systemId the system identifier (URI). * @throws SAXException Any SAX exception, possibly wrapping * another exception. * @see #parse(org.xml.sax.InputSource) */ public void parse(String systemId) throws SAXException { this.parse(); } /** * Parse an XML document. * <p/> * The application can use this method to instruct the XML * reader to begin parsing an XML document from any valid input * source (a character stream, a byte stream, or a URI).</p> * <p/> * Applications may not invoke this method while a parse is in * progress (they should create a new XMLReader instead for each * nested XML document). Once a parse is complete, an * application may reuse the same XMLReader object, possibly * with a different input source.</p> * <p/> * During the parse, the XMLReader will provide information * about the XML document through the registered event * handlers.</p> * <p/> * This method is synchronous: it will not return until parsing * has ended. If a client application wants to terminate * parsing early, it should throw an exception.</p> * <p/> * <strong>Note</strong>: As a custom SAX parser, this class * ignores the <code>source</code> argument of this method * and relies on the proprietary SAX property * {@link #SOURCE_OBJECT_LIST_PROPERTY}) to define the list of * objects to serialize.</p> * * @param input The input source for the top-level of the * XML document. * @throws SAXException Any SAX exception, possibly wrapping * another exception. * @see org.xml.sax.InputSource * @see #parse(java.lang.String) * @see #setEntityResolver * @see #setDTDHandler * @see #setContentHandler * @see #setErrorHandler */ public void parse(InputSource input) throws SAXException { this.parse(); } /** * Serializes the Java objects of the configured list into a flow * of SAX events. * * @throws SAXException if the configured object list is invalid * or object serialization failed. */ private void parse() throws SAXException { XStream xstream = (XStream) (this.properties.get(CONFIGURED_XSTREAM_PROPERTY)); if (xstream == null) { xstream = new XStream(); } List source = (List) (this.properties.get(SOURCE_OBJECT_LIST_PROPERTY)); if ((source == null) || (source.isEmpty())) { throw new SAXException("Missing or empty source object list. Setting property \"" + SOURCE_OBJECT_LIST_PROPERTY + "\" is mandatory"); } try { this.startDocument(true); for (Iterator i = source.iterator(); i.hasNext();) { xstream.marshal(i.next(), this); } this.endDocument(true); } catch (StreamException e) { if (e.getCause() instanceof SAXException) { throw (SAXException) (e.getCause()); } else { throw new SAXException(e); } } } //========================================================================= // XStream HierarchicalStreamWriter interface support //========================================================================= private int depth = 0; private List elementStack = new LinkedList(); private char[] buffer = new char[128]; private boolean startTagInProgress = false; private final AttributesImpl attributeList = new AttributesImpl(); public void startNode(String name) { try { if (this.depth != 0) { this.flushStartTag(); } else if (includeEnclosingDocument) { this.startDocument(false); } this.elementStack.add(0, name); this.startTagInProgress = true; this.depth++; } catch (SAXException e) { throw new StreamException(e); } } public void addAttribute(String name, String value) { if (this.startTagInProgress) { this.attributeList.addAttribute("", name, name, "CDATA", value); } else { throw new StreamException(new IllegalStateException("No startElement being processed")); } } public void setValue(String text) { try { this.flushStartTag(); int lg = text.length(); if (lg > buffer.length) { buffer = new char[lg]; } text.getChars(0, lg, buffer, 0); this.contentHandler.characters(buffer, 0, lg); } catch (SAXException e) { throw new StreamException(e); } } public void endNode() { try { this.flushStartTag(); String tagName = (String) (this.elementStack.remove(0)); this.contentHandler.endElement("", tagName, tagName); this.depth--; if (this.depth == 0 && includeEnclosingDocument) { this.endDocument(false); } } catch (SAXException e) { throw new StreamException(e); } } /** * Fires the SAX startDocument event towards the configured * ContentHandler. * * @param multiObjectMode whether serialization of several * object will be merge into a single * SAX document. * @throws SAXException if thrown by the ContentHandler. */ private void startDocument(boolean multiObjectMode) throws SAXException { if (this.depth == 0) { // Notify contentHandler of document start. this.contentHandler.startDocument(); if (multiObjectMode) { // Prevent marshalling of each object to fire its own // start/endDocument events. this.depth++; } } } /** * Fires the SAX endDocument event towards the configured * ContentHandler. * * @param multiObjectMode whether serialization of several * object will be merge into a single * SAX document. * @throws SAXException if thrown by the ContentHandler. */ private void endDocument(boolean multiObjectMode) throws SAXException { if ((this.depth == 0) || ((this.depth == 1) && (multiObjectMode))) { this.contentHandler.endDocument(); this.depth = 0; } } /** * Fires any pending SAX startElement event towards the * configured ContentHandler. * * @throws SAXException if thrown by the ContentHandler. */ private void flushStartTag() throws SAXException { if (this.startTagInProgress) { String tagName = (String) (this.elementStack.get(0)); this.contentHandler.startElement("", tagName, tagName, this.attributeList); this.attributeList.clear(); this.startTagInProgress = false; } } public void flush() { // don't need to do anything } public void close() { // don't need to do anything } public HierarchicalStreamWriter underlyingWriter() { return this; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -