KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > soto > state > cocoon > view > XslSource


1 package org.sapia.soto.state.cocoon.view;
2
3 import org.sapia.soto.Env;
4 import org.sapia.soto.EnvAware;
5 import org.sapia.soto.config.ThreadState;
6 import org.sapia.soto.util.Resource;
7 import org.sapia.soto.util.ResourceHandler;
8 import org.sapia.soto.util.Utils;
9
10 import org.sapia.util.text.TemplateContextIF;
11
12 import java.io.IOException JavaDoc;
13 import java.io.InputStream JavaDoc;
14
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import javax.xml.transform.Templates JavaDoc;
19 import javax.xml.transform.TransformerConfigurationException JavaDoc;
20 import javax.xml.transform.TransformerFactory JavaDoc;
21 import javax.xml.transform.sax.SAXTransformerFactory JavaDoc;
22 import javax.xml.transform.sax.TransformerHandler JavaDoc;
23 import javax.xml.transform.stream.StreamSource JavaDoc;
24
25
26 /**
27  * An instance of this class encapsulates the behavior necessary to create
28  * <code>TransformerHandler</code>s from XSL stylesheets. The instance is
29  * given the source to a stylesheet; whenever possible, it detects if the
30  * stylesheet has been modified and reloads it - currently, such a feature
31  * is available only for URIs that correspond to a <code>java.io.File</code>
32  * object.
33  *
34  * @author Yanick Duchesne
35  *
36  * @deprecated
37  *
38  * <dl>
39  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
40  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
41  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
42  * </dl>
43  */

44 public class XslSource implements EnvAware {
45   private Templates JavaDoc _templ;
46   private SAXTransformerFactory JavaDoc _fac;
47   private Resource _file;
48   private Env _env;
49   private long _lastModified = System.currentTimeMillis();
50
51   public XslSource() {
52   }
53
54   /**
55    * @param uri the URI to an XSL stylesheet.
56    * @throws IOException
57    * @throws TransformerConfigurationException
58    */

59   public void setSrc(String JavaDoc uri)
60     throws IOException JavaDoc, TransformerConfigurationException JavaDoc {
61     if (_fac == null) {
62       _fac = (SAXTransformerFactory JavaDoc) TransformerFactory.newInstance();
63     }
64     ResourceHandler handler = (ResourceHandler) _env.getResourceHandlerFor(uri);
65     _file = handler.getResourceObject(uri);
66     _templ = _fac.newTemplates(new StreamSource JavaDoc(process(
67             _file.getInputStream(), uri)));
68   }
69   
70   /**
71    * @return this instance's URI.
72    */

73   public String JavaDoc getUri(){
74     return _file.getURI();
75   }
76
77   /**
78    * @return a <code>TransformerHandler</code>.
79    * @throws TransformerConfigurationException
80    * @throws IOException
81    */

82   public TransformerHandler JavaDoc getTransformerHandler(Map JavaDoc params)
83     throws TransformerConfigurationException JavaDoc, IOException JavaDoc {
84     if ((_file != null) && (_file.lastModified() != _lastModified)) {
85       reload();
86     }
87
88     TransformerHandler JavaDoc handler = _fac.newTransformerHandler(_templ);
89     Object JavaDoc key;
90     Object JavaDoc val;
91
92     for (Iterator JavaDoc iter = params.keySet().iterator(); iter.hasNext();) {
93       key = iter.next();
94       val = params.get(key);
95
96       if (val != null) {
97         handler.getTransformer().setParameter(key.toString(), val);
98       }
99     }
100
101     return handler;
102   }
103
104   /**
105    * @see org.sapia.soto.EnvAware#setEnv(org.sapia.soto.Env)
106    */

107   public void setEnv(Env env) {
108     _env = env;
109   }
110
111   /**
112    * @param fac the <code>SAXTransformerFactory</code> that is instance
113    * is to internally use.
114    */

115   protected void setSaxTransformerFactory(SAXTransformerFactory JavaDoc fac) {
116     _fac = fac;
117   }
118
119   private synchronized void reload()
120     throws TransformerConfigurationException JavaDoc, IOException JavaDoc {
121     if (_file.lastModified() != _lastModified) {
122       _templ = _fac.newTemplates(new StreamSource JavaDoc(_file.getInputStream(),
123             _file.getURI()));
124     }
125
126     _lastModified = _file.lastModified();
127   }
128
129   private InputStream JavaDoc process(InputStream JavaDoc is, String JavaDoc uri)
130     throws IOException JavaDoc {
131     TemplateContextIF ctx = ThreadState.currentTemplateContext();
132
133     return Utils.replaceVars(ctx, is, uri);
134   }
135 }
136
Popular Tags