1 package xdoclet.junit; 2 3 17 18 import java.io.*; 19 import java.lang.reflect.Method ; 20 import java.util.ArrayList ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 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 36 public class XDocletRunner extends Thread 37 { 38 private FileHandling _classWriter = new FileHandling(); 39 private ArrayList _srcFiles = new ArrayList (); 40 private ArrayList _srcDirectories = new ArrayList (); 41 private File _destFile = null; 42 private String _taskName = null; 43 private String _subTaskName = null; 44 private HashMap _taskProperties = new HashMap (); 45 private HashMap _subTaskProperties = new HashMap (); 46 private String _result = null; 47 48 public XDocletRunner() 49 { 50 super("XDocletRunner"); 51 } 52 53 public void addClass(String name, String content) 54 { 55 try 56 { 57 _srcFiles.add(_classWriter.write(name, content)); 58 } 59 catch (IOException ex) 60 { 61 cleanFiles(); 62 throw new RuntimeException (ex); 63 } 64 } 65 66 public void setDestFile(String path) 67 { 68 _destFile = _classWriter.ensurePath(path); 69 _destFile.delete(); 70 } 71 72 public void setTaskName(String className) 73 { 74 _taskName = className; 75 } 76 77 public void setSubTaskName(String className) 78 { 79 _subTaskName = className; 80 } 81 82 public void setTaskProperty(String name, Object value) 83 { 84 _taskProperties.put(name, value); 85 } 86 87 public void setSubTaskProperty(String name, Object value) 88 { 89 _subTaskProperties.put(name, value); 90 } 91 92 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 ex) 110 { 111 cleanFiles(); 112 throw new RuntimeException (ex); 113 } 114 115 try 116 { 117 task.setVerbose(false); 118 task.init(); 119 task.execute(); 120 } 121 catch (Exception ex) 122 { 123 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 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 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 includes = new StringBuffer (); 170 171 for (Iterator 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 name; 184 185 for (Iterator it = _taskProperties.keySet().iterator(); it.hasNext();) 186 { 187 name = (String )it.next(); 188 setProperty(task, name, _taskProperties.get(name)); 189 } 190 191 return task; 192 } 193 194 private XmlSubTask initSubTask() throws Exception 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 name; 205 206 for (Iterator it = _subTaskProperties.keySet().iterator(); it.hasNext();) 207 { 208 name = (String )it.next(); 209 setProperty(subTask, name, _subTaskProperties.get(name)); 210 } 211 212 return subTask; 213 } 214 215 private void setProperty(Object obj, String propertyName, Object propertyValue) throws Exception 216 { 217 String methodName = "set"+Character.toUpperCase(propertyName.charAt(0)); 218 219 if (propertyName.length() > 1) 220 { 221 methodName += propertyName.substring(1); 222 } 223 224 Method method = null; 225 226 try 227 { 228 method = obj.getClass().getMethod(methodName, new Class []{propertyValue.getClass()}); 229 } 230 catch (NoSuchMethodException ex) 231 { 232 Method [] 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 []{propertyValue}); 249 } 250 251 private String getDestFileContents(String filename) throws IOException 252 { 253 BufferedReader input = new BufferedReader(new FileReader(filename)); 254 StringBuffer result = new StringBuffer (); 255 String 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 |