KickJava   Java API By Example, From Geeks To Geeks.

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


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.parsers.DocumentBuilder JavaDoc;
27 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
28
29 import org.jboss.deployers.spi.deployer.DeploymentUnit;
30 import org.jboss.util.xml.JBossEntityResolver;
31 import org.jboss.virtual.VirtualFile;
32 import org.w3c.dom.Document JavaDoc;
33 import org.xml.sax.InputSource JavaDoc;
34
35 /**
36  * SchemaResolverDeployer.
37  *
38  * @param <T> the expected type
39  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
40  * @version $Revision: 1.1 $
41  */

42 public abstract class JAXPDeployer<T> extends AbstractParsingDeployer<T>
43 {
44    /** Use a namespace aware parser */
45    private boolean useNamespaceAwareParser = true;
46
47    /** A flag indicating if deployment descriptors should be validated */
48    private boolean validateDTDs;
49    
50    /** The document builder factory */
51    private DocumentBuilderFactory JavaDoc documentBuilderFactory;
52    
53    /**
54     * Create a new JAXPDeployer.
55     *
56     * @param deploymentType the deployment type
57     * @throws IllegalArgumentException for a null deployment type
58     */

59    public JAXPDeployer(Class JavaDoc<T> deploymentType)
60    {
61       super(deploymentType);
62    }
63
64    /**
65     * Get the useNamespaceAwareParser.
66     *
67     * @return the useNamespaceAwareParser.
68     */

69    public boolean isUseNamespaceAwareParser()
70    {
71       return useNamespaceAwareParser;
72    }
73
74    /**
75     * Set the useNamespaceAwareParser.
76     *
77     * @param useNamespaceAwareParser the useNamespaceAwareParser.
78     */

79    public void setUseNamespaceAwareParser(boolean useNamespaceAwareParser)
80    {
81       this.useNamespaceAwareParser = useNamespaceAwareParser;
82    }
83
84    /**
85     * Get the validateDTDs.
86     *
87     * @return the validateDTDs.
88     */

89    public boolean isValidateDTDs()
90    {
91       return validateDTDs;
92    }
93
94    /**
95     * Set the validateDTDs.
96     *
97     * @param validateDTDs the validateDTDs.
98     */

99    public void setValidateDTDs(boolean validateDTDs)
100    {
101       this.validateDTDs = validateDTDs;
102    }
103
104    /**
105     * Get the documentBuilderFactory.
106     *
107     * @return the documentBuilderFactory.
108     * @throws IllegalStateException if the create method has not been invoked
109     */

110    protected DocumentBuilderFactory JavaDoc getDocumentBuilderFactory()
111    {
112       if (documentBuilderFactory == null)
113          throw new IllegalStateException JavaDoc("Document builder factory has not been constructed");
114       return documentBuilderFactory;
115    }
116
117    /**
118     * Create lifecycle
119     *
120     * @throws Exception for any problem
121     */

122    public void create() throws Exception JavaDoc
123    {
124       documentBuilderFactory = DocumentBuilderFactory.newInstance();
125       documentBuilderFactory.setNamespaceAware(useNamespaceAwareParser);
126       documentBuilderFactory.setValidating(validateDTDs);
127    }
128
129    /**
130     * Destroy lifecycle
131     */

132    public void destroy()
133    {
134       documentBuilderFactory = null;
135    }
136    
137    /**
138     * Parse a deployment
139     *
140     * @param unit the deployment unit
141     * @param file the metadata file
142     * @param root - possibly null pre-existing root
143     * @return the metadata
144     * @throws Exception for any error
145     */

146    protected T parse(DeploymentUnit unit, VirtualFile file, T root) throws Exception JavaDoc
147    {
148       Document JavaDoc document = doParse(unit, file);
149       return parse(unit, file, document);
150    }
151    
152    /**
153     * Do the parsing
154     *
155     * @param unit the deployment unit
156     * @param file the metadata file
157     * @return the document
158     * @throws Exception for any error
159     */

160    protected Document JavaDoc doParse(DeploymentUnit unit, VirtualFile file) throws Exception JavaDoc
161    {
162       if (file == null)
163          throw new IllegalArgumentException JavaDoc("Null file");
164       
165       InputStream JavaDoc is = file.openStream();
166       try
167       {
168          DocumentBuilder JavaDoc parser = getDocumentBuilderFactory().newDocumentBuilder();
169          InputSource JavaDoc source = new InputSource JavaDoc(is);
170          source.setSystemId(file.toURI().toString());
171          parser.setEntityResolver(new JBossEntityResolver());
172          return parser.parse(is);
173       }
174       finally
175       {
176          try
177          {
178             is.close();
179          }
180          catch (Exception JavaDoc ignored)
181          {
182          }
183       }
184    }
185
186    /**
187     * Parse a deployment
188     *
189     * @param unit the deployment unit
190     * @param file the metadata file
191     * @param document the document
192     * @return the metadata
193     * @throws Exception for any error
194     */

195    protected abstract T parse(DeploymentUnit unit, VirtualFile file, Document JavaDoc document) throws Exception JavaDoc;
196 }
197
Popular Tags