⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 htmlserializer.java

📁 uPortal是开放源码的Portal门户产品
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* * 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/>. */// Sep 14, 2000://  Fixed serializer to report IO exception directly, instead at//  the end of document processing.//  Reported by Patrick Higgins <phiggins@transzap.com>// Aug 21, 2000://  Fixed bug in startDocument not calling prepare.//  Reported by Mikael Staldal <d96-mst-ingen-reklam@d.kth.se>// Aug 21, 2000://  Added ability to omit DOCTYPE declaration.// Sep 1, 2000://   If no output format is provided the serializer now defaults//   to ISO-8859-1 encoding. Reported by Mikael Staldal//   <d96-mst@d.kth.se>package org.jasig.portal.serialize;import java.io.IOException;import java.io.OutputStream;import java.io.Writer;import java.util.Enumeration;import org.jasig.portal.IAnchoringSerializer;import org.w3c.dom.Attr;import org.w3c.dom.Element;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.xml.sax.AttributeList;import org.xml.sax.Attributes;import org.xml.sax.SAXException;/** * Implements an HTML/XHTML serializer supporting both DOM and SAX * pretty serializing. HTML/XHTML mode is determined in the * constructor.  For usage instructions see {@link Serializer}. * <p> * If an output stream is used, the encoding is taken from the * output format (defaults to <tt>UTF-8</tt>). If a writer is * used, make sure the writer uses the same encoding (if applies) * as specified in the output format. * <p> * The serializer supports both DOM and SAX. DOM serializing is done * by calling {@link #serialize(Element)} and SAX serializing is done by firing * SAX events and using the serializer as a document handler. * <p> * If an I/O exception occurs while serializing, the serializer * will not throw an exception directly, but only throw it * at the end of serializing (either DOM or SAX's {@link * org.xml.sax.DocumentHandler#endDocument}. * <p> * For elements that are not specified as whitespace preserving, * the serializer will potentially break long text lines at space * boundaries, indent lines, and serialize elements on separate * lines. Line terminators will be regarded as spaces, and * spaces at beginning of line will be stripped. * <p> * XHTML is slightly different than HTML: * <ul> * <li>Element/attribute names are lower case and case matters * <li>Attributes must specify value, even if empty string * <li>Empty elements must have '/' in empty tag * <li>Contents of SCRIPT and STYLE elements serialized as CDATA * </ul> * * * @version $Revision: 1.6.2.1 $ $Date: 2004/11/02 20:41:47 $ * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a> * @see Serializer */public class HTMLSerializer extends BaseMarkupSerializer implements IAnchoringSerializer {    /**     * True if serializing in XHTML format.     */    private static boolean _xhtml;    public static String XHTMLNamespace = "";    private String anchorId = null;    /**     * Constructs a new HTML/XHTML serializer depending on the value of     * <tt>xhtml</tt>. The serializer cannot be used without calling     * init() first.     *     * @param xhtml True if XHTML serializing     */    protected HTMLSerializer( boolean xhtml, OutputFormat format )    {        super( format );        _xhtml = xhtml;    }    /**     * Constructs a new serializer. The serializer cannot be used without     * calling {@link #setOutputCharStream(Writer)} or {@link #setOutputByteStream(OutputStream)}     * first.     */    public HTMLSerializer()    {        this( false, new OutputFormat( Method.HTML, "ISO-8859-1", false ) );    }    /**     * Constructs a new serializer. The serializer cannot be used without     * calling {@link #setOutputCharStream(Writer)} or {@link #setOutputByteStream(OutputStream)}     * first.     */    public HTMLSerializer( OutputFormat format )    {        this( false, format != null ? format : new OutputFormat( Method.HTML, "ISO-8859-1", false ) );    }    /**     * Constructs a new serializer that writes to the specified writer     * using the specified output format. If <tt>format</tt> is null,     * will use a default output format.     *     * @param writer The writer to use     * @param format The output format to use, null for the default     */    public HTMLSerializer( Writer writer, OutputFormat format )    {        this( false, format != null ? format : new OutputFormat( Method.HTML, "ISO-8859-1", false ) );        setOutputCharStream( writer );    }    /**     * Constructs a new serializer that writes to the specified output     * stream using the specified output format. If <tt>format</tt>     * is null, will use a default output format.     *     * @param output The output stream to use     * @param format The output format to use, null for the default     */    public HTMLSerializer( OutputStream output, OutputFormat format )    {        this( false, format != null ? format : new OutputFormat( Method.HTML, "ISO-8859-1", false ) );        setOutputByteStream( output );    }    public void setOutputFormat( OutputFormat format )    {        super.setOutputFormat( format != null ? format : new OutputFormat( Method.HTML, "ISO-8859-1", false ) );    }    //-----------------------------------------//    // SAX content handler serializing methods //    //-----------------------------------------//    public void startElement( String namespaceURI, String localName,                              String rawName, Attributes attrs )        throws SAXException    {        int          i;        boolean      preserveSpace;        ElementState state;        String       name;        String       value;        String       htmlName;        boolean      addNSAttr = false;        try {            if ( _printer == null )                throw new IllegalStateException( "SER002 No writer supplied for serializer" );            state = getElementState();            if ( isDocumentState() ) {                // If this is the root element handle it differently.                // If the first root element in the document, serialize                // the document's DOCTYPE. Space preserving defaults                // to that of the output format.                if ( ! _started )                    startDocument( localName == null ? rawName : localName );            } else {                // For any other element, if first in parent, then                // close parent's opening tag and use the parnet's                // space preserving.                if ( state.empty )                    _printer.printText( '>' );                // Indent this element on a new line if the first                // content of the parent element or immediately                // following an element.                if ( _indenting && ! state.preserveSpace &&                     ( state.empty || state.afterElement ) )                    _printer.breakLine();            }            preserveSpace = state.preserveSpace;            // Do not change the current element state yet.            // This only happens in endElement().            if ( rawName == null ) {                rawName = localName;                if ( namespaceURI != null ) {                    String prefix;                    prefix = getPrefix( namespaceURI );                    if ( prefix.length() > 0 )                        rawName = prefix + ":" + localName;                }                addNSAttr = true;            }            if ( namespaceURI == null )                htmlName = rawName;            else {                if ( namespaceURI.equals( XHTMLNamespace ) )                    htmlName = localName;                else                    htmlName = null;            }            // XHTML: element names are lower case, DOM will be different            _printer.printText( '<' );            if ( _xhtml )                _printer.printText( rawName.toLowerCase() );            else                _printer.printText( rawName );            _printer.indent();            // For each attribute serialize it's name and value as one part,            // separated with a space so the element can be broken on            // multiple lines.            if ( attrs != null ) {                for ( i = 0 ; i < attrs.getLength() ; ++i ) {                    _printer.printSpace();                    name = attrs.getQName( i ).toLowerCase();;                    value = attrs.getValue( i );                    if ( _xhtml || namespaceURI != null ) {                        // XHTML: print empty string for null values.                        if ( value == null ) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -