KickJava   Java API By Example, From Geeks To Geeks.

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


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
//
5
// $Id: OPPBean.java,v 1.4.2.1 2004/01/11 20:42:34 per_nyfelt Exp $
6
package org.ozoneDB.tools.OPP;
7
8 import org.ozoneDB.tools.OPP.srcgen.ClassBuilder;
9 import org.ozoneDB.tools.OPP.srcgen.ClassDirector;
10 import org.ozoneDB.tools.OPP.srcgen.builder.*;
11 import org.ozoneDB.tools.OPP.srcgen.builder.CompositeBuilder;
12 import org.ozoneDB.tools.OPP.srcgen.builder.FactoryBuilder;
13 import org.ozoneDB.tools.OPP.srcgen.builder.MethodNameUpgradingClassBuilderDecorator;
14 import org.ozoneDB.tools.OPP.srcgen.builder.OcdBuilder;
15 import org.ozoneDB.tools.OPP.srcgen.query.ReflectionClassQuery;
16 import org.ozoneDB.tools.OPP.srcgen.streamfactory.InputStreamFactory;
17 import org.ozoneDB.tools.OPP.srcgen.streamfactory.FileInputStreamFactory;
18 import org.ozoneDB.tools.OPP.srcgen.streamfactory.OutputStreamFactory;
19 import org.ozoneDB.tools.OPP.srcgen.streamfactory.FileOutputStreamFactory;
20 import org.ozoneDB.tools.OPP.srcgen.direct.ReflectionClassDirector;
21 import org.ozoneDB.tools.OPP.srcgen.resolve.ClassDescriptorUpdateMethodResolver;
22 import org.ozoneDB.tools.OPP.srcgen.resolve.CompositeMethodResolver;
23 import org.ozoneDB.tools.OPP.srcgen.resolve.SourceUpdateMethodResolver;
24 import org.ozoneDB.tools.OPP.message.CompositeOPPMessageListener;
25 import org.ozoneDB.tools.OPP.message.MessageWriter;
26 import org.ozoneDB.tools.OPP.message.SummaryMessageWriterDecorator;
27
28 import java.io.*;
29
30 /**
31  * API for running the OPP code. This class makes it simplier to JUnit test
32  * the ant task by being able to testrun the opp functionality without having
33  * the ant environment running.<br/>
34  *
35  * The OPP bean does not have all the functionality that the OPP command line
36  * tool does.<br/>
37  * <list>
38  * <li>The OPPBean does not support compiling proxies or factories</li>
39  * <li>The OPPBean does not support odmg postprocessing because the current
40  * implementation implies compilation </li>
41  *
42  * Date: 2003-sep-09
43  * Time: 19:53:47
44  * @author Joakim Ohlrogge
45  */

46 public class OPPBean {
47     private boolean printStackTrace = false;
48     private String JavaDoc updateExpression = ".*_update";
49     private boolean cache = true;
50     private CompositeOPPMessageListener compListener = new CompositeOPPMessageListener();
51     private SummaryMessageWriterDecorator summaryListenerDecorator;
52     private boolean useOcd = true;
53     private boolean useSource = true;
54     private boolean generateFactory = true;
55     private boolean generateProxy = true;
56     private boolean generateOcd = false;
57
58     public OPPBean() {
59         summaryListenerDecorator = new SummaryMessageWriterDecorator(compListener);
60     }
61
62     public boolean isUseOcd() {
63         return useOcd;
64     }
65
66     public void setUseOcd(boolean useOcd) {
67         this.useOcd = useOcd;
68     }
69
70     public boolean isUseSource() {
71         return useSource;
72     }
73
74     public void setUseSource(boolean useSource) {
75         this.useSource = useSource;
76     }
77
78     public boolean isGenerateFactory() {
79         return generateFactory;
80     }
81
82     public void setGenerateFactory(boolean generateFactory) {
83         this.generateFactory = generateFactory;
84     }
85
86     public boolean isGenerateProxy() {
87         return generateProxy;
88     }
89
90     public void setGenerateProxy(boolean generateProxy) {
91         this.generateProxy = generateProxy;
92     }
93
94     public boolean isGenerateOcd() {
95         return generateOcd;
96     }
97
98     public void setGenerateOcd(boolean generateOcd) {
99         this.generateOcd = generateOcd;
100     }
101
102     private MessageWriter getSummaryListener() {
103         return summaryListenerDecorator;
104     }
105
106     public void setCache(boolean cache) {
107         this.cache = cache;
108     }
109
110     public void setUpdateExpression(String JavaDoc updateExpression) {
111         this.updateExpression = updateExpression;
112     }
113
114     public void setPrintStackTrace(boolean printStackTrace) {
115         this.printStackTrace = printStackTrace;
116     }
117
118     public ClassDirector createDirector(InputStreamFactory source, InputStreamFactory ocdsource) {
119         CompositeMethodResolver resolver = new CompositeMethodResolver();
120         if (source != null) {
121             resolver.addResolver(new SourceUpdateMethodResolver(source, getSummaryListener(), new ReflectionClassQuery()));
122         }
123         if (ocdsource != null) {
124             resolver.addResolver(new ClassDescriptorUpdateMethodResolver(ocdsource, getSummaryListener()));
125         }
126         return new ReflectionClassDirector(getSummaryListener(), resolver);
127     }
128
129     /**
130      * Creates a class direct for the class identified by the class string.
131      * The method resolvers are created based on the value of getUseOcd and getUseSource.
132      */

133     public ClassDirector createDirector(File sourceDir) {
134         InputStreamFactory ocdFactory = null;
135         InputStreamFactory sourceFactory = null;
136         if (useOcd) {
137             ocdFactory = new FileInputStreamFactory(sourceDir, "ocd");
138         }
139         if (useSource) {
140             sourceFactory = new FileInputStreamFactory(sourceDir, "java");
141         }
142         return createDirector(sourceFactory, ocdFactory);
143     }
144
145     public ClassBuilder createBuilder(OutputStreamFactory factory, String JavaDoc factoryPost, OutputStreamFactory proxy, String JavaDoc proxyPost, OutputStreamFactory ocd) {
146         CompositeBuilder builder = new CompositeBuilder();
147         if (factory != null) {
148             builder.addBuilder(new FactoryBuilder(factory, factoryPost));
149         }
150         if (proxy != null) {
151             ClassBuilder proxyBuilder = new ProxyBuilder(proxy, proxyPost, printStackTrace, cache, getSummaryListener());
152             // Allow promotion of methods
153
MethodNameUpgradingClassBuilderDecorator decorator =
154                     new MethodNameUpgradingClassBuilderDecorator(proxyBuilder, updateExpression);
155             builder.addBuilder(decorator);
156         }
157         if (ocd != null) {
158             builder.addBuilder(new OcdBuilder(ocd));
159         }
160         return builder;
161     }
162
163     public ClassBuilder createBuilder(File outputDir, String JavaDoc factoryPost, String JavaDoc proxyPost) {
164         OutputStreamFactory factoryOsFactory = null;
165         OutputStreamFactory proxyOsFactory = null;
166         OutputStreamFactory ocdOsFactory = null;
167         if (generateFactory) {
168             factoryOsFactory = new FileOutputStreamFactory(outputDir, "java");
169         }
170         if (generateProxy) {
171             proxyOsFactory = new FileOutputStreamFactory(outputDir, "java");
172         }
173         if (generateOcd) {
174             ocdOsFactory = new FileOutputStreamFactory(outputDir, "ocd");
175         }
176         return createBuilder(factoryOsFactory, factoryPost, proxyOsFactory, proxyPost, ocdOsFactory);
177     }
178
179     public void addGenrationEventListener(MessageWriter listener) {
180         compListener.addListener(listener);
181     }
182
183     public void removeGenrationEventListener(MessageWriter listener) {
184         compListener.removeListener(listener);
185     }
186 }
187
Popular Tags