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.project;
043:
044: import java.awt.*;
045: import java.io.File;
046: import java.util.Vector;
047:
048: import javax.swing.*;
049:
050: import org.openide.filesystems.FileUtil;
051:
052: import org.netbeans.spi.project.support.ant.PropertyUtils;
053: import org.netbeans.spi.project.support.ant.AntProjectHelper;
054: import org.netbeans.spi.project.support.ant.EditableProperties;
055: import org.netbeans.api.java.platform.JavaPlatform;
056: import org.netbeans.api.java.platform.JavaPlatformManager;
057: import org.netbeans.modules.j2ee.common.project.ui.ProjectProperties;
058:
059: public class Utils {
060:
061: // COPIED FROM TOMCAT
062: private static final String javaKeywords[] = { "abstract",
063: "assert", "boolean", "break", "byte", "case", "catch",
064: "char", "class", "const", "continue", "default", "do",
065: "double", "else", "enum", "extends", "final", "finally",
066: "float", "for", "goto", "if", "implements", "import",
067: "instanceof", "int", "interface", "long", "native", "new",
068: "package", "private", "protected", "public", "return",
069: "short", "static", "strictfp", "super", "switch",
070: "synchronized", "this", "throws", "transient", "try",
071: "void", "volatile", "while" };
072:
073: private static final String JSP_PACKAGE_NAME = "org.apache.jsp";
074:
075: private static final String PLATFORM_ANT_NAME = "platform.ant.name"; //NOI18N
076: public static final String SPECIFICATION_J2SE = "j2se"; //NOI18N
077:
078: /** Create a valid default for context path from project name.
079: */
080: public static String createDefaultContext(String projectName) {
081: return "/" + PropertyUtils.getUsablePropertyName(projectName);
082: }
083:
084: /**
085: * Updates property file at given location of ant based project
086: * @param h helper of the project
087: * @param path a relative URI in the project directory
088: * @param ep new or updated properties
089: */
090: public static void updateProperties(AntProjectHelper h,
091: String path, EditableProperties ep) {
092: EditableProperties properties = h.getProperties(path);
093: properties.putAll(ep);
094: h.putProperties(path, properties);
095: }
096:
097: /**
098: * Recursively checks whether the file lies underneath or equals the folder
099: * @param folder the root of folders hierarchy to search in
100: * @param file the file to search for
101: * @return <code>true</code>, if <code>file</code> lies somewhere underneath or equals the <code>folder</code>,
102: * <code>false</code> otherwise
103: */
104: public static boolean isParentOrEqual(File folder, File file) {
105: if (folder != null || file != null) {
106: folder = FileUtil.normalizeFile(folder);
107: file = FileUtil.normalizeFile(file);
108: while (file != null) {
109: if (file.equals(folder)) {
110: return true;
111: }
112: file = file.getParentFile();
113: }
114: }
115: return false;
116: }
117:
118: /**
119: * Searches Java platform according to platform name
120: * Specification of the platform has to be J2SE
121: * @param platformName
122: * @return related JavaPlatform object if found, otherwise null
123: */
124: public static JavaPlatform findJ2seJavaPlatform(String platformName) {
125: return findJavaPlatform(platformName, SPECIFICATION_J2SE);
126: }
127:
128: /**
129: * Searches Java platform according to platform name
130: * The platform sepecification does not need to be J2SE
131: * @param platformName
132: * @return related JavaPlatform object if found, otherwise null
133: */
134: public static JavaPlatform findJavaPlatform(String platformName) {
135: return findJavaPlatform(platformName, null);
136: }
137:
138: /**
139: * Get the default value of the <tt>debug.classpath</tt> property.
140: * @return the default value of the <tt>debug.classpath</tt> property.
141: */
142: public static String getDefaultDebugClassPath() {
143: return "${" + ProjectProperties.BUILD_CLASSES_DIR + "}:${"
144: + ProjectProperties.JAVAC_CLASSPATH + "}"; // NOI18N
145: }
146:
147: /**
148: * Correct given classpath, that means remove obsolete properties, add missing ones etc.
149: * If the given parameter is <code>null</code> or empty, the default debug classpath is returned.
150: * @return corrected classpath, never <code>null</code>.
151: * @see #getDefaultClassPath()
152: */
153: public static String correctDebugClassPath(String debugClassPath) {
154:
155: if (debugClassPath == null || debugClassPath.length() == 0) {
156: // should not happen
157: return Utils.getDefaultDebugClassPath();
158: }
159:
160: // "invalid" strings
161: final String buildEarWebDir = "${build.ear.web.dir}"; // NOI18N
162: final String buildEarClassesDir = "${build.ear.classes.dir}"; // NOI18N
163: final String buildEarPrefix = "${build.ear."; // NOI18N
164:
165: if (!debugClassPath.contains(buildEarPrefix)) {
166: return debugClassPath;
167: }
168:
169: StringBuilder buffer = new StringBuilder(debugClassPath
170: .length());
171: for (String token : PropertyUtils.tokenizePath(debugClassPath)) {
172: // check NB 5.5.x obsolete properties
173: if (!buildEarWebDir.equals(token)
174: && !buildEarClassesDir.equals(token)) {
175: if (buffer.length() > 0) {
176: buffer.append(":"); // NOI18N
177: }
178: buffer.append(token);
179: }
180: }
181:
182: return buffer.toString();
183: }
184:
185: private static JavaPlatform findJavaPlatform(String platformName,
186: String specFilter) {
187: if (platformName != null) {
188: JavaPlatform[] platforms = JavaPlatformManager.getDefault()
189: .getInstalledPlatforms();
190: for (int i = 0; i < platforms.length; i++) {
191: JavaPlatform platform = platforms[i];
192: String antName = (String) platform.getProperties().get(
193: PLATFORM_ANT_NAME);
194: if (antName != null && antName.equals(platformName)) {
195: if (specFilter == null
196: || specFilter.equalsIgnoreCase(platform
197: .getSpecification().getName()))
198: return platform;
199: }
200: }
201: }
202: return null;
203: }
204:
205: // COPIED FROM TOMCAT
206: /** Returns a slash-delimited resource path for the servlet generated from
207: * JSP, given a resource path of the original JSP. Uses code copied from Tomcat.
208: * Note: does not handle tag files yet, only JSP files.
209: */
210: static String getGeneratedJavaResource(String jspUri) {
211: int iSep = jspUri.lastIndexOf('/');
212: String packageName = (iSep > 0) ? makeJavaPackage(jspUri
213: .substring(0, iSep)) : ""; // NOI18N
214: if (packageName.length() == 0) {
215: packageName = JSP_PACKAGE_NAME;
216: } else {
217: packageName = JSP_PACKAGE_NAME + "." + packageName; // NOI18N
218: }
219: String className = makeJavaIdentifier(jspUri
220: .substring(iSep + 1));
221: return packageName.replace('.', '/') + "/" + className
222: + ".java"; // NOI18N
223: }
224:
225: // COPIED FROM TOMCAT
226: /**
227: * Converts the given path to a Java package or fully-qualified class name
228: *
229: * @param path Path to convert
230: *
231: * @return Java package corresponding to the given path
232: */
233: private static final String makeJavaPackage(String path) {
234: String classNameComponents[] = split(path, "/");
235: StringBuffer legalClassNames = new StringBuffer();
236: for (int i = 0; i < classNameComponents.length; i++) {
237: legalClassNames
238: .append(makeJavaIdentifier(classNameComponents[i]));
239: if (i < classNameComponents.length - 1) {
240: legalClassNames.append('.');
241: }
242: }
243: return legalClassNames.toString();
244: }
245:
246: // COPIED FROM TOMCAT
247: /**
248: * Splits a string into it's components.
249: * @param path String to split
250: * @param pat Pattern to split at
251: * @return the components of the path
252: */
253: private static final String[] split(String path, String pat) {
254: Vector comps = new Vector();
255: int pos = path.indexOf(pat);
256: int start = 0;
257: while (pos >= 0) {
258: if (pos > start) {
259: String comp = path.substring(start, pos);
260: comps.add(comp);
261: }
262: start = pos + pat.length();
263: pos = path.indexOf(pat, start);
264: }
265: if (start < path.length()) {
266: comps.add(path.substring(start));
267: }
268: String[] result = new String[comps.size()];
269: for (int i = 0; i < comps.size(); i++) {
270: result[i] = (String) comps.elementAt(i);
271: }
272: return result;
273: }
274:
275: // COPIED FROM TOMCAT
276: /**
277: * Converts the given identifier to a legal Java identifier
278: *
279: * @param identifier Identifier to convert
280: *
281: * @return Legal Java identifier corresponding to the given identifier
282: */
283: private static final String makeJavaIdentifier(String identifier) {
284: StringBuffer modifiedIdentifier = new StringBuffer(identifier
285: .length());
286: if (!Character.isJavaIdentifierStart(identifier.charAt(0))) {
287: modifiedIdentifier.append('_');
288: }
289: for (int i = 0; i < identifier.length(); i++) {
290: char ch = identifier.charAt(i);
291: if (Character.isJavaIdentifierPart(ch) && ch != '_') {
292: modifiedIdentifier.append(ch);
293: } else if (ch == '.') {
294: modifiedIdentifier.append('_');
295: } else {
296: modifiedIdentifier.append(mangleChar(ch));
297: }
298: }
299: if (isJavaKeyword(modifiedIdentifier.toString())) {
300: modifiedIdentifier.append('_');
301: }
302: return modifiedIdentifier.toString();
303: }
304:
305: // COPIED FROM TOMCAT
306: /**
307: * Mangle the specified character to create a legal Java class name.
308: */
309: private static final String mangleChar(char ch) {
310: char[] result = new char[5];
311: result[0] = '_';
312: result[1] = Character.forDigit((ch >> 12) & 0xf, 16);
313: result[2] = Character.forDigit((ch >> 8) & 0xf, 16);
314: result[3] = Character.forDigit((ch >> 4) & 0xf, 16);
315: result[4] = Character.forDigit(ch & 0xf, 16);
316: return new String(result);
317: }
318:
319: // COPIED FROM TOMCAT
320: /**
321: * Test whether the argument is a Java keyword
322: */
323: private static boolean isJavaKeyword(String key) {
324: int i = 0;
325: int j = javaKeywords.length;
326: while (i < j) {
327: int k = (i + j) / 2;
328: int result = javaKeywords[k].compareTo(key);
329: if (result == 0) {
330: return true;
331: }
332: if (result < 0) {
333: i = k + 1;
334: } else {
335: j = k;
336: }
337: }
338: return false;
339: }
340:
341: public static Color getErrorColor() {
342: // inspired by org.openide.WizardDescriptor
343: Color c = UIManager.getColor("nb.errorForeground"); //NOI18N
344: return c == null ? new Color(89, 79, 191) : c;
345: }
346:
347: public static String toClasspathString(File[] classpathEntries) {
348: if (classpathEntries == null) {
349: return "";
350: }
351: StringBuffer classpath = new StringBuffer();
352: for (int i = 0; i < classpathEntries.length; i++) {
353: classpath.append(classpathEntries[i].getAbsolutePath());
354: if (i + 1 < classpathEntries.length) {
355: classpath.append(':'); // NOI18N
356: }
357: }
358: return classpath.toString();
359: }
360: }
|