📄 outputformat.java
字号:
/* * The Apache Software License, Version 1.1 * * * Copyright (c) 1999 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 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 end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Xerces" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``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 THE APACHE SOFTWARE FOUNDATION 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. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation and was * originally based on software copyright (c) 1999, International * Business Machines, Inc., http://www.apache.org. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */// Aug 21, 2000:// Added ability to omit DOCTYPE declaration.// Reported by Lars Martin <lars@smb-tec.com>// Aug 25, 2000:// Added ability to omit comments.// Contributed by Anupam Bagchi <abagchi@jtcsv.com>package org.jasig.portal.serialize;import org.w3c.dom.Document;import org.w3c.dom.DocumentType;import org.w3c.dom.Node;import org.w3c.dom.html.HTMLDocument;/** * Specifies an output format to control the serializer. Based on the * XSLT specification for output format, plus additional parameters. * Used to select the suitable serializer and determine how the * document should be formatted on output. * <p> * The two interesting constructors are: * <ul> * <li>{@link #OutputFormat(String,String,boolean)} creates a format * for the specified method (XML, HTML, Text, etc), encoding and indentation * <li>{@link #OutputFormat(Document,String,boolean)} creates a format * compatible with the document type (XML, HTML, Text, etc), encoding and * indentation * </ul> * * * @version $Revision: 1.4 $ $Date: 2004/09/10 17:17:27 $ * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a> * <a href="mailto:visco@intalio.com">Keith Visco</a> * @see Serializer * @see Method * @see LineSeparator */public class OutputFormat{ public static class DTD { /** * Public identifier for HTML document type. */ public static final String HTMLPublicId = "-//W3C//DTD HTML 4.0//EN"; /** * System identifier for HTML document type. */ public static final String HTMLSystemId = "http://www.w3.org/TR/WD-html-in-xml/DTD/xhtml1-strict.dtd"; /** * Public identifier for XHTML document type. */ public static final String XHTMLPublicId = "-//W3C//DTD XHTML 1.0 Strict//EN"; /** * System identifier for XHTML document type. */ public static final String XHTMLSystemId = "http://www.w3.org/TR/WD-html-in-xml/DTD/xhtml1-strict.dtd"; } public static class Defaults { /** * If indentation is turned on, the default identation * level is 4. * * @see #setIndenting(boolean) */ public static final int Indent = 4; /** * The default encoding for Web documents it UTF-8. * * @see #getEncoding() */ public static final String Encoding = "UTF-8"; /** * The default line width at which to break long lines * when identing. This is set to 72. */ public static final int LineWidth = 72; } /** * Holds the output method specified for this document, * or null if no method was specified. */ private String _method; /** * Specifies the version of the output method. */ private String _version; /** * The indentation level, or zero if no indentation * was requested. */ private int _indent = 0; /** * The encoding to use, if an input stream is used. * The default is always UTF-8. */ private String _encoding = Defaults.Encoding; /** * The EncodingInfo instance for _encoding. */ private EncodingInfo _encodingInfo = null; /** * The specified media type or null. */ private String _mediaType; /** * The specified document type system identifier, or null. */ private String _doctypeSystem; /** * The specified document type public identifier, or null. */ private String _doctypePublic; /** * Ture if the XML declaration should be ommited; */ private boolean _omitXmlDeclaration = false; /** * Ture if the DOCTYPE declaration should be ommited; */ private boolean _omitDoctype = false; /** * Ture if comments should be ommited; */ private boolean _omitComments = false; /** * Ture if the comments should be ommited; */ private boolean _stripComments = false; /** * True if the document type should be marked as standalone. */ private boolean _standalone = false; /** * List of element tag names whose text node children must * be output as CDATA. */ private String[] _cdataElements; /** * List of element tag names whose text node children must * be output unescaped. */ private String[] _nonEscapingElements; /** * The selected line separator. */ private String _lineSeparator = LineSeparator.Web; /** * The line width at which to wrap long lines when indenting. */ private int _lineWidth = Defaults.LineWidth; /** * True if spaces should be preserved in elements that do not * specify otherwise, or specify the default behavior. */ private boolean _preserve = false; /** * Constructs a new output format with the default values. */ public OutputFormat() { } /** * Constructs a new output format with the default values for * the specified method and encoding. If <tt>indent</tt> * is true, the document will be pretty printed with the default * indentation level and default line wrapping. * * @param method The specified output method * @param encoding The specified encoding * @param indenting True for pretty printing * @see #setEncoding(String) * @see #setIndenting(boolean) * @see #setMethod(String) */ public OutputFormat( String method, String encoding, boolean indenting ) { setMethod( method ); setEncoding( encoding ); setIndenting( indenting ); } /** * Constructs a new output format with the proper method, * document type identifiers and media type for the specified * document. * * @param doc The document to output * @see #whichMethod */ public OutputFormat( Document doc ) { setMethod( whichMethod( doc ) ); setDoctype( whichDoctypePublic( doc ), whichDoctypeSystem( doc ) ); setMediaType( whichMediaType( getMethod() ) ); } /** * Constructs a new output format with the proper method, * document type identifiers and media type for the specified * document, and with the specified encoding. If <tt>indent</tt> * is true, the document will be pretty printed with the default * indentation level and default line wrapping. * * @param doc The document to output * @param encoding The specified encoding * @param indenting True for pretty printing * @see #setEncoding(String) * @see #setIndenting(boolean) * @see #whichMethod(Document) */ public OutputFormat( Document doc, String encoding, boolean indenting ) { this( doc ); setEncoding( encoding ); setIndenting( indenting ); } /** * Returns the method specified for this output format. * Typically the method will be <tt>xml</tt>, <tt>html</tt> * or <tt>text</tt>, but it might be other values. * If no method was specified, null will be returned * and the most suitable method will be determined for * the document by calling {@link #whichMethod}. * * @return The specified output method, or null */ public String getMethod() { return _method; } /** * Sets the method for this output format. * * @see #getMethod * @param method The output method, or null */ public void setMethod( String method ) { _method = method; } /** * Returns the version for this output method. * If no version was specified, will return null * and the default version number will be used. * If the serializerr does not support that particular * version, it should default to a supported version. * * @return The specified method version, or null */ public String getVersion() { return _version; } /** * Sets the version for this output method. * For XML the value would be "1.0", for HTML * it would be "4.0". * * @see #getVersion * @param version The output method version, or null */ public void setVersion( String version ) { _version = version; } /** * Returns the indentation specified. If no indentation * was specified, zero is returned and the document * should not be indented. * * @return The indentation or zero * @see #setIndenting */ public int getIndent() { return _indent; } /** * Returns true if indentation was specified. */ public boolean getIndenting() { return ( _indent > 0 ); } /** * Sets the indentation. The document will not be * indented if the indentation is set to zero. * Calling {@link #setIndenting} will reset this * value to zero (off) or the default (on). * * @param indent The indentation, or zero */ public void setIndent( int indent ) { if ( indent < 0 ) _indent = 0; else _indent = indent; } /** * Sets the indentation on and off. When set on, the default * indentation level and default line wrapping is used * (see <code>Defaults.Indent</code> and <code>Defaults.LineWidth</code>). * To specify a different indentation level or line wrapping, * use {@link #setIndent} and {@link #setLineWidth}. * * @param on True if indentation should be on */ public void setIndenting( boolean on ) { if ( on ) { _indent = Defaults.Indent; _lineWidth = Defaults.LineWidth; } else { _indent = 0; _lineWidth = 0; } } /** * Returns the specified encoding. If no encoding was * specified, the default is always "UTF-8". * * @return The encoding */ public String getEncoding() { return _encoding; } /** * Sets the encoding for this output method. If no * encoding was specified, the default is always "UTF-8". * Make sure the encoding is compatible with the one * used by the {@link java.io.Writer}. * * @see #getEncoding * @param encoding The encoding, or null */ public void setEncoding( String encoding ) { _encoding = encoding; _encodingInfo = null; } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -