001: // Copyright 1997-@year@ by SMB GmbH. All rights reserved.
002: // Copyright Tim Brown <Tim.Brown@incenter.org>. All rights reserved.
003: //
004: // You can redistribute this software and/or modify it under the terms of
005: // the Ozone Library License version 1 published by ozone-db.org.
006: //
007: // $Id: OIG.java,v 1.1 2001/12/18 10:31:31 per_nyfelt Exp $
008:
009: package org.ozoneDB.tools;
010:
011: import java.lang.*;
012: import java.util.*;
013: import java.io.*;
014:
015: /**
016: * Ozone Interface Generator
017: *
018: * <p>This class is meant to be run statically only. It will generate an
019: * OzoneRemote interface for your OzoneObject (s)
020: *
021: * <p>USAGE:
022: *
023: * <p> java OzoneInterfaceGenerator your_OzoneObject
024: *
025: * <p>CONVENTIONS:
026: *
027: * <p> Class Names
028: *
029: * <p> OzoneObject classes must be named like *Impl.java. Interfaces
030: * are generated with a _Int inserted into the name.
031: *
032: * <p>File Naming:
033: *
034: * <p>OzoneObjects must have names like: CarImpl.java The interface generated
035: * will be: CarImpl_Int.java
036: *
037: * <p>You do not have to use "Impl". Any name will do. I use a standard because
038: * it is then easy to write makefile rules like:
039: *
040: * <p>%.class: %.java
041: * <br> javac $(JFLAGS) -classpath $(CPATH) $<
042: * <p>%Impl_Proxy.class: %Impl.java
043: * <br> /projects/ozone/bin/opp $*Impl
044: * <p>%Impl_Int.java: %Impl.java
045: * <br> java -Djava.compiler=tya -classpath $(CPATH) OzoneInterfaceGenerator $*Impl
046: * <p>javasrc = $(wildcard *.java)
047: * <br>classes = $(javasrc:.java=.class)
048: * <br>htmlobjs = $(javasrc:.java=.html)
049: * <br>IntSrc = $(wildcard *Impl.java)
050: * <br>IntObjs = $(IntSrc:Impl.java=Impl_Int.java)
051: * <br>proxies = $(IntSrc:Impl.java=Impl_Proxy.class)
052: * <br>proxysrc = $(IntSrc:Impl.java=Impl.class)
053: *
054: * <p>Coding Style
055: *
056: * <p> Public methods must have ALL parameters on one line. Infact
057: * everything up to and including the open curly brace must be on the first
058: * line.
059: *
060: * <p>Patterns for identifying methods which should be locked are stored in
061: * a file called int_config. This file should be in the same directory as
062: * the .java files. You need to list one pattern per line. Like:
063: * <br> set
064: * <br> update
065: * <br> reset
066: * <br> delete
067: * <br> modify
068: * <br>
069: * <br>
070: *
071: * <p>OzoneInterfaceGenerator looks for and reads these in. If the first line
072: * of your public method contains any of these strings it appends //update
073: * to the end signaling OPP to make that method a locked() method.
074: *
075: * <p>If you do not provide a int_config file OIG uses my favorites, listed
076: * above.
077: *
078: * <p>Public Method Examples:
079: * <p>public void setName(String _name) { this.name = _name; }
080: * <br> in the generated interface it produces:
081: * <br> public void setName(String _name);//update
082: * <br>
083: * <br>
084: *
085: * <p>In the following example I use a comment to identify the update method.
086: * One of the patterns just has to be on the first line.
087: * <br>
088: * <br>
089: * <br>public void killMe() { // update
090: * <br> Produces:
091: * <br> public void killMe();//update
092: * <br>
093: * <br>
094: * <p>NOTE:
095: * <p> OIG will handle this:
096: * <p> public static final String x = new String("test");
097: * <br>
098: * <br>
099: * <p>BUT not this: (working on this :) It will get mistaken for a method.
100: * <p> public String x = new String("test");
101: * <br>
102: * <br>
103: * <p>
104: *
105: *
106: */
107: public class OIG {
108:
109: public static void main(String[] args) {
110: Vector updatePatterns = null;
111: BufferedReader in = null;
112: PrintWriter out = null;
113:
114: updatePatterns = getPatterns();
115:
116: try {
117: // first try to use args
118: in = new BufferedReader(new FileReader(args[0] + ".java"),
119: 1024);
120: out = new PrintWriter(new FileWriter(args[0] + "_Int.java"));
121: } catch (Exception e) {
122: System.err.println("Using stdin");
123: in = new BufferedReader(new InputStreamReader(System.in),
124: 1024);
125: out = new PrintWriter(System.out);
126: }
127: try {
128: String buf;
129: String tbuf;
130: StringTokenizer tok;
131: boolean fndOpen = false;
132: boolean fndConst = false;
133: String classname = null;
134: String intname = null;
135:
136: while ((buf = in.readLine()) != null) {
137: if (buf.startsWith("{")) {
138: out.println(buf);
139: fndOpen = true;
140: }
141:
142: if (!fndOpen) {
143: if (buf.startsWith("import")
144: || buf.startsWith("package")) {
145: out.println(buf);
146: } else {
147:
148: if (buf.startsWith("public class")
149: || buf
150: .startsWith("public abstract class")) {
151: int beg;
152: int end;
153: beg = buf.indexOf("class") + 6;
154: end = buf.indexOf(" ", beg);
155: classname = buf.substring(beg, end);
156: intname = classname + "_Int";
157:
158: out
159: .print("import org.ozoneDB.OzoneRemote;\n"
160: + "import org.ozoneDB.DxLib.*;\n"
161: + "\npublic interface "
162: + intname
163: + " extends OzoneRemote\n");
164:
165: }
166: }
167: } else {
168: if (buf.indexOf("static") == -1) {
169: buf = remove(buf, "synchronized");
170: tok = new StringTokenizer(buf);
171: if (tok.hasMoreTokens()) {
172: tbuf = tok.nextToken();
173: if (tbuf.startsWith("public")
174: && (buf.indexOf("(") > 0
175: && buf.indexOf("{") > 0 || buf
176: .indexOf(");") > 0)) {
177: if (!fndConst) {
178: fndConst = true;
179: } else {
180: tbuf = tok.nextToken();
181: if (!tbuf.startsWith(classname)) {
182: out.print(buf.substring(0, buf
183: .indexOf(")") + 1)
184: + ";");
185: Enumeration en = updatePatterns
186: .elements();
187: while (en.hasMoreElements()) {
188: String s = (String) en
189: .nextElement();
190: if (buf.indexOf(s) != -1) {
191: out.println("//update");
192: break;
193: }
194: }
195: out.print("\n");
196: }
197: }
198: }
199: }
200: }
201: }
202: }
203: out.println("}");
204: out.close();
205:
206: } catch (Exception e) {
207: System.out.flush();
208: System.err.println(e);
209: }
210: }
211:
212: private static Vector getPatterns() {
213: BufferedReader in = null;
214: Vector lst = new Vector();
215: try {
216: in = new BufferedReader(new FileReader("int_config"), 1024);
217: String buf = null;
218: while ((buf = in.readLine()) != null) {
219: lst.addElement(buf);
220: }
221: in.close();
222: return lst;
223: } catch (Exception e) {
224: lst.addElement("delete");
225: lst.addElement("set");
226: lst.addElement("update");
227: lst.addElement("modify");
228: lst.addElement("reset");
229: return lst;
230: }
231: }
232:
233: private static String remove(String buf, String rem) {
234: try {
235: int beg;
236: int end;
237:
238: beg = buf.indexOf(rem);
239: if (beg == -1) {
240: return buf;
241: }
242: end = buf.indexOf(" ", beg);
243: if (end == -1) {
244: return buf;
245: }
246: return buf.substring(0, beg - 1) + buf.substring(end);
247: } catch (Exception e) {
248: return buf;
249: }
250:
251: }
252: }
|