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.web.jsf.refactoring;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import java.net.URISyntaxException;
047: import java.net.URL;
048: import java.net.URLDecoder;
049: import java.util.List;
050: import java.util.logging.Level;
051: import java.util.logging.Logger;
052: import org.netbeans.api.java.classpath.ClassPath;
053: import org.netbeans.api.java.source.ClasspathInfo;
054: import org.netbeans.api.java.source.CompilationController;
055: import org.netbeans.api.java.source.CompilationInfo;
056: import org.netbeans.api.java.source.JavaSource;
057: import org.netbeans.api.java.source.Task;
058: import org.netbeans.modules.refactoring.api.AbstractRefactoring;
059: import org.netbeans.modules.refactoring.spi.RefactoringElementsBag;
060: import org.netbeans.modules.web.api.webmodule.WebModule;
061: import org.openide.filesystems.FileObject;
062: import org.openide.filesystems.FileUtil;
063:
064: /**
065: *
066: * @author Petr Pisl
067: */
068: public class JSFRefactoringUtils {
069:
070: private static final Logger LOGGER = Logger
071: .getLogger(JSFRefactoringUtils.class.getName());
072:
073: private JSFRefactoringUtils() {
074: }
075:
076: //TODO this is copy from org.netbeans.modules.refactoring.java.RetoucheUtils
077: //Probably this methods will be moved to an api
078: public static String getPackageName(FileObject folder) {
079: assert folder.isFolder() : "argument must be folder"; //NOI18N
080: return ClassPath.getClassPath(folder, ClassPath.SOURCE)
081: .getResourceName(folder, '.', false);
082: }
083:
084: //TODO this is copy from org.netbeans.modules.refactoring.java.RetoucheUtils
085: //Probably this methods will be moved to an api
086: public static String getPackageName(URL url) {
087: File file = null;
088: try {
089: file = FileUtil.normalizeFile(new File(url.toURI()));
090: } catch (URISyntaxException uRISyntaxException) {
091: throw new IllegalArgumentException(
092: "Cannot create package name for url " + url); //NOI18N
093: }
094: String suffix = "";
095:
096: do {
097: FileObject fileObject = FileUtil.toFileObject(file);
098: if (fileObject != null) {
099: if ("".equals(suffix))
100: return getPackageName(fileObject);
101: String prefix = getPackageName(fileObject);
102: return prefix + ("".equals(prefix) ? "" : ".") + suffix;
103: }
104: if (!"".equals(suffix)) {
105: suffix = "." + suffix;
106: }
107: suffix = URLDecoder
108: .decode(file.getPath().substring(
109: file.getPath().lastIndexOf(
110: File.separatorChar) + 1))
111: + suffix;
112: file = file.getParentFile();
113: } while (file != null);
114: throw new IllegalArgumentException(
115: "Cannot create package name for url " + url); //NOI18N
116: }
117:
118: public static boolean containsRenamingPackage(String oldFQCN,
119: String oldPackage, boolean renameSubpackages) {
120: boolean contains = false;
121: if (oldFQCN != null && oldPackage != null) {
122: if (!renameSubpackages) {
123: if (oldFQCN.startsWith(oldPackage)
124: && oldFQCN.substring(oldPackage.length() + 1)
125: .indexOf('.') < 0
126: && oldFQCN.substring(oldPackage.length())
127: .charAt(0) == '.') {
128: contains = true;
129: }
130: } else {
131: if (oldFQCN.startsWith(oldPackage)
132: && oldFQCN.substring(oldPackage.length())
133: .charAt(0) == '.') {
134: contains = true;
135: }
136: }
137: }
138: return contains;
139: }
140:
141: public static void renamePackage(AbstractRefactoring refactoring,
142: RefactoringElementsBag refactoringElements,
143: FileObject folder, String oldFQPN, String newFQPN,
144: boolean recursive) {
145: WebModule webModule = WebModule.getWebModule(folder);
146: if (webModule != null) {
147: List<Occurrences.OccurrenceItem> items = Occurrences
148: .getPackageOccurrences(webModule, oldFQPN, newFQPN,
149: recursive);
150: Modifications modification = new Modifications();
151: for (Occurrences.OccurrenceItem item : items) {
152: Modifications.Difference difference = new Modifications.Difference(
153: Modifications.Difference.Kind.CHANGE, item
154: .getChangePosition().getBegin(), item
155: .getChangePosition().getEnd(), item
156: .getOldValue(), item.getNewValue(),
157: item.getRenamePackageMessage());
158: modification.addDifference(item.getFacesConfig(),
159: difference);
160: refactoringElements.add(refactoring,
161: new DiffElement.ChangeFQCNElement(difference,
162: item, modification));
163: }
164: }
165: }
166:
167: private static final String JAVA_MIME_TYPE = "text/x-java"; //NOI18N
168:
169: public static boolean isJavaFile(FileObject f) {
170: return JAVA_MIME_TYPE.equals(f.getMIMEType());
171: }
172:
173: public static CompilationInfo getCompilationInfo(
174: final AbstractRefactoring refactoring,
175: final FileObject fileObject) {
176: CompilationInfo compilationInfo = refactoring.getContext()
177: .lookup(CompilationInfo.class);
178:
179: if (compilationInfo == null && fileObject != null) {
180: final ClasspathInfo cpInfo = refactoring.getContext()
181: .lookup(ClasspathInfo.class);
182: JavaSource source = JavaSource.create(cpInfo,
183: new FileObject[] { fileObject });
184: try {
185: source.runUserActionTask(
186: new Task<CompilationController>() {
187:
188: public void run(
189: CompilationController compilationController)
190: throws Exception {
191: compilationController
192: .toPhase(JavaSource.Phase.RESOLVED);
193: refactoring.getContext().add(
194: compilationController);
195: }
196: }, false);
197: } catch (IOException exception) {
198: LOGGER.log(Level.WARNING,
199: "Exception in JSFSafeDeletePlugin", exception); //NOI18NN
200: }
201: compilationInfo = refactoring.getContext().lookup(
202: CompilationInfo.class);
203: }
204: return compilationInfo;
205: }
206: }
|