4 Wrapper for generic TrAX XSLT processors, to be easily invoked
7 Author: Ovidiu Predescu <ovidiu@cup.hp.com>
8 Based on initial version from Allan Erskine <a.erskine@cs.ucl.ac.uk>
10 Copyright (C) 2001 Allan Erskine and Ovidiu Predescu
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of the
15 License, or (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
30 // Imported TraX classes
31 import javax.xml.transform.TransformerFactory;
32 import javax.xml.transform.Transformer;
33 import javax.xml.transform.Source;
34 import javax.xml.transform.stream.StreamSource;
35 import javax.xml.transform.stream.StreamResult;
36 import javax.xml.transform.TransformerException;
37 import javax.xml.transform.TransformerConfigurationException;
39 // Imported java classes
41 import java.io.FileOutputStream;
42 import java.io.IOException;
43 import java.util.HashMap;
44 import java.net.URLConnection;
46 import java.net.MalformedURLException;
50 static TransformerFactory tFactory = TransformerFactory.newInstance();
51 static HashMap sheetCache = new HashMap();
53 public static void invoke(String filename, String outFilename)
54 throws TransformerException, TransformerConfigurationException
57 File inFile = new File(filename);
58 FileOutputStream outStream = new FileOutputStream(outFilename);
60 StreamSource in = new StreamSource(inFile);
61 StreamResult result = new StreamResult(outStream);
63 String media = null, title = null, charset = null;
66 = tFactory.getAssociatedStylesheet(in, media, title, charset);
67 String stylesheetId = stylesheet.getSystemId();
69 // Look for a transformer already in the cache
70 XSLTSheetInfo sheetInfo = (XSLTSheetInfo)sheetCache.get(stylesheetId);
71 Transformer transformer;
73 if (sheetInfo == null) {
74 sheetInfo = new XSLTSheetInfo(stylesheet);
75 sheetCache.put(stylesheetId, sheetInfo);
76 transformer = sheetInfo.transformer;
79 // We have already encountered this particular XSLT
80 // sheet. Check to see whether it has changed since the last
82 transformer = sheetInfo.getTransformer(stylesheet);
85 if (transformer != null)
86 transformer.transform(in, result);
97 public Transformer transformer = null;
98 public long lastModified = Long.MIN_VALUE;
100 public XSLTSheetInfo(Source stylesheet)
102 String stylesheetId = stylesheet.getSystemId();
105 lastModified = getLastModified(stylesheetId);
106 transformer = TrAX.tFactory.newTransformer(stylesheet);
108 catch (TransformerConfigurationException e) {}
111 long getLastModified(String stylesheetId)
113 long lastModified = Long.MIN_VALUE;
117 url = new URL(stylesheetId);
118 if (url.getProtocol().equals("file")) {
119 File file = new File(url.getFile());
120 lastModified = file.lastModified();
123 URLConnection conn = url.openConnection();
125 lastModified = conn.getLastModified();
128 catch (MalformedURLException e) {
129 System.err.println("Invalid URL " + url + ": " + e.toString());
131 catch (IOException e) {
132 System.err.println("Cannot access " + url + ": "+ e.toString());
138 public Transformer getTransformer(Source stylesheet)
140 String stylesheetId = stylesheet.getSystemId();
141 long currentTime = Long.MIN_VALUE;
143 currentTime = getLastModified(stylesheetId);
145 if (currentTime <= lastModified && transformer != null) {
146 // The XSLT sheet has not been modified since the last access,
147 // return the cached one
151 // The XSLT sheet has been modified, create a new transformer
154 transformer = TrAX.tFactory.newTransformer(stylesheet);
155 lastModified = currentTime;
157 catch (TransformerConfigurationException e) {
158 System.out.println("Could not create transformer for: " + stylesheetId);