KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > GPL


1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  * of Java bytecode.
4  *
5  * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16  * more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  */

22 package proguard;
23
24 import java.io.*;
25 import java.util.*;
26
27 /**
28  * This class checks and prints out information about the GPL.
29  *
30  * @author Eric Lafortune
31  */

32 public class GPL
33 {
34     /**
35      * Prints out a note about the GPL if ProGuard is linked against unknown
36      * code.
37      */

38     public static void check()
39     {
40         ByteArrayOutputStream out = new ByteArrayOutputStream();
41         new Exception JavaDoc().printStackTrace(new PrintStream(out));
42         LineNumberReader reader = new LineNumberReader(
43                                   new InputStreamReader(
44                                   new ByteArrayInputStream(out.toByteArray())));
45
46         Set unknownPackageNames = unknownPackageNames(reader);
47
48         if (unknownPackageNames.size() > 0)
49         {
50             String JavaDoc uniquePackageNames = uniquePackageNames(unknownPackageNames);
51
52             System.out.println("ProGuard is released under the GNU General Public License. The authors of all");
53             System.out.println("programs or plugins that link to it ("+uniquePackageNames+"...) therefore");
54             System.out.println("must ensure that these programs carry the GNU General Public License as well.");
55         }
56     }
57
58
59     /**
60      * Returns a set of package names from the given stack trace.
61      */

62     private static Set unknownPackageNames(LineNumberReader reader)
63     {
64         Set packageNames = new HashSet();
65
66         try
67         {
68             while (true)
69             {
70                 String JavaDoc line = reader.readLine();
71                 if (line == null)
72                 {
73                     break;
74                 }
75
76                 line = line.trim();
77                 if (line.startsWith("at "))
78                 {
79                     line = line.substring(2).trim();
80                     line = trimSuffix(line, '(');
81                     line = trimSuffix(line, '.');
82                     line = trimSuffix(line, '.');
83
84                     if (line.length() > 0 && !isKnown(line))
85                     {
86                         packageNames.add(line);
87                     }
88                 }
89             }
90         }
91         catch (IOException ex)
92         {
93         }
94
95         return packageNames;
96     }
97
98
99     /**
100      * Returns a comma-separated list of package names from the set, excluding
101      * any subpackages of packages in the set.
102      */

103     private static String JavaDoc uniquePackageNames(Set packageNames)
104     {
105         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
106
107         Iterator iterator = packageNames.iterator();
108         while (iterator.hasNext())
109         {
110             String JavaDoc packageName = (String JavaDoc)iterator.next();
111             if (!containsPrefix(packageNames, packageName))
112             {
113                 buffer.append(packageName).append(", ");
114             }
115         }
116
117         return buffer.toString();
118     }
119
120
121     /**
122      * Returns a given string without the suffix, as defined by the given
123      * separator.
124      */

125     private static String JavaDoc trimSuffix(String JavaDoc string, char separator)
126     {
127         int index = string.lastIndexOf(separator);
128         return index < 0 ? "" : string.substring(0, index);
129     }
130
131
132     /**
133      * Returns whether the given set contains a prefix of the given name.
134      */

135     private static boolean containsPrefix(Set set, String JavaDoc name)
136     {
137         int index = 0;
138
139         while (!set.contains(name.substring(0, index)))
140         {
141             index = name.indexOf('.', index + 1);
142             if (index < 0)
143             {
144                 return false;
145             }
146         }
147
148         return true;
149     }
150
151
152     /**
153      * Returns whether the given package name has been granted an exception
154      * against the GPL linking clause, by the copyright holder of ProGuard.
155      * This method is not legally binding, but of course the actual license is.
156      * Please contact the copyright holder if you would like an exception for
157      * your code as well.
158      */

159     private static boolean isKnown(String JavaDoc packageName)
160     {
161         return packageName.startsWith("java") ||
162                packageName.startsWith("sun.reflect") ||
163                packageName.startsWith("proguard") ||
164                packageName.startsWith("org.apache.tools.ant") ||
165                packageName.startsWith("org.apache.tools.maven") ||
166                packageName.startsWith("org.eclipse") ||
167                packageName.startsWith("org.netbeans") ||
168                packageName.startsWith("com.sun.kvem") ||
169                packageName.startsWith("jg.j2me") ||
170                packageName.startsWith("jg.common") ||
171                packageName.startsWith("jg.buildengine");
172     }
173
174
175     public static void main(String JavaDoc[] args)
176     {
177         LineNumberReader reader = new LineNumberReader(
178                                   new InputStreamReader(System.in));
179
180         Set unknownPackageNames = unknownPackageNames(reader);
181
182         if (unknownPackageNames.size() > 0)
183         {
184             String JavaDoc uniquePackageNames = uniquePackageNames(unknownPackageNames);
185
186             System.out.println(uniquePackageNames);
187         }
188     }
189 }
190
Popular Tags