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 org.netbeans.modules.visualweb.project.jsf.api.JsfProjectUtils;
045: import java.io.IOException;
046: import org.openide.ErrorManager;
047:
048: import org.openide.filesystems.FileObject;
049: import org.openide.loaders.DataObject;
050: import org.openide.loaders.UniFileLoader;
051: import org.openide.loaders.MultiDataObject;
052: import org.openide.loaders.DataObjectExistsException;
053: import org.openide.util.NbBundle;
054:
055: /**
056: * Data lodaer of JSF Jsp DataObjects.
057: *
058: * @author Peter Zavadsky
059: */
060: public class JsfJspDataLoader extends UniFileLoader {
061:
062: static final long serialVersionUID = -5809935261731217882L;
063: static final ThreadLocal<Boolean> jspTemplateCreation = new ThreadLocal<Boolean>();
064:
065: public JsfJspDataLoader() {
066: // Use String representation instead of JsfJspDataObject.class.getName() for classloading performance
067: super (
068: "org.netbeans.modules.visualweb.project.jsfloader.JsfJspDataObject"); // NOI18N
069: }
070:
071: protected void initialize() {
072: super .initialize();
073: getExtensions().addExtension("jsp"); // NOI18N
074: getExtensions().addExtension("jspf"); // NOI18N
075:
076: }
077:
078: protected FileObject findPrimaryFile(FileObject fo) {
079: if (fo.isFolder()) {
080: return null;
081: }
082:
083: FileObject primaryFile = super .findPrimaryFile(fo);
084: if (primaryFile == null) {
085: return null;
086: }
087:
088: Object attrib = primaryFile
089: .getAttribute(JsfJspDataObject.JSF_ATTRIBUTE);
090: if (attrib != null && attrib instanceof Boolean) {
091: if (((Boolean) attrib).booleanValue() == true) {
092: return primaryFile;
093: } else {
094: return null;
095: }
096: }
097:
098: // XXX JsfProjectUtils.isJsfProjectFile() is very slow and should be revisited
099: // since this affects all loaded jsp files in NetBeans
100: boolean isTemplate = Utils.isTemplateFileObject(primaryFile);
101: if (!isTemplate
102: && !JsfProjectUtils.isJsfProjectFile(primaryFile)) {
103: return null;
104: }
105:
106: // It is most likely a JSP file, however, we need to see if there is already a JsfJspDataObject registered
107: // for this file object. There is a case where by in middle of refactoring, the JSP and the JAVA file are not
108: // linked as they should be, but there is still a JsfJspDataObject registered uner this file object
109: DataObject dataObject = JsfJavaDataLoader
110: .DataObjectPoolFind(fo);
111: if (dataObject instanceof JsfJspDataObject) {
112: return fo;
113: }
114:
115: // Handle DataObject during template instantiation specially since the
116: // corresponding java file may not have been generated yet
117: if (jspTemplateCreation.get() == Boolean.TRUE) {
118: return primaryFile;
119: }
120:
121: // Now do the normal stuff
122: FileObject javaFile = Utils.findJavaForJsp(primaryFile);
123: if (javaFile == null) {
124: // If there is no java backing file, then this is not a JSF jsp data object.
125: return null;
126: }
127: // It has to be linked vice versa too.
128: if (primaryFile != Utils.findJspForJava(javaFile)) {
129: return null;
130: }
131:
132: if (!isTemplate)
133: setJsfFileAttribute(primaryFile);
134:
135: return primaryFile;
136: }
137:
138: protected MultiDataObject createMultiObject(
139: final FileObject primaryFile)
140: throws DataObjectExistsException, IOException {
141: return new JsfJspDataObject(primaryFile, this );
142: }
143:
144: protected String defaultDisplayName() {
145: return NbBundle.getMessage(JsfJspDataLoader.class,
146: "PROP_JsfJspDataLoader_Name");
147: }
148:
149: protected String actionsContext() {
150: return "Loaders/text/x-jsp/visual/Actions/"; // NOI18N
151: }
152:
153: private void setJsfFileAttribute(FileObject fo) {
154: try {
155: fo.setAttribute(JsfJspDataObject.JSF_ATTRIBUTE,
156: Boolean.TRUE);
157: } catch (IOException ex) {
158: ErrorManager.getDefault().notify(
159: ErrorManager.INFORMATIONAL, ex);
160: }
161: }
162: }
|