001: /*
002: * ProGuard -- shrinking, optimization, obfuscation, and preverification
003: * of Java bytecode.
004: *
005: * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the Free
009: * Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful, but WITHOUT
013: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
015: * more details.
016: *
017: * You should have received a copy of the GNU General Public License along
018: * with this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: package proguard.wtk;
022:
023: import com.sun.kvem.environment.Obfuscator;
024: import proguard.*;
025:
026: import java.io.*;
027:
028: /**
029: * ProGuard plug-in for the J2ME Wireless Toolkit.
030: * <p>
031: * In order to integrate this plug-in in the toolkit, you'll have to put the
032: * following lines in the file
033: * {j2mewtk.dir}<code>/wtklib/Linux/ktools.properties</code> or
034: * {j2mewtk.dir}<code>\wtklib\Windows\ktools.properties</code> (whichever is
035: * applicable).
036: * <p>
037: * <pre>
038: * obfuscator.runner.class.name: proguard.wtk.ProGuardObfuscator
039: * obfuscator.runner.classpath: /usr/local/java/proguard1.6/lib/proguard.jar
040: * </pre>
041: * Please make sure the class path is set correctly for your system.
042: *
043: * @author Eric Lafortune
044: */
045: public class ProGuardObfuscator implements Obfuscator {
046: private static final String DEFAULT_CONFIGURATION = "default.pro";
047:
048: // Implementations for Obfuscator.
049:
050: public void createScriptFile(File jadFile, File projectDir) {
051: // We don't really need to create a script file;
052: // we'll just fill out all options in the run method.
053: }
054:
055: public void run(File obfuscatedJarFile, String wtkBinDir,
056: String wtkLibDir, String jarFileName,
057: String projectDirName, String classPath, String emptyAPI)
058: throws IOException {
059: // Create the ProGuard configuration.
060: Configuration configuration = new Configuration();
061:
062: // Parse the default configuration file.
063: ConfigurationParser parser = new ConfigurationParser(this
064: .getClass().getResource(DEFAULT_CONFIGURATION));
065:
066: try {
067: parser.parse(configuration);
068:
069: // Fill out the library class path.
070: configuration.libraryJars = classPath(classPath);
071:
072: // Fill out the program class path (input and output).
073: configuration.programJars = new ClassPath();
074: configuration.programJars.add(new ClassPathEntry(new File(
075: jarFileName), false));
076: configuration.programJars.add(new ClassPathEntry(
077: obfuscatedJarFile, true));
078:
079: // The preverify tool seems to unpack the resulting classes,
080: // so we must not use mixed-case class names on Windows.
081: configuration.useMixedCaseClassNames = !System.getProperty(
082: "os.name").regionMatches(true, 0, "windows", 0, 7);
083:
084: // Run ProGuard with these options.
085: ProGuard proGuard = new ProGuard(configuration);
086: proGuard.execute();
087:
088: } catch (ParseException ex) {
089: throw new IOException(ex.getMessage());
090: } finally {
091: parser.close();
092: }
093: }
094:
095: /**
096: * Converts the given class path String into a ClassPath object.
097: */
098: private ClassPath classPath(String classPathString) {
099: ClassPath classPath = new ClassPath();
100:
101: String separator = System.getProperty("path.separator");
102:
103: int index = 0;
104: while (index < classPathString.length()) {
105: // Find the next separator, or the end of the String.
106: int next_index = classPathString.indexOf(separator, index);
107: if (next_index < 0) {
108: next_index = classPathString.length();
109: }
110:
111: // Create and add the found class path entry.
112: ClassPathEntry classPathEntry = new ClassPathEntry(
113: new File(classPathString.substring(index,
114: next_index)), false);
115:
116: classPath.add(classPathEntry);
117:
118: // Continue after the separator.
119: index = next_index + 1;
120: }
121:
122: return classPath;
123: }
124: }
|