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-2006 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.j2me.cdc.platform.platformdefinition;
043:
044: import java.io.File;
045: import java.net.MalformedURLException;
046: import java.net.URL;
047: import java.text.MessageFormat;
048: import java.util.ArrayList;
049: import java.util.Collection;
050: import java.util.List;
051: import java.util.StringTokenizer;
052:
053: import org.netbeans.api.java.classpath.ClassPath;
054: import org.netbeans.api.java.platform.JavaPlatform;
055: import org.netbeans.spi.java.classpath.PathResourceImplementation;
056: import org.netbeans.spi.java.classpath.support.ClassPathSupport;
057: import org.openide.ErrorManager;
058: import org.openide.filesystems.FileObject;
059: import org.openide.filesystems.FileUtil;
060: import org.openide.modules.SpecificationVersion;
061: import org.openide.util.NbBundle;
062: import org.openide.util.Utilities;
063:
064: public class Util {
065:
066: private Util() {
067: }
068:
069: public static ClassPath createClassPath(String classpath) {
070: StringTokenizer tokenizer = new StringTokenizer(classpath,
071: File.pathSeparator);
072: List<PathResourceImplementation> list = new ArrayList<PathResourceImplementation>();
073: while (tokenizer.hasMoreTokens()) {
074: String item = tokenizer.nextToken();
075: File f = FileUtil.normalizeFile(new File(item));
076: URL url = getRootURL(f);
077: if (url != null) {
078: list.add(ClassPathSupport.createResource(url));
079: }
080: }
081: return ClassPathSupport.createClassPath(list);
082: }
083:
084: // XXX this method could probably be removed... use standard FileUtil stuff
085: static URL getRootURL(File f) {
086: URL url = null;
087: try {
088: if (isArchiveFile(f)) {
089: url = FileUtil.getArchiveRoot(f.toURI().toURL());
090: } else {
091: url = f.toURI().toURL();
092: String surl = url.toExternalForm();
093: if (!surl.endsWith("/")) {
094: url = new URL(surl + "/");
095: }
096: }
097: } catch (MalformedURLException e) {
098: throw new AssertionError(e);
099: }
100: return url;
101: }
102:
103: /**
104: * Returns normalized name from display name.
105: * The normalized name should be used in the Ant properties and external files.
106: * @param displayName
107: * @return String
108: */
109: public static String normalizeName(String displayName) {
110: StringBuffer normalizedName = new StringBuffer();
111: for (int i = 0; i < displayName.length(); i++) {
112: char c = displayName.charAt(i);
113: if (Character.isJavaIdentifierPart(c) || c == '-'
114: || c == '.') {
115: normalizedName.append(c);
116: } else {
117: normalizedName.append('_');
118: }
119: }
120: return normalizedName.toString();
121: }
122:
123: /**
124: * Returns specification version of the given platform.
125: *
126: * @return instance of SpecificationVersion representing the version; never null
127: */
128: public static SpecificationVersion getSpecificationVersion(
129: JavaPlatform plat) {
130: String version = (String) plat.getSystemProperties().get(
131: "java.specification.version"); // NOI18N
132: if (version == null) {
133: version = "1.1";
134: }
135: return makeSpec(version);
136: }
137:
138: public static FileObject findTool(String folder, String toolName,
139: Collection<FileObject> installFolders) {
140: assert toolName != null;
141: for (FileObject root : installFolders) {
142: FileObject bin = root.getFileObject(folder); //NOI18N
143: if (bin == null) {
144: continue;
145: }
146: String suffix = null;
147: if (Utilities.isWindows()) {
148: suffix = toolName.indexOf('.') != -1 ? suffix : "exe";
149: FileObject tool = bin.getFileObject(toolName, suffix); //NOI18N
150: if (tool != null) {
151: return tool;
152: }
153: suffix = toolName.indexOf('.') != -1 ? suffix : "bat";
154: tool = bin.getFileObject(toolName, suffix); //NOI18N
155: if (tool != null) {
156: return tool;
157: }
158: } else {
159: suffix = toolName.indexOf('.') != -1 ? suffix : null;
160: FileObject tool = bin.getFileObject(toolName, suffix); //NOI18N
161: if (tool != null) {
162: return tool;
163: }
164: suffix = toolName.indexOf('.') != -1 ? suffix : "sh";
165: tool = bin.getFileObject(toolName, suffix); //NOI18N
166: if (tool != null) {
167: return tool;
168: }
169: }
170: }
171: return null;
172: }
173:
174: /**
175: * Get JRE extension JARs/ZIPs.
176: * @param extPath a native-format path for e.g. jre/lib/ext
177: * @return a native-format classpath for extension JARs and ZIPs found in it
178: */
179: public static String getExtensions(String extPath) {
180: if (extPath == null) {
181: return null;
182: }
183: StringBuffer sb = new StringBuffer();
184: StringTokenizer tk = new StringTokenizer(extPath,
185: File.pathSeparator);
186: while (tk.hasMoreTokens()) {
187: File extFolder = FileUtil.normalizeFile(new File(tk
188: .nextToken()));
189: File[] files = extFolder.listFiles();
190: if (files != null) {
191: for (File f : files) {
192: if (!f.exists()) {
193: //May happen, eg. broken link, it is safe to ignore it
194: //since it is an extension directory, but log it.
195: ErrorManager.getDefault().log(
196: ErrorManager.WARNING,
197: MessageFormat.format(NbBundle
198: .getMessage(Util.class,
199: "MSG_BrokenExtension"),
200: new Object[] { f, extFolder }));
201: continue;
202: }
203: FileObject fo = FileUtil.toFileObject(f);
204: assert fo != null : "Must have defined a FileObject for existent file "
205: + f;
206: if (!FileUtil.isArchiveFile(fo)) {
207: // #42961: Mac OS X has e.g. libmlib_jai.jnilib.
208: continue;
209: }
210: sb.append(File.pathSeparator);
211: sb.append(f.getAbsolutePath());
212: }
213: }
214: }
215: if (sb.length() == 0) {
216: return null;
217: }
218: return sb.substring(File.pathSeparator.length());
219: }
220:
221: // copy pasted from org.openide.modules.Dependency:
222: /** Try to make a specification version from a string.
223: * Deal with errors gracefully and try to recover something from it.
224: * E.g. "1.4.0beta" is technically erroneous; correct to "1.4.0".
225: */
226: private static SpecificationVersion makeSpec(String vers) {
227: if (vers != null) {
228: try {
229: //platform supports only 1.2 version!
230: return new SpecificationVersion("1.2");//vers);
231: } catch (NumberFormatException nfe) {
232: System.err
233: .println("WARNING: invalid specification version: "
234: + vers); // NOI18N
235: }
236: do {
237: vers = vers.substring(0, vers.length() - 1);
238: try {
239: return new SpecificationVersion(vers);
240: } catch (NumberFormatException nfe) {
241: // ignore
242: }
243: } while (vers.length() > 0);
244: }
245: // Nothing decent in it at all; use zero.
246: return new SpecificationVersion("0"); // NOI18N
247: }
248:
249: // XXX this method could probably be removed...
250: private static boolean isArchiveFile(File f) {
251: // the f might not exist and so you cannot use e.g. f.isFile() here
252: String fileName = f.getName().toLowerCase();
253: return fileName.endsWith(".jar") || fileName.endsWith(".zip"); //NOI18N
254: }
255:
256: }
|