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.jsfloader;
043:
044: import java.beans.PropertyVetoException;
045: import org.netbeans.modules.visualweb.project.jsf.api.JsfProjectUtils;
046:
047: import org.openide.ErrorManager;
048: import org.openide.filesystems.FileObject;
049: import org.openide.filesystems.FileUtil;
050: import org.openide.filesystems.Repository;
051: import org.openide.loaders.DataObject;
052: import org.openide.loaders.DataObjectNotFoundException;
053:
054: /**
055: * Utilities class for this package.
056: *
057: * @author Peter Zavadsky
058: */
059: final class Utils {
060:
061: private Utils() {
062: }
063:
064: /** Finds corresponding 'backing' java fileobject for specified jsp file objecjt.
065: * @return <code>FileObject</code> of corresponding java backing file or <code>null</code> if doesn't exist */
066: public static FileObject findJavaForJsp(FileObject jsfFileObject) {
067: if (jsfFileObject == null) {
068: return null;
069: }
070:
071: if (isTemplateFileObject(jsfFileObject)) {
072: // If this is a template, then the backing file will be in the same dir.
073: FileObject folder = jsfFileObject.getParent();
074: return folder
075: .getFileObject(jsfFileObject.getName(), "java"); // NOI18N
076: } else {
077: return JsfProjectUtils.getJavaForJsp(jsfFileObject);
078: }
079: }
080:
081: /** Finds corresponding jsp java file object for specified java 'backing' file object.
082: * @return <code>FileObject</code> of corresponding java backing file or <code>null</code> if doesn't exist */
083: public static FileObject findJspForJava(FileObject javaFileObject) {
084: if (javaFileObject == null) {
085: return null;
086: }
087:
088: if (isTemplateFileObject(javaFileObject)) {
089: // If this is a template, then the backing file will be in the same dir.
090: FileObject folder = javaFileObject.getParent();
091: FileObject jspFileObject = folder.getFileObject(
092: javaFileObject.getName(), "jsp"); // NOI18N
093: if (jspFileObject != null) {
094: return jspFileObject;
095: }
096: return folder.getFileObject(javaFileObject.getName(),
097: "jspf"); // NOI18N
098: } else {
099: return JsfProjectUtils.getJspForJava(javaFileObject);
100: }
101: }
102:
103: /** Finds corresponding jsp java folder object for specified jsp file object.
104: * @return <code>FileObject</code> of corresponding java backing file or <code>null</code> if doesn't exist */
105: public static FileObject findJavaFolderForJsp(
106: FileObject jspFileObject) {
107: if (jspFileObject == null) {
108: return null;
109: }
110:
111: if (isTemplateFileObject(jspFileObject)) {
112: return jspFileObject.getParent();
113: } else {
114: return JsfProjectUtils.getJavaFolderForJsp(jspFileObject);
115: }
116: }
117:
118: /** Finds corresponding jsf jsp folder object for specified java 'backing' file object.
119: * @return <code>FileObject</code> of corresponding java backing file or <code>null</code> if doesn't exist */
120: public static FileObject findJspFolderForJava(
121: FileObject javaFileObject) {
122: if (javaFileObject == null) {
123: return null;
124: }
125:
126: if (isTemplateFileObject(javaFileObject)) {
127: return javaFileObject.getParent();
128: } else {
129: return JsfProjectUtils.getJspFolderForJava(javaFileObject);
130: }
131: }
132:
133: public static boolean isTemplateFileObject(FileObject fo) {
134: Object o = fo.getAttribute(DataObject.PROP_TEMPLATE);
135: boolean hasTemplateAttribute;
136: if (o instanceof Boolean) {
137: hasTemplateAttribute = ((Boolean) o).booleanValue();
138: } else {
139: hasTemplateAttribute = false;
140: }
141:
142: if (hasTemplateAttribute) {
143: return true;
144: }
145:
146: FileObject templatesFolder = Repository.getDefault()
147: .getDefaultFileSystem().getRoot().getFileObject(
148: "Templates"); // NOI18N
149: if (templatesFolder != null) {
150: return FileUtil.isParentOf(templatesFolder, fo);
151: } else {
152: return false;
153: }
154: }
155:
156: /** Finds corresponding Jsf Jsp data object to the specified java 'backing' file object.
157: * @param quietly TODO
158: * @return <code>JsfJspDataObject</code> instance of <code>null</code> if it doesn't exist */
159: public static JsfJspDataObject findCorrespondingJsfJspDataObject(
160: FileObject jsfJavaFileObject, boolean quietly) {
161: FileObject jspFile = Utils.findJspForJava(jsfJavaFileObject);
162: if (jspFile == null) {
163: if (!quietly)
164: ErrorManager.getDefault().notify(
165: ErrorManager.INFORMATIONAL,
166: new IllegalStateException(
167: "Can't find corresponding jsp file to "
168: + jsfJavaFileObject)); // NOI18N
169: return null;
170: } else {
171: try {
172: DataObject dob = DataObject.find(jspFile);
173: if (dob instanceof JsfJspDataObject) {
174: return (JsfJspDataObject) dob;
175: } else {
176: try {
177: dob.setValid(false);
178: dob = DataObject.find(jspFile);
179: } catch (PropertyVetoException ex) {
180: ErrorManager.getDefault().notify(
181: ErrorManager.INFORMATIONAL,
182: new IllegalStateException(
183: "Corresponding jsp data object is not jsf "
184: + dob)); // NOI18N
185: }
186:
187: if (!quietly)
188: ErrorManager.getDefault().notify(
189: ErrorManager.INFORMATIONAL,
190: new IllegalStateException(
191: "Corresponding jsp data object is not jsf "
192: + dob)); // NOI18N
193: return null;
194: }
195: } catch (DataObjectNotFoundException dnfe) {
196: if (!quietly)
197: ErrorManager.getDefault().notify(
198: ErrorManager.INFORMATIONAL, dnfe);
199: return null;
200: }
201: }
202: }
203:
204: /** Finds corresponding Jsf Jsp editor support to the specified java 'backing' file object.
205: * @param quietly TODO
206: * @return <code>JsfJspEditorSupport</code> instance of <code>null</code> if it doesn't exist */
207: public static JsfJspEditorSupport findCorrespondingJsfJspEditorSupport(
208: FileObject jsfJavaFileObject, boolean quietly) {
209: JsfJspDataObject jsfJspDataObject = findCorrespondingJsfJspDataObject(
210: jsfJavaFileObject, false);
211: if (jsfJspDataObject == null) {
212: ErrorManager.getDefault().notify(
213: ErrorManager.INFORMATIONAL,
214: new IllegalStateException(
215: "Can't find jsp data object to "
216: + jsfJavaFileObject)); // NOI18N
217: return null;
218: } else {
219: return (JsfJspEditorSupport) jsfJspDataObject
220: .getCookie(JsfJspEditorSupport.class);
221: }
222: }
223:
224: /** Finds corresponding Jsf Java data object to the specified jsp file object.
225: * @param quietly TODO
226: * @return <code>JsfJspEditorSupport</code> instance of <code>null</code> if it doesn't exist */
227: public static JsfJavaDataObject findCorrespondingJsfJavaDataObject(
228: FileObject jsfJspFileObject, boolean quietly) {
229: FileObject javaFile = Utils.findJavaForJsp(jsfJspFileObject);
230: JsfJavaDataObject jsfJavaDataObject;
231: if (javaFile == null) {
232: if (!quietly)
233: ErrorManager.getDefault().notify(
234: ErrorManager.INFORMATIONAL,
235: new IllegalStateException(
236: "Can't find corresponding java file to "
237: + jsfJspFileObject)); // NOI18N
238: return null;
239: } else {
240: try {
241: DataObject dob = DataObject.find(javaFile);
242: if (dob instanceof JsfJavaDataObject) {
243: return (JsfJavaDataObject) dob;
244: } else {
245: try {
246: dob.setValid(false);
247: dob = DataObject.find(javaFile);
248: } catch (PropertyVetoException ex) {
249: ErrorManager.getDefault().notify(
250: ErrorManager.INFORMATIONAL,
251: new IllegalStateException(
252: "Corresponding java data object is not jsf "
253: + dob)); // NOI18N
254: }
255:
256: return (dob instanceof JsfJavaDataObject) ? (JsfJavaDataObject) dob
257: : null;
258: }
259: } catch (DataObjectNotFoundException dnfe) {
260: ErrorManager.getDefault().notify(
261: ErrorManager.INFORMATIONAL, dnfe);
262: return null;
263: }
264: }
265: }
266:
267: /** Finds corresponding Java 'backing' editor support to the specified jsf jsp file object.
268: * @param quietly TODO
269: * @return <code>JsfJavaEditorSupport</code> instance of <code>null</code> if it doesn't exist */
270: public static JsfJavaEditorSupport findCorrespondingJsfJavaEditorSupport(
271: FileObject jsfJspFileObject, boolean quietly) {
272: JsfJavaDataObject jsfJavaDataObject = findCorrespondingJsfJavaDataObject(
273: jsfJspFileObject, quietly);
274: if (jsfJavaDataObject == null) {
275: if (!quietly)
276: ErrorManager.getDefault().notify(
277: ErrorManager.INFORMATIONAL,
278: new IllegalStateException(
279: "Can't find java data object to "
280: + jsfJspFileObject)); // NOI18N
281: return null;
282: } else {
283: return (JsfJavaEditorSupport) jsfJavaDataObject
284: .getCookie(JsfJavaEditorSupport.class);
285: }
286: }
287:
288: /**
289: * Return the logical bean name of a jsp.
290: */
291: public static final String getBeanNameForJsp(FileObject jspFile) {
292: String beanName = JsfProjectUtils.getBasePathForJsp(jspFile);
293: if (beanName == null)
294: return null;
295: if (beanName.length() == 0)
296: return beanName;
297: if (beanName.charAt(0) == '/')
298: beanName = beanName.substring(1);
299: beanName = beanName.replace('/', '$');
300: return beanName;
301: }
302: }
|