KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.sun.enterprise.tools.verifier.Context;
27 import com.sun.enterprise.deployment.WebBundleDescriptor;
28 import com.sun.enterprise.deployment.deploy.shared.FileArchive;
29 import com.sun.enterprise.deployment.util.ModuleDescriptor;
30 import com.sun.enterprise.util.io.FileUtils;
31 import java.io.File JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.List JavaDoc;
35 import org.w3c.dom.Document JavaDoc;
36 import org.xml.sax.InputSource JavaDoc;
37 import javax.xml.parsers.DocumentBuilder JavaDoc;
38 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
39 import org.w3c.dom.NodeList JavaDoc;
40
41 /**
42  *
43  * Class which represents WEB-INF/faces-config.xml
44  *
45  * @author bshankar@sun.com
46  *
47  */

48 public class FacesConfigDescriptor {
49     
50     private final String JavaDoc MANAGED_BEAN_CLASS = "managed-bean-class"; // NOI18N
51
private final String JavaDoc facesConfigFileName = "WEB-INF/faces-config.xml"; // NOI18N
52

53     private Context context;
54     private Document JavaDoc facesConfigDocument;
55     
56     public FacesConfigDescriptor(Context context, WebBundleDescriptor descriptor) {
57         try {
58             this.context = context;
59             readFacesConfigDocument(descriptor);
60         } catch(Exception JavaDoc ex) {
61             facesConfigDocument = null;
62         }
63     }
64     
65     private void readFacesConfigDocument(WebBundleDescriptor webd) throws Exception JavaDoc {
66         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
67         factory.setValidating(false);
68         DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
69         ModuleDescriptor moduleDesc = webd.getModuleDescriptor();
70         String JavaDoc archBase = context.getAbstractArchive().getArchiveUri();
71         String JavaDoc uri = null;
72         if(moduleDesc.isStandalone()){
73             uri = archBase;
74         } else {
75             uri = archBase + File.separator +
76                     FileUtils.makeFriendlyFileName(moduleDesc.getArchiveUri());
77         }
78         FileArchive arch = new FileArchive();
79         arch.open(uri);
80         InputStream JavaDoc is = arch.getEntry(facesConfigFileName);
81         InputSource JavaDoc source = new InputSource JavaDoc(is);
82         try {
83             facesConfigDocument = builder.parse(source);
84         } finally {
85             try{
86                 if(is != null)
87                     is.close();
88             } catch(Exception JavaDoc e) {}
89         }
90     }
91     
92     public List JavaDoc<String JavaDoc> getManagedBeanClasses() {
93         if (facesConfigDocument == null) {
94             return new ArrayList JavaDoc<String JavaDoc>();
95         }
96         NodeList JavaDoc nl = facesConfigDocument.getElementsByTagName(MANAGED_BEAN_CLASS);
97         List JavaDoc<String JavaDoc> classes = new ArrayList JavaDoc<String JavaDoc>();
98         if (nl != null) {
99             int size = nl.getLength();
100             for (int i = 0; i < size; i++) {
101                 classes.add(nl.item(i).getFirstChild().getNodeValue().trim());
102             }
103         }
104         return classes;
105     }
106     
107 }
108
Popular Tags