Initial Commit
[packages] / xemacs-packages / xslt-process / java / xslt / TrAX.java
1 /*
2     TrAX.java
3
4     Wrapper for generic TrAX XSLT processors, to be easily invoked
5     from BSH.
6
7     Author: Ovidiu Predescu <ovidiu@cup.hp.com>
8     Based on initial version from Allan Erskine <a.erskine@cs.ucl.ac.uk>
9
10     Copyright (C) 2001 Allan Erskine and Ovidiu Predescu
11
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.
16
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.
21
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
25     02111-1307, USA.
26  */
27
28 package xslt;
29
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;
38
39 // Imported java classes
40 import java.io.File;
41 import java.io.FileOutputStream;
42 import java.io.IOException;     
43 import java.util.HashMap;
44 import java.net.URLConnection;
45 import java.net.URL;
46 import java.net.MalformedURLException;
47
48 public class TrAX 
49 {
50   static TransformerFactory tFactory = TransformerFactory.newInstance();
51   static HashMap sheetCache = new HashMap();
52   
53   public static void invoke(String filename, String outFilename)
54     throws TransformerException, TransformerConfigurationException
55   {
56     try {
57       File inFile = new File(filename);
58       FileOutputStream outStream = new FileOutputStream(outFilename);
59
60       StreamSource in = new StreamSource(inFile);
61       StreamResult result = new StreamResult(outStream);
62
63       String media = null, title = null, charset = null;
64
65       Source stylesheet
66         = tFactory.getAssociatedStylesheet(in, media, title, charset);
67       String stylesheetId = stylesheet.getSystemId();
68
69       // Look for a transformer already in the cache
70       XSLTSheetInfo sheetInfo = (XSLTSheetInfo)sheetCache.get(stylesheetId);
71       Transformer transformer;
72       
73       if (sheetInfo == null) {
74         sheetInfo = new XSLTSheetInfo(stylesheet);
75         sheetCache.put(stylesheetId, sheetInfo);
76         transformer = sheetInfo.transformer;
77       }
78       else {
79         // We have already encountered this particular XSLT
80         // sheet. Check to see whether it has changed since the last
81         // access
82         transformer = sheetInfo.getTransformer(stylesheet);
83       }
84
85       if (transformer != null)
86         transformer.transform(in, result);
87       outStream.close();
88     }
89     catch(Exception e) {
90       e.printStackTrace();
91     }
92   }
93 }
94
95 class XSLTSheetInfo
96 {
97   public Transformer transformer = null;
98   public long lastModified = Long.MIN_VALUE;
99
100   public XSLTSheetInfo(Source stylesheet)
101   {
102     String stylesheetId = stylesheet.getSystemId();
103
104     try {
105       lastModified = getLastModified(stylesheetId);
106       transformer = TrAX.tFactory.newTransformer(stylesheet);
107     }
108     catch (TransformerConfigurationException e) {}
109   }
110
111   long getLastModified(String stylesheetId)
112   {
113     long lastModified = Long.MIN_VALUE;
114     URL url = null;
115
116     try {
117       url = new URL(stylesheetId);
118       if (url.getProtocol().equals("file")) {
119         File file = new File(url.getFile());
120         lastModified = file.lastModified();
121       }
122       else {
123         URLConnection conn = url.openConnection();
124         conn.connect();
125         lastModified = conn.getLastModified();
126       }
127     }
128     catch (MalformedURLException e) {
129       System.err.println("Invalid URL " + url + ": " + e.toString());
130     }
131     catch (IOException e) {
132       System.err.println("Cannot access " + url + ": "+ e.toString());
133     }
134
135     return lastModified;
136   }
137
138   public Transformer getTransformer(Source stylesheet)
139   {
140     String stylesheetId = stylesheet.getSystemId();
141     long currentTime = Long.MIN_VALUE;
142
143     currentTime = getLastModified(stylesheetId);
144
145     if (currentTime <= lastModified && transformer != null) {
146       // The XSLT sheet has not been modified since the last access,
147       // return the cached one
148       return transformer;
149     }
150
151     // The XSLT sheet has been modified, create a new transformer
152     // and return it
153     try {
154       transformer = TrAX.tFactory.newTransformer(stylesheet);
155       lastModified = currentTime;
156     }
157     catch (TransformerConfigurationException e) {
158       System.out.println("Could not create transformer for: " + stylesheetId);
159     }
160
161     return transformer;
162   }
163 }
164
165 // Local Variables:
166 // c-basic-offset: 2
167 // End: