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-2007 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.visualweb.project.jsf.framework;
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 Po-Ting Wu
067: */
068: public class JSFConfigUtilities {
069:
070: private static String CONFIG_FILES_PARAM_NAME = "javax.faces.CONFIG_FILES"; //NOI18N
071: private static String DEFAULT_FACES_CONFIG_PATH = "WEB-INF/faces-config.xml"; //NOI18N
072:
073: /* <RAVE> Not needed and comment out because of No access to JSFConfigDataObject
074: public static NavigationRule findNavigationRule(JSFConfigDataObject data, String fromView){
075: NavigationRule navigationRule = null;
076: FacesConfig config = ConfigurationUtils.getConfigModel(data.getPrimaryFile(), true).getRootComponent();
077: Collection<NavigationRule> rules = config.getNavigationRules();
078: for (Iterator<NavigationRule> it = rules.iterator(); it.hasNext();) {
079: NavigationRule nRule = it.next();
080: if ((fromView != null && fromView.equals(nRule.getFromViewId()))
081: || (fromView == null && (nRule.getFromViewId() == null || nRule.getFromViewId().trim().length()==0))){
082: navigationRule = nRule;
083: continue;
084: }
085: }
086: return navigationRule;
087: }
088: </RAVE> */
089:
090: /** Returns the navigation rule, where the FromViewID is the parameter. If the rule doesn't exist
091: * then returns null.
092: */
093: // public static NavigationRule findNavigationRule(FacesConfig config, String fromView){
094: // if (fromView != null){
095: // FacesConfig config = getConfigModel(data.getPrimaryFile(), true).getRootComponent();
096: // NavigationRule [] rules = config.getNavigationRule();
097: // for (int i = 0; i < rules.length; i++)
098: // if (fromView.equals(rules[i].getFromViewId()))
099: // return rules[i];
100: // }
101: // return null;
102: // }
103: /** Returns WebPages for the project, where the fo is located.
104: */
105: public static SourceGroup[] getDocBaseGroups(FileObject fileObject)
106: throws java.io.IOException {
107: Project proj = FileOwnerQuery.getOwner(fileObject);
108: if (proj == null)
109: return new SourceGroup[] {};
110: Sources sources = ProjectUtils.getSources(proj);
111: return sources
112: .getSourceGroups(WebProjectConstants.TYPE_DOC_ROOT);
113: }
114:
115: public static String getResourcePath(SourceGroup[] groups,
116: FileObject fileObject, char separator, boolean withExt) {
117: for (int i = 0; i < groups.length; i++) {
118: FileObject root = groups[i].getRootFolder();
119: if (FileUtil.isParentOf(root, fileObject)) {
120: String relativePath = FileUtil.getRelativePath(root,
121: fileObject);
122: if (relativePath != null) {
123: if (separator != '/')
124: relativePath = relativePath.replace('/',
125: separator);
126: if (!withExt) {
127: int index = relativePath.lastIndexOf((int) '.');
128: if (index > 0)
129: relativePath = relativePath.substring(0,
130: index);
131: }
132: return relativePath;
133: } else {
134: return "";
135: }
136: }
137: }
138: return "";
139: }
140:
141: public static boolean validateXML(FileObject deploymentDesc) {
142: boolean value = false; // the default value of the com.sun.faces.validateXml
143: if (deploymentDesc != null) {
144: try {
145: WebApp webApp = DDProvider.getDefault().getDDRoot(
146: deploymentDesc);
147: InitParam param = null;
148: if (webApp != null)
149: param = (InitParam) webApp.findBeanByName(
150: "InitParam", "ParamName",
151: "com.sun.faces.validateXml"); //NOI18N
152: if (param != null)
153: value = "true".equals(param.getParamValue().trim()); //NOI18N
154: } catch (java.io.IOException e) {
155: Exceptions.printStackTrace(e);
156: }
157: }
158: return value;
159: }
160:
161: public static boolean verifyObjects(FileObject deploymentDesc) {
162: boolean value = false; // the default value of the com.sun.faces.verifyObjects
163: if (deploymentDesc != null) {
164: try {
165: WebApp webApp = DDProvider.getDefault().getDDRoot(
166: deploymentDesc);
167: InitParam param = null;
168: if (webApp != null)
169: param = (InitParam) webApp.findBeanByName(
170: "InitParam", "ParamName",
171: "com.sun.faces.verifyObjects"); //NOI18N
172: if (param != null)
173: value = "true".equals(param.getParamValue().trim());
174: } catch (java.io.IOException e) {
175: Exceptions.printStackTrace(e);
176: }
177: }
178: return value;
179: }
180:
181: /** Returns relative path for all jsf configuration files in the web module. If there is no
182: * configuration file, then returns String array with lenght = 0.
183: */
184: public static String[] getConfigFiles(FileObject deploymentDesc) {
185: ArrayList<String> files = new ArrayList();
186: String[] filesURI;
187: // looking for WEB-INF/faces-config.xml
188: WebModule webModule = null;
189: if (deploymentDesc != null) {
190: webModule = WebModule.getWebModule(deploymentDesc);
191: }
192: if (webModule != null) {
193: FileObject baseDir = webModule.getDocumentBase();
194: FileObject fileObject = baseDir
195: .getFileObject(DEFAULT_FACES_CONFIG_PATH);
196: if (fileObject != null)
197: files.add(DEFAULT_FACES_CONFIG_PATH);
198: if (deploymentDesc != null) {
199: InitParam param = null;
200: try {
201: WebApp webApp = DDProvider.getDefault().getDDRoot(
202: deploymentDesc);
203: if (webApp != null) {
204: // Fix for #117845. Cannot be used the method webApp.findBeanByName,
205: // because this method doesn't trim the names of attribute.
206: InitParam[] params = webApp.getContextParam();
207: for (int i = 0; i < params.length; i++) {
208: InitParam initParam = params[i];
209: if (CONFIG_FILES_PARAM_NAME
210: .equals(initParam.getParamName()
211: .trim())) {
212: param = initParam;
213: break;
214: }
215: }
216: }
217: } catch (java.io.IOException e) {
218: Exceptions.printStackTrace(e);
219: }
220:
221: if (param != null) {
222: // the configuration files are defined
223: String value = param.getParamValue().trim();
224: if (value != null) {
225: filesURI = value.split(",");
226: for (int i = 0; i < filesURI.length; i++) {
227: String file = filesURI[i].trim();
228: // prevent to be default faces config twice listed twice
229: if (!DEFAULT_FACES_CONFIG_PATH.equals(file) // need to check absolute and relative path
230: && !("/" + DEFAULT_FACES_CONFIG_PATH)
231: .equals(file)) { //NOI18N
232: files.add(file);
233: }
234: }
235: }
236: }
237: }
238: }
239: filesURI = new String[files.size()];
240: return files.toArray(filesURI);
241: }
242: }
|