001: package tools;
002:
003: import java.util.Enumeration;
004: import java.io.File;
005: import java.io.FileInputStream;
006: import java.io.FileOutputStream;
007: import java.io.PrintStream;
008: import java.io.IOException;
009: import java.io.InputStreamReader;
010: import java.io.LineNumberReader;
011: import java.util.TreeSet;
012: import java.util.Iterator;
013: import anvil.java.util.Hashlist;
014: import anvil.java.util.BindingEnumeration;
015:
016: public class SymbolTool {
017: private String _dir;
018: private Hashlist _symbols = new Hashlist();
019: private TreeSet _set = new TreeSet();
020: private int _count = 0;
021: private boolean _changed = false;
022:
023: public SymbolTool(String dir) throws IOException {
024: _dir = dir;
025: File file = new File(dir + "/symbols.index");
026: System.out.println("Index: " + file.getPath());
027: FileInputStream in = new FileInputStream(file);
028: LineNumberReader reader = new LineNumberReader(
029: new InputStreamReader(in));
030: String line;
031: for (;;) {
032: line = reader.readLine();
033: if (line == null) {
034: break;
035: }
036: _symbols.put(line, new Integer(_count++));
037: _set.add(line);
038: }
039: in.close();
040: System.out.println("Read " + _count + " symbols");
041: }
042:
043: public int getIndex(String symbol) {
044: Integer index = (Integer) _symbols.get(symbol);
045: if (index != null) {
046: return index.intValue();
047: }
048: return -1;
049: }
050:
051: public int update(String symbol) {
052: Integer index = (Integer) _symbols.get(symbol);
053: if (index == null) {
054: System.out.println("Added '" + symbol + "' at #" + _count);
055: _changed = true;
056: _symbols.put(symbol, new Integer(_count++));
057: _set.add(symbol);
058: return _count - 1;
059: }
060: return index.intValue();
061: }
062:
063: public void save() throws IOException
064: {
065: if (!_changed) {
066: System.out.println("No changes.");
067: return;
068: }
069:
070: File file = new File(_dir + "/symbols.index");
071: System.out.println("Writing "+file.getPath());
072: PrintStream out = new PrintStream(new FileOutputStream(file));
073: Enumeration keys = _symbols.keys();
074: int i= 0;
075: while(keys.hasMoreElements()) {
076: String name = keys.nextElement().toString();
077: out.write(name.getBytes());
078: out.write('\n');
079: i++;
080: }
081: out.close();
082:
083: file = new File(_dir + "/Symbols.java");
084: System.out.println("Writing "+file.getPath());
085: out = new PrintStream(new FileOutputStream(file));
086: out.println("/*");
087: out.println("* $Id: SymbolTool.java,v 1.2 2001/10/26 12:14:47 jkl Exp $");
088: out.println("*");
089: out.println("* Copyright 2000 Njet Communications Ltd. All Rights Reserved.");
090: out.println("*");
091: out.println("* This software is the proprietary information of Njet Communications Ltd.");
092: out.println("* Use is subject to license terms.");
093: out.println("*");
094: out.println("* THIS FILE IS AUTOMATICALLY GENERATED. DO NOT CHANGE.");
095: out.println("*/");
096:
097: out.println("package anvil.core;");
098: out.println();
099: out.println("public interface Symbols");
100: out.println("{");
101: out.println();
102:
103: out.println(" public static final Object[][] SYMBOLS =");
104: out.println(" {");
105: Iterator iter = _set.iterator();
106: while(iter.hasNext()) {
107: String name = iter.next().toString();
108: out.print(" { \"");
109: out.print(name);
110: out.print("\", new Integer(");
111: out.print(getIndex(name));
112: out.println(") },");
113: }
114: out.println();
115: out.println(" };");
116: out.println();
117:
118: BindingEnumeration enum = _symbols.keysAndElements();
119: while(enum.hasMoreElements()) {
120: String name = enum.nextKey().toString();
121: int index = ((Integer)enum.nextElement()).intValue();
122: out.print(" public static final int _");
123: out.print(name);
124: out.print(' ');
125: out.print('=');
126: out.print(' ');
127: out.print(index);
128: out.println(';');
129: }
130: out.print(" public static final int LAST_INDEX = ");
131: out.print(_count);
132: out.println(';');
133:
134: out.println();
135: out.println("}");
136: out.close();
137: }
138:
139: public static final void main(String[] args) {
140: try {
141: String dir = args.length > 0 ? args[0] : "./";
142: SymbolTool n = new SymbolTool(dir);
143: LineNumberReader reader = new LineNumberReader(
144: new InputStreamReader(System.in));
145: int count = 0;
146: for (;;) {
147: String line = reader.readLine();
148: if (line == null) {
149: break;
150: }
151: n.update(line.trim());
152: count++;
153: }
154: System.out.println("Processed " + count + " methods");
155: n.save();
156: System.out.println();
157: System.exit(0);
158:
159: } catch (Throwable t) {
160: t.printStackTrace();
161: System.exit(1);
162: }
163: }
164:
165: }
|