📄 xmlwriter.java
字号:
/** Writes the given object which should be a String, a Node or a List * of Nodes. * * @param object is the object to output. */ public void write(Object object) throws IOException { if (object instanceof Node) { write((Node) object); } else if (object instanceof String) { write((String) object); } else if (object instanceof List) { List list = (List) object; for ( int i = 0, size = list.size(); i < size; i++ ) { write( list.get(i) ); } } else if (object != null) { throw new IOException( "Invalid object: " + object ); } } /** <p>Writes the opening tag of an {@link Element}, * including its {@link Attribute}s * but without its content.</p> * * @param element <code>Element</code> to output. */ public void writeOpen(Element element) throws IOException { writer.write("<"); writer.write( element.getQualifiedName() ); writeAttributes(element); writer.write(">"); } /** <p>Writes the closing tag of an {@link Element}</p> * * @param element <code>Element</code> to output. */ public void writeClose(Element element) throws IOException { writeClose( element.getQualifiedName() ); } // XMLFilterImpl methods //------------------------------------------------------------------------- public void parse(InputSource source) throws IOException, SAXException { installLexicalHandler(); super.parse(source); } public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException { for (int i = 0; i < LEXICAL_HANDLER_NAMES.length; i++) { if (LEXICAL_HANDLER_NAMES[i].equals(name)) { setLexicalHandler((LexicalHandler) value); return; } } super.setProperty(name, value); } public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException { for (int i = 0; i < LEXICAL_HANDLER_NAMES.length; i++) { if (LEXICAL_HANDLER_NAMES[i].equals(name)) { return getLexicalHandler(); } } return super.getProperty(name); } public void setLexicalHandler (LexicalHandler handler) { if (handler == null) { throw new NullPointerException("Null lexical handler"); } else { this.lexicalHandler = handler; } } public LexicalHandler getLexicalHandler(){ return lexicalHandler; } // ContentHandler interface //------------------------------------------------------------------------- public void setDocumentLocator(Locator locator) { super.setDocumentLocator(locator); } public void startDocument() throws SAXException { try { writeDeclaration(); super.startDocument(); } catch (IOException e) { handleException(e); } } public void endDocument() throws SAXException { super.endDocument(); if ( autoFlush ) { try { flush(); } catch ( IOException e) {} } } public void startPrefixMapping(String prefix, String uri) throws SAXException { if ( namespacesMap == null ) { namespacesMap = new HashMap(); } namespacesMap.put(prefix, uri); super.startPrefixMapping(prefix, uri); } public void endPrefixMapping(String prefix) throws SAXException { super.endPrefixMapping(prefix); } public void startElement(String namespaceURI, String localName, String qName, Attributes attributes) throws SAXException { try { charactersAdded = false; writePrintln(); indent(); writer.write("<"); writer.write(qName); writeNamespaces(); writeAttributes( attributes ); writer.write(">"); ++indentLevel; lastOutputNodeType = Node.ELEMENT_NODE; super.startElement( namespaceURI, localName, qName, attributes ); } catch (IOException e) { handleException(e); } } public void endElement(String namespaceURI, String localName, String qName) throws SAXException { try { charactersAdded = false; --indentLevel; if ( lastOutputNodeType == Node.ELEMENT_NODE ) { writePrintln(); indent(); } // XXXX: need to determine this using a stack and checking for // content / children boolean hadContent = true; if ( hadContent ) { writeClose(qName); } else { writeEmptyElementClose(qName); } lastOutputNodeType = Node.ELEMENT_NODE; super.endElement( namespaceURI, localName, qName ); } catch (IOException e) { handleException(e); } } public void characters(char[] ch, int start, int length) throws SAXException { if (ch == null || ch.length == 0 || length <= 0) { return; } try { /* * we can't use the writeString method here because it's possible * we don't receive all characters at once and calling writeString * would cause unwanted spaces to be added in between these chunks * of character arrays. */ String string = new String(ch, start, length); if (escapeText) { string = escapeElementEntities(string); } if (format.isTrimText()) { if ((lastOutputNodeType == Node.TEXT_NODE) && !charactersAdded) { writer.write(" "); } else if (charactersAdded && Character.isWhitespace(lastChar)) { writer.write(lastChar); } String delim = ""; StringTokenizer tokens = new StringTokenizer(string); while (tokens.hasMoreTokens()) { writer.write(delim); writer.write(tokens.nextToken()); delim = " "; } } else { writer.write(string); } charactersAdded = true; lastChar = ch[start + length - 1]; lastOutputNodeType = Node.TEXT_NODE; super.characters(ch, start, length); } catch (IOException e) { handleException(e); } } public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { super.ignorableWhitespace(ch, start, length); } public void processingInstruction(String target, String data) throws SAXException { try { indent(); writer.write("<?"); writer.write(target); writer.write(" "); writer.write(data); writer.write("?>"); writePrintln(); lastOutputNodeType = Node.PROCESSING_INSTRUCTION_NODE; super.processingInstruction(target, data); } catch (IOException e) { handleException(e); } } // DTDHandler interface //------------------------------------------------------------------------- public void notationDecl(String name, String publicID, String systemID) throws SAXException { super.notationDecl(name, publicID, systemID); } public void unparsedEntityDecl(String name, String publicID, String systemID, String notationName) throws SAXException { super.unparsedEntityDecl(name, publicID, systemID, notationName); } // LexicalHandler interface //------------------------------------------------------------------------- public void startDTD(String name, String publicID, String systemID) throws SAXException { inDTD = true; try { writeDocType(name, publicID, systemID); } catch (IOException e) { handleException(e); } if (lexicalHandler != null) { lexicalHandler.startDTD(name, publicID, systemID); } } public void endDTD() throws SAXException { inDTD = false; if (lexicalHandler != null) { lexicalHandler.endDTD(); } } public void startCDATA() throws SAXException { try { writer.write( "<![CDATA[" ); } catch (IOException e) { handleException(e); } if (lexicalHandler != null) { lexicalHandler.startCDATA(); } } public void endCDATA() throws SAXException { try { writer.write( "]]>" ); } catch (IOException e) { handleException(e); } if (lexicalHandler != null) { lexicalHandler.endCDATA(); } } public void startEntity(String name) throws SAXException { try { writeEntityRef(name); } catch (IOException e) { handleException(e); } if (lexicalHandler != null) { lexicalHandler.startEntity(name); } } public void endEntity(String name) throws SAXException { if (lexicalHandler != null) { lexicalHandler.endEntity(name); } } public void comment(char[] ch, int start, int length) throws SAXException { if ( showCommentsInDTDs || ! inDTD ) { try { charactersAdded = false; writeComment( new String(ch, start, length) ); } catch (IOException e) { handleException(e); } } if (lexicalHandler != null) { lexicalHandler.comment(ch, start, length); } } // Implementation methods //------------------------------------------------------------------------- protected void writeElement(Element element) throws IOException { int size = element.nodeCount(); String qualifiedName = element.getQualifiedName(); writePrintln(); indent(); writer.write("<"); writer.write(qualifiedName); int previouslyDeclaredNamespaces = namespaceStack.size(); Namespace ns = element.getNamespace(); if (isNamespaceDeclaration( ns ) ) { namespaceStack.push(ns); writeNamespace(ns); } // Print out additional namespace declarations boolean textOnly = true; for ( int i = 0; i < size; i++ ) { Node node = element.node(i); if ( node instanceof Namespace ) { Namespace additional = (Namespace) node; if (isNamespaceDeclaration( additional ) ) { namespaceStack.push(additional); writeNamespace(additional); } } else if ( node instanceof Element) { textOnly = false;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -