KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > tools > OIG


1 // Copyright 1997-@year@ by SMB GmbH. All rights reserved.
2
// Copyright Tim Brown <Tim.Brown@incenter.org>. All rights reserved.
3
//
4
// You can redistribute this software and/or modify it under the terms of
5
// the Ozone Library License version 1 published by ozone-db.org.
6
//
7
// $Id: OIG.java,v 1.9 2000/10/28 16:55:20 daniela Exp $
8

9 package org.ozoneDB.tools;
10
11 import java.lang.*;
12 import java.util.*;
13 import java.io.*;
14
15
16 /**
17  * Ozone Interface Generator
18  *
19  * <p>This class is meant to be run statically only. It will generate an
20  * OzoneRemote interface for your OzoneObject (s)
21  *
22  * <p>USAGE:
23  *
24  * <p>&nbsp;java OzoneInterfaceGenerator your_OzoneObject
25  *
26  * <p>CONVENTIONS:
27  *
28  * <p>&nbsp;Class Names
29  *
30  * <p>&nbsp;OzoneObject classes must be named like *Impl.java. Interfaces
31  * are generated with a _Int inserted into the name.
32  *
33  * <p>File Naming:
34  *
35  * <p>OzoneObjects must have names like: CarImpl.java The interface generated
36  * will be: CarImpl_Int.java
37  *
38  * <p>You do not have to use "Impl". Any name will do. I use a standard because
39  * it is then easy to write makefile rules like:
40  *
41  * <p>%.class: %.java
42  * <br>&nbsp;&nbsp;&nbsp; javac $(JFLAGS) -classpath $(CPATH) $&lt;
43  * <p>%Impl_Proxy.class: %Impl.java
44  * <br>&nbsp;&nbsp;&nbsp;/projects/ozone/bin/opp $*Impl
45  * <p>%Impl_Int.java: %Impl.java
46  * <br>&nbsp;&nbsp;&nbsp;java -Djava.compiler=tya -classpath $(CPATH) OzoneInterfaceGenerator $*Impl
47  * <p>javasrc = $(wildcard *.java)
48  * <br>classes = $(javasrc:.java=.class)
49  * <br>htmlobjs = $(javasrc:.java=.html)
50  * <br>IntSrc = $(wildcard *Impl.java)
51  * <br>IntObjs = $(IntSrc:Impl.java=Impl_Int.java)
52  * <br>proxies = $(IntSrc:Impl.java=Impl_Proxy.class)
53  * <br>proxysrc = $(IntSrc:Impl.java=Impl.class)
54  *
55  * <p>Coding Style
56  *
57  * <p>&nbsp;Public methods must have ALL parameters on one line. Infact
58  * everything up to and including the open curly brace must be on the first
59  * line.
60  *
61  * <p>Patterns for identifying methods which should be locked are stored in
62  * a file called int_config. This file should be in the same directory as
63  * the .java files. You need to list one pattern per line. Like:
64  * <br>&nbsp;set
65  * <br>&nbsp;update
66  * <br>&nbsp;reset
67  * <br>&nbsp;delete
68  * <br>&nbsp;modify
69  * <br>&nbsp;
70  * <br>&nbsp;
71  *
72  * <p>OzoneInterfaceGenerator looks for and reads these in. If the first line
73  * of your public method contains any of these strings it appends //update
74  * to the end signaling OPP to make that method a locked() method.
75  *
76  * <p>If you do not provide a int_config file OIG uses my favorites, listed
77  * above.
78  *
79  * <p>Public Method Examples:
80  * <p>public void setName(String _name) { this.name = _name; }
81  * <br>&nbsp;in the generated interface it produces:
82  * <br>&nbsp;public void setName(String _name);//update
83  * <br>&nbsp;
84  * <br>&nbsp;
85  *
86  * <p>In the following example I use a comment to identify the update method.
87  * One of the patterns just has to be on the first line.
88  * <br>&nbsp;
89  * <br>&nbsp;
90  * <br>public void killMe() { // update
91  * <br>&nbsp;Produces:
92  * <br>&nbsp;public void killMe();//update
93  * <br>&nbsp;
94  * <br>&nbsp;
95  * <p>NOTE:
96  * <p>&nbsp;OIG will handle this:
97  * <p>&nbsp;public static final String x = new String("test");
98  * <br>&nbsp;
99  * <br>&nbsp;
100  * <p>BUT not this: (working on this :) It will get mistaken for a method.
101  * <p>&nbsp;public String x = new String("test");
102  * <br>&nbsp;
103  * <br>&nbsp;
104  * <p>
105  *
106  *
107  */

108 public class OIG {
109     
110     
111     public static void main( String JavaDoc[] args ) {
112         Vector updatePatterns = null;
113         BufferedReader in = null;
114         PrintWriter out = null;
115         
116         updatePatterns = getPatterns();
117         
118         try {
119             // first try to use args
120
in = new BufferedReader( new FileReader( args[0] + ".java" ), 1024 );
121             out = new PrintWriter( new FileWriter( args[0] + "_Int.java" ) );
122         } catch (Exception JavaDoc e) {
123             System.err.println( "Using stdin" );
124             in = new BufferedReader( new InputStreamReader( System.in ), 1024 );
125             out = new PrintWriter( System.out );
126         }
127         try {
128             String JavaDoc buf;
129             String JavaDoc tbuf;
130             StringTokenizer tok;
131             boolean fndOpen = false;
132             boolean fndConst = false;
133             String JavaDoc classname = null;
134             String JavaDoc 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" ) || buf.startsWith( "package" )) {
144                         out.println( buf );
145                     } else {
146                         
147                         if (buf.startsWith( "public class" ) || buf.startsWith( "public abstract class" )) {
148                             int beg;
149                             int end;
150                             beg = buf.indexOf( "class" ) + 6;
151                             end = buf.indexOf( " ", beg );
152                             classname = buf.substring( beg, end );
153                             intname = classname + "_Int";
154                             
155                             out.print( "import org.ozoneDB.OzoneRemote;\n" + "import org.ozoneDB.DxLib.*;\n"
156                                     + "\npublic interface " + intname + " extends OzoneRemote\n" );
157                         
158                         }
159                     }
160                 } else {
161                     if (buf.indexOf( "static" ) == -1) {
162                         buf = remove( buf, "synchronized" );
163                         tok = new StringTokenizer( buf );
164                         if (tok.hasMoreTokens()) {
165                             tbuf = tok.nextToken();
166                             if (tbuf.startsWith( "public" ) && (buf.indexOf( "(" ) > 0 && buf.indexOf( "{" ) > 0
167                                     || buf.indexOf( ");" ) > 0)) {
168                                 if (!fndConst) {
169                                     fndConst = true;
170                                 } else {
171                                     tbuf = tok.nextToken();
172                                     if (!tbuf.startsWith( classname )) {
173                                         out.print( buf.substring( 0, buf.indexOf( ")" ) + 1 ) + ";" );
174                                         Enumeration en = updatePatterns.elements();
175                                         while (en.hasMoreElements()) {
176                                             String JavaDoc s = (String JavaDoc)en.nextElement();
177                                             if (buf.indexOf( s ) != -1) {
178                                                 out.println( "//update" );
179                                                 break;
180                                             }
181                                         }
182                                         out.print( "\n" );
183                                     }
184                                 }
185                             }
186                         }
187                     }
188                 }
189             }
190             out.println( "}" );
191             out.close();
192         
193         } catch (Exception JavaDoc e) {
194             System.out.flush();
195             System.err.println( e );
196         }
197     }
198     
199     
200     private static Vector getPatterns() {
201         BufferedReader in = null;
202         Vector lst = new Vector();
203         try {
204             in = new BufferedReader( new FileReader( "int_config" ), 1024 );
205             String JavaDoc buf = null;
206             while ((buf = in.readLine()) != null) {
207                 lst.addElement( buf );
208             }
209             in.close();
210             return lst;
211         } catch (Exception JavaDoc e) {
212             lst.addElement( "delete" );
213             lst.addElement( "set" );
214             lst.addElement( "update" );
215             lst.addElement( "modify" );
216             lst.addElement( "reset" );
217             return lst;
218         }
219     }
220     
221     
222     private static String JavaDoc remove( String JavaDoc buf, String JavaDoc rem ) {
223         try {
224             int beg;
225             int end;
226             
227             beg = buf.indexOf( rem );
228             if (beg == -1) {
229                 return buf;
230             }
231             end = buf.indexOf( " ", beg );
232             if (end == -1) {
233                 return buf;
234             }
235             return buf.substring( 0, beg - 1 ) + buf.substring( end );
236         } catch (Exception JavaDoc e) {
237             return buf;
238         }
239     
240     }
241 }
242
Popular Tags