transformerimpl.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 640 行 · 第 1/2 页
JAVA
640 行
} } else if (result instanceof DOMResult) { DOMResult domResult = (DOMResult) result; Node resultNode = domResult.getNode(); domResult.setNode(transform(node, resultNode)); } else if (result instanceof SAXResult) { SAXResult sax = (SAXResult) result; transform(node, sax.getHandler(), sax.getLexicalHandler()); } else throw new TransformerException(String.valueOf(result)); } catch (TransformerException e) { throw e; } catch (Exception e) { throw new TransformerExceptionWrapper(e); } } public void transform(Node node, OutputStream os) throws TransformerException { if (os instanceof WriteStream) { String encoding = ((WriteStream) os).getEncoding(); if (encoding == null) encoding = "ISO-8859-1"; transform(node, os, encoding, null); } else transform(node, os, null, null); } /** * Transforms from a DOM node to an output stream. * * @param node the source node * @param os the destination stream */ public void transform(Node node, OutputStream os, String encoding, String systemId) throws TransformerException { if (node == null) throw new IllegalArgumentException("can't transform null node"); try { _lineMap = null; Properties output = getOutputProperties(); WriteStream ws; if (os instanceof WriteStream) ws = (WriteStream) os; else { ws = Vfs.openWrite(os); if (systemId != null) ws.setPath(Vfs.lookup(systemId)); else if (node instanceof QNode) { String baseURI = ((QNode) node).getBaseURI(); if (baseURI != null) ws.setPath(Vfs.lookup(baseURI)); } } XmlPrinter out = new XmlPrinter(ws); String method = (String) output.get(OutputKeys.METHOD); out.setMethod(method); if (encoding == null) encoding = (String) output.get(OutputKeys.ENCODING); if (encoding == null && ! (os instanceof WriteStream) && ! "html".equals(method)) encoding = "UTF-8"; if (encoding != null) out.setEncoding(encoding); out.setMimeType((String) output.get(OutputKeys.MEDIA_TYPE)); String omit = (String) output.get(OutputKeys.OMIT_XML_DECLARATION); if (omit == null || omit.equals("false") || omit.equals("no")) out.setPrintDeclaration(true); out.setStandalone((String) output.get(OutputKeys.STANDALONE)); out.setSystemId((String) output.get(OutputKeys.DOCTYPE_SYSTEM)); out.setPublicId((String) output.get(OutputKeys.DOCTYPE_PUBLIC)); String indent = (String) output.get(OutputKeys.INDENT); if (indent != null) out.setPretty(indent.equals("true") || indent.equals("yes")); String jsp = (String) output.get("caucho.jsp"); if (jsp != null) out.setJSP(jsp.equals("true") || jsp.equals("yes")); out.setVersion((String) output.get(OutputKeys.VERSION)); String includeContentType = (String) output.get("include-content-type"); if (includeContentType != null) out.setIncludeContentType(includeContentType.equals("true") || includeContentType.equals("yes")); if (! _stylesheet.getGenerateLocation()) { } else if (node instanceof CauchoNode) { String filename = ((CauchoNode) node).getFilename(); if (filename != null) out.setLineMap(filename); else out.setLineMap("anonymous.xsl"); } else out.setLineMap("anonymous.xsl"); //out.beginDocument(); _stylesheet.transform(node, out, this); //out.endDocument(); _lineMap = out.getLineMap(); if (os != ws) { ws.flush(); ws.free(); } } catch (TransformerException e) { throw e; } catch (Exception e) { throw new TransformerExceptionWrapper(e); } } /** * Transforms from the source node to the destination node, returning * the destination node. */ public Node transform(Node sourceNode, Node destNode) throws SAXException, IOException { _lineMap = null; if (destNode == null) destNode = Xml.createDocument(); DOMBuilder out = new DOMBuilder(); out.init(destNode); try { out.startDocument(); _stylesheet.transform(sourceNode, out, this); //out.endDocument(); } catch (Exception e) { throw new IOExceptionWrapper(e); } return destNode; } /** * Transforms from the source node to the sax handlers. */ public void transform(Node sourceNode, ContentHandler contentHandler, LexicalHandler lexicalHandler) throws SAXException, IOException, TransformerException { if (contentHandler == null) throw new IllegalArgumentException(L.l("null content handler")); _lineMap = null; SAXBuilder out = new SAXBuilder(); out.setContentHandler(contentHandler); out.startDocument(); _stylesheet.transform(sourceNode, out, this); //out.endDocument(); } /** * Parses the source XML document from the source. * * @param source the JAXP source. * * @return the parsed document. */ protected Node parseDocument(Source source) throws IOException, SAXException, TransformerException { if (source instanceof StreamSource) { StreamSource stream = (StreamSource) source; InputSource in = new InputSource(); in.setSystemId(stream.getSystemId()); in.setByteStream(stream.getInputStream()); in.setCharacterStream(stream.getReader()); XmlParser parser = Xml.create(); Node node = parser.parseDocument(in); parser.free(); return node; // return new QDocument(); } else if (source instanceof DOMSource){ Node node = ((DOMSource) source).getNode(); return node != null ? node : new QDocument(); } else if (source instanceof StringSource) { String string = ((StringSource) source).getString(); if (string != null) return parseStringDocument(string, source.getSystemId()); else return new QDocument(); } else if (source instanceof SAXSource) { SAXSource saxSource = (SAXSource) source; XMLReader reader = saxSource.getXMLReader(); if (reader == null) return new QDocument(); InputSource inputSource = saxSource.getInputSource(); Document doc = new QDocument(); DOMBuilder builder = new DOMBuilder(); builder.init(doc); reader.setContentHandler(builder); reader.parse(inputSource); return doc; } else throw new TransformerException(L.l("unknown source {0}", source)); } /** * Parses the source XML document from the input stream. * * @param is the source input stream. * @param systemId the path of the source * * @return document DOM node for the parsed XML. */ protected Node parseDocument(InputStream is, String systemId) throws IOException, SAXException { XmlParser parser = Xml.create(); Node node = parser.parseDocument(is); parser.free(); return node; } /** * Parses the source document specified by a URL * * @param url path to the document to be parsed. * * @return the parsed document. */ protected Node parseDocument(String url) throws IOException, SAXException { XmlParser parser = Xml.create(); Node node = parser.parseDocument(url); parser.free(); return node; } /** * Parses a string as an XML document. * * @param source the string to use as the XML source * @param systemId the URL for the string document. * * @return the parsed document. */ protected Node parseStringDocument(String source, String systemId) throws IOException, SAXException { XmlParser parser = Xml.create(); Node node = parser.parseDocumentString(source); parser.free(); return node; } public void addCacheDepend(Path path) { _cacheDepends.add(path); } protected void addCacheDepend(String path) { _cacheDepends.add(Vfs.lookup(path)); } public ArrayList<Path> getCacheDepends() { return _cacheDepends; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?