domserializerimpl.java
来自「JAVA 所有包」· Java 代码 · 共 1,212 行 · 第 1/4 页
JAVA
1,212 行
*/ public String getNewLine() { return serializer._format.getLineSeparator(); } /** * When the application provides a filter, the serializer will call out * to the filter before serializing each Node. Attribute nodes are never * passed to the filter. The filter implementation can choose to remove * the node from the stream or to terminate the serialization early. */ public LSSerializerFilter getFilter(){ return serializer.fDOMFilter; } /** * When the application provides a filter, the serializer will call out * to the filter before serializing each Node. Attribute nodes are never * passed to the filter. The filter implementation can choose to remove * the node from the stream or to terminate the serialization early. */ public void setFilter(LSSerializerFilter filter){ serializer.fDOMFilter = filter; } // this initializes a newly-created serializer private void initSerializer(XMLSerializer ser) { ser.fNSBinder = new NamespaceSupport(); ser.fLocalNSBinder = new NamespaceSupport(); ser.fSymbolTable = new SymbolTable(); } // copies all settings that could have been modified // by calls to LSSerializer methods from one serializer to another. // IMPORTANT: if new methods are implemented or more settings of // the serializer are made alterable, this must be // reflected in this method! private void copySettings(XMLSerializer src, XMLSerializer dest) { dest.fDOMErrorHandler = fErrorHandler; dest._format.setEncoding(src._format.getEncoding()); dest._format.setLineSeparator(src._format.getLineSeparator()); dest.fDOMFilter = src.fDOMFilter; }//copysettings /** * Serialize the specified node as described above in the general * description of the <code>LSSerializer</code> interface. The output * is written to the supplied <code>LSOutput</code>. * <br> When writing to a <code>LSOutput</code>, the encoding is found by * looking at the encoding information that is reachable through the * <code>LSOutput</code> and the item to be written (or its owner * document) in this order: * <ol> * <li> <code>LSOutput.encoding</code>, * </li> * <li> * <code>Document.actualEncoding</code>, * </li> * <li> * <code>Document.xmlEncoding</code>. * </li> * </ol> * <br> If no encoding is reachable through the above properties, a * default encoding of "UTF-8" will be used. * <br> If the specified encoding is not supported an * "unsupported-encoding" error is raised. * <br> If no output is specified in the <code>LSOutput</code>, a * "no-output-specified" error is raised. * @param node The node to serialize. * @param destination The destination for the serialized DOM. * @return Returns <code>true</code> if <code>node</code> was * successfully serialized and <code>false</code> in case the node * couldn't be serialized. */ public boolean write(Node node, LSOutput destination) throws LSException{ if (node == null) return false; Method getVersion = null; XMLSerializer ser = null; String ver = null; Document fDocument =(node.getNodeType() == Node.DOCUMENT_NODE) ? (Document) node : node.getOwnerDocument(); // this should run under JDK 1.1.8... try { getVersion = fDocument.getClass().getMethod("getXmlVersion", new Class[] {}); if (getVersion != null) { ver = (String) getVersion.invoke(fDocument, (Object[]) null); } } catch (Exception e) { //no way to test the version... //ignore the exception } //determine which serializer to use: if (ver != null && ver.equals("1.1")) { if (xml11Serializer == null) { xml11Serializer = new XML11Serializer(); initSerializer(xml11Serializer); } //copy setting from "main" serializer to XML 1.1 serializer copySettings(serializer, xml11Serializer); ser = xml11Serializer; } else { ser = serializer; } String encoding = null; if ((encoding = destination.getEncoding()) == null) { try { Method getEncoding = fDocument.getClass().getMethod("getInputEncoding", new Class[] {}); if (getEncoding != null) { encoding = (String) getEncoding.invoke(fDocument, (Object[]) null); } } catch (Exception e) { // ignore the exception } if (encoding == null) { try { Method getEncoding = fDocument.getClass().getMethod("getXmlEncoding", new Class[] {}); if (getEncoding != null) { encoding = (String) getEncoding.invoke(fDocument, (Object[]) null); } } catch (Exception e) { // ignore the exception } if (encoding == null) { encoding = "UTF-8"; } } } try { prepareForSerialization(ser, node); ser._format.setEncoding(encoding); OutputStream outputStream = destination.getByteStream(); Writer writer = destination.getCharacterStream(); String uri = destination.getSystemId(); if (writer == null) { if (outputStream == null) { if (uri == null) { String msg = DOMMessageFormatter.formatMessage( DOMMessageFormatter.SERIALIZER_DOMAIN, "no-output-specified", null); if (ser.fDOMErrorHandler != null) { DOMErrorImpl error = new DOMErrorImpl(); error.fType = "no-output-specified"; error.fMessage = msg; error.fSeverity = DOMError.SEVERITY_FATAL_ERROR; ser.fDOMErrorHandler.handleError(error); } throw new LSException(LSException.SERIALIZE_ERR, msg); } else { // URI was specified. Handle relative URIs. String expanded = XMLEntityManager.expandSystemId(uri, null, true); URL url = new URL(expanded != null ? expanded : uri); OutputStream out = null; String protocol = url.getProtocol(); String host = url.getHost(); // Use FileOutputStream if this URI is for a local file. if (protocol.equals("file") && (host == null || host.length() == 0 || host.equals("localhost"))) { out = new FileOutputStream(getPathWithoutEscapes(url.getFile())); } // Try to write to some other kind of URI. Some protocols // won't support this, though HTTP should work. else { URLConnection urlCon = url.openConnection(); urlCon.setDoInput(false); urlCon.setDoOutput(true); urlCon.setUseCaches(false); // Enable tunneling. if (urlCon instanceof HttpURLConnection) { // The DOM L3 LS CR says if we are writing to an HTTP URI // it is to be done with an HTTP PUT. HttpURLConnection httpCon = (HttpURLConnection) urlCon; httpCon.setRequestMethod("PUT"); } out = urlCon.getOutputStream(); } ser.setOutputByteStream(out); } } else { // byte stream was specified ser.setOutputByteStream(outputStream); } } else { // character stream is specified ser.setOutputCharStream(writer); } if (node.getNodeType() == Node.DOCUMENT_NODE) ser.serialize((Document) node); else if (node.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE) ser.serialize((DocumentFragment) node); else if (node.getNodeType() == Node.ELEMENT_NODE) ser.serialize((Element) node); else if (node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.COMMENT_NODE || node.getNodeType() == Node.ENTITY_REFERENCE_NODE || node.getNodeType() == Node.CDATA_SECTION_NODE || node.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE ) { ser.serialize(node); } else return false; } catch( UnsupportedEncodingException ue) { if (ser.fDOMErrorHandler != null) { DOMErrorImpl error = new DOMErrorImpl(); error.fException = ue; error.fType = "unsupported-encoding"; error.fMessage = ue.getMessage(); error.fSeverity = DOMError.SEVERITY_FATAL_ERROR; ser.fDOMErrorHandler.handleError(error); } throw new LSException(LSException.SERIALIZE_ERR, DOMMessageFormatter.formatMessage( DOMMessageFormatter.SERIALIZER_DOMAIN, "unsupported-encoding", null)); //return false; } catch (LSException lse) { // Rethrow LSException. throw lse; } catch (RuntimeException e) { if (e == DOMNormalizer.abort){ // stopped at user request return false; } throw new LSException(LSException.SERIALIZE_ERR, e.toString()); } catch (Exception e) { if (ser.fDOMErrorHandler != null) { DOMErrorImpl error = new DOMErrorImpl(); error.fException = e; error.fMessage = e.getMessage(); error.fSeverity = DOMError.SEVERITY_ERROR; ser.fDOMErrorHandler.handleError(error); } e.printStackTrace(); throw new LSException(LSException.SERIALIZE_ERR, e.toString()); } return true; } //write /** * Serialize the specified node as described above in the general * description of the <code>LSSerializer</code> interface. The output * is written to the supplied URI. * <br> When writing to a URI, the encoding is found by looking at the * encoding information that is reachable through the item to be written * (or its owner document) in this order: * <ol> * <li> * <code>Document.inputEncoding</code>, * </li> * <li> * <code>Document.xmlEncoding</code>. * </li> * </ol> * <br> If no encoding is reachable through the above properties, a * default encoding of "UTF-8" will be used. * <br> If the specified encoding is not supported an * "unsupported-encoding" error is raised. * @param node The node to serialize. * @param URI The URI to write to. * @return Returns <code>true</code> if <code>node</code> was * successfully serialized and <code>false</code> in case the node * couldn't be serialized. */ public boolean writeToURI(Node node, String URI) throws LSException{ if (node == null){ return false; } Method getXmlVersion = null; XMLSerializer ser = null; String ver = null; String encoding = null; Document fDocument =(node.getNodeType() == Node.DOCUMENT_NODE) ? (Document) node : node.getOwnerDocument(); // this should run under JDK 1.1.8... try { getXmlVersion = fDocument.getClass().getMethod("getXmlVersion", new Class[] {}); if (getXmlVersion != null) { ver = (String) getXmlVersion.invoke(fDocument, (Object[]) null); } } catch (Exception e) { // no way to test the version... // ignore the exception } if (ver != null && ver.equals("1.1")) { if (xml11Serializer == null) { xml11Serializer = new XML11Serializer(); initSerializer(xml11Serializer); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?