KickJava   Java API By Example, From Geeks To Geeks.

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


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: OPPTask.java,v 1.4 2003/11/27 19:34:05 per_nyfelt Exp $
6
package org.ozoneDB.tools.OPP;
7
8 import org.apache.tools.ant.*;
9 import org.apache.tools.ant.util.LoaderUtils;
10 import org.apache.tools.ant.types.FileSet;
11 import org.apache.tools.ant.types.Path;
12 import org.ozoneDB.tools.OPP.OPPBean;
13 import org.ozoneDB.tools.OPP.srcgen.ClassDirector;
14 import org.ozoneDB.tools.OPP.srcgen.ClassBuilder;
15 import org.ozoneDB.tools.OPP.srcgen.BuilderException;
16 import org.ozoneDB.tools.OPP.message.MessageWriter;
17
18 import java.io.*;
19 import java.util.StringTokenizer JavaDoc;
20
21 public class OPPTask extends Task {
22     private FileSet source;
23     File output;
24     boolean quiet = false;
25     private final OPPBean oppBean = new OPPBean();
26     private final MessageWriter genListener = new AntLogOPPGenerationeventListener();
27     private Path classPath = null;
28
29     public void setClasspath(Path classpath) {
30         this.classPath = classpath;
31     }
32
33     public Path createClasspath() {
34         if (classPath == null) {
35             classPath = new Path(project);
36         }
37         return classPath.createPath();
38     }
39
40     public void addConfiguredSource(FileSet source) {
41         this.source = source;
42     }
43
44     public void setOutput(File output) {
45         this.output = output;
46     }
47
48     private String JavaDoc convertFileToClassName(String JavaDoc file) {
49         if ((file == null) || file.equals(""))
50             return "";
51         String JavaDoc token;
52         StringBuffer JavaDoc className = new StringBuffer JavaDoc();
53         for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(file, File.separator); st.hasMoreTokens();) {
54             token = st.nextToken();
55             if (st.hasMoreTokens()) {
56                 className.append(token + '.');
57             } else {
58                 className.append(token.substring(0, token.lastIndexOf('.')));
59             }
60         }
61         return className.toString();
62     }
63
64     private void verifyParameters() {
65         if (output == null) {
66             output = getProject().getBaseDir();
67         }
68         if (source == null) {
69             throw new BuildException("resolver must be specified");
70         }
71     }
72
73     public void setCache(boolean cache) {
74         oppBean.setCache(cache);
75     }
76
77     /* public void setNoDirectInvokes(boolean noDirectInvokes)
78      {
79        oppBean.setNoDirectInvokes(noDirectInvokes);
80      }*/

81
82     public void setUpdateExpression(String JavaDoc updateExpression) {
83         oppBean.setUpdateExpression(updateExpression);
84     }
85
86     /* public void setIgnorePackageNames(boolean ignorePackageNames)
87      {
88        oppBean.setIgnorePackageNames(ignorePackageNames);
89      }*/

90
91     /*public void setSaveSource(boolean saveSource)
92     {
93       oppBean.setSaveSource(saveSource);
94     } */

95
96     public void setPrintStackTrace(boolean printStackTrace) {
97         oppBean.setPrintStackTrace(printStackTrace);
98     }
99
100     void setGenerate_ocd(boolean value) {
101         oppBean.setGenerateOcd(value);
102     }
103
104     void setGenerate_factory(boolean value) {
105         oppBean.setGenerateFactory(value);
106     }
107
108     void setGenerate_proxy(boolean value) {
109         oppBean.setGenerateProxy(value);
110     }
111
112     void setUse_ocd(boolean value) {
113         oppBean.setUseOcd(value);
114     }
115
116     void setUse_source(boolean value) {
117         oppBean.setUseSource(value);
118     }
119
120     public void execute() throws BuildException {
121         oppBean.addGenrationEventListener(genListener);
122         verifyParameters();
123         DirectoryScanner scanner = source.getDirectoryScanner(project);
124         scanner.scan();
125         String JavaDoc files[] = scanner.getIncludedFiles();
126
127         ClassDirector cd = oppBean.createDirector(scanner.getBasedir());
128         ClassBuilder cb = oppBean.createBuilder(output, "Factory", "_Proxy");
129
130         for (int i = 0; i < files.length; i++) {
131             String JavaDoc className = convertFileToClassName(files[i]);
132             final ClassLoader JavaDoc loader = getConfiguredClassLoader();
133             final ClassLoader JavaDoc oldLoader = LoaderUtils.getContextClassLoader();
134             log("Loader is set");
135             try {
136                 LoaderUtils.setContextClassLoader(loader);
137                 try {
138
139                     log("Begin build");
140                     cd.build(className, cb);
141                     log("End build");
142                 } finally {
143                     LoaderUtils.setContextClassLoader(oldLoader);
144                 }
145             } catch (BuilderException e) {
146                 throw new BuildException(e);
147             }
148         }
149     }
150
151     private ClassLoader JavaDoc getConfiguredClassLoader() {
152         if (classPath == null) {
153             return this.getClass().getClassLoader();
154         } else {
155             return new AntClassLoader(this.getClass().getClassLoader(), getProject(), classPath, false);
156         }
157     }
158
159     class AntLogOPPGenerationeventListener implements MessageWriter {
160         public void startGeneration(String JavaDoc object) {
161             //
162
}
163
164         public void error(String JavaDoc message) {
165             log(message, Project.MSG_ERR);
166         }
167
168         public void warning(String JavaDoc message) {
169             log(message, Project.MSG_WARN);
170         }
171
172         public void warning(String JavaDoc filename, int row, String JavaDoc message) {
173             log(filename + " [" + row + "]: " + message, Project.MSG_WARN);
174         }
175
176         public void info(String JavaDoc message) {
177             log(message, Project.MSG_INFO);
178         }
179
180         public void debug(String JavaDoc message) {
181             log(message, Project.MSG_DEBUG);
182         }
183
184         public void endGeneration() {
185         }
186     }
187 }
188
189
Popular Tags