KickJava   Java API By Example, From Geeks To Geeks.

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


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id$
8
package org.ozoneDB.tools.OPP;
9
10 import java.io.File JavaDoc;
11 import java.util.*;
12
13 import org.ozoneDB.core.ObjectContainer;
14 import org.ozoneDB.core.helper.ReflectionHelper;
15 import org.ozoneDB.tools.OPP.compiler.JavaCompiler;
16 import org.ozoneDB.tools.OPP.compiler.CompilerException;
17 import org.ozoneDB.tools.OPP.message.MessageWriter;
18 import org.ozoneDB.tools.OPP.message.SummaryMessageWriterDecorator;
19 import org.ozoneDB.tools.OPP.message.StdOutMessageWriter;
20 import org.ozoneDB.tools.OPP.srcgen.ClassDirector;
21 import org.ozoneDB.tools.OPP.srcgen.ClassBuilder;
22
23 /**
24  * Command line driver of the OPP tool.
25  *
26  * @author <a HREF="http://www.softwarebuero.de/">SMB</a>
27  * @version $Revision$Date$2
28  */

29 public class OPP {
30
31     public final static String JavaDoc SIGNATURE_DELIMITER = ReflectionHelper.SIGNATURE_DELIMITER;
32
33     /**
34      * Update methods can marked with the following in the lines following the method signature:
35      * <pre>
36      * //update
37      * </pre>
38      */

39     public final static String JavaDoc UPDATE_SIGN = "/[*]+ *update *[*]/|// *update";
40
41     /**
42      * All method signatures in build interfaces must look as foollows,
43      * otherwise OPP is unable to find them:
44      * <pre>
45      * public package.return.Class[] methodname (
46      * </pre>
47      *
48      */

49     public final static String JavaDoc METHOD_PATTERN
50             = "^[ \\t]*public[ \\t]+[_a-zA-Z][_a-zA-Z0-9\\.\\[\\]]*[ \\t]+([_a-zA-Z][\\w]*)[ \\t]*\\(";
51
52     /**
53      * Update methods can marked with the following tag in its appropriate Javadoc Comment:
54      * <pre>
55      * "@update"
56      * </pre>
57      */

58     public final static String JavaDoc JAVADOC_PATTERN
59             = "^[ \\t]+\\*[ \\t]*@update";
60     private boolean keepSource = false;
61     private boolean compileSource = true;
62
63     // private boolean printStackTrace = false;
64

65     private String JavaDoc outputDirName = "." + File.separator;
66     private String JavaDoc sourceDirName = "." + File.separator;
67     private StdOutMessageWriter msgOut = new StdOutMessageWriter(false, false);
68     private OPPBean oppBean = new OPPBean();
69     private boolean odmg = false;
70
71     //private Collection classes;
72

73
74     public OPP() {
75     }
76
77     public static void main(String JavaDoc[] args) {
78         Set classes = new HashSet();
79         if (args.length == 0) {
80             printUsage();
81             System.exit(0);
82         }
83         OPP opp = new OPP();
84         boolean preservePackageNames = true;
85         for (int argCount = 0; argCount < args.length; argCount++) {
86             if (args[argCount].equals("-q")) {
87                 opp.msgOut.setQuiet(true);
88             } else if (args[argCount].equals("-ks")) {
89                 opp.keepSource = true;
90             } else if (args[argCount].equals("-KS")) {
91                 opp.keepSource = true;
92                 opp.compileSource = false;
93             } else if (args[argCount].equals("-odmg")) {
94                 opp.odmg = true;
95             } else if (args[argCount].equals("-nc")) {
96                 opp.oppBean.setCache(false);
97             } else if (args[argCount].equals("-nf")) {
98                 opp.oppBean.setGenerateFactory(false);
99             } else if (args[argCount].equals("-st")) {
100                 opp.oppBean.setPrintStackTrace(true);
101             } else if (args[argCount].equals("-ni")) {
102                 opp.oppBean.setUseSource(false);
103             } else if (args[argCount].startsWith("-p")) {
104                 opp.oppBean.setUpdateExpression(args[argCount].substring(2));
105             } else if (args[argCount].equals("-version")) {
106                 System.out.println("$Id$");
107                 System.exit(0);
108             } else if (args[argCount].equals("-h")) {
109                 opp.printUsage();
110                 System.exit(0);
111             } else if (args[argCount].startsWith("-o")) {
112                 opp.outputDirName = args[argCount].substring(2) + File.separator;
113             } else if (args[argCount].startsWith("-s")) {
114                 opp.sourceDirName = args[argCount].substring(2) + File.separator;
115             } else if (args[argCount].equals("-ip")) {
116                 preservePackageNames = false;
117             } else {
118                 if (args[argCount].startsWith("-")) {
119                     System.out.println("Unknown option '" + args[argCount] + "'!\n");
120                     printUsage();
121                     System.exit(0);
122                 } else {
123                     classes.add(args[argCount]);
124                 }
125             }
126         }
127         opp.generate(classes, preservePackageNames);
128     }
129
130     private void generate(Collection classes, boolean preservePackageNames) {
131         MessageWriter genListener = new SummaryMessageWriterDecorator(msgOut);
132         oppBean.removeGenrationEventListener(msgOut);
133         oppBean.addGenrationEventListener(genListener);
134         genListener.startGeneration("OPP");
135
136         // We internally use the new directory parameter format,
137
// but the user has specified the directory parameters in the old format
138
// where packages were ignored
139
try {
140             if (!preservePackageNames) {
141                 Iterator iter = classes.iterator();
142                 if (iter.hasNext()) {
143                     String JavaDoc className = (String JavaDoc) iter.next();
144                     if (className != null) {
145                         int dotCount = 0;
146                         StringTokenizer st = new StringTokenizer(className, ".");
147                         while (st.hasMoreTokens()) {
148                             dotCount++;
149                             st.nextToken();
150                         }
151                         outputDirName = OPP.parentDirectoryName(outputDirName, dotCount);
152                         sourceDirName = OPP.parentDirectoryName(sourceDirName, dotCount);
153                     }
154                 }
155             }
156             try {
157                 File JavaDoc outputDir = new File JavaDoc(outputDirName);
158                 File JavaDoc sourceDir = new File JavaDoc(sourceDirName);
159                 makeProxiesAndFactories(classes, sourceDir, outputDir, "Factory", ObjectContainer.PROXYNAME_POSTFIX, genListener);
160                 if (compileSource) {
161                     // the proxy source will be in the output dir
162
compileSource(outputDir, outputDir, genListener, classes);
163                 }
164                 if (odmg) {
165                     genListener.startGeneration("ODMG");
166                     try {
167                         for (Iterator iter = classes.iterator(); iter.hasNext();) {
168                             String JavaDoc className = (String JavaDoc) iter.next();
169                             manipulateClass(className, genListener);
170                             manipulateClass(className + ObjectContainer.PROXYNAME_POSTFIX, genListener);
171                         }
172                     } finally {
173                         genListener.endGeneration();
174                     }
175                 }
176             } catch (Exception JavaDoc e) {
177                 e.printStackTrace(System.out);
178                 System.exit(1);
179             }
180         } finally {
181             genListener.endGeneration();
182         }
183     }
184
185     private void manipulateClass(String JavaDoc className, MessageWriter genListener) throws Exception JavaDoc {
186         Class JavaDoc cl = Class.forName(className);
187         ImplManipulator manipulator = new ImplManipulator(outputDirName, genListener, cl.getClassLoader());
188         String JavaDoc newClassName = cl.getName() + ObjectContainer.IMPLNAME_POSTFIX;
189         manipulator.changeClassFile(null, outputDirName + OPPHelper.classFileBasename(cl) + ".class", newClassName);
190     }
191
192     private void compileSource(File JavaDoc sourceDir, File JavaDoc outputDir, MessageWriter genListener, Collection classes) throws CompilerException {
193         JavaCompiler compiler = OPPCompilerFactory.getCompilerInstance(sourceDir, outputDir);
194         genListener.startGeneration("Compile proxies");
195         try {
196             compiler.compile(getModifiedNames(sourceDir, classes, ObjectContainer.PROXYNAME_POSTFIX));
197         } finally {
198             genListener.endGeneration();
199         }
200         genListener.startGeneration("Compile factories");
201         try {
202             compiler.compile(getModifiedNames(sourceDir, classes, "Factory"));
203         } finally {
204             genListener.endGeneration();
205         }
206     }
207
208     private Collection getModifiedNames(File JavaDoc sourceDir, Collection classes, String JavaDoc postfix) {
209         Collection modified = new ArrayList(classes.size());
210         for (Iterator iter = classes.iterator(); iter.hasNext();) {
211             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(sourceDir.getPath());
212             buffer.append(File.separatorChar);
213             buffer.append((iter.next() + postfix).replace('.', File.separatorChar));
214             buffer.append(".java");
215             modified.add(buffer.toString());
216         }
217         return modified;
218     }
219
220     protected static String JavaDoc parentDirectoryName(String JavaDoc directoryName, int dotCount) {
221         StringBuffer JavaDoc b = new StringBuffer JavaDoc(directoryName);
222         for (int i = 0; i < dotCount; i++) {
223             b.append("..");
224             b.append(File.separatorChar);
225         }
226         return b.toString();
227     }
228
229
230     protected void makeProxiesAndFactories(Collection classes, File JavaDoc sourceDir, File JavaDoc outputDir, String JavaDoc factoryPost, String JavaDoc proxyPost, MessageWriter msgListener) throws Exception JavaDoc {
231         Iterator it = classes.iterator();
232         ClassDirector cd = oppBean.createDirector(sourceDir);
233         ClassBuilder cb = oppBean.createBuilder(outputDir, factoryPost, proxyPost);
234         while (it.hasNext()) {
235             String JavaDoc name = (String JavaDoc) it.next();
236             msgListener.info("Processing class: " + name);
237             cd.build(name, cb);
238         }
239     }
240
241     public static void printUsage() {
242         System.out.println("Ozone Post Processor");
243         System.out.println(
244                 "usage: opp [-ks] [-st] [-p<pattern>] [-ni] [-nf] [-nc] [-q] [-h] [-o<directory>] [-odmg] [-ip] class [class]*");
245         System.out.println(" -ks save the generated resolver files");
246         System.out.println(" -KS save the generated resolver files; do not invoke compiler");
247         System.out.println(" -st print stack trace");
248         System.out.println(" -p regular expression to specify update methods");
249         System.out.println(" -ni do not search interface code for update methods");
250         System.out.println(" -nf do not create a Factory class");
251         System.out.println(" -q supress output of any messages");
252         System.out.println(" -o output directory");
253         System.out.println(" -s resolver directory");
254         System.out.println(" -odmg create proxies for the ozone ODMG interface");
255         System.out.println(" -ip ignore package names");
256         System.out.println(" -nc do not create code needed for direct invokes and ClientCacheDatabase");
257         System.out.println(" -version shows version information");
258         System.out.println(" -h shows this help");
259     }
260 }
Popular Tags