📄 xmlwriter.java
字号:
/*====================================================================*\XmlWriter.javaXML writer class.------------------------------------------------------------------------This file is part of FuncPlotter, a combined Java application and appletfor plotting explicit functions in one variable.Copyright 2005-2007 Andy Morgan-Richards.FuncPlotter is free software: you can redistribute it and/or modify itunder the terms of the GNU General Public License as published by theFree Software Foundation, either version 3 of the License, or (at youroption) any later version.This program is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public License alongwith this program. If not, see <http://www.gnu.org/licenses/>.\*====================================================================*/// PACKAGEpackage xml;//----------------------------------------------------------------------// IMPORTSimport java.io.BufferedWriter;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.io.UnsupportedEncodingException;import java.io.Writer;import java.util.ArrayList;import java.util.List;import org.w3c.dom.Element;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.w3c.dom.NodeList;//----------------------------------------------------------------------// XML WRITER CLASSpublic class XmlWriter{////////////////////////////////////////////////////////////////////////// Constants//////////////////////////////////////////////////////////////////////// public enum Standalone { NONE, NO, YES } public static final int INDENT_INCREMENT = 2; private static final String DEFAULT_LINE_SEPARATOR = "\n"; private static final String VERSION_STR = " version="; private static final String ENCODING_STR = " encoding="; private static final String STANDALONE_STR = " standalone="; private static final String DOCTYPE_STR = "<!DOCTYPE "; private static final String PUBLIC_STR = "PUBLIC "; private static final String SYSTEM_STR = "SYSTEM "; private static final String NO_STR = "no"; private static final String YES_STR = "yes"; private interface AttrName { String XMLNS = "xmlns"; }////////////////////////////////////////////////////////////////////////// Member classes : non-inner classes//////////////////////////////////////////////////////////////////////// // XML ATTRIBUTE CLASS public static class Attribute { //////////////////////////////////////////////////////////////////// // Constructors //////////////////////////////////////////////////////////////////// public Attribute( String name, boolean value ) { this( name, value ? YES_STR : NO_STR ); } //-------------------------------------------------------------- public Attribute( String name, int value ) { this( name, Integer.toString( value ) ); } //-------------------------------------------------------------- public Attribute( String name, long value ) { this( name, Long.toString( value ) ); } //-------------------------------------------------------------- public Attribute( String name, double value ) { this( name, Double.toString( value ) ); } //-------------------------------------------------------------- public Attribute( String name, Object value ) { this( name, value.toString( ) ); } //-------------------------------------------------------------- public Attribute( String name, Object value, boolean escape ) { this( name, value.toString( ), escape ); } //-------------------------------------------------------------- public Attribute( String name, String value ) { this.name = name; this.value = value; } //-------------------------------------------------------------- public Attribute( String name, String value, boolean escape ) { this( name, escape ? escape( value ) : value ); } //-------------------------------------------------------------- //////////////////////////////////////////////////////////////////// // Instance variables //////////////////////////////////////////////////////////////////// String name; String value; } //==================================================================////////////////////////////////////////////////////////////////////////// Constructors//////////////////////////////////////////////////////////////////////// public XmlWriter( File file, String charsetName ) throws FileNotFoundException, SecurityException, UnsupportedEncodingException { fileOutStream = new FileOutputStream( file ); init( new BufferedWriter( new OutputStreamWriter( fileOutStream, charsetName ) ) ); } //------------------------------------------------------------------ public XmlWriter( OutputStream outStream, String charsetName ) throws UnsupportedEncodingException { init( new BufferedWriter( new OutputStreamWriter( outStream, charsetName ) ) ); } //------------------------------------------------------------------ public XmlWriter( Writer writer ) { init( writer ); } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Class methods//////////////////////////////////////////////////////////////////////// public static String escape( char ch ) { switch ( ch ) { case '<': return "<"; case '>': return ">"; case '\'': return "'"; case '"': return """; case '&': return "&"; default: return Character.toString( ch ); } } //------------------------------------------------------------------ public static String escape( CharSequence charSeq ) { StringBuilder buffer = new StringBuilder( ); for ( int i = 0; i < charSeq.length( ); ++i ) buffer.append( escape( charSeq.charAt( i ) ) ); return buffer.toString( ); } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance methods//////////////////////////////////////////////////////////////////////// public FileOutputStream getFileOutStream( ) { return fileOutStream; } //------------------------------------------------------------------ public String getLineSeparator( ) { return lineSeparator; } //------------------------------------------------------------------ public int getOutLength( ) { return outBuffer.length( ); } //------------------------------------------------------------------ public void setLineSeparator( String lineSeparator ) { this.lineSeparator = (lineSeparator == null) ? System.getProperty( "line.separator", DEFAULT_LINE_SEPARATOR ) : lineSeparator; } //------------------------------------------------------------------ public void close( ) throws IOException { try { if ( outStream != null ) { outStream.append( outBuffer ); outStream.close( ); } } catch ( IOException e ) { throw e; } finally { outStream = null; outBuffer.setLength( 0 ); } } //------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -