1 11 package org.ashkelon.ant; 12 13 import java.io.File ; 14 import java.io.FileReader ; 15 import java.util.ArrayList ; 16 import java.util.Arrays ; 17 import java.util.Collection ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 21 import org.apache.tools.ant.BuildException; 22 import org.apache.tools.ant.taskdefs.Javadoc; 23 import org.apache.tools.ant.types.PatternSet; 24 import org.ashkelon.API; 25 import org.ashkelon.Ashkelon; 26 import org.ashkelon.util.Logger; 27 28 34 public class AshkelonTask extends Javadoc 35 { 36 39 public AshkelonTask() 40 { 41 super(); 42 } 43 44 49 public void execute() throws BuildException 50 { 51 Logger log = Logger.getInstance(); 52 log.setPrefix(_PROGRAM_NAME); 53 if (_verbose) 54 { 55 log.setTraceLevel(Logger.VERBOSE); 56 } 57 if (_debug) 58 { 59 log.setTraceLevel(Logger.DEBUG); 60 } 61 try 62 { 63 _parameterCheck(); 64 if (_operation.compareTo(_RESET_OPERATION) == 0) 65 { 66 Ashkelon.resetCmd(); 67 } 68 else if (_operation.compareTo(_LIST_OPERATION) == 0) 69 { 70 Ashkelon.listCmd(); 72 } 73 else if (_operation.compareTo(_REMOVE_OPERATION) == 0) 74 { 75 String [] includes = 76 _includePatterns.getIncludePatterns(getProject()); 77 includes = 78 _appendStringPrefixToArray(new String ("remove"), includes); 79 Ashkelon.removeCmd(includes); 80 } 81 else if (_operation.compareTo(_UPDATEREFS_OPERATION) == 0) 82 { 83 Ashkelon.updateRefsCmd(); 84 } 85 else if (_operation.compareTo(_ADD_OPERATION) == 0) 86 { 87 FileReader reader = new FileReader (_descriptionFile); 94 API api = new API(); 95 api = api.load(reader); 96 reader.close(); 97 Collection packagenames = api.getPackagenames(); 99 Iterator iterator = packagenames.iterator(); 100 while (iterator.hasNext()) 101 { 102 Object item = iterator.next(); 103 PackageName pn = new PackageName(); 104 pn.setName(item.toString()); 105 addPackage(pn); 106 } 107 super.execute(); 109 } 110 } 111 catch (BuildException bex) 112 { 113 throw bex; 114 } 115 catch (Exception ex) 116 { 117 ex.printStackTrace(log.getWriter()); 118 throw new BuildException( 119 _PROGRAM_NAME + " task threw an exception", 120 ex); 121 } 122 } 123 124 public void setApi(File f) throws BuildException 125 { 126 _descriptionFile = f; 127 } 128 129 public void setOperation(String op) 130 { 131 _operation = op; 132 } 133 134 public void setVerbose(boolean v) 135 { 136 _verbose = v; 137 } 138 139 public void setDebug(boolean d) 140 { 141 _debug = d; 142 } 143 144 147 public PatternSet.NameEntry createInclude() 148 { 149 return _includePatterns.createInclude(); 150 } 151 152 public PatternSet getIncludes() 153 { 154 return _includePatterns; 155 } 156 161 private boolean _isOperationSet() 162 { 163 return !(_operation == null); 164 } 165 166 173 private String [] _appendStringPrefixToArray(String aStr, String [] aArray) 174 { 175 List al = new ArrayList (Arrays.asList(aArray)); 176 al.add(0, new String (aStr)); 177 String [] newArray = (String []) al.toArray(new String [0]); 178 return newArray; 179 } 180 181 186 private boolean _isDescriptionFileSet() 187 { 188 return !(_descriptionFile == null); 189 } 190 191 196 private void _parameterCheck() throws BuildException 197 { 198 if (_isOperationSet()) 200 { 201 boolean match = false; 202 for (int i = 0; i < _VALID_OPERATIONS.length; i++) 203 { 204 if (_operation.compareTo(_VALID_OPERATIONS[i]) == 0) 205 { 206 match = true; 207 break; 208 } 209 } 210 if (!match) 211 { 212 throw new BuildException( 213 _PROGRAM_NAME 214 + " task valid operations are " 215 + _convertArrayToString(_VALID_OPERATIONS)); 216 } 217 } 218 else 219 { 220 throw new BuildException( 221 _PROGRAM_NAME 222 + " task requires the operation attribute; valid operations are " 223 + _convertArrayToString(_VALID_OPERATIONS)); 224 } 225 if (_operation.compareTo(_ADD_OPERATION) == 0) 226 { 227 if (_isDescriptionFileSet()) 228 { 229 if (!_descriptionFile.exists()) 230 { 231 throw new BuildException( 232 _PROGRAM_NAME 233 + " task cannot find api file " 234 + _descriptionFile); 235 } 236 } 237 else 238 { 239 throw new BuildException( 240 _PROGRAM_NAME 241 + " task requires the api attribute, which is the XML file to describe the project"); 242 } 243 } 244 else if (_operation.compareTo(_REMOVE_OPERATION) == 0) 245 { 246 String [] list = _includePatterns.getIncludePatterns(getProject()); 247 if (list.length <= 0) 248 { 249 throw new BuildException( 250 _PROGRAM_NAME 251 + " task operation remove requires an include list"); 252 } 253 } 254 } 255 256 private String _convertArrayToString(String [] a) 257 { 258 StringBuffer buffer = new StringBuffer (); 259 for (int i = 0; i < a.length; i++) 260 { 261 if (i != 0) 262 { 263 buffer.append(", "); 264 } 265 buffer.append(a[i]); 266 } 267 return buffer.toString(); 268 } 269 270 private static final String _PROGRAM_NAME = "Ashkelon"; 271 private static final String _ADD_OPERATION = "add"; 272 private static final String _RESET_OPERATION = "reset"; 273 private static final String _LIST_OPERATION = "list"; 274 private static final String _REMOVE_OPERATION = "remove"; 275 private static final String _UPDATEREFS_OPERATION = "updaterefs"; 276 277 private static final String [] _VALID_OPERATIONS = 278 { 279 _RESET_OPERATION, 280 _LIST_OPERATION, 281 _REMOVE_OPERATION, 282 _ADD_OPERATION, 283 _UPDATEREFS_OPERATION }; 284 285 private boolean _verbose = false; 286 private boolean _debug = false; 287 private String _operation = null; 288 private String _extdirs = null; 289 private File _descriptionFile = null; 290 private PatternSet _includePatterns = new PatternSet(); 291 } 292 | Popular Tags |