KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > obfuscate > DictionaryNameFactory


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  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21 package proguard.obfuscate;
22
23 import java.io.*;
24 import java.util.*;
25
26 /**
27  * This <code>NameFactory</code> generates names that are read from a
28  * specified input file.
29  * Comments (everything starting with '#' on a single line) are ignored.
30  *
31  * @author Eric Lafortune
32  */

33 public class DictionaryNameFactory implements NameFactory
34 {
35     private static final char COMMENT_CHARACTER = '#';
36
37
38     private final NameFactory nameFactory;
39     private final List names = new ArrayList();
40
41     private int index = 0;
42
43
44     /**
45      * Creates a new <code>DictionaryNameFactory</code>.
46      * @param file the file from which the names can be read.
47      * @param nameFactory the name factory from which names will be retrieved
48      * if the list of read names has been exhausted.
49      */

50     public DictionaryNameFactory(File file,
51                                  NameFactory nameFactory) throws IOException
52     {
53         this.nameFactory = nameFactory;
54
55         Reader reader = new FileReader(file);
56
57         try
58         {
59             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
60
61             while (true)
62             {
63                 // Read the next character.
64
int c = reader.read();
65
66                 // Is it a valid identifier character?
67
if (c != -1 &&
68                     (buffer.length() == 0 ?
69                          Character.isJavaIdentifierStart((char)c) :
70                          Character.isJavaIdentifierPart((char)c)))
71                 {
72                     // Append it to the current identifier.
73
buffer.append((char)c);
74                 }
75                 else
76                 {
77                     // Did we collect a new identifier?
78
if (buffer.length() > 0)
79                     {
80                         // Add the completed name to the list of names, if it's
81
// not in it yet.
82
String JavaDoc name = buffer.toString();
83                         if (!names.contains(name))
84                         {
85                             names.add(name);
86                         }
87
88                         // Clear the buffer.
89
buffer.setLength(0);
90                     }
91
92                     // Is this the beginning of a comment line?
93
if (c == COMMENT_CHARACTER)
94                     {
95                         // Skip all characters till the end of the line.
96
do
97                         {
98                             c = reader.read();
99                         }
100                         while (c != -1 &&
101                                c != '\n' &&
102                                c != '\r');
103                     }
104
105                     // Is this the end of the file?
106
if (c == -1)
107                     {
108                         // Just return.
109
return;
110                     }
111                 }
112             }
113         }
114         finally
115         {
116             reader.close();
117         }
118     }
119
120
121     // Implementations for NameFactory.
122

123     public void reset()
124     {
125         index = 0;
126
127         nameFactory.reset();
128     }
129
130
131     public String JavaDoc nextName()
132     {
133         String JavaDoc name;
134
135         // Do we still have names?
136
if (index < names.size())
137         {
138             // Return the next name.
139
name = (String JavaDoc)names.get(index++);
140         }
141         else
142         {
143             // Return the next different name from the other name factory.
144
do
145             {
146                 name = nameFactory.nextName();
147             }
148             while (names.contains(name));
149         }
150
151         return name;
152     }
153
154
155     public static void main(String JavaDoc[] args)
156     {
157         try
158         {
159             DictionaryNameFactory factory =
160                 new DictionaryNameFactory(new File(args[0]), new SimpleNameFactory());
161
162             for (int counter = 0; counter < 50; counter++)
163             {
164                 System.out.println("["+factory.nextName()+"]");
165             }
166         }
167         catch (IOException ex)
168         {
169             ex.printStackTrace();
170         }
171     }
172 }
173
Popular Tags