📄 transformerfactoryimpl.java
字号:
*/ public InputSource loadSource(String href, String context, XSLTC xsltc) { try { if (_uriResolver != null) { final Source source = _uriResolver.resolve(href, context); if (source != null) { return Util.getInputSource(xsltc, source); } } } catch (TransformerException e) { // should catch it when the resolver explicitly throws the exception final ErrorMsg msg = new ErrorMsg(ErrorMsg.INVALID_URI_ERR, href + "\n" + e.getMessage(), this); xsltc.getParser().reportError(Constants.FATAL, msg); } return null; } /** * Reset the per-session attributes to their default values */ private void resetTransientAttributes() { _transletName = DEFAULT_TRANSLET_NAME; _destinationDirectory = null; _packageName = null; _jarFileName = null; } /** * Load the translet classes from local .class files and return * the bytecode array. * * @param source The xsl source * @param fullClassName The full name of the translet * @return The bytecode array */ private byte[][] getBytecodesFromClasses(Source source, String fullClassName) { if (fullClassName == null) return null; String xslFileName = getStylesheetFileName(source); File xslFile = null; if (xslFileName != null) xslFile = new File(xslFileName); // Find the base name of the translet final String transletName; int lastDotIndex = fullClassName.lastIndexOf('.'); if (lastDotIndex > 0) transletName = fullClassName.substring(lastDotIndex+1); else transletName = fullClassName; // Construct the path name for the translet class file String transletPath = fullClassName.replace('.', '/'); if (_destinationDirectory != null) { transletPath = _destinationDirectory + "/" + transletPath + ".class"; } else { if (xslFile != null && xslFile.getParent() != null) transletPath = xslFile.getParent() + "/" + transletPath + ".class"; else transletPath = transletPath + ".class"; } // Return null if the translet class file does not exist. File transletFile = new File(transletPath); if (!transletFile.exists()) return null; // Compare the timestamps of the translet and the xsl file. // If the translet is older than the xsl file, return null // so that the xsl file is used for the transformation and // the translet is regenerated. if (xslFile != null && xslFile.exists()) { long xslTimestamp = xslFile.lastModified(); long transletTimestamp = transletFile.lastModified(); if (transletTimestamp < xslTimestamp) return null; } // Load the translet into a bytecode array. Vector bytecodes = new Vector(); int fileLength = (int)transletFile.length(); if (fileLength > 0) { FileInputStream input = null; try { input = new FileInputStream(transletFile); } catch (FileNotFoundException e) { return null; } byte[] bytes = new byte[fileLength]; try { readFromInputStream(bytes, input, fileLength); input.close(); } catch (IOException e) { return null; } bytecodes.addElement(bytes); } else return null; // Find the parent directory of the translet. String transletParentDir = transletFile.getParent(); if (transletParentDir == null) transletParentDir = System.getProperty("user.dir"); File transletParentFile = new File(transletParentDir); // Find all the auxiliary files which have a name pattern of "transletClass$nnn.class". final String transletAuxPrefix = transletName + "$"; File[] auxfiles = transletParentFile.listFiles(new FilenameFilter() { public boolean accept(File dir, String name) { return (name.endsWith(".class") && name.startsWith(transletAuxPrefix)); } }); // Load the auxiliary class files and add them to the bytecode array. for (int i = 0; i < auxfiles.length; i++) { File auxfile = auxfiles[i]; int auxlength = (int)auxfile.length(); if (auxlength > 0) { FileInputStream auxinput = null; try { auxinput = new FileInputStream(auxfile); } catch (FileNotFoundException e) { continue; } byte[] bytes = new byte[auxlength]; try { readFromInputStream(bytes, auxinput, auxlength); auxinput.close(); } catch (IOException e) { continue; } bytecodes.addElement(bytes); } } // Convert the Vector of byte[] to byte[][]. final int count = bytecodes.size(); if ( count > 0) { final byte[][] result = new byte[count][1]; for (int i = 0; i < count; i++) { result[i] = (byte[])bytecodes.elementAt(i); } return result; } else return null; } /** * Load the translet classes from the jar file and return the bytecode. * * @param source The xsl source * @param fullClassName The full name of the translet * @return The bytecode array */ private byte[][] getBytecodesFromJar(Source source, String fullClassName) { String xslFileName = getStylesheetFileName(source); File xslFile = null; if (xslFileName != null) xslFile = new File(xslFileName); // Construct the path for the jar file String jarPath = null; if (_destinationDirectory != null) jarPath = _destinationDirectory + "/" + _jarFileName; else { if (xslFile != null && xslFile.getParent() != null) jarPath = xslFile.getParent() + "/" + _jarFileName; else jarPath = _jarFileName; } // Return null if the jar file does not exist. File file = new File(jarPath); if (!file.exists()) return null; // Compare the timestamps of the jar file and the xsl file. Return null // if the xsl file is newer than the jar file. if (xslFile != null && xslFile.exists()) { long xslTimestamp = xslFile.lastModified(); long transletTimestamp = file.lastModified(); if (transletTimestamp < xslTimestamp) return null; } // Create a ZipFile object for the jar file ZipFile jarFile = null; try { jarFile = new ZipFile(file); } catch (IOException e) { return null; } String transletPath = fullClassName.replace('.', '/'); String transletAuxPrefix = transletPath + "$"; String transletFullName = transletPath + ".class"; Vector bytecodes = new Vector(); // Iterate through all entries in the jar file to find the // translet and auxiliary classes. Enumeration entries = jarFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry)entries.nextElement(); String entryName = entry.getName(); if (entry.getSize() > 0 && (entryName.equals(transletFullName) || (entryName.endsWith(".class") && entryName.startsWith(transletAuxPrefix)))) { try { InputStream input = jarFile.getInputStream(entry); int size = (int)entry.getSize(); byte[] bytes = new byte[size]; readFromInputStream(bytes, input, size); input.close(); bytecodes.addElement(bytes); } catch (IOException e) { return null; } } } // Convert the Vector of byte[] to byte[][]. final int count = bytecodes.size(); if (count > 0) { final byte[][] result = new byte[count][1]; for (int i = 0; i < count; i++) { result[i] = (byte[])bytecodes.elementAt(i); } return result; } else return null; } /** * Read a given number of bytes from the InputStream into a byte array. * * @param bytes The byte array to store the input content. * @param input The input stream. * @param size The number of bytes to read. */ private void readFromInputStream(byte[] bytes, InputStream input, int size) throws IOException { int n = 0; int offset = 0; int length = size; while (length > 0 && (n = input.read(bytes, offset, length)) > 0) { offset = offset + n; length = length - n; } } /** * Return the base class name of the translet. * The translet name is resolved using the following rules: * 1. if the _transletName attribute is set and its value is not "GregorSamsa", * then _transletName is returned. * 2. otherwise get the translet name from the base name of the system ID * 3. return "GregorSamsa" if the result from step 2 is null. * * @param source The input Source * @return The name of the translet class */ private String getTransletBaseName(Source source) { String transletBaseName = null; if (!_transletName.equals(DEFAULT_TRANSLET_NAME)) return _transletName; else { String systemId = source.getSystemId(); if (systemId != null) { String baseName = Util.baseName(systemId); if (baseName != null) { baseName = Util.noExtName(baseName); transletBaseName = Util.toJavaName(baseName); } } } return (transletBaseName != null) ? transletBaseName : DEFAULT_TRANSLET_NAME; } /** * Return the local file name from the systemId of the Source object * * @param source The Source * @return The file name in the local filesystem, or null if the * systemId does not represent a local file. */ private String getStylesheetFileName(Source source) { String systemId = source.getSystemId(); if (systemId != null) { File file = new File(systemId); if (file.exists()) return systemId; else { URL url = null; try { url = new URL(systemId); } catch (MalformedURLException e) { return null; } if ("file".equals(url.getProtocol())) return url.getFile(); else return null; } } else return null; } /** * Returns the Class object the provides the XSLTC DTM Manager service. */ protected Class getDTMManagerClass() { return m_DTMManagerClass; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -