KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > junit > XDocletRunner


1 package xdoclet.junit;
2
3 /* Copyright 2003-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import java.io.*;
19 import java.lang.reflect.Method JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 import org.apache.tools.ant.Project;
25 import org.apache.tools.ant.types.FileSet;
26
27 import xdoclet.DocletTask;
28 import xdoclet.XmlSubTask;
29 import xdoclet.template.TemplateEngine;
30
31 /**
32  * Runs xdoclet in a separate thread (with a special class loader).
33  *
34  * @author <a HREF="mailto:tomdz@users.sourceforge.net">Thomas Dudziak (tomdz@users.sourceforge.net)</a>
35  */

36 public class XDocletRunner extends Thread JavaDoc
37 {
38     private FileHandling _classWriter = new FileHandling();
39     private ArrayList JavaDoc _srcFiles = new ArrayList JavaDoc();
40     private ArrayList JavaDoc _srcDirectories = new ArrayList JavaDoc();
41     private File _destFile = null;
42     private String JavaDoc _taskName = null;
43     private String JavaDoc _subTaskName = null;
44     private HashMap JavaDoc _taskProperties = new HashMap JavaDoc();
45     private HashMap JavaDoc _subTaskProperties = new HashMap JavaDoc();
46     private String JavaDoc _result = null;
47
48     public XDocletRunner()
49     {
50         super("XDocletRunner");
51     }
52
53     public void addClass(String JavaDoc name, String JavaDoc content)
54     {
55         try
56         {
57             _srcFiles.add(_classWriter.write(name, content));
58         }
59         catch (IOException ex)
60         {
61             cleanFiles();
62             throw new RuntimeException JavaDoc(ex);
63         }
64     }
65
66     public void setDestFile(String JavaDoc path)
67     {
68         _destFile = _classWriter.ensurePath(path);
69         _destFile.delete();
70     }
71
72     public void setTaskName(String JavaDoc className)
73     {
74         _taskName = className;
75     }
76
77     public void setSubTaskName(String JavaDoc className)
78     {
79         _subTaskName = className;
80     }
81
82     public void setTaskProperty(String JavaDoc name, Object JavaDoc value)
83     {
84         _taskProperties.put(name, value);
85     }
86
87     public void setSubTaskProperty(String JavaDoc name, Object JavaDoc value)
88     {
89         _subTaskProperties.put(name, value);
90     }
91
92     /* (non-Javadoc)
93      * @see java.lang.Runnable#run()
94      */

95     public void run()
96     {
97         _result = null;
98
99         DocletTask task = null;
100
101         try
102         {
103             task = initTask();
104             if (_subTaskName != null)
105             {
106                 task.addSubTask(initSubTask());
107             }
108         }
109         catch (Exception JavaDoc ex)
110         {
111             cleanFiles();
112             throw new RuntimeException JavaDoc(ex);
113         }
114
115         try
116         {
117             task.setVerbose(false);
118             task.init();
119             task.execute();
120         }
121         catch (Exception JavaDoc ex)
122         {
123             // what to do with any exception ? rethrow it ?
124
cleanFiles();
125             return;
126         }
127
128         if (_destFile != null)
129         {
130             if (_destFile.exists())
131             {
132                 try
133                 {
134                     _result = getDestFileContents(_destFile.getAbsolutePath());
135                 }
136                 catch (IOException ex)
137                 {
138                 }
139             }
140         }
141         cleanFiles();
142     }
143
144     public String JavaDoc getResult()
145     {
146         return _result;
147     }
148
149     public void destroy()
150     {
151         cleanFiles();
152     }
153
154     private void cleanFiles()
155     {
156         _destFile = null;
157         _srcFiles.clear();
158         _classWriter.removeTmpDir();
159     }
160
161     private DocletTask initTask() throws Exception JavaDoc
162     {
163         DocletTask task = (DocletTask)this.getContextClassLoader().loadClass(_taskName).newInstance();
164         FileSet files = new FileSet();
165
166         task.setDestDir(_classWriter.getTmpDir());
167         files.setDir(_classWriter.getTmpDir());
168
169         StringBuffer JavaDoc includes = new StringBuffer JavaDoc();
170         
171         for (Iterator JavaDoc it = _srcFiles.iterator() ; it.hasNext();)
172         {
173             if (includes.length() > 0)
174             {
175                 includes.append(" ");
176             }
177             includes.append(((File)it.next()).getName());
178         }
179
180         task.addFileset(files);
181         task.setProject(new Project());
182
183         String JavaDoc name;
184
185         for (Iterator JavaDoc it = _taskProperties.keySet().iterator(); it.hasNext();)
186         {
187             name = (String JavaDoc)it.next();
188             setProperty(task, name, _taskProperties.get(name));
189         }
190
191         return task;
192     }
193
194     private XmlSubTask initSubTask() throws Exception JavaDoc
195     {
196         XmlSubTask subTask = (XmlSubTask)this.getContextClassLoader().loadClass(_subTaskName).newInstance();
197
198         subTask.setEngine(TemplateEngine.getEngineInstance());
199         if (_destFile != null)
200         {
201             subTask.setDestinationFile(_destFile.getName());
202         }
203
204         String JavaDoc name;
205
206         for (Iterator JavaDoc it = _subTaskProperties.keySet().iterator(); it.hasNext();)
207         {
208             name = (String JavaDoc)it.next();
209             setProperty(subTask, name, _subTaskProperties.get(name));
210         }
211
212         return subTask;
213     }
214
215     private void setProperty(Object JavaDoc obj, String JavaDoc propertyName, Object JavaDoc propertyValue) throws Exception JavaDoc
216     {
217         String JavaDoc methodName = "set"+Character.toUpperCase(propertyName.charAt(0));
218
219         if (propertyName.length() > 1)
220         {
221             methodName += propertyName.substring(1);
222         }
223
224         Method JavaDoc method = null;
225
226         try
227         {
228             method = obj.getClass().getMethod(methodName, new Class JavaDoc[]{propertyValue.getClass()});
229         }
230         catch (NoSuchMethodException JavaDoc ex)
231         {
232             // we trying any method with that name then
233
Method JavaDoc[] methods = obj.getClass().getMethods();
234
235             for (int idx = 0; idx < methods.length; idx++)
236             {
237                 if (methodName.equals(methods[idx].getName()))
238                 {
239                     method = methods[idx];
240                     break;
241                 }
242             }
243             if (method == null)
244             {
245                 throw ex;
246             }
247         }
248         method.invoke(obj, new Object JavaDoc[]{propertyValue});
249     }
250
251     private String JavaDoc getDestFileContents(String JavaDoc filename) throws IOException
252     {
253         BufferedReader input = new BufferedReader(new FileReader(filename));
254         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
255         String JavaDoc line;
256
257         while ((line = input.readLine()) != null)
258         {
259             result.append(line);
260             result.append("\n");
261         }
262         input.close();
263         return result.toString().trim();
264     }
265 }
266
Popular Tags