abstractstylesheetfactory.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,258 行 · 第 1/3 页
JAVA
1,258 行
parser.setContentHandler(handler); ReadStream rs = openPath(source); try { parser.parse(rs); } finally { rs.close(); } } else if (source instanceof DOMSource) { DOMSource domSource = (DOMSource) source; Node node = domSource.getNode(); XmlUtil.toSAX(node, handler); } } catch (Exception e) { throw new TransformerConfigurationException(e); } } /** * Create a transformer from an input stream. * * @param source the source stream * * @return the compiled stylesheet */ public javax.xml.transform.Transformer newTransformer(Source source) throws TransformerConfigurationException { Templates templates = newTemplates(source); return templates.newTransformer(); } /** * Create an identity transformer. * * @return the compiled stylesheet */ public javax.xml.transform.Transformer newTransformer() throws TransformerConfigurationException { return new TransformerImpl(new IdentityStylesheet()); } /** * Creates a new stylesheet from an XML document. */ public StylesheetImpl newStylesheet(Document xsl) throws Exception { return (StylesheetImpl) generate(xsl, null); } /** * Create a new stylesheet from a reader. */ public StylesheetImpl newStylesheet(Reader reader) throws Exception { ReadStream rs = Vfs.openRead(reader); return (StylesheetImpl) generate(parseXSL(rs), rs.getPath()); } /** * Create a new stylesheet from an input stream. */ public StylesheetImpl newStylesheet(InputStream is) throws Exception { ReadStream rs = Vfs.openRead(is); return (StylesheetImpl) generate(parseXSL(rs), rs.getPath()); } /** * Loads a stylesheet from a named file * * @param systemId the URL of the file */ public StylesheetImpl newStylesheet(String systemId) throws Exception { StylesheetImpl stylesheet = loadPrecompiledStylesheet(systemId, systemId); if (stylesheet != null) return stylesheet; synchronized (AbstractStylesheetFactory.class) { stylesheet = loadPrecompiledStylesheet(systemId, systemId); if (stylesheet != null) return stylesheet; ReadStream is; if (_stylePath != null) is = _stylePath.lookup(systemId).openRead(); else is = Vfs.lookup(systemId).openRead(); try { return newStylesheet(is); } finally { if (is != null) is.close(); } } } public StylesheetImpl newStylesheet(Path path) throws Exception { StylesheetImpl stylesheet = loadPrecompiledStylesheet(path.getFullPath(), path.getUserPath()); if (stylesheet != null) return stylesheet; synchronized (AbstractStylesheetFactory.class) { stylesheet = loadPrecompiledStylesheet(path.getFullPath(), path.getUserPath()); if (stylesheet != null) return stylesheet; Path oldStylePath = _stylePath; if (_stylePath == null) _stylePath = path.getParent(); InputStream is = null; try { is = path.openRead(); return newStylesheet(is); } finally { _stylePath = oldStylePath; if (is != null) is.close(); } } } /** * Create a compiled stylesheet from an input stream. * * @param source the source stream * * @return the compiled stylesheet */ public Templates newTemplates(Source source) throws TransformerConfigurationException { String systemId = source.getSystemId(); try { if (systemId != null) { StylesheetImpl stylesheet = loadPrecompiledStylesheet(systemId, systemId); if (stylesheet != null) return stylesheet; } if (source instanceof DOMSource) { Node node = ((DOMSource) source).getNode(); return generateFromNode(node, systemId); } else if (source instanceof SAXSource) { SAXSource saxSource = (SAXSource) source; XMLReader reader = saxSource.getXMLReader(); InputSource inputSource = saxSource.getInputSource(); Document doc = new QDocument(); DOMBuilder builder = new DOMBuilder(); builder.init(doc); reader.setContentHandler(builder); reader.parse(inputSource); return generateFromNode(doc, systemId); } ReadStream rs = openPath(source); try { Path path = rs.getPath(); Document doc = parseXSL(rs); if (systemId != null) { String mangledName = getMangledName(systemId); Path genPath = getWorkPath().lookup(mangledName); genPath.setUserPath(systemId); return generate(doc, genPath); } else return generateFromNode(doc, null); } finally { if (rs != null) rs.close(); } } catch (TransformerConfigurationException e) { throw e; } catch (Exception e) { throw new XslParseException(e); } } private Templates generateFromNode(Node node, String systemId) throws IOException, TransformerConfigurationException { Path tempPath = writeTempFile(node); String tempId = tempPath.getTail(); StylesheetImpl stylesheet = loadPrecompiledStylesheet(tempId, tempId, false); if (systemId != null) tempPath.setUserPath(systemId); if (stylesheet != null) return stylesheet; return generate(node, tempPath); } private Path writeTempFile(Node node) throws IOException { Path workDir = CauchoSystem.getWorkPath().lookup("_xsl"); workDir.mkdirs(); // Path temp = workDir.createTempFile("tmp", "xsl"); WriteStream os = Vfs.lookup("null:").openWrite(); Crc64Stream crcStream = new Crc64Stream(os.getSource()); os.init(crcStream); try { XmlPrinter printer = new XmlPrinter(os); printer.printNode(node); } finally { os.close(); } long crc = crcStream.getCRC(); CharBuffer cb = new CharBuffer(); Base64.encode(cb, crc); String crcValue = cb.toString().replace('/', '-'); Path xslPath = workDir.lookup(crcValue + ".xsl"); // temp.renameTo(xslPath); return xslPath; } /** * Create a new transformer handler. */ public TransformerHandler newTransformerHandler() throws TransformerConfigurationException { return newTransformerHandler(new StylesheetImpl()); } /** * Create a new transformer handler based on a source. */ public TransformerHandler newTransformerHandler(Source source) throws TransformerConfigurationException { return newTransformerHandler(newTemplates(source)); } /** * Create a new transformer handler based on a stylesheet. */ public TransformerHandler newTransformerHandler(Templates templates) throws TransformerConfigurationException { return new TransformerHandlerImpl(templates.newTransformer()); } /** * Returns a templates handler. * * @param source the source file */ public TemplatesHandler newTemplatesHandler() throws TransformerConfigurationException { return new TemplatesHandlerImpl(this); } /** * Returns an XML filter from the transformer. * * @param source the source file */ public XMLFilter newXMLFilter(Source source) throws TransformerConfigurationException { Templates templates = newTemplates(source); return newXMLFilter(templates); } /** * Returns an XML filter from the transformer. * * @param source the source file */ public XMLFilter newXMLFilter(Templates templates) throws TransformerConfigurationException { return new SAXFilterImpl((TransformerImpl) templates.newTransformer()); } /** * Parses a stylesheet from the source. */ protected Node parseStylesheet(Source source) throws TransformerConfigurationException { if (source instanceof DOMSource) return ((DOMSource) source).getNode(); else if (source instanceof StreamSource) { InputStream is = ((StreamSource) source).getInputStream(); ReadStream rs = null; try { rs = Vfs.openRead(is); return parseXSL(rs); } catch (Exception e) { throw new TransformerConfigurationException(e); } finally { if (rs != null) rs.close(); } } else return null; } /** * Convenience class to create a compiled stylesheet. * * @param node DOM source for the stylesheet. * * @return a compiled stylesheet */ public javax.xml.transform.Templates newTemplates(Node node) throws TransformerConfigurationException { Document doc = node.getOwnerDocument(); if (node instanceof Document) doc = (Document) node; DocumentType dtd = doc.getDoctype(); if (dtd != null && dtd.getSystemId() != null) return generate(node, getSearchPath().lookup(dtd.getSystemId())); else if (doc instanceof CauchoDocument) { String systemId = ((CauchoDocument) doc).getFilename(); return generate(node, getSearchPath().lookup(systemId)); } else return generate(node, null); } /** * Convenience class to create a compiled stylesheet. * * @param systemId source path for the stylesheet. * * @return a compiled stylesheet */ public javax.xml.transform.Templates newTemplates(String systemId) throws TransformerConfigurationException { StylesheetImpl stylesheet = loadPrecompiledStylesheet(systemId, systemId); if (stylesheet != null) return stylesheet; else if (systemId == null) return generate(new QDocument(), null); Path path = getSearchPath().lookup(systemId); try { ReadStream is = path.openRead(); Document doc; try { doc = parseXSL(is); } finally { is.close(); } return generate(doc, path); } catch (TransformerConfigurationException e) { throw e; } catch (IOException e) { System.out.println("MP: " + ((MergePath) getSearchPath()).getMergePaths()); throw new TransformerConfigurationExceptionWrapper(e); } catch (Exception e) { throw new TransformerConfigurationExceptionWrapper(e);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?