001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.web.jsf;
043:
044: import java.util.ArrayList;
045: import java.util.Collection;
046: import java.util.Iterator;
047: import org.netbeans.api.project.FileOwnerQuery;
048: import org.netbeans.api.project.Project;
049: import org.netbeans.api.project.ProjectUtils;
050: import org.netbeans.api.project.SourceGroup;
051: import org.netbeans.api.project.Sources;
052: import org.netbeans.modules.j2ee.dd.api.common.InitParam;
053: import org.netbeans.modules.j2ee.dd.api.web.WebApp;
054: import org.netbeans.modules.web.api.webmodule.WebModule;
055: import org.netbeans.modules.web.api.webmodule.WebProjectConstants;
056: import org.netbeans.modules.web.jsf.api.ConfigurationUtils;
057: import org.netbeans.modules.web.jsf.api.facesmodel.FacesConfig;
058: import org.netbeans.modules.web.jsf.api.facesmodel.NavigationRule;
059: import org.openide.filesystems.FileObject;
060: import org.openide.filesystems.FileUtil;
061: import org.netbeans.modules.j2ee.dd.api.web.DDProvider;
062: import org.openide.util.Exceptions;
063:
064: /**
065: *
066: * @author petr
067: * @author Po-Ting Wu
068: */
069: public class JSFConfigUtilities {
070:
071: private static String CONFIG_FILES_PARAM_NAME = "javax.faces.CONFIG_FILES"; //NOI18N
072: private static String DEFAULT_FACES_CONFIG_PATH = "WEB-INF/faces-config.xml"; //NOI18N
073:
074: public static NavigationRule findNavigationRule(
075: JSFConfigDataObject data, String fromView) {
076: NavigationRule navigationRule = null;
077: FacesConfig config = ConfigurationUtils.getConfigModel(
078: data.getPrimaryFile(), true).getRootComponent();
079: Collection<NavigationRule> rules = config.getNavigationRules();
080: for (Iterator<NavigationRule> it = rules.iterator(); it
081: .hasNext();) {
082: NavigationRule nRule = it.next();
083: if ((fromView != null && fromView.equals(nRule
084: .getFromViewId()))
085: || (fromView == null && (nRule.getFromViewId() == null || nRule
086: .getFromViewId().trim().length() == 0))) {
087: navigationRule = nRule;
088: continue;
089: }
090: }
091: return navigationRule;
092: }
093:
094: /** Returns the navigation rule, where the FromViewID is the parameter. If the rule doesn't exist
095: * then returns null.
096: */
097: // public static NavigationRule findNavigationRule(FacesConfig config, String fromView){
098: // if (fromView != null){
099: // FacesConfig config = getConfigModel(data.getPrimaryFile(), true).getRootComponent();
100: // NavigationRule [] rules = config.getNavigationRule();
101: // for (int i = 0; i < rules.length; i++)
102: // if (fromView.equals(rules[i].getFromViewId()))
103: // return rules[i];
104: // }
105: // return null;
106: // }
107: /** Returns WebPages for the project, where the fo is located.
108: */
109: public static SourceGroup[] getDocBaseGroups(FileObject fileObject)
110: throws java.io.IOException {
111: Project proj = FileOwnerQuery.getOwner(fileObject);
112: if (proj == null)
113: return new SourceGroup[] {};
114: Sources sources = ProjectUtils.getSources(proj);
115: return sources
116: .getSourceGroups(WebProjectConstants.TYPE_DOC_ROOT);
117: }
118:
119: public static String getResourcePath(SourceGroup[] groups,
120: FileObject fileObject, char separator, boolean withExt) {
121: for (int i = 0; i < groups.length; i++) {
122: FileObject root = groups[i].getRootFolder();
123: if (FileUtil.isParentOf(root, fileObject)) {
124: String relativePath = FileUtil.getRelativePath(root,
125: fileObject);
126: if (relativePath != null) {
127: if (separator != '/')
128: relativePath = relativePath.replace('/',
129: separator);
130: if (!withExt) {
131: int index = relativePath.lastIndexOf((int) '.');
132: if (index > 0)
133: relativePath = relativePath.substring(0,
134: index);
135: }
136: return relativePath;
137: } else {
138: return "";
139: }
140: }
141: }
142: return "";
143: }
144:
145: public static boolean validateXML(FileObject deploymentDesc) {
146: boolean value = false; // the default value of the com.sun.faces.validateXml
147: if (deploymentDesc != null) {
148: try {
149: WebApp webApp = DDProvider.getDefault().getDDRoot(
150: deploymentDesc);
151: InitParam param = null;
152: if (webApp != null)
153: param = (InitParam) webApp.findBeanByName(
154: "InitParam", "ParamName",
155: "com.sun.faces.validateXml"); //NOI18N
156: if (param != null)
157: value = "true".equals(param.getParamValue().trim()); //NOI18N
158: } catch (java.io.IOException e) {
159: Exceptions.printStackTrace(e);
160: }
161: }
162: return value;
163: }
164:
165: public static boolean verifyObjects(FileObject deploymentDesc) {
166: boolean value = false; // the default value of the com.sun.faces.verifyObjects
167: if (deploymentDesc != null) {
168: try {
169: WebApp webApp = DDProvider.getDefault().getDDRoot(
170: deploymentDesc);
171: InitParam param = null;
172: if (webApp != null)
173: param = (InitParam) webApp.findBeanByName(
174: "InitParam", "ParamName",
175: "com.sun.faces.verifyObjects"); //NOI18N
176: if (param != null)
177: value = "true".equals(param.getParamValue().trim());
178: } catch (java.io.IOException e) {
179: Exceptions.printStackTrace(e);
180: }
181: }
182: return value;
183: }
184:
185: /** Returns relative path for all jsf configuration files in the web module. If there is no
186: * configuration file, then returns String array with lenght = 0.
187: */
188: public static String[] getConfigFiles(WebModule webModule) {
189: if (webModule == null) {
190: return new String[0];
191: }
192:
193: return getConfigFiles(webModule, webModule
194: .getDeploymentDescriptor());
195: }
196:
197: public static String[] getConfigFiles(FileObject deploymentDesc) {
198: if (deploymentDesc == null) {
199: return new String[0];
200: }
201:
202: return getConfigFiles(WebModule.getWebModule(deploymentDesc),
203: deploymentDesc);
204: }
205:
206: public static String[] getConfigFiles(WebModule webModule,
207: FileObject deploymentDesc) {
208: ArrayList<String> files = new ArrayList();
209: String[] filesURI;
210: if (webModule != null) {
211: // looking for WEB-INF/faces-config.xml
212: FileObject baseDir = webModule.getDocumentBase();
213: FileObject fileObject = baseDir
214: .getFileObject(DEFAULT_FACES_CONFIG_PATH);
215: if (fileObject != null)
216: files.add(DEFAULT_FACES_CONFIG_PATH);
217: if (deploymentDesc != null) {
218: InitParam param = null;
219: try {
220: WebApp webApp = DDProvider.getDefault().getDDRoot(
221: deploymentDesc);
222: if (webApp != null) {
223: // Fix for #117845. Cannot be used the method webApp.findBeanByName,
224: // because this method doesn't trim the names of attribute.
225: InitParam[] params = webApp.getContextParam();
226: for (int i = 0; i < params.length; i++) {
227: InitParam initParam = params[i];
228: if (CONFIG_FILES_PARAM_NAME
229: .equals(initParam.getParamName()
230: .trim())) {
231: param = initParam;
232: break;
233: }
234: }
235: }
236: } catch (java.io.IOException e) {
237: Exceptions.printStackTrace(e);
238: }
239:
240: if (param != null) {
241: // the configuration files are defined
242: String value = param.getParamValue().trim();
243: if (value != null) {
244: filesURI = value.split(",");
245: for (int i = 0; i < filesURI.length; i++) {
246: String file = filesURI[i].trim();
247: // prevent to be default faces config twice listed twice
248: if (!DEFAULT_FACES_CONFIG_PATH.equals(file) // need to check absolute and relative path
249: && !("/" + DEFAULT_FACES_CONFIG_PATH)
250: .equals(file)) { //NOI18N
251: files.add(file);
252: }
253: }
254: }
255: }
256: }
257: }
258: filesURI = new String[files.size()];
259: return files.toArray(filesURI);
260: }
261: }
|