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: package org.netbeans.spi.mobility.project.support;
042:
043: import java.io.BufferedReader;
044: import java.io.File;
045: import java.io.IOException;
046: import java.io.StringReader;
047: import java.net.URI;
048: import java.util.ArrayList;
049: import java.util.HashMap;
050: import java.util.Iterator;
051: import java.util.List;
052: import java.util.Map;
053: import org.netbeans.api.java.platform.JavaPlatform;
054: import org.netbeans.api.java.platform.JavaPlatformManager;
055: import org.netbeans.api.java.project.JavaProjectConstants;
056: import org.netbeans.api.project.ant.AntArtifact;
057: import org.netbeans.api.project.libraries.Library;
058: import org.netbeans.api.project.libraries.LibraryManager;
059: import org.netbeans.mobility.antext.preprocessor.CommentingPreProcessor;
060: import org.netbeans.modules.mobility.project.ui.customizer.J2MEProjectProperties;
061: import org.netbeans.spi.mobility.deployment.DeploymentPlugin;
062: import org.netbeans.spi.mobility.project.PropertyParser;
063: import org.netbeans.spi.project.support.ant.AntProjectHelper;
064: import org.netbeans.spi.project.support.ant.PropertyUtils;
065: import org.netbeans.spi.project.support.ant.ReferenceHelper;
066: import org.netbeans.modules.mobility.project.ui.customizer.VisualClassPathItem;
067: import org.openide.filesystems.FileUtil;
068: import org.openide.util.Lookup;
069: import org.openide.util.NbBundle;
070:
071: /**
072: *
073: * @author Adam Sotona
074: */
075: public class DefaultPropertyParsers {
076:
077: public static final PropertyParser STRING_PARSER = new StringParser();
078: public static final PropertyParser BOOLEAN_PARSER = new BooleanParser();
079: public static final PropertyParser INVERSE_BOOLEAN_PARSER = new InverseBooleanParser();
080: public static final PropertyParser PATH_PARSER = new PathParser();
081: public static final PropertyParser PLATFORM_PARSER = new PlatformParser();
082: public static final PropertyParser INTEGER_PARSER = new IntegerParser();
083: public static final PropertyParser MANIFEST_PROPERTY_PARSER = new ManifestPropertyParser();
084: public static final PropertyParser FILE_REFERENCE_PARSER = new FileReferenceParser();
085: public static final PropertyParser ABILITIES_PARSER = new AbilitiesParser();
086: public static final PropertyParser DEPLOYMENT_TYPE_PARSER = new DeploymentTypeParser();
087: public static final PropertyParser DEBUG_LEVEL_PARSER = new DebugLevelParser();
088:
089: private static class StringParser implements PropertyParser {
090:
091: public Object decode(String raw,
092: AntProjectHelper antProjectHelper,
093: ReferenceHelper refHelper) {
094: return raw;
095: }
096:
097: public String encode(Object value,
098: AntProjectHelper antProjectHelper,
099: ReferenceHelper refHelper) {
100: return value.toString();
101: }
102:
103: }
104:
105: private static class DebugLevelParser implements PropertyParser {
106:
107: public Object decode(String raw,
108: AntProjectHelper antProjectHelper,
109: ReferenceHelper refHelper) {
110: return raw == null || raw.trim().length() == 0 ? "debug"
111: : raw.trim(); //NOI18N
112: }
113:
114: public String encode(Object value,
115: AntProjectHelper antProjectHelper,
116: ReferenceHelper refHelper) {
117: String s = ((String) value).trim();
118: return s.length() == 0 ? "debug" : s; //NOI18N
119: }
120:
121: }
122:
123: private static class FileReferenceParser implements PropertyParser {
124:
125: public Object decode(String raw,
126: AntProjectHelper antProjectHelper,
127: ReferenceHelper refHelper) {
128: if (raw == null || !raw.startsWith("${")
129: || !raw.endsWith("}"))
130: return raw;//NOI18N
131: String ref = raw.substring(2, raw.length() - 1);
132: String val = antProjectHelper.getProperties(
133: AntProjectHelper.PROJECT_PROPERTIES_PATH)
134: .getProperty(ref);
135: if (val == null)
136: val = antProjectHelper.getProperties(
137: AntProjectHelper.PRIVATE_PROPERTIES_PATH)
138: .getProperty(ref);
139: if (val == null)
140: return raw;
141: File f = new File(val);
142: return FileUtil.normalizeFile(
143: f.isAbsolute() ? f : new File(FileUtil
144: .toFile(antProjectHelper
145: .getProjectDirectory()), val))
146: .getAbsolutePath();
147: }
148:
149: public String encode(Object value,
150: AntProjectHelper antProjectHelper,
151: ReferenceHelper refHelper) {
152: File f = new File((String) value);
153: return refHelper.createForeignFileReference(FileUtil
154: .normalizeFile(f.isAbsolute() ? f : new File(
155: FileUtil.toFile(antProjectHelper
156: .getProjectDirectory()),
157: (String) value)), "anyfile"); //NOI18N
158: }
159:
160: }
161:
162: private static class BooleanParser implements PropertyParser {
163:
164: public Object decode(String raw,
165: AntProjectHelper antProjectHelper,
166: ReferenceHelper refHelper) {
167: if (raw != null) {
168: String lowecaseRaw = raw.toLowerCase();
169: if ("true".equals(lowecaseRaw) || //NOI18N
170: "yes".equals(lowecaseRaw) || //NOI18N
171: "enabled".equals(lowecaseRaw)) //NOI18N
172: return Boolean.TRUE;
173: }
174: return Boolean.FALSE;
175: }
176:
177: public String encode(Object value,
178: AntProjectHelper antProjectHelper,
179: ReferenceHelper refHelper) {
180: return ((Boolean) value).booleanValue() ? "true" : "false"; // NOI18N
181: }
182:
183: }
184:
185: private static class IntegerParser implements PropertyParser {
186:
187: public Object decode(String raw,
188: AntProjectHelper antProjectHelper,
189: ReferenceHelper refHelper) {
190: return Integer.decode(raw);
191: }
192:
193: public String encode(Object value,
194: AntProjectHelper antProjectHelper,
195: ReferenceHelper refHelper) {
196: return value.toString();
197: }
198:
199: }
200:
201: private static class InverseBooleanParser extends BooleanParser {
202:
203: public Object decode(String raw,
204: AntProjectHelper antProjectHelper,
205: ReferenceHelper refHelper) {
206: return ((Boolean) super .decode(raw, antProjectHelper,
207: refHelper)).booleanValue() ? Boolean.FALSE
208: : Boolean.TRUE;
209: }
210:
211: public String encode(Object value,
212: AntProjectHelper antProjectHelper,
213: ReferenceHelper refHelper) {
214: return super
215: .encode(
216: ((Boolean) value).booleanValue() ? Boolean.FALSE
217: : Boolean.TRUE, antProjectHelper,
218: refHelper);
219: }
220:
221: }
222:
223: private static class PathParser implements PropertyParser {
224:
225: // XXX Define in the LibraryManager
226: private static final String LIBRARY_PREFIX = "${libs."; // NOI18N
227: private static final String ARTIFACT_PREFIX = "${reference.";
228:
229: // Contains well known paths in the J2MEProject
230: private static final String[][] WELL_KNOWN_PATHS = new String[][] { {
231: "libs.classpath",
232: NbBundle.getMessage(J2MEProjectProperties.class,
233: "LBL_JavacClasspath_DisplayName") }, //NOI18N
234: };
235:
236: public List<VisualClassPathItem> decode(String raw,
237: AntProjectHelper antProjectHelper,
238: ReferenceHelper refHelper) {
239: String pe[] = PropertyUtils.tokenizePath(raw);
240: List<VisualClassPathItem> cpItems = new ArrayList<VisualClassPathItem>(
241: pe.length);
242: for (String pei : pe) {
243: VisualClassPathItem cpItem = null;
244:
245: // First try to find out whether the item is well known classpath
246: // in the J2ME project type
247: int wellKnownPathIndex = -1;
248: for (int j = 0; j < WELL_KNOWN_PATHS.length; j++) {
249: if (WELL_KNOWN_PATHS[j][0]
250: .equals(getAntPropertyName(pei))) {
251: wellKnownPathIndex = j;
252: break;
253: }
254: }
255:
256: if (wellKnownPathIndex != -1) {
257: cpItem = new VisualClassPathItem(pei,
258: VisualClassPathItem.TYPE_CLASSPATH, pei,
259: WELL_KNOWN_PATHS[wellKnownPathIndex][1]);
260: } else if (pei.startsWith(LIBRARY_PREFIX)) {
261: // Library from library manager
262: String eval = pei.substring(
263: LIBRARY_PREFIX.length(), pei
264: .lastIndexOf('.')); //NOI18N
265: Library lib = LibraryManager.getDefault()
266: .getLibrary(eval);
267: String dName;
268: if (lib != null)
269: dName = lib.getDisplayName();
270: else {
271: dName = eval;
272: }
273: cpItem = new VisualClassPathItem(lib,
274: VisualClassPathItem.TYPE_LIBRARY, pei,
275: dName);
276: } else if (pei.startsWith(ARTIFACT_PREFIX)) {
277: Object o[] = refHelper.findArtifactAndLocation(pei);
278: File f = null;
279: if (o[0] != null && o[1] != null) {
280: // Sub project artifact
281: String eval = antProjectHelper
282: .getStandardPropertyEvaluator()
283: .getProperty(getAntPropertyName(pei));
284: if (eval != null) {
285: f = FileUtil.normalizeFile(antProjectHelper
286: .resolveFile(eval));
287: }
288: }
289: cpItem = new VisualClassPathItem(
290: (AntArtifact) o[0], (URI) o[1],
291: VisualClassPathItem.TYPE_ARTIFACT, pei,
292: f != null ? f.getPath() : pei.substring(
293: ARTIFACT_PREFIX.length(), pei
294: .lastIndexOf('.'))); //NOI18N
295: } else {
296: // Standalone jar or property
297: String eval = antProjectHelper
298: .getStandardPropertyEvaluator()
299: .getProperty(getAntPropertyName(pei));
300: File f = null;
301: if (eval != null) {
302: f = FileUtil.normalizeFile(antProjectHelper
303: .resolveFile(eval));
304: }
305: String name = f != null ? f.getName()
306: : getAntPropertyName(pei);
307: String displayName = f != null ? f.getPath() : name;
308: cpItem = new VisualClassPathItem(f,
309: isJar(name) ? VisualClassPathItem.TYPE_JAR
310: : VisualClassPathItem.TYPE_FOLDER,
311: pei, displayName);
312: }
313: cpItems.add(cpItem);
314: }
315:
316: return cpItems;
317: }
318:
319: private boolean isJar(String s) {
320: if (s == null)
321: return false;
322: s = s.toLowerCase();
323: return s.endsWith(".jar") || s.endsWith(".zip"); //NOI18N
324: }
325:
326: public String encode(Object value, AntProjectHelper helper,
327: ReferenceHelper refHelper) {
328: Iterator<VisualClassPathItem> it = ((List<VisualClassPathItem>) value)
329: .iterator();
330: StringBuffer sb = new StringBuffer();
331: while (it.hasNext()) {
332: VisualClassPathItem vcpi = it.next();
333:
334: if (vcpi.getElement() == null) {
335: if (vcpi.getRawText() != null) {
336: sb.append(vcpi.getRawText());
337: if (it.hasNext()) {
338: sb.append(File.pathSeparatorChar);
339: }
340: }
341: continue;
342: }
343:
344: switch (vcpi.getType()) {
345: case VisualClassPathItem.TYPE_JAR:
346: case VisualClassPathItem.TYPE_FOLDER:
347: File f = (File) vcpi.getElement();
348: String reference = refHelper
349: .createForeignFileReference(
350: f,
351: JavaProjectConstants.ARTIFACT_TYPE_JAR);
352: sb.append(reference);
353: break;
354: case VisualClassPathItem.TYPE_LIBRARY:
355: sb.append(vcpi.getRawText()); //NOI18N
356: break;
357: case VisualClassPathItem.TYPE_ARTIFACT:
358: AntArtifact aa = (AntArtifact) vcpi.getElement();
359: URI u = vcpi.getURI();
360: reference = refHelper.addReference(aa, u);
361: sb.append(reference);
362: break;
363: case VisualClassPathItem.TYPE_CLASSPATH:
364: sb.append((String) vcpi.getElement());
365: break;
366: }
367:
368: if (it.hasNext()) {
369: sb.append(File.pathSeparatorChar);
370: }
371: }
372: return sb.toString();
373: }
374:
375: static String getAntPropertyName(final String property) {
376: if (property != null && property.startsWith("${") && // NOI18N
377: property.endsWith("}")) { // NOI18N
378: return property.substring(2, property.length() - 1);
379: }
380: return property;
381: }
382:
383: }
384:
385: private static class PlatformParser implements PropertyParser {
386:
387: public Object decode(String raw,
388: AntProjectHelper antProjectHelper,
389: ReferenceHelper refHelper) {
390:
391: JavaPlatform[] platforms = JavaPlatformManager.getDefault()
392: .getInstalledPlatforms();
393: for (int i = 0; i < platforms.length; i++) {
394: String normalizedName = (String) platforms[i]
395: .getProperties().get("platform.ant.name"); //NOI18N
396: if (normalizedName != null
397: && normalizedName.equals(raw)) {
398: return platforms[i].getDisplayName();
399: }
400: }
401:
402: return JavaPlatformManager.getDefault()
403: .getDefaultPlatform().getDisplayName();
404: }
405:
406: public String encode(Object value,
407: AntProjectHelper antProjectHelper,
408: ReferenceHelper refHelper) {
409: JavaPlatform[] platforms = JavaPlatformManager.getDefault()
410: .getPlatforms((String) value, null);
411: if (platforms.length == 0) {
412: return null;
413: }
414: return (String) platforms[0].getProperties().get(
415: "platform.ant.name"); //NOI18N
416: }
417:
418: }
419:
420: private static class DeploymentTypeParser implements PropertyParser {
421:
422: public Object decode(String raw,
423: AntProjectHelper antProjectHelper,
424: ReferenceHelper refHelper) {
425: Iterator it = Lookup.getDefault().lookup(
426: new Lookup.Template<DeploymentPlugin>(
427: DeploymentPlugin.class)).allInstances()
428: .iterator();
429: while (it.hasNext()) {
430: DeploymentPlugin p = (DeploymentPlugin) it.next();
431: if (p.getDeploymentMethodName().equals(raw))
432: return p.getDeploymentMethodDisplayName();
433: }
434: return "";//NOI18N
435: }
436:
437: public String encode(Object value,
438: AntProjectHelper antProjectHelper,
439: ReferenceHelper refHelper) {
440: Iterator it = Lookup.getDefault().lookup(
441: new Lookup.Template<DeploymentPlugin>(
442: DeploymentPlugin.class)).allInstances()
443: .iterator();
444: while (it.hasNext()) {
445: DeploymentPlugin p = (DeploymentPlugin) it.next();
446: if (p.getDeploymentMethodDisplayName().equals(value))
447: return p.getDeploymentMethodName();
448: }
449: return null;//NOI18N
450: }
451:
452: }
453:
454: private static class ManifestPropertyParser implements
455: PropertyParser {
456:
457: public Object decode(String raw,
458: AntProjectHelper antProjectHelper,
459: ReferenceHelper refHelper) {
460: try {
461: if (raw == null)
462: return null;
463: BufferedReader br = new BufferedReader(
464: new StringReader(raw));
465: HashMap<String, String> map = new HashMap<String, String>();
466: for (;;) {
467: String line = br.readLine();
468: if (line == null)
469: break;
470: int i = line.indexOf(':');
471: if (i < 0)
472: continue;
473: map.put(line.substring(0, i), line.substring(i + 1)
474: .trim());
475: }
476: return map;
477: } catch (IOException ioe) {
478: assert false : ioe;
479: return null;
480: }
481: }
482:
483: public String encode(Object val,
484: AntProjectHelper antProjectHelper,
485: ReferenceHelper refHelper) {
486: HashMap<String, String> map = (HashMap<String, String>) val;
487: if (map == null)
488: return null;
489: StringBuffer buffer = new StringBuffer();
490: for (String key : map.keySet()) {
491: if (key == null)
492: continue;
493: String value = map.get(key);
494: if (value == null)
495: continue;
496: buffer.append(key).append(": ").append(value).append(
497: '\n'); //NOI18N
498: }
499: return buffer.toString();
500: }
501: }
502:
503: private static class AbilitiesParser implements PropertyParser { //XXX escape/unescape equal and comma
504: public Object decode(String raw,
505: AntProjectHelper antProjectHelper,
506: ReferenceHelper refHelper) {
507: return CommentingPreProcessor.decodeAbilitiesMap(raw);
508: }
509:
510: public String encode(Object value,
511: AntProjectHelper antProjectHelper,
512: ReferenceHelper refHelper) {
513: return CommentingPreProcessor
514: .encodeAbilitiesMap((Map<String, String>) value);
515: }
516: }
517:
518: }
|