|
在JAXP中透明的缓存XSL转换器(3) throws TransformerConfigurationException { // Check that source in a StreamSource if (source instanceof StreamSource) try { // Create URI of the source final URI uri = new URI(source.getSystemId()); // If URI points to a file, load transformer from the file // (or from the cache) if ("file".equalsIgnoreCase(uri.getScheme())) return newTransformer(new File(uri)); } catch (URISyntaxException urise) { throw new TransformerConfigurationException(urise); } return super.newTransformer(source); } 像你所看到的,如果transformer的source不是一个StreamSource或者未被指向一个文件,父类的newTransformer(…)会返回transformer。如果source是一个基于文件的StreamSource,这样我们就可以使用缓存来实现更智能的transformation载入过程。 基于文件的样式表缓存算法非常简单:根据一个给定的文件,我们首先检查缓存中已经有绝对文件名相同的Templates对象。如果不存在,我们为这个文件创建并缓存一个新的Templates对象。如果已经存在于缓存,我们检查在Template载入后文件是否曾修改过,比较文件的最后修改时间和缓存中的时间。如果文件被更新过,Templates必须被再次载入,否则就从缓存中取出。最后,用Template对象(根据情况而定,可能从缓存或磁盘中载入)生成一个新的transformer。下面有一个根据这个算法实现的方法: protected Transformer newTransformer(final File file)
|