KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > web > TagLibFactory


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.tools.verifier.web;
25
26 import javax.xml.parsers.DocumentBuilder JavaDoc;
27 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
28 import javax.xml.parsers.ParserConfigurationException JavaDoc;
29
30 import java.io.File JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.Enumeration JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.logging.Level JavaDoc;
37 import java.util.logging.LogRecord JavaDoc;
38 import java.util.logging.Logger JavaDoc;
39
40 import org.w3c.dom.Document JavaDoc;
41 import org.w3c.dom.NodeList JavaDoc;
42 import org.xml.sax.EntityResolver JavaDoc;
43 import org.xml.sax.ErrorHandler JavaDoc;
44 import org.xml.sax.InputSource JavaDoc;
45 import org.xml.sax.SAXException JavaDoc;
46 import org.xml.sax.SAXParseException JavaDoc;
47 import tools.com.sun.enterprise.util.XMLValidationHandler;
48
49 import com.sun.enterprise.deployment.TagLibConfigurationDescriptor;
50 import com.sun.enterprise.deployment.WebBundleDescriptor;
51 import com.sun.enterprise.deployment.util.ModuleDescriptor;
52 import com.sun.enterprise.deployment.deploy.shared.FileArchive;
53 import com.sun.enterprise.logging.LogDomains;
54 import com.sun.enterprise.tools.verifier.Context;
55 import com.sun.enterprise.tools.verifier.FrameworkContext;
56 import com.sun.enterprise.tools.verifier.StringManagerHelper;
57 import com.sun.enterprise.tools.verifier.TagLibDescriptor;
58 import com.sun.enterprise.util.LocalStringManagerImpl;
59 import com.sun.enterprise.util.io.FileUtils;
60
61 /** This is the factory class used for obtainig the TagLibDescriptor objects for
62  * the tag libraries defined in the war archive.
63  *
64  * @author Sudipto Ghosh
65  */

66
67 public class TagLibFactory {
68
69     private DocumentBuilder JavaDoc builder;
70     private Context context;
71     private FrameworkContext frameworkContext;
72     private Logger JavaDoc logger = LogDomains.getLogger(
73             LogDomains.AVK_VERIFIER_LOGGER);
74     private boolean uninitialised = false;
75     private final LocalStringManagerImpl smh = StringManagerHelper.getLocalStringsManager();
76
77
78     /**
79      * Constructor to create a factory object.
80      *
81      * @param context
82      * @param frameworkContext
83      */

84     public TagLibFactory(Context context, FrameworkContext frameworkContext) {
85         this.context = context;
86         this.frameworkContext = frameworkContext;
87     }
88
89     /**
90      * This method is responsible for creating a document object from the tld
91      * files and then returning the array TagLibDescriptor objects based on tld
92      * version.
93      *
94      * @param descriptor the web bundle descriptor giving the tlds contained
95      * in the war.
96      * @return the tag lib descriptor array for all the tlds defined in the war.
97      */

98     public TagLibDescriptor[] getTagLibDescriptors(
99             WebBundleDescriptor descriptor) {
100         ArrayList JavaDoc<TagLibDescriptor> tmp = new ArrayList JavaDoc<TagLibDescriptor>();
101         Enumeration JavaDoc taglibConfig = null;
102
103         if (descriptor.getJspConfigDescriptor() != null) {
104             taglibConfig = descriptor.getJspConfigDescriptor().getTagLibs();
105         } else {
106             return null;
107         }
108         init();
109         while (taglibConfig.hasMoreElements()) {
110             // test all the Tag lib descriptors.
111
TagLibConfigurationDescriptor taglibDescriptor = (TagLibConfigurationDescriptor) taglibConfig.nextElement();
112
113             String JavaDoc taglibLocation = taglibDescriptor.getTagLibLocation();
114             Document JavaDoc d = null;
115             try {
116                 d = createDocument(taglibLocation, descriptor);
117             } catch (Exception JavaDoc e) {
118                 logger.log(Level.WARNING, smh.getLocalString
119                         (getClass().getName() + ".exception", // NOI18N
120
"Continuing, though problem in creating taglib document. Cause: {0}", // NOI18N
121
new Object JavaDoc[]{e.getMessage()}));
122                 if (e instanceof SAXParseException JavaDoc) {
123                     LogRecord JavaDoc logRecord = new LogRecord JavaDoc(Level.SEVERE,
124                                             smh.getLocalString
125                                                 (getClass().getName() + ".exception2", // NOI18N
126
"XML Error line : {0} in [ {1} ]. {2}", // NOI18N
127
new Object JavaDoc[] {((SAXParseException JavaDoc) e).getLineNumber(),
128                                                         taglibLocation,
129                                                         e.getLocalizedMessage()}));
130                     logRecord.setThrown(e);
131                     frameworkContext.getResultManager().log(logRecord);
132                 }
133                 continue; // we are continuing with creating the next document.
134
}
135             String JavaDoc version = getTLDSpecVersion(d);
136             TagLibDescriptor taglib = null;
137             taglib = new TagLibDescriptor(d, version, taglibLocation);
138             tmp.add(taglib);
139         }
140
141         int count = tmp.size();
142         TagLibDescriptor arr[] = new TagLibDescriptor[count];
143         int i = 0;
144         for (Iterator JavaDoc e = tmp.iterator(); e.hasNext(); i++) {
145             arr[i] = (TagLibDescriptor) e.next();
146         }
147         return arr;
148     }
149
150     /**
151      * this method is called once to create the Documentbuilder required for
152      * creating documen objects from tlds
153      */

154
155     private void init() {
156         if (!uninitialised) {
157             synchronized (this) {
158                 if (!uninitialised) {
159                     DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
160                     String JavaDoc W3C_XML_SCHEMA = "http://www.w3c.org/2001/XMLSchema"; // NOI18N
161
String JavaDoc JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; // NOI18N
162
factory.setNamespaceAware(true);
163                     factory.setValidating(true);
164                     factory.setAttribute(
165                             "http://apache.org/xml/features/validation/schema", // NOI18N
166
Boolean.TRUE);
167                     factory.setAttribute(
168                             "http://xml.org/sax/features/validation", // NOI18N
169
Boolean.TRUE);
170                     factory.setAttribute(
171                             "http://apache.org/xml/features/allow-java-encodings", // NOI18N
172
Boolean.TRUE);
173                     factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
174                     try {
175                         builder = factory.newDocumentBuilder();
176                     } catch (ParserConfigurationException JavaDoc e) {
177                         logger.log(Level.SEVERE, e.getMessage());
178                     }
179                     EntityResolver JavaDoc dh = new XMLValidationHandler(true);
180                     ErrorHandler JavaDoc eh = new XMLValidationHandler(true);
181                     builder.setErrorHandler(eh);
182                     builder.setEntityResolver(dh);
183                     uninitialised = true;
184                 }
185             }
186         }
187     }
188
189     /**
190      * Helper method to create the document object from the tld files specified
191      * by the location in web.xml of the war archive.
192      *
193      * @param location location of jsp tlds
194      * @param webd has all the information about the web.xml of the war archive
195      * @return document object created from tlds specified at the given location
196      * @throws IOException
197      * @throws SAXException
198      */

199     private Document JavaDoc createDocument(String JavaDoc location, WebBundleDescriptor webd)
200             throws IOException JavaDoc, SAXException JavaDoc {
201         Document JavaDoc document = null;
202         InputSource JavaDoc source = null;
203         if (location.startsWith("/")) // NOI18N
204
location = location.substring(1);
205         else
206             location = "WEB-INF/" + location; // NOI18N
207
ModuleDescriptor moduleDesc = webd.getModuleDescriptor();
208         String JavaDoc archBase = context.getAbstractArchive().getArchiveUri();
209         String JavaDoc uri = null;
210         if(moduleDesc.isStandalone()){
211             uri = archBase;
212         } else {
213             uri = archBase + File.separator +
214                 FileUtils.makeFriendlyFileName(moduleDesc.getArchiveUri());
215         }
216         FileArchive arch = new FileArchive();
217         arch.open(uri);
218         InputStream JavaDoc is = arch.getEntry(location);
219         try {
220         // is can be null if wrong location of tld is specified in web.xml
221
if (is == null)
222                 throw new IOException JavaDoc(smh.getLocalString
223                         (getClass().getName() + ".exception1", // NOI18N
224
"Wrong tld [ {0} ] specified in the web.xml of [ {1} ]", // NOI18N
225
new Object JavaDoc[]{location, moduleDesc.getArchiveUri()}));
226             source = new InputSource JavaDoc(is);
227             document = builder.parse(source);
228         } finally {
229             try{
230                 if(is != null)
231                     is.close();
232             } catch(Exception JavaDoc e) {}
233         }
234
235         return document;
236     }
237
238     /**
239      * Returns the spec version of the tag lib descriptor file.
240      *
241      * @param doc document object created from tlds
242      * @return spec version of the tag lib descriptor file defined by this doc.
243      */

244     private String JavaDoc getTLDSpecVersion(Document JavaDoc doc) {
245         String JavaDoc str = null;
246         if (doc.getDoctype() != null) {
247             NodeList JavaDoc nl = doc.getElementsByTagName("jsp-version"); // NOI18N
248
if (nl != null && nl.getLength() != 0) {
249                 str = nl.item(0).getFirstChild().getNodeValue(); // web-jsptaglib dtd vesion 1.2
250
} else {
251                 nl = doc.getElementsByTagName("jspversion"); // NOI18N
252
if (nl.getLength() != 0) {
253                     str = nl.item(0).getFirstChild().getNodeValue(); // web-jsptaglib dtd vesion 1.1
254
} else
255                     return "1.1"; // NOI18N
256
}
257         } else
258             str = doc.getElementsByTagName("taglib").item(0).getAttributes() // NOI18N
259
.getNamedItem("version") // NOI18N
260
.getNodeValue();
261
262         return str;
263     }
264 }
265
Popular Tags