📄 htmlserializer.java
字号:
} } catch ( IOException except ) { throw new SAXException( except ); } } public void endElement( String tagName ) throws SAXException { endElement( null, null, tagName ); } //------------------------------------------// // Generic node serializing methods methods // //------------------------------------------// /** * Called to serialize the document's DOCTYPE by the root element. * The document type declaration must name the root element, * but the root element is only known when that element is serialized, * and not at the start of the document. * <p> * This method will check if it has not been called before ({@link #_started}), * will serialize the document type declaration, and will serialize all * pre-root comments and PIs that were accumulated in the document * (see {@link #serializePreRoot()}). Pre-root will be serialized even if * this is not the first root element of the document. */ protected void startDocument( String rootTagName ) throws IOException { StringBuffer buffer; // Not supported in HTML/XHTML, but we still have to switch // out of DTD mode. _printer.leaveDTD(); if ( ! _started ) { // If the public and system identifiers were not specified // in the output format, use the appropriate ones for HTML // or XHTML. if ( _docTypePublicId == null && _docTypeSystemId == null ) { if ( _xhtml ) { _docTypePublicId = HTMLdtd.XHTMLPublicId; _docTypeSystemId = HTMLdtd.XHTMLSystemId; } else { _docTypePublicId = HTMLdtd.HTMLPublicId; _docTypeSystemId = HTMLdtd.HTMLSystemId; } } if ( ! _format.getOmitDocumentType() ) { // XHTML: If public idnentifier and system identifier // specified, print them, else print just system identifier // HTML: If public identifier specified, print it with // system identifier, if specified. if ( _docTypePublicId != null && ( ! _xhtml || _docTypeSystemId != null ) ) { _printer.printText( "<!DOCTYPE HTML PUBLIC " ); printDoctypeURL( _docTypePublicId ); if ( _docTypeSystemId != null ) { if ( _indenting ) { _printer.breakLine(); _printer.printText( " " ); } else _printer.printText( ' ' ); printDoctypeURL( _docTypeSystemId ); } _printer.printText( '>' ); _printer.breakLine(); } else if ( _docTypeSystemId != null ) { _printer.printText( "<!DOCTYPE HTML SYSTEM " ); printDoctypeURL( _docTypeSystemId ); _printer.printText( '>' ); _printer.breakLine(); } } } _started = true; // Always serialize these, even if not te first root element. serializePreRoot(); } /** * Called to serialize a DOM element. Equivalent to calling {@link * #startElement(String, String, String, Attributes)}, {@link #endElement(String, String, String)} and serializing everything * inbetween, but better optimized. */ protected void serializeElement( Element elem ) throws IOException { Attr attr; NamedNodeMap attrMap; int i; Node child; ElementState state; boolean preserveSpace; String name; String value; String tagName; tagName = elem.getTagName(); 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(); // Lookup the element's attribute, but only print specified // attributes. (Unspecified attributes are derived from the DTD. // For each attribute print it's name and value as one part, // separated with a space so the element can be broken on // multiple lines. attrMap = elem.getAttributes(); if ( attrMap != null ) { for ( i = 0 ; i < attrMap.getLength() ; ++i ) { attr = (Attr) attrMap.item( i ); name = attr.getName().toLowerCase(); value = attr.getValue(); if ( attr.getSpecified() ) { _printer.printSpace(); 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; // If element has children, or if element is not an empty tag, // serialize an opening tag. if ( elem.hasChildNodes() || ! HTMLdtd.isEmptyTag( tagName ) ) { // Enter an element state, and serialize the children // one by one. Finally, end the element. 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; } } child = elem.getFirstChild(); while ( child != null ) { serializeNode( child ); child = child.getNextSibling(); } endElementIO( null, null, tagName ); } else { _printer.unindent(); // XHTML: Close empty tag with ' />' so it's XML and HTML compatible. // HTML: Empty tags are defined as such in DTD no in document. if ( _xhtml ) _printer.printText( " />" ); else _printer.printText( '>' ); // After element but parent element is no longer empty. state.afterElement = true; state.empty = false; if ( isDocumentState() ) _printer.flush(); } } protected void characters( String text ) throws IOException { ElementState state; // HTML: no CDATA section state = content(); super.characters( text ); } protected String getEntityRef( int ch ) { return HTMLdtd.fromChar( ch ); } protected String escapeURI( String uri ) { int index; // XXX Apparently Netscape doesn't like if we escape the URI // using %nn, so we leave it as is, just remove any quotes. index = uri.indexOf( "\"" ); if ( index >= 0 ) return uri.substring( 0, index ); else return uri; } public void startAnchoring(String anchorId) { this.anchorId = anchorId; } public void stopAnchoring() { this.anchorId = null; } protected String appendAnchorIfNecessary(String elementName, String attributeName, String attributeValue) { if (anchorId != null) { // looking for an <a> or <form> tag element if (elementName.equalsIgnoreCase("a") || elementName.equalsIgnoreCase("form")) { // found an <a> or <form>, let's peek at the attributes it contains // does it contain either an "href" or "action" attribute if (attributeName.equalsIgnoreCase("href") || attributeName.equalsIgnoreCase("action")) { // found the attribute, now lets make sure it points back to a channel if (attributeValue.indexOf(".render.") != -1 && attributeValue.indexOf("javascript:") == -1) { // this link points back to a channel, so let's // rewrite it and place back into the Attribute Object attributeValue += "#" + anchorId; } } } } return attributeValue; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -