KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > jsf > JSFConfigUtilities


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.web.jsf;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import org.netbeans.api.project.FileOwnerQuery;
26 import org.netbeans.api.project.Project;
27 import org.netbeans.api.project.ProjectUtils;
28 import org.netbeans.api.project.SourceGroup;
29 import org.netbeans.api.project.Sources;
30 import org.netbeans.modules.j2ee.dd.api.common.InitParam;
31 import org.netbeans.modules.j2ee.dd.api.web.WebApp;
32 import org.netbeans.modules.web.api.webmodule.WebModule;
33 import org.netbeans.modules.web.api.webmodule.WebProjectConstants;
34 import org.netbeans.modules.web.jsf.api.ConfigurationUtils;
35 import org.netbeans.modules.web.jsf.api.facesmodel.FacesConfig;
36 import org.netbeans.modules.web.jsf.api.facesmodel.NavigationRule;
37 import org.openide.ErrorManager;
38 import org.openide.filesystems.FileObject;
39 import org.openide.filesystems.FileUtil;
40 import org.netbeans.modules.j2ee.dd.api.web.DDProvider;
41
42 /**
43  *
44  * @author petr
45  */

46 public class JSFConfigUtilities {
47     
48     
49     
50     
51     public static NavigationRule findNavigationRule(JSFConfigDataObject data, String JavaDoc fromView){
52         NavigationRule navigationRule = null;
53         FacesConfig config = ConfigurationUtils.getConfigModel(data.getPrimaryFile(), true).getRootComponent();
54         Collection JavaDoc<NavigationRule> rules = config.getNavigationRules();
55         for (Iterator JavaDoc<NavigationRule> it = rules.iterator(); it.hasNext();) {
56             NavigationRule nRule = it.next();
57             if (fromView.equals(nRule.getFromViewId())){
58                 navigationRule = nRule;
59                 continue;
60             }
61         }
62         return navigationRule;
63     }
64     
65     /** Returns the navigation rule, where the FromViewID is the parameter. If the rule doesn't exist
66      * then returns null.
67      */

68     // public static NavigationRule findNavigationRule(FacesConfig config, String fromView){
69
// if (fromView != null){
70
// FacesConfig config = getConfigModel(data.getPrimaryFile(), true).getRootComponent();
71
// NavigationRule [] rules = config.getNavigationRule();
72
// for (int i = 0; i < rules.length; i++)
73
// if (fromView.equals(rules[i].getFromViewId()))
74
// return rules[i];
75
// }
76
// return null;
77
// }
78

79     /** Returns WebPages for the project, where the fo is located.
80      */

81     public static SourceGroup[] getDocBaseGroups(FileObject fileObject) throws java.io.IOException JavaDoc {
82         Project proj = FileOwnerQuery.getOwner(fileObject);
83         if (proj==null) return new SourceGroup[]{};
84         Sources sources = ProjectUtils.getSources(proj);
85         return sources.getSourceGroups(WebProjectConstants.TYPE_DOC_ROOT);
86     }
87     
88     public static String JavaDoc getResourcePath(SourceGroup[] groups,FileObject fileObject, char separator, boolean withExt) {
89         for (int i=0;i<groups.length;i++) {
90             FileObject root = groups[i].getRootFolder();
91             if (FileUtil.isParentOf(root,fileObject)) {
92                 String JavaDoc relativePath = FileUtil.getRelativePath(root,fileObject);
93                 if (relativePath!=null) {
94                     if (separator!='/') relativePath = relativePath.replace('/',separator);
95                     if (!withExt) {
96                         int index = relativePath.lastIndexOf((int)'.');
97                         if (index>0) relativePath = relativePath.substring(0,index);
98                     }
99                     return relativePath;
100                 } else {
101                     return "";
102                 }
103             }
104         }
105         return "";
106     }
107     
108     
109     
110     
111     
112     public static boolean validateXML(FileObject deploymentDesc){
113         boolean value = false; // the default value of the com.sun.faces.validateXml
114
if (deploymentDesc != null){
115             try{
116                 WebApp webApp = DDProvider.getDefault().getDDRoot(deploymentDesc);
117                 InitParam param = null;
118                 if (webApp != null)
119                     param = (InitParam)webApp.findBeanByName("InitParam", "ParamName", "com.sun.faces.validateXml"); //NOI18N
120
if (param != null)
121                     value = "true".equals(param.getParamValue().trim()); //NOI18N
122
} catch (java.io.IOException JavaDoc e) {
123                 ErrorManager.getDefault().notify(e);
124             }
125         }
126         return value;
127     }
128     
129     public static boolean verifyObjects(FileObject deploymentDesc){
130         boolean value = false; // the default value of the com.sun.faces.verifyObjects
131
if (deploymentDesc != null){
132             try{
133                 WebApp webApp = DDProvider.getDefault().getDDRoot(deploymentDesc);
134                 InitParam param = null;
135                 if (webApp != null)
136                     param = (InitParam)webApp.findBeanByName("InitParam", "ParamName", "com.sun.faces.verifyObjects"); //NOI18N
137
if (param != null)
138                     value = "true".equals(param.getParamValue().trim());
139             } catch (java.io.IOException JavaDoc e) {
140                 ErrorManager.getDefault().notify(e);
141             }
142         }
143         return value;
144     }
145     
146     /** Returns relative path for all jsf configuration files in the web module. If there is no
147      * configuration file, then returns String array with lenght = 0.
148      */

149     public static String JavaDoc[] getConfigFiles(FileObject deploymentDesc){
150         ArrayList JavaDoc<String JavaDoc> files = new ArrayList JavaDoc();
151         String JavaDoc[] filesURI;
152         if (deploymentDesc != null){
153             InitParam param = null;
154             try{
155                 WebApp webApp = DDProvider.getDefault().getDDRoot(deploymentDesc);
156                 if (webApp != null)
157                     param = (InitParam)webApp.findBeanByName("InitParam", "ParamName", "javax.faces.CONFIG_FILES"); //NOI18N
158
} catch (java.io.IOException JavaDoc e) {
159                 ErrorManager.getDefault().notify(e);
160             }
161             
162             if (param != null){
163                 // the configuration files are defined
164
String JavaDoc value = param.getParamValue().trim();
165                 if (value != null){
166                     filesURI = value.split(",");
167                     for (int i = 0; i < filesURI.length; i++)
168                         files.add(filesURI[i].trim());
169                 }
170             }
171             // looking for WEB-INF/faces-config.xml
172
WebModule webModule = WebModule.getWebModule(deploymentDesc);
173             FileObject baseDir = webModule.getDocumentBase();
174             FileObject fileObject = baseDir.getFileObject("WEB-INF/faces-config.xml");
175             if (fileObject != null)
176                 files.add("WEB-INF/faces-config.xml");
177         }
178         filesURI = new String JavaDoc[files.size()];
179         return files.toArray(filesURI);
180     }
181 }
182
Popular Tags