📄 basemarkupserializer.java
字号:
public void startPrefixMapping( String prefix, String uri ) throws SAXException { if ( _prefixes == null ) _prefixes = new Hashtable(); _prefixes.put( uri, prefix == null ? "" : prefix ); } public void endPrefixMapping( String prefix ) throws SAXException { } //------------------------------------------// // SAX DTD/Decl handler serializing methods // //------------------------------------------// public final void startDTD( String name, String publicId, String systemId ) throws SAXException { try { _printer.enterDTD(); _docTypePublicId = publicId; _docTypeSystemId = systemId; } catch ( IOException except ) { throw new SAXException( except ); } } public void endDTD() { // Nothing to do here, all the magic occurs in startDocument(String). } public void elementDecl( String name, String model ) throws SAXException { try { _printer.enterDTD(); _printer.printText( "<!ELEMENT " ); _printer.printText( name ); _printer.printText( ' ' ); _printer.printText( model ); _printer.printText( '>' ); if ( _indenting ) _printer.breakLine(); } catch ( IOException except ) { throw new SAXException( except ); } } public void attributeDecl( String eName, String aName, String type, String valueDefault, String value ) throws SAXException { try { _printer.enterDTD(); _printer.printText( "<!ATTLIST " ); _printer.printText( eName ); _printer.printText( ' ' ); _printer.printText( aName ); _printer.printText( ' ' ); _printer.printText( type ); if ( valueDefault != null ) { _printer.printText( ' ' ); _printer.printText( valueDefault ); } if ( value != null ) { _printer.printText( " \"" ); printEscaped( value ); _printer.printText( '"' ); } _printer.printText( '>' ); if ( _indenting ) _printer.breakLine(); } catch ( IOException except ) { throw new SAXException( except ); } } public void internalEntityDecl( String name, String value ) throws SAXException { try { _printer.enterDTD(); _printer.printText( "<!ENTITY " ); _printer.printText( name ); _printer.printText( " \"" ); printEscaped( value ); _printer.printText( "\">" ); if ( _indenting ) _printer.breakLine(); } catch ( IOException except ) { throw new SAXException( except ); } } public void externalEntityDecl( String name, String publicId, String systemId ) throws SAXException { try { _printer.enterDTD(); unparsedEntityDecl( name, publicId, systemId, null ); } catch ( IOException except ) { throw new SAXException( except ); } } public void unparsedEntityDecl( String name, String publicId, String systemId, String notationName ) throws SAXException { try { _printer.enterDTD(); if ( publicId == null ) { _printer.printText( "<!ENTITY " ); _printer.printText( name ); _printer.printText( " SYSTEM " ); printDoctypeURL( systemId ); } else { _printer.printText( "<!ENTITY " ); _printer.printText( name ); _printer.printText( " PUBLIC " ); printDoctypeURL( publicId ); _printer.printText( ' ' ); printDoctypeURL( systemId ); } if ( notationName != null ) { _printer.printText( " NDATA " ); _printer.printText( notationName ); } _printer.printText( '>' ); if ( _indenting ) _printer.breakLine(); } catch ( IOException except ) { throw new SAXException( except ); } } public void notationDecl( String name, String publicId, String systemId ) throws SAXException { try { _printer.enterDTD(); if ( publicId != null ) { _printer.printText( "<!NOTATION " ); _printer.printText( name ); _printer.printText( " PUBLIC " ); printDoctypeURL( publicId ); if ( systemId != null ) { _printer.printText( ' ' ); printDoctypeURL( systemId ); } } else { _printer.printText( "<!NOTATION " ); _printer.printText( name ); _printer.printText( " SYSTEM " ); printDoctypeURL( systemId ); } _printer.printText( '>' ); if ( _indenting ) _printer.breakLine(); } catch ( IOException except ) { throw new SAXException( except ); } } //------------------------------------------// // Generic node serializing methods methods // //------------------------------------------// /** * Serialize the DOM node. This method is shared across XML, HTML and XHTML * serializers and the differences are masked out in a separate {@link * #serializeElement}. * * @param node The node to serialize * @see #serializeElement * @throws IOException An I/O exception occured while * serializing */ protected void serializeNode( Node node ) throws IOException { // Based on the node type call the suitable SAX handler. // Only comments entities and documents which are not // handled by SAX are serialized directly. switch ( node.getNodeType() ) { case Node.TEXT_NODE : { String text; text = node.getNodeValue(); if ( text != null ) if ( !_indenting || getElementState().preserveSpace || (text.replace('\n',' ').trim().length() != 0)) characters( text ); break; } case Node.CDATA_SECTION_NODE : { String text; text = node.getNodeValue(); if ( text != null ) { startCDATA(); characters( text ); endCDATA(); } break; } case Node.COMMENT_NODE : { String text; if ( ! _format.getOmitComments() ) { text = node.getNodeValue(); if ( text != null ) comment( text ); } break; } case Node.ENTITY_REFERENCE_NODE : { Node child; endCDATA(); content(); child = node.getFirstChild(); while ( child != null ) { serializeNode( child ); child = child.getNextSibling(); } break; } case Node.PROCESSING_INSTRUCTION_NODE : processingInstructionIO( node.getNodeName(), node.getNodeValue() ); break; case Node.ELEMENT_NODE : 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 * @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 ) { StringBuffer buffer;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -