📄 htmlserializer.java
字号:
_printer.printText( name ); _printer.printText( "=\"\"" ); } else { _printer.printText( name ); _printer.printText( "=\"" ); value = ProxyWriter.considerProxyRewrite(name,localName,value); value = appendAnchorIfNecessary(rawName.toLowerCase(),name,value); printEscaped( value ); _printer.printText( '"' ); } } else { // HTML: Empty values print as attribute name, no value. // HTML: URI attributes will print unescaped if ( value == null || value.length() == 0 ) _printer.printText( name ); else if ( HTMLdtd.isURI( rawName, name ) ) { _printer.printText( name ); _printer.printText( "=\"" ); value = ProxyWriter.considerProxyRewrite(name,localName,value); _printer.printText( escapeURI( value ) ); _printer.printText( '"' ); } else if ( HTMLdtd.isBoolean( rawName, name ) ) _printer.printText( name ); else { _printer.printText( name ); _printer.printText( "=\"" ); printEscaped( value ); _printer.printText( '"' ); } } } } if ( htmlName != null && HTMLdtd.isPreserveSpace( htmlName ) ) preserveSpace = true; if ( addNSAttr ) { Enumeration enum; enum = _prefixes.keys(); while ( enum.hasMoreElements() ) { _printer.printSpace(); value = (String) enum.nextElement(); name = (String) _prefixes.get( value ); if ( name.length() == 0 ) { _printer.printText( "xmlns=\"" ); printEscaped( value ); _printer.printText( '"' ); } else { _printer.printText( "xmlns:" ); _printer.printText( name ); _printer.printText( "=\"" ); printEscaped( value ); _printer.printText( '"' ); } } } // Now it's time to enter a new element state // with the tag name and space preserving. // We still do not change the curent element state. state = enterElementState( namespaceURI, localName, rawName, preserveSpace ); // Prevents line breaks inside A/TD if ( htmlName != null && ( htmlName.equalsIgnoreCase( "A" ) || htmlName.equalsIgnoreCase( "TD" ) ) ) { state.empty = false; _printer.printText( '>' ); } // Handle SCRIPT and STYLE specifically by changing the // state of the current element to CDATA (XHTML) or // unescaped (HTML). if ( htmlName != null && ( rawName.equalsIgnoreCase( "SCRIPT" ) || rawName.equalsIgnoreCase( "STYLE" ) ) ) { if ( _xhtml ) { // XHTML: Print contents as CDATA section state.doCData = true; } else { // HTML: Print contents unescaped state.unescaped = true; } } } catch ( IOException except ) { throw new SAXException( except ); } } public void endElement( String namespaceURI, String localName, String rawName ) throws SAXException { try { endElementIO( namespaceURI, localName, rawName ); } catch ( IOException except ) { throw new SAXException( except ); } } public void endElementIO( String namespaceURI, String localName, String rawName ) throws IOException { ElementState state; String htmlName; // Works much like content() with additions for closing // an element. Note the different checks for the closed // element's state and the parent element's state. _printer.unindent(); state = getElementState(); if ( state.namespaceURI == null ) htmlName = state.rawName; else { if ( state.namespaceURI.equals( XHTMLNamespace ) ) htmlName = state.localName; else htmlName = null; } if ( _xhtml) { if ( state.empty ) { _printer.printText( " />" ); } else { // Must leave CData section first if ( state.inCData ) _printer.printText( "]]>" ); // XHTML: element names are lower case, DOM will be different _printer.printText( "</" ); _printer.printText( state.rawName.toLowerCase() ); _printer.printText( '>' ); } } else { if ( state.empty ) _printer.printText( '>' ); // This element is not empty and that last content was // another element, so print a line break before that // last element and this element's closing tag. // [keith] Provided this is not an anchor. // HTML: some elements do not print closing tag (e.g. LI) if ( htmlName == null || ! HTMLdtd.isOnlyOpening( htmlName ) ) { if ( _indenting && ! state.preserveSpace && state.afterElement ) _printer.breakLine(); // Must leave CData section first (Illegal in HTML, but still) if ( state.inCData ) _printer.printText( "]]>" ); _printer.printText( "</" ); _printer.printText( state.rawName ); _printer.printText( '>' ); } } // Leave the element state and update that of the parent // (if we're not root) to not empty and after element. state = leaveElementState(); // Temporary hack to prevent line breaks inside A/TD if ( htmlName == null || ( ! htmlName.equalsIgnoreCase( "A" ) && ! htmlName.equalsIgnoreCase( "TD" ) ) ) state.afterElement = true; state.empty = false; if ( isDocumentState() ) _printer.flush(); } //------------------------------------------// // SAX document handler serializing methods // //------------------------------------------// public void characters( char[] chars, int start, int length ) throws SAXException { ElementState state; try { // HTML: no CDATA section state = content(); state.doCData = false; super.characters( chars, start, length ); } catch ( IOException except ) { throw new SAXException( except ); } } public void startElement( String tagName, AttributeList attrs ) throws SAXException { int i; boolean preserveSpace; ElementState state; String name; String value; 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( tagName ); } 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(). // XHTML: element names are lower case, DOM will be different _printer.printText( '<' ); if ( _xhtml ) _printer.printText( tagName.toLowerCase() ); else _printer.printText( tagName ); _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.getName( i ).toLowerCase();; value = attrs.getValue( i ); if ( _xhtml ) { // XHTML: print empty string for null values. if ( value == null ) { _printer.printText( name ); _printer.printText( "=\"\"" ); } else { _printer.printText( name ); _printer.printText( "=\"" ); printEscaped( value ); _printer.printText( '"' ); } } else { // HTML: Empty values print as attribute name, no value. // HTML: URI attributes will print unescaped if ( value == null || value.length() == 0 ) _printer.printText( name ); else if ( HTMLdtd.isURI( tagName, name ) ) { _printer.printText( name ); _printer.printText( "=\"" ); _printer.printText( escapeURI( value ) ); _printer.printText( '"' ); } else if ( HTMLdtd.isBoolean( tagName, name ) ) _printer.printText( name ); else { _printer.printText( name ); _printer.printText( "=\"" ); printEscaped( value ); _printer.printText( '"' ); } } } } if ( HTMLdtd.isPreserveSpace( tagName ) ) preserveSpace = true; // Now it's time to enter a new element state // with the tag name and space preserving. // We still do not change the curent element state. state = enterElementState( null, null, tagName, preserveSpace ); // Prevents line breaks inside A/TD if ( tagName.equalsIgnoreCase( "A" ) || tagName.equalsIgnoreCase( "TD" ) ) { state.empty = false; _printer.printText( '>' ); } // Handle SCRIPT and STYLE specifically by changing the // state of the current element to CDATA (XHTML) or // unescaped (HTML). if ( tagName.equalsIgnoreCase( "SCRIPT" ) || tagName.equalsIgnoreCase( "STYLE" ) ) { if ( _xhtml ) { // XHTML: Print contents as CDATA section state.doCData = true; } else { // HTML: Print contents unescaped state.unescaped = true; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -