📄 xmlwriter.java
字号:
// this is illegal; but as yet we don't throw an exception // if someone tries to do this for ( int i = 0, size = element.attributeCount(); i < size; i++ ) { Attribute attribute = element.attribute(i); Namespace ns = attribute.getNamespace(); if (ns != null && ns != AbstractNamespace.NO_NAMESPACE && ns != AbstractNamespace.XML_NAMESPACE) { String prefix = ns.getPrefix(); String uri = namespaceStack.getURI(prefix); if (!ns.getURI().equals(uri)) { // output a new namespace declaration writeNamespace(ns); namespaceStack.push(ns); } } writer.write(" "); writer.write(attribute.getQualifiedName()); writer.write("=\""); writeEscapeAttributeEntities(attribute.getValue()); writer.write("\""); } } protected void writeAttribute(Attribute attribute) throws IOException { writer.write(" "); writer.write(attribute.getQualifiedName()); writer.write("="); writer.write("\""); writeEscapeAttributeEntities(attribute.getValue()); writer.write("\""); lastOutputNodeType = Node.ATTRIBUTE_NODE; } protected void writeAttributes(Attributes attributes) throws IOException { for (int i = 0, size = attributes.getLength(); i < size; i++) { writeAttribute( attributes, i ); } } protected void writeAttribute(Attributes attributes, int index) throws IOException { writer.write(" "); writer.write(attributes.getQName(index)); writer.write("=\""); writeEscapeAttributeEntities(attributes.getValue(index)); writer.write("\""); } protected void indent() throws IOException { String indent = format.getIndent(); if ( indent != null && indent.length() > 0 ) { for ( int i = 0; i < indentLevel; i++ ) { writer.write(indent); } } } /** * <p> * This will print a new line only if the newlines flag was set to true * </p> * */ protected void writePrintln() throws IOException { if (format.isNewlines()) { writer.write( format.getLineSeparator() ); } } /** * Get an OutputStreamWriter, use preferred encoding. */ protected Writer createWriter(OutputStream outStream, String encoding) throws UnsupportedEncodingException { return new BufferedWriter( new OutputStreamWriter( outStream, encoding ) ); } /** * <p> * This will write the declaration to the given Writer. * Assumes XML version 1.0 since we don't directly know. * </p> */ protected void writeDeclaration() throws IOException { String encoding = format.getEncoding(); // Only print of declaration is not suppressed if (! format.isSuppressDeclaration()) { // Assume 1.0 version if (encoding.equals("UTF8")) { writer.write("<?xml version=\"1.0\""); if (!format.isOmitEncoding()) { writer.write(" encoding=\"UTF-8\""); } writer.write("?>"); } else { writer.write("<?xml version=\"1.0\""); if (! format.isOmitEncoding()) { writer.write(" encoding=\"" + encoding + "\""); } writer.write("?>"); } println(); } } protected void writeClose(String qualifiedName) throws IOException { writer.write("</"); writer.write(qualifiedName); writer.write(">"); } protected void writeEmptyElementClose(String qualifiedName) throws IOException { // Simply close up if (! format.isExpandEmptyElements()) { writer.write("/>"); } else { writer.write("></"); writer.write(qualifiedName); writer.write(">"); } } protected boolean isExpandEmptyElements() { return format.isExpandEmptyElements(); } /** This will take the pre-defined entities in XML 1.0 and * convert their character representation to the appropriate * entity reference, suitable for XML attributes. */ protected String escapeElementEntities(String text) { char[] block = null; int i, last = 0, size = text.length(); for ( i = 0; i < size; i++ ) { String entity = null; char c = text.charAt(i); switch( c ) { case '<' : entity = "<"; break; case '>' : entity = ">"; break; case '&' : entity = "&"; break; case '\t': case '\n': case '\r': // don't encode standard whitespace characters if (preserve) { entity=String.valueOf(c); } break; default: if (c < 32 || shouldEncodeChar(c)) { entity = "&#" + (int) c + ";"; } break; } if (entity != null) { if ( block == null ) { block = text.toCharArray(); } buffer.append(block, last, i - last); buffer.append(entity); last = i + 1; } } if ( last == 0 ) { return text; } if ( last < size ) { if ( block == null ) { block = text.toCharArray(); } buffer.append(block, last, i - last); } String answer = buffer.toString(); buffer.setLength(0); return answer; } protected void writeEscapeAttributeEntities(String text) throws IOException { if ( text != null ) { String escapedText = escapeAttributeEntities( text ); writer.write( escapedText ); } } /** This will take the pre-defined entities in XML 1.0 and * convert their character representation to the appropriate * entity reference, suitable for XML attributes. */ protected String escapeAttributeEntities(String text) { char[] block = null; int i, last = 0, size = text.length(); for ( i = 0; i < size; i++ ) { String entity = null; char c = text.charAt(i); switch( c ) { case '<' : entity = "<"; break; case '>' : entity = ">"; break; case '\'' : entity = "'"; break; case '\"' : entity = """; break; case '&' : entity = "&"; break; case '\t': case '\n': case '\r': // don't encode standard whitespace characters break; default: if (c < 32 || shouldEncodeChar(c)) { entity = "&#" + (int) c + ";"; } break; } if (entity != null) { if ( block == null ) { block = text.toCharArray(); } buffer.append(block, last, i - last); buffer.append(entity); last = i + 1; } } if ( last == 0 ) { return text; } if ( last < size ) { if ( block == null ) { block = text.toCharArray(); } buffer.append(block, last, i - last); } String answer = buffer.toString(); buffer.setLength(0); return answer; } /** * Should the given character be escaped. This depends on the * encoding of the document. * * @return boolean */ protected boolean shouldEncodeChar(char c) { int max = getMaximumAllowedCharacter(); return max > 0 && c > max; } /** * Returns the maximum allowed character code that should be allowed * unescaped which defaults to 127 in US-ASCII (7 bit) or * 255 in ISO-* (8 bit). */ protected int defaultMaximumAllowedCharacter() { String encoding = format.getEncoding(); if (encoding != null) { if (encoding.equals("US-ASCII")) { return 127; } } // no encoding for things like ISO-*, UTF-8 or UTF-16 return -1; } protected boolean isNamespaceDeclaration( Namespace ns ) { if (ns != null && ns != AbstractNamespace.XML_NAMESPACE) { String uri = ns.getURI(); if ( uri != null ) { if ( ! namespaceStack.contains( ns ) ) { return true; } } } return false; } protected void handleException(IOException e) throws SAXException { throw new SAXException(e); } protected String getPadText() { return null; } //Laramie Crocker 4/8/2002 10:38AM /** Lets subclasses get at the current format object, so they can call setTrimText, setNewLines, etc. * Put in to support the HTMLWriter, in the way * that it pushes the current newline/trim state onto a stack and overrides * the state within preformatted tags. */ protected OutputFormat getOutputFormat() { return format; } public boolean resolveEntityRefs() { return resolveEntityRefs; } public void setResolveEntityRefs(boolean resolve) { this.resolveEntityRefs = resolve; }}/* * Redistribution and use of this software and associated documentation * ("Software"), with or without modification, are permitted provided * that the following conditions are met: * * 1. Redistributions of source code must retain copyright * statements and notices. Redistributions must also contain a * copy of this document. * * 2. Redistributions in binary form must reproduce the * above copyright notice, this list of conditions and the * following disclaimer in the documentation and/or other * materials provided with the distribution. * * 3. The name "DOM4J" must not be used to endorse or promote * products derived from this Software without prior written * permission of MetaStuff, Ltd. For written permission, * please contact dom4j-info@metastuff.com. * * 4. Products derived from this Software may not be called "DOM4J" * nor may "DOM4J" appear in their names without prior written * permission of MetaStuff, Ltd. DOM4J is a registered * trademark of MetaStuff, Ltd. * * 5. Due credit should be given to the DOM4J Project * (http://dom4j.org/). * * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. * * $Id: XMLWriter.java,v 1.4 2003/11/02 18:04:59 per_nyfelt Exp $ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -