KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > deployers > plugins > deployers > helpers > XSLDeployer


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.deployers.plugins.deployers.helpers;
23
24 import java.io.InputStream JavaDoc;
25
26 import javax.xml.transform.Source JavaDoc;
27 import javax.xml.transform.Templates JavaDoc;
28 import javax.xml.transform.Transformer JavaDoc;
29 import javax.xml.transform.TransformerFactory JavaDoc;
30 import javax.xml.transform.dom.DOMResult JavaDoc;
31 import javax.xml.transform.dom.DOMSource JavaDoc;
32 import javax.xml.transform.stream.StreamSource JavaDoc;
33
34 import org.jboss.deployers.spi.deployer.DeploymentUnit;
35 import org.jboss.util.xml.DOMWriter;
36 import org.jboss.util.xml.JBossErrorHandler;
37 import org.jboss.virtual.VirtualFile;
38 import org.w3c.dom.Document JavaDoc;
39
40 /**
41  * SchemaResolverDeployer.
42  *
43  * @param <T> the expected type
44  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
45  * @version $Revision: 1.1 $
46  */

47 public abstract class XSLDeployer<T> extends JAXPDeployer<T>
48 {
49    /** The xsl resource path */
50    protected String JavaDoc xslPath;
51
52    /** The templates */
53    private Templates JavaDoc templates;
54
55    /**
56     * Create a new XSLDeployer.
57     *
58     * @param deploymentType the deployment type
59     * @throws IllegalArgumentException for a null deployment type
60     */

61    public XSLDeployer(Class JavaDoc<T> deploymentType)
62    {
63       super(deploymentType);
64    }
65
66    /**
67     * Get the xslPath.
68     *
69     * @return the xslPath.
70     */

71    public String JavaDoc getXSLPath()
72    {
73       return xslPath;
74    }
75
76    /**
77     * Set the xslPath.
78     *
79     * @param xslPath the xslPath.
80     */

81    public void setXSLPath(String JavaDoc xslPath)
82    {
83       this.xslPath = xslPath;
84    }
85
86    /**
87     * Get the templates.
88     *
89     * @return the templates.
90     * @throws IllegalStateException if the create method has not been invoked
91     */

92    public Templates JavaDoc getTemplates()
93    {
94       if (templates == null)
95          throw new IllegalStateException JavaDoc("Templates have not been constructed");
96       return templates;
97    }
98
99    /**
100     * Create lifecycle
101     *
102     * @throws Exception for any problem
103     */

104    public void create() throws Exception JavaDoc
105    {
106       super.create();
107
108       TransformerFactory JavaDoc tf = TransformerFactory.newInstance();
109       
110       InputStream JavaDoc is = Thread.currentThread().getContextClassLoader().getResourceAsStream(xslPath);
111       try
112       {
113          StreamSource JavaDoc ss = new StreamSource JavaDoc(is);
114          ss.setSystemId(xslPath);
115          templates = tf.newTemplates(ss);
116          log.debug("Created templates: " + templates);
117       }
118       finally
119       {
120          try
121          {
122             is.close();
123          }
124          catch (Exception JavaDoc ignored)
125          {
126          }
127       }
128    }
129
130    /**
131     * Destroy lifecycle
132     */

133    public void destroy()
134    {
135       super.destroy();
136       templates = null;
137    }
138    
139    @Override JavaDoc
140    protected T parse(DeploymentUnit unit, VirtualFile file, T root) throws Exception JavaDoc
141    {
142       if (file == null)
143          throw new IllegalArgumentException JavaDoc("Null file");
144
145       Document JavaDoc document = doParse(unit, file);
146
147       Transformer JavaDoc trans = getTemplates().newTransformer();
148       trans.setErrorListener(new JBossErrorHandler(file.getPathName(), null));
149       Source JavaDoc s = new DOMSource JavaDoc(document);
150       DOMResult JavaDoc r = new DOMResult JavaDoc();
151       setParameters(trans);
152       
153       trans.transform(s, r);
154       
155       document = (Document JavaDoc) r.getNode();
156       String JavaDoc docStr = DOMWriter.printNode(document, true);
157       log.debug("Transformed " + file.getPathName() + " into " + docStr);
158       
159       return parse(unit, file, document);
160    }
161
162    /**
163     * Set parameters for the transformation
164     *
165     * @param trans the transformer
166     * @throws Exception for any problem
167     */

168    protected void setParameters(Transformer JavaDoc trans) throws Exception JavaDoc
169    {
170       // nothing by default
171
}
172
173    /**
174     * Parse a deployment
175     *
176     * @param unit the deployment unit
177     * @param file the metadata file
178     * @param document the document
179     * @return the metadata
180     * @throws Exception for any error
181     */

182    protected abstract T parse(DeploymentUnit unit, VirtualFile file, Document JavaDoc document) throws Exception JavaDoc;
183 }
184
Popular Tags