transformerimpl.java

来自「Mac OS X 10.4.9 for x86 Source Code gcc」· Java 代码 · 共 581 行 · 第 1/2 页

JAVA
581
字号
                parent.appendChild(doctype);              }          }      }    if (version != null)      {        parent.setUserData("version", version, stylesheet);      }    if (omitXmlDeclaration)      {        parent.setUserData("omit-xml-declaration", "yes", stylesheet);      }    if (standalone)      {        parent.setUserData("standalone", "yes", stylesheet);      }    if (mediaType != null)      {        parent.setUserData("media-type", mediaType, stylesheet);      }    // Render result to the target device    if (outputTarget instanceof DOMResult)      {        if (created)          {            DOMResult dr = (DOMResult) outputTarget;            dr.setNode(parent);            dr.setNextSibling(null);          }      }    else if (outputTarget instanceof StreamResult)      {        StreamResult sr = (StreamResult) outputTarget;        IOException ex = null;        try          {            writeStreamResult(parent, sr, outputMethod, encoding);          }        catch (UnsupportedEncodingException e)          {            try              {                writeStreamResult(parent, sr, outputMethod, "UTF-8");              }            catch (IOException e2)              {                ex = e2;              }          }        catch (IOException e)          {            ex = e;          }        if (ex != null)          {            if (errorListener != null)              {                errorListener.error(new TransformerException(ex));              }            else              {                ex.printStackTrace(System.err);              }          }      }    else if (outputTarget instanceof SAXResult)      {        SAXResult sr = (SAXResult) outputTarget;        try          {            ContentHandler ch = sr.getHandler();            LexicalHandler lh = sr.getLexicalHandler();            if (lh == null && ch instanceof LexicalHandler)              {                lh = (LexicalHandler) ch;              }            SAXSerializer serializer = new SAXSerializer();            serializer.serialize(parent, ch, lh);          }        catch (SAXException e)          {            if (errorListener != null)              {                errorListener.error(new TransformerException(e));              }            else              {                e.printStackTrace(System.err);              }          }      }  }  /**   * Strip whitespace from the source tree.   */  void strip(Node node)    throws TransformerConfigurationException  {    short nt = node.getNodeType();    if (nt == Node.ENTITY_REFERENCE_NODE)      {        // Replace entity reference with its content        Node parent = node.getParentNode();        Node child = node.getFirstChild();        if (child != null)          {            strip(child);          }        while (child != null)          {            Node next = child.getNextSibling();            node.removeChild(child);            parent.insertBefore(child, node);            child = next;          }        parent.removeChild(node);      }    if (nt == Node.TEXT_NODE) // CDATA sections ?      {        if (!stylesheet.isPreserved((Text) node))          {            node.getParentNode().removeChild(node);          }      }    else      {        for (Node child = node.getFirstChild(); child != null;             child = child.getNextSibling())          {            strip(child);          }      }  }  /**   * Obtain a suitable output stream for writing the result to,   * and use the StreamSerializer to write the result tree to the stream.   */  void writeStreamResult(Node node, StreamResult sr, int outputMethod,                         String encoding)    throws IOException  {    OutputStream out = null;    try      {        out = sr.getOutputStream();        if (out == null)          {            Writer writer = sr.getWriter();            if (writer != null)              {                out = new WriterOutputStream(writer);              }          }        if (out == null)          {            String systemId = sr.getSystemId();            try              {                URL url = new URL(systemId);                URLConnection connection = url.openConnection();                connection.setDoOutput(true);                out = connection.getOutputStream();              }            catch (MalformedURLException e)              {                out = new FileOutputStream(systemId);              }            catch (UnknownServiceException e)              {                URL url = new URL(systemId);                out = new FileOutputStream(url.getPath());              }          }        out = new BufferedOutputStream(out);        StreamSerializer serializer =          new StreamSerializer(outputMethod, encoding, null);        if (stylesheet != null)          {            Collection celem = stylesheet.outputCdataSectionElements;            serializer.setCdataSectionElements(celem);          }        serializer.serialize(node, out);        out.flush();      }    finally      {        try          {            if (out != null)              {                out.close();              }          }        catch (IOException e)          {          }      }  }  void copyChildren(Document dstDoc, Node src, Node dst)  {    Node srcChild = src.getFirstChild();    while (srcChild != null)      {        Node dstChild = dstDoc.adoptNode(srcChild);        dst.appendChild(dstChild);        srcChild = srcChild.getNextSibling();      }  }  public void setParameter(String name, Object value)  {    if (stylesheet != null)      {        stylesheet.bindings.set(name, value, false);      }  }  public Object getParameter(String name)  {    if (stylesheet != null)      {        return stylesheet.bindings.get(name, null, 1, 1);      }    return null;  }  public void clearParameters()  {    if (stylesheet != null)      {        stylesheet.bindings.pop(false);        stylesheet.bindings.push(false);      }  }  public void setURIResolver(URIResolver resolver)  {    uriResolver = resolver;  }  public URIResolver getURIResolver()  {    return uriResolver;  }  public void setOutputProperties(Properties oformat)    throws IllegalArgumentException  {    if (oformat == null)      {        outputProperties.clear();      }    else      {        outputProperties.putAll(oformat);      }  }  public Properties getOutputProperties()  {    return (Properties) outputProperties.clone();  }  public void setOutputProperty(String name, String value)    throws IllegalArgumentException  {    outputProperties.put(name, value);  }  public String getOutputProperty(String name)    throws IllegalArgumentException  {    return outputProperties.getProperty(name);  }  public void setErrorListener(ErrorListener listener)  {    errorListener = listener;  }  public ErrorListener getErrorListener()  {    return errorListener;  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?