📄 basemarkupserializer.java
字号:
} //----------------------------------// // DOM document serializing methods // //----------------------------------// /** * Serializes the DOM element using the previously specified * writer and output format. Throws an exception only if * an I/O exception occured while serializing. * * @param elem The element to serialize * @throws IOException An I/O exception occured while * serializing */ public void serialize( Element elem ) throws IOException { prepare(); serializeNode( elem ); _printer.flush(); if ( _printer.getException() != null ) throw _printer.getException(); } /** * Serializes the DOM document fragmnt using the previously specified * writer and output format. Throws an exception only if * an I/O exception occured while serializing. * * @param frag the document fragment to serialize * @throws IOException An I/O exception occured while * serializing */ public void serialize( DocumentFragment frag ) throws IOException { prepare(); serializeNode( frag ); _printer.flush(); if ( _printer.getException() != null ) throw _printer.getException(); } /** * Serializes the DOM document using the previously specified * writer and output format. Throws an exception only if * an I/O exception occured while serializing. * * @param doc The document to serialize * @throws IOException An I/O exception occured while * serializing */ public void serialize( Document doc ) throws IOException { prepare(); serializeNode( doc ); serializePreRoot(); _printer.flush(); if ( _printer.getException() != null ) throw _printer.getException(); } //------------------------------------------// // SAX document handler serializing methods // //------------------------------------------// public void startDocument() throws SAXException { try { prepare(); } catch ( IOException except ) { throw new SAXException( except.toString() ); } // Nothing to do here. All the magic happens in startDocument(String) } public void characters( char[] chars, int start, int length ) throws SAXException { ElementState state; try { 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 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 ); for ( int index = 0 ; index < length ; ++index ) { if ( index + 2 < length && chars[ index ] == ']' && chars[ index + 1 ] == ']' && chars[ index + 2 ] == '>' ) { printText( chars, start, index + 2, true, true ); _printer.printText( "]]><![CDATA[" ); start += index + 2; length -= index + 2; index = 0; } } if ( length > 0 ) printText( chars, start, length, true, true ); _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( chars, start, length, true, state.unescaped ); _printer.setNextIndent( saveIndent ); } else { printText( chars, start, length, false, state.unescaped ); } } } catch ( IOException except ) { throw new SAXException( except ); } } public void ignorableWhitespace( char[] chars, int start, int length ) throws SAXException { int i; try { content(); // Print ignorable whitespaces only when indenting, after // all they are indentation. Cancel the indentation to // not indent twice. if ( _indenting ) { _printer.setThisIndent( 0 ); for ( i = start ; length-- > 0 ; ++i ) _printer.printText( chars[ i ] ); } } catch ( IOException except ) { throw new SAXException( except ); } } public final void processingInstruction( String target, String code ) throws SAXException { try { processingInstructionIO( target, code ); } catch ( IOException except ) { throw new SAXException( except ); } } public void processingInstructionIO( String target, String code ) throws IOException { int index; StringBuffer buffer; ElementState state; state = content(); buffer = new StringBuffer( 40 ); // Create the processing instruction textual representation. // Make sure we don't have '?>' inside either target or code. index = target.indexOf( "?>" ); if ( index >= 0 ) buffer.append( "<?" ).append( target.substring( 0, index ) ); else buffer.append( "<?" ).append( target ); if ( code != null ) { buffer.append( ' ' ); index = code.indexOf( "?>" ); if ( index >= 0 ) buffer.append( code.substring( 0, index ) ); else buffer.append( code ); } buffer.append( "?>" ); // If before the root element (or after it), do not print // the PI directly but place it in the pre-root vector. if ( isDocumentState() ) { if ( _preRoot == null ) _preRoot = new Vector(); _preRoot.addElement( buffer.toString() ); } else { _printer.indent(); printText( buffer.toString(), true, true ); _printer.unindent(); if ( _indenting ) state.afterElement = true; } if (_allowDisableOutputEscaping) { if (target.equals(Result.PI_DISABLE_OUTPUT_ESCAPING)) startNonEscaping(); else if (target.equals(Result.PI_ENABLE_OUTPUT_ESCAPING)) endNonEscaping(); } } public void comment( char[] chars, int start, int length ) throws SAXException { try { comment( new String( chars, start, length ) ); } catch ( IOException except ) { throw new SAXException( except ); } } public void comment( String text ) throws IOException { StringBuffer buffer; int index; ElementState state; if ( _format.getOmitComments() ) return; state = content(); buffer = new StringBuffer( 40 ); // Create the processing comment textual representation. // Make sure we don't have '-->' inside the comment. index = text.indexOf( "-->" ); if ( index >= 0 ) buffer.append( "<!--" ).append( text.substring( 0, index ) ).append( "-->" ); else buffer.append( "<!--" ).append( text ).append( "-->" ); // If before the root element (or after it), do not print // the comment directly but place it in the pre-root vector. if ( isDocumentState() ) { if ( _preRoot == null ) _preRoot = new Vector(); _preRoot.addElement( buffer.toString() ); } else { // Indent this element on a new line if the first // content of the parent element or immediately // following an element. if ( _indenting && ! state.preserveSpace) _printer.breakLine(); _printer.indent(); printText( buffer.toString(), true, true ); _printer.unindent(); if ( _indenting ) state.afterElement = true; } state.afterComment = true; state.afterElement = false; } public void startCDATA() { ElementState state; state = getElementState(); state.doCData = true; } public void endCDATA() { ElementState state; state = getElementState(); state.doCData = false; } public void startNonEscaping() { ElementState state; state = getElementState(); state.unescaped = true; } public void endNonEscaping() { ElementState state; state = getElementState(); state.unescaped = false; } public void startPreserving() { ElementState state; state = getElementState(); state.preserveSpace = true; } public void endPreserving() { ElementState state; state = getElementState(); state.preserveSpace = false; } /** * Called at the end of the document to wrap it up. * Will flush the output stream and throw an exception * if any I/O error occured while serializing. * * @throws SAXException An I/O exception occured during * serializing */ public void endDocument() throws SAXException { try { // Print all the elements accumulated outside of // the root element. serializePreRoot(); // Flush the output, this is necessary for buffered output. _printer.flush(); } catch ( IOException except ) { throw new SAXException( except ); } } public void startEntity( String name ) { // ??? } public void endEntity( String name ) { // ??? } public void setDocumentLocator( Locator locator ) { // Nothing to do } //-----------------------------------------// // SAX content handler serializing methods // //-----------------------------------------// public void skippedEntity ( String name ) throws SAXException { try { endCDATA(); content(); _printer.printText( '&' ); _printer.printText( name ); _printer.printText( ';' ); } catch ( IOException except ) { throw new SAXException( except ); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -