View Javadoc

1   /*
2   * $Log: ViewTransform.java,v $
3   * Revision 1.1.1.1  2004/07/22 14:02:25  xwarzee
4   * Initial import.
5   *
6   * Revision 1.11  2003/06/04 20:03:24  jtilani
7   * SVCge23933
8   *
9   */
10  
11  package org.opensync.tools;
12  
13  import org.opensync.tools.StylesheetCache;
14  
15  import java.io.*;
16  import java.util.*;
17  import org.xml.sax.*;
18  import java.util.jar.*;
19  import java.net.MalformedURLException;
20  
21  // Begin JAXP 1.1.3 packages  used with Xalan2 (not Xalan1)
22  import javax.xml.transform.*;
23  import javax.xml.transform.stream.*;
24  // End JAXP 1.1.3 packages
25  
26  /*
27  import org.apache.xalan.xslt.*;
28  */
29  
30  public class ViewTransform {
31  
32    // Begin JAXP 1.1.3 variable used with Xalan2 (not Xalan1)
33    static protected TransformerFactory factory = TransformerFactory.newInstance();
34    // Begin JAXP 1.1.3 variable
35  
36    protected String xalanVer = null;
37    protected String path;
38    protected String servletServerName;
39    protected Hashtable xslInputs = new Hashtable();
40  
41  
42    static protected ViewTransform instance;
43  
44    void initXalanVer() {
45      xalanVer = "1.0";
46      try {
47            // Open the JAR file
48            JarFile jarfile = new JarFile(path+"/WEB-INF/lib/xalan1.jar");
49  
50            // Get the manifest
51            Manifest manifest = jarfile.getManifest();
52  
53            // Get the manifest entries
54            Map map = manifest.getEntries();
55  
56            // Enumerate each entry
57            for (Iterator it=map.keySet().iterator(); it.hasNext(); ) {
58  
59                // Get entry name
60                String entryName = (String)it.next();
61  
62                if ((entryName != null) && (entryName.equals("org/apache/xalan/xslt/"))) {
63  
64                  // Get all attributes for the entry
65                  java.util.jar.Attributes attrs = (java.util.jar.Attributes)map.get(entryName);
66  
67                  // Enumerate each attribute
68                  for (Iterator it2=attrs.keySet().iterator(); it2.hasNext(); ) {
69                      // Get attribute name
70                      java.util.jar.Attributes.Name attrName = (java.util.jar.Attributes.Name)it2.next();
71  
72                      // Get attribute value
73                      String attrValue = attrs.getValue(attrName);
74                      if ((attrName != null) && (attrName.toString().equals("Implementation-Version"))) xalanVer = attrValue;
75                  }
76                }
77            }
78        } catch (IOException e) {
79          }
80    }
81  
82    protected ViewTransform(String path) {
83      this.path = path;
84    }
85    public static ViewTransform getInstance(String path){
86      if(instance == null){
87        setInstance(path);
88      }
89      return instance;
90    }
91    public static ViewTransform getInstance(){
92      if(instance == null){
93        throw new IllegalStateException("Use getInstance(String path) first");
94      }
95      return instance;
96    }
97    protected static synchronized void setInstance(String path){
98      if(instance == null){
99        instance = new ViewTransform(path);
100     }
101   }
102 
103   synchronized public void buildHtml (String xmlInput, String xslInputFile, Writer out) throws SAXException
104   //TransformerConfigurationException,TransformerException
105   {
106 
107     //if (xalanVer == null) initXalanVer();
108 
109     //if (servletServerName.equals("Apache Tomcat/4.0.")) {
110     /*if ( ( (servletServerName.indexOf("Tomcat") >= 0) && (servletServerName.indexOf("4") >= 0))
111           && (xalanVer.indexOf("2.") == 0) ) {
112     */
113       try {
114         // Code use with Xalan2 (JAXP 1.1.x).
115         StreamSource xslSource;
116 
117         synchronized(xslInputs){
118           xslSource = (StreamSource) xslInputs.get(xslInputFile);
119           if(xslSource == null) {
120             xslSource = new StreamSource(new File(path + xslInputFile).toURL().toExternalForm());
121             xslInputs.put(xslInputFile,xslSource);
122           }
123         }
124         //Transformer transformer = factory.newTransformer(xslSource);
125         Transformer transformer = StylesheetCache.newTransformer(path + xslInputFile);
126         StreamSource xmlSource = new StreamSource(new StringReader(xmlInput));
127         StreamResult result = new StreamResult(out);
128         transformer.transform(xmlSource, result);
129         }
130         catch (java.net.MalformedURLException mue) {
131           throw new SAXException(mue);
132         }
133         catch (TransformerConfigurationException tce) {
134           throw new SAXException("Line: "+tce.getLocationAsString()+"; Error: "+tce.getMessageAndLocation()+"\n Inputs:\n"+xmlInput);
135         }
136         catch (TransformerException te) {
137           throw new SAXException(te.getLocalizedMessage());
138         }
139 
140         // Code use with Xalan1
141       /*
142         XSLTInputSource xslSource;
143         synchronized(xslInputs){
144           xslSource = (XSLTInputSource) xslInputs.get(xslInputFile);
145           if(xslSource == null) {
146             xslSource = new XSLTInputSource(path + xslInputFile);
147             xslInputs.put(xslInputFile,xslSource);
148           }
149         }
150         XSLTInputSource xmlSource = new XSLTInputSource();
151         xmlSource.setCharacterStream(new StringReader(xmlInput));
152         XSLTResultTarget result = new XSLTResultTarget();
153         result.setCharacterStream(out);
154         XSLTProcessor processor = XSLTProcessorFactory.getProcessor();
155         processor.process(xmlSource,xslSource,result);
156       */
157     }
158 
159   //}
160 
161   public void setServletServerName(String _servletServerName) {
162     servletServerName = _servletServerName;
163   }
164 
165   public void buildHtmlFromFile (String xmlInputFile, String xslInputFile, PrintWriter out)
166   throws FileNotFoundException, SAXException{
167 
168     try {
169     StreamSource xslSource;
170     //XSLTInputSource xslSource;
171     synchronized(xslInputs){
172       xslSource = (StreamSource) xslInputs.get(xslInputFile);
173       //xslSource = (XSLTInputSource) xslInputs.get(xslInputFile);
174       if(xslSource == null) {
175         if (new File(path + xslInputFile).exists()){
176           xslSource = new StreamSource(new File(path + xslInputFile).toURL().toExternalForm());
177         }else{
178           xslSource = new StreamSource(path + xslInputFile);
179         }
180         //xslSource = new XSLTInputSource(path + xslInputFile);
181         xslInputs.put(xslInputFile,xslSource);
182       }
183     }
184 
185     Transformer transformer = factory.newTransformer(xslSource);
186     StreamSource xmlSource=null;
187     if(new File(path + xmlInputFile).exists()){
188       xmlSource = new StreamSource(new File(path + xmlInputFile).toURL().toExternalForm());
189     }else{
190       xmlSource = new StreamSource(path + xmlInputFile);
191     }
192     StreamResult result = new StreamResult(out);
193     transformer.transform(xmlSource, result);
194 
195 /*
196     XSLTInputSource xmlSource = new XSLTInputSource();
197     xmlSource.setCharacterStream(new FileReader(xmlInputFile));
198     XSLTResultTarget result = new XSLTResultTarget();
199     result.setCharacterStream(out);
200     XSLTProcessor processor = XSLTProcessorFactory.getProcessor();
201     processor.process(xmlSource,xslSource,result);
202 */
203         }
204         catch (MalformedURLException mue) {
205           throw new SAXException(mue.getMessage());
206         } catch (TransformerConfigurationException tce) {
207           throw new SAXException("Line: "+tce.getLocationAsString()+"; Error: "+tce.getMessageAndLocation());
208         }
209         catch (TransformerException te) {
210           throw new SAXException(te.getLocalizedMessage());
211         }
212   }
213 
214 
215   //P.V. 09-07-2002
216   //method to insert PARAMETERS to the XSL style sheet
217   public void buildHtml (String xmlInput, String xslInputFile, Writer out, HashMap hm) throws SAXException {
218       try {
219         // Code use with Xalan2 (JAXP 1.1.x).
220         StreamSource xslSource;
221 
222         synchronized(xslInputs){
223           xslSource = (StreamSource) xslInputs.get(xslInputFile);
224           if(xslSource == null) {
225             xslSource = new StreamSource(path + xslInputFile);
226             xslInputs.put(xslInputFile,xslSource);
227           }
228         }
229         Transformer transformer = StylesheetCache.newTransformer(path + xslInputFile);
230 
231         //process the hashmap of parameters for the stylesheet
232 	     Set keys = hm.keySet();
233 	     String[] array = (String[])keys.toArray(new String[keys.size()]);
234         for (int i=0;i<keys.size();i++) {
235 		    String [] values = (String[])hm.get(array[i]);
236 	       transformer.setParameter(array[i], values[0]);
237 		    }
238 
239         StreamSource xmlSource = new StreamSource(new StringReader(xmlInput));
240         StreamResult result = new StreamResult(out);
241         transformer.transform(xmlSource, result);
242         } catch (MalformedURLException mue) {
243           throw new SAXException(mue.getMessage());
244         } catch (TransformerConfigurationException tce) {
245           throw new SAXException("Line: "+tce.getLocationAsString()+"; Error: "+tce.getMessageAndLocation());
246         }
247         catch (TransformerException te) {
248           throw new SAXException(te.getLocalizedMessage());
249         }
250     }
251 
252   //P.V. 09-07-2002
253   //method to insert PARAMETERS to the XSL style sheet
254   public void buildHtmlFromFile (String xmlInputFile, String xslInputFile, PrintWriter out, HashMap hm)
255   throws FileNotFoundException, SAXException{
256 
257     try {
258     StreamSource xslSource;
259     synchronized(xslInputs){
260       xslSource = (StreamSource) xslInputs.get(xslInputFile);
261       if(xslSource == null) {
262         xslSource = new StreamSource(new File(path + xslInputFile).toURL().toExternalForm());
263         xslInputs.put(xslInputFile,xslSource);
264       }
265     }
266 
267     Transformer transformer = factory.newTransformer(xslSource);
268 
269 	 //process the hashmap of parameters for the stylesheet
270 	 Set keys = hm.keySet();
271 	 String[] array = (String[])keys.toArray(new String[keys.size()]);
272     for (int i=0;i<keys.size();i++) {
273 		String [] values = (String[])hm.get(array[i]);
274 	   transformer.setParameter(array[i], values[0]);
275 		}
276 
277     StreamSource xmlSource = new StreamSource(new File(path + xmlInputFile).toURL().toExternalForm());
278     StreamResult result = new StreamResult(out);
279     transformer.transform(xmlSource, result);
280 
281         } catch (MalformedURLException mue) {
282           throw new SAXException(mue.getMessage());
283         } catch (TransformerConfigurationException tce) {
284           throw new SAXException("Line: "+tce.getLocationAsString()+"; Error: "+tce.getMessageAndLocation());
285         }
286         catch (TransformerException te) {
287           throw new SAXException(te.getLocalizedMessage());
288         }
289   }
290 }