basemarkupserializer.java
来自「JAVA 所有包」· Java 代码 · 共 1,946 行 · 第 1/5 页
JAVA
1,946 行
(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; serializeDocument(); // 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", (Class[]) null); if (getPublicId.getReturnType().equals(String.class)) { docTypePublicId = (String)getPublicId.invoke(docType, (Object[]) null); } } catch (Exception e) { // ignore } try { java.lang.reflect.Method getSystemId = docTypeClass.getMethod("getSystemId", (Class[]) null); if (getSystemId.getReturnType().equals(String.class)) { docTypeSystemId = (String)getSystemId.invoke(docType, (Object[]) null); } } catch (Exception e) { // ignore } _printer.enterDTD(); _docTypePublicId = docTypePublicId; _docTypeSystemId = docTypeSystemId; endDTD(); } serializeDTD(docType.getName()); } _started = true; // !! 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; } } /* Serializes XML Declaration, according to 'xml-declaration' property. */ protected void serializeDocument()throws IOException { int i; String dtd = _printer.leaveDTD(); if (! _started) { if (! _format.getOmitXMLDeclaration()) { StringBuffer buffer; // Serialize the document declaration appreaing at the head // of very XML document (unless asked not to). buffer = new StringBuffer( "<?xml version=\"" ); if (_format.getVersion() != null) buffer.append( _format.getVersion() ); else buffer.append( "1.0" ); buffer.append( '"' ); String format_encoding = _format.getEncoding(); if (format_encoding != null) { buffer.append( " encoding=\"" ); buffer.append( format_encoding ); buffer.append( '"' ); } if (_format.getStandalone() && _docTypeSystemId == null && _docTypePublicId == null) buffer.append( " standalone=\"yes\"" ); buffer.append( "?>" ); _printer.printText( buffer ); _printer.breakLine(); } } // Always serialize these, even if not te first root element. serializePreRoot(); } /* Serializes DTD, if present. */ protected void serializeDTD(String name) throws IOException{ String dtd = _printer.leaveDTD(); if (! _format.getOmitDocumentType()) { if (_docTypeSystemId != null) { // System identifier must be specified to print DOCTYPE. // If public identifier is specified print 'PUBLIC // <public> <system>', if not, print 'SYSTEM <system>'. _printer.printText( "<!DOCTYPE " ); _printer.printText( name ); if (_docTypePublicId != null) { _printer.printText( " PUBLIC " ); printDoctypeURL( _docTypePublicId ); if (_indenting) { _printer.breakLine(); for (int i = 0 ; i < 18 + name.length() ; ++i) _printer.printText( " " ); } else _printer.printText( " " ); printDoctypeURL( _docTypeSystemId ); } else { _printer.printText( " SYSTEM " ); printDoctypeURL( _docTypeSystemId ); } // If we accumulated any DTD contents while printing. // this would be the place to print it. if (dtd != null && dtd.length() > 0) { _printer.printText( " [" ); printText( dtd, true, true ); _printer.printText( ']' ); } _printer.printText( ">" ); _printer.breakLine(); } else if (dtd != null && dtd.length() > 0) { _printer.printText( "<!DOCTYPE " ); _printer.printText( name ); _printer.printText( " [" ); printText( dtd, true, true ); _printer.printText( "]>" ); _printer.breakLine(); } } } /** * 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>'&'</tt> * will return <tt>"&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) { String msg = DOMMessageFormatter.formatMessage( DOMMessageFormatter.SERIALIZER_DOMAIN, "EndingCDATA", null); if ((features & DOMSerializerImpl.WELLFORMED) != 0) { // issue fatal error modifyDOMError(msg, DOMError.SEVERITY_FATAL_ERROR, "wf-invalid-character", fCurrentNode); fDOMErrorHandler.handleError(fDOMError); throw new LSException(LSException.SERIALIZE_ERR, msg); } else { // issue error modifyDOMError(msg, DOMError.SEVERITY_ERROR, "cdata-section-not-splitted", fCurrentNode); if (!fDOMErrorHandler.handleError(fDOMError)) { throw new LSException(LSException.SERIALIZE_ERR, msg); } } } else { // issue warning String msg = DOMMessageFormatter.formatMessage( DOMMessageFormatter.SERIALIZER_DOMAIN, "SplittingCDATA", null); modifyDOMError( msg, DOMError.SEVERITY_WARNING, null, fCurrentNode);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?