basemarkupserializer.java

来自「JAVA的一些源码 JAVA2 STANDARD EDITION DEVELO」· Java 代码 · 共 1,887 行 · 第 1/5 页

JAVA
1,887
字号
                      switch (code) {                        case NodeFilter.FILTER_REJECT:{                            return; // remove the node                          }                          case NodeFilter.FILTER_SKIP: {                               child = node.getFirstChild();                              while ( child != null ) {                                  serializeNode( child );                                  child = child.getNextSibling();                              }                              return;                          }                          default: {                               // fall through                          }                      }                  }                         checkUnboundNamespacePrefixedNode(node);                              _printer.printText("&");                _printer.printText(node.getNodeName());                _printer.printText(";");            }            else {                child = node.getFirstChild();                while ( child != null ) {                    serializeNode( child );                    child = child.getNextSibling();                }            }            break;        }        case Node.PROCESSING_INSTRUCTION_NODE : {                    if (fDOMFilter !=null &&                   (fDOMFilter.getWhatToShow() & NodeFilter.SHOW_PROCESSING_INSTRUCTION)!= 0) {                  short code = fDOMFilter.acceptNode(node);                  switch (code) {                    case NodeFilter.FILTER_REJECT:                                          case NodeFilter.FILTER_SKIP: {                           return;  // skip this node                                          }                    default: { // fall through                    }                  }            }            processingInstructionIO( node.getNodeName(), node.getNodeValue() );            break;        }        case Node.ELEMENT_NODE :  {            if (fDOMFilter !=null &&                   (fDOMFilter.getWhatToShow() & NodeFilter.SHOW_ELEMENT)!= 0) {                  short code = fDOMFilter.acceptNode(node);                  switch (code) {                    case NodeFilter.FILTER_REJECT: {                        return;                                         }                    case NodeFilter.FILTER_SKIP: {                         Node child = node.getFirstChild();                        while ( child != null ) {                            serializeNode( child );                            child = child.getNextSibling();                        }                        return;  // skip this node                                          }                    default: { // fall through                    }                  }            }            serializeElement( (Element) node );            break;        }        case Node.DOCUMENT_NODE : {            DocumentType      docType;            DOMImplementation domImpl;            NamedNodeMap      map;            Entity            entity;            Notation          notation;            int               i;                        // If there is a document type, use the SAX events to            // serialize it.            docType = ( (Document) node ).getDoctype();            if (docType != null) {                // DOM Level 2 (or higher)                domImpl = ( (Document) node ).getImplementation();                try {                    String internal;                    _printer.enterDTD();                    _docTypePublicId = docType.getPublicId();                    _docTypeSystemId = docType.getSystemId();                    internal = docType.getInternalSubset();                    if ( internal != null && internal.length() > 0 )                        _printer.printText( internal );                    endDTD();                }                // DOM Level 1 -- does implementation have methods?                catch (NoSuchMethodError nsme) {                    Class docTypeClass = docType.getClass();                    String docTypePublicId = null;                    String docTypeSystemId = null;                    try {                        java.lang.reflect.Method getPublicId = docTypeClass.getMethod("getPublicId", null);                        if (getPublicId.getReturnType().equals(String.class)) {                            docTypePublicId = (String)getPublicId.invoke(docType, null);                        }                    }                    catch (Exception e) {                        // ignore                    }                    try {                        java.lang.reflect.Method getSystemId = docTypeClass.getMethod("getSystemId", null);                        if (getSystemId.getReturnType().equals(String.class)) {                            docTypeSystemId = (String)getSystemId.invoke(docType, null);                        }                    }                    catch (Exception e) {                        // ignore                    }                    _printer.enterDTD();                    _docTypePublicId = docTypePublicId;                    _docTypeSystemId = docTypeSystemId;                    endDTD();                }            }            // !! Fall through        }        case Node.DOCUMENT_FRAGMENT_NODE : {            Node         child;            // By definition this will happen if the node is a document,            // document fragment, etc. Just serialize its contents. It will            // work well for other nodes that we do not know how to serialize.            child = node.getFirstChild();            while ( child != null ) {                serializeNode( child );                child = child.getNextSibling();            }            break;        }        default:            break;        }    }    /**     * Must be called by a method about to print any type of content.     * If the element was just opened, the opening tag is closed and     * will be matched to a closing tag. Returns the current element     * state with <tt>empty</tt> and <tt>afterElement</tt> set to false.     *     * @return The current element state     * @throws IOException An I/O exception occured while     *   serializing     */    protected ElementState content()        throws IOException    {        ElementState state;        state = getElementState();        if ( ! isDocumentState() ) {            // Need to close CData section first            if ( state.inCData && ! state.doCData ) {                _printer.printText( "]]>" );                state.inCData = false;            }            // If this is the first content in the element,            // change the state to not-empty and close the            // opening element tag.            if ( state.empty ) {                _printer.printText( '>' );                state.empty = false;            }            // Except for one content type, all of them            // are not last element. That one content            // type will take care of itself.            state.afterElement = false;            // Except for one content type, all of them            // are not last comment. That one content            // type will take care of itself.            state.afterComment = false;        }        return state;    }    /**     * Called to print the text contents in the prevailing element format.     * Since this method is capable of printing text as CDATA, it is used     * for that purpose as well. White space handling is determined by the     * current element state. In addition, the output format can dictate     * whether the text is printed as CDATA or unescaped.     *     * @param text The text to print     * @param unescaped True is should print unescaped     * @throws IOException An I/O exception occured while     *   serializing     */    protected void characters( String text )        throws IOException    {        ElementState state;        state = content();        // Check if text should be print as CDATA section or unescaped        // based on elements listed in the output format (the element        // state) or whether we are inside a CDATA section or entity.        if ( state.inCData || state.doCData ) {            int          index;            int          saveIndent;            // Print a CDATA section. The text is not escaped, but ']]>'            // appearing in the code must be identified and dealt with.            // The contents of a text node is considered space preserving.            if ( ! state.inCData ) {                _printer.printText("<![CDATA[");                state.inCData = true;            }            saveIndent = _printer.getNextIndent();            _printer.setNextIndent( 0 );            printCDATAText( text);            _printer.setNextIndent( saveIndent );        } else {            int saveIndent;            if ( state.preserveSpace ) {                // If preserving space then hold of indentation so no                // excessive spaces are printed at line breaks, escape                // the text content without replacing spaces and print                // the text breaking only at line breaks.                saveIndent = _printer.getNextIndent();                _printer.setNextIndent( 0 );                printText( text, true, state.unescaped );                _printer.setNextIndent( saveIndent );            } else {                printText( text, false, state.unescaped );            }        }    }    /**     * Returns the suitable entity reference for this character value,     * or null if no such entity exists. Calling this method with <tt>'&amp;'</tt>     * will return <tt>"&amp;amp;"</tt>.     *     * @param ch Character value     * @return Character entity name, or null     */    protected abstract String getEntityRef( int ch );    /**     * Called to serializee the DOM element. The element is serialized based on     * the serializer's method (XML, HTML, XHTML).     *     * @param elem The element to serialize     * @throws IOException An I/O exception occured while     *   serializing     */    protected abstract void serializeElement( Element elem )        throws IOException;    /**     * Comments and PIs cannot be serialized before the root element,     * because the root element serializes the document type, which     * generally comes first. Instead such PIs and comments are     * accumulated inside a vector and serialized by calling this     * method. Will be called when the root element is serialized     * and when the document finished serializing.     *     * @throws IOException An I/O exception occured while     *   serializing     */    protected void serializePreRoot()        throws IOException    {        int i;        if ( _preRoot != null ) {            for ( i = 0 ; i < _preRoot.size() ; ++i ) {                printText( (String) _preRoot.elementAt( i ), true, true );                if ( _indenting )                _printer.breakLine();            }            _preRoot.removeAllElements();        }    }    //---------------------------------------------//    // Text pretty printing and formatting methods //    //---------------------------------------------//    protected void printCDATAText( String text ) throws IOException {        int length = text.length();        char ch;        for ( int index = 0 ; index <  length; ++index ) {            ch = text.charAt( index );                        if (ch == ']'                && index + 2 < length                && text.charAt(index + 1) == ']'                && text.charAt(index + 2) == '>') { // check for ']]>'                if (fDOMErrorHandler != null) {                    // REVISIT: this means that if DOM Error handler is not registered we don't report any                    // fatal errors and might serialize not wellformed document                    if ((features & DOMSerializerImpl.SPLITCDATA) == 0                        && (features & DOMSerializerImpl.WELLFORMED) == 0) {                        // issue fatal error                        String msg =                            DOMMessageFormatter.formatMessage(                                DOMMessageFormatter.SERIALIZER_DOMAIN,                                "EndingCDATA",                                null);                        modifyDOMError(                            msg,                            DOMError.SEVERITY_FATAL_ERROR,                            fCurrentNode);                        boolean continueProcess =                            fDOMErrorHandler.handleError(fDOMError);                        if (!continueProcess) {                            throw new IOException();                        }                    } else {                        // issue warning                        String msg =                            DOMMessageFormatter.formatMessage(                                DOMMessageFormatter.SERIALIZER_DOMAIN,                                "SplittingCDATA",                                null);                        modifyDOMError(                            msg,                            DOMError.SEVERITY_WARNING,                            fCurrentNode);                        fDOMErrorHandler.handleError(fDOMError);                    }                }                // split CDATA section                _printer.printText("]]]]><![CDATA[>");                index += 2;                continue;            }                        if (!XMLChar.isValid(ch)) {                // check if it is surrogate                if (++index <length) {                    surrogates(ch, text.charAt(index));                }                 else {                    fatalError("The character '"+(char)ch+"' is an invalid XML character");                 }                continue;            } else {                if ( ( ch >= ' ' && _encodingInfo.isPrintable((char)ch) && ch != 0xF7 ) ||                     ch == '\n' || ch == '\r' || ch == '\t' ) {                    _printer.printText((char)ch);                } else {                    // The character is not printable -- split CDATA section                    _printer.printText("]]>&#x");                                            _printer.printText(Integer.toHexString(ch));                                            _printer.printText(";<![CDATA[");                }

⌨️ 快捷键说明

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