1 18 19 package org.apache.tools.ant; 20 21 import java.io.BufferedReader ; 22 import java.io.File ; 23 import java.io.InputStream ; 24 import java.io.InputStreamReader ; 25 import java.util.Hashtable ; 26 import java.util.Locale ; 27 import java.util.Vector ; 28 import org.apache.tools.ant.helper.ProjectHelper2; 29 import org.apache.tools.ant.util.LoaderUtils; 30 import org.xml.sax.AttributeList ; 31 32 50 public class ProjectHelper { 51 52 public static final String ANT_CORE_URI = "antlib:org.apache.tools.ant"; 53 54 55 public static final String ANT_CURRENT_URI = "ant:current"; 56 57 58 public static final String ANTLIB_URI = "antlib:"; 59 60 61 public static final String ANT_TYPE = "ant-type"; 62 63 67 public static final String HELPER_PROPERTY = 68 "org.apache.tools.ant.ProjectHelper"; 69 70 74 public static final String SERVICE_ID = 75 "META-INF/services/org.apache.tools.ant.ProjectHelper"; 76 77 80 public static final String PROJECTHELPER_REFERENCE = "ant.projectHelper"; 81 82 92 public static void configureProject(Project project, File buildFile) 93 throws BuildException { 94 ProjectHelper helper = ProjectHelper.getProjectHelper(); 95 project.addReference(PROJECTHELPER_REFERENCE, helper); 96 helper.parse(project, buildFile); 97 } 98 99 100 public ProjectHelper() { 101 } 102 103 107 114 private Vector importStack = new Vector (); 115 116 120 124 131 public Vector getImportStack() { 132 return importStack; 133 } 134 135 136 150 public void parse(Project project, Object source) throws BuildException { 151 throw new BuildException("ProjectHelper.parse() must be implemented " 152 + "in a helper plugin " + this.getClass().getName()); 153 } 154 155 156 168 public static ProjectHelper getProjectHelper() 169 throws BuildException { 170 ProjectHelper helper = null; 173 174 String helperClass = System.getProperty(HELPER_PROPERTY); 176 try { 177 if (helperClass != null) { 178 helper = newHelper(helperClass); 179 } 180 } catch (SecurityException e) { 181 System.out.println("Unable to load ProjectHelper class \"" 182 + helperClass + " specified in system property " 183 + HELPER_PROPERTY); 184 } 185 186 if (helper == null) { 189 try { 190 ClassLoader classLoader = LoaderUtils.getContextClassLoader(); 191 InputStream is = null; 192 if (classLoader != null) { 193 is = classLoader.getResourceAsStream(SERVICE_ID); 194 } 195 if (is == null) { 196 is = ClassLoader.getSystemResourceAsStream(SERVICE_ID); 197 } 198 199 if (is != null) { 200 InputStreamReader isr; 203 try { 204 isr = new InputStreamReader (is, "UTF-8"); 205 } catch (java.io.UnsupportedEncodingException e) { 206 isr = new InputStreamReader (is); 207 } 208 BufferedReader rd = new BufferedReader (isr); 209 210 String helperClassName = rd.readLine(); 211 rd.close(); 212 213 if (helperClassName != null 214 && !"".equals(helperClassName)) { 215 216 helper = newHelper(helperClassName); 217 } 218 } 219 } catch (Exception ex) { 220 System.out.println("Unable to load ProjectHelper " 221 + "from service \"" + SERVICE_ID); 222 } 223 } 224 225 if (helper != null) { 226 return helper; 227 } else { 228 return new ProjectHelper2(); 229 } 230 } 231 232 245 private static ProjectHelper newHelper(String helperClass) 246 throws BuildException { 247 ClassLoader classLoader = LoaderUtils.getContextClassLoader(); 248 try { 249 Class clazz = null; 250 if (classLoader != null) { 251 try { 252 clazz = classLoader.loadClass(helperClass); 253 } catch (ClassNotFoundException ex) { 254 } 256 } 257 if (clazz == null) { 258 clazz = Class.forName(helperClass); 259 } 260 return ((ProjectHelper) clazz.newInstance()); 261 } catch (Exception e) { 262 throw new BuildException(e); 263 } 264 } 265 266 276 public static ClassLoader getContextClassLoader() { 277 if (!LoaderUtils.isContextLoaderAvailable()) { 278 return null; 279 } 280 281 return LoaderUtils.getContextClassLoader(); 282 } 283 284 286 302 public static void configure(Object target, AttributeList attrs, 303 Project project) throws BuildException { 304 if (target instanceof TypeAdapter) { 305 target = ((TypeAdapter) target).getProxy(); 306 } 307 308 IntrospectionHelper ih = 309 IntrospectionHelper.getHelper(project, target.getClass()); 310 311 for (int i = 0; i < attrs.getLength(); i++) { 312 String value = replaceProperties(project, attrs.getValue(i), 314 project.getProperties()); 315 try { 316 ih.setAttribute(project, target, 317 attrs.getName(i).toLowerCase(Locale.US), value); 318 319 } catch (BuildException be) { 320 if (!attrs.getName(i).equals("id")) { 322 throw be; 323 } 324 } 325 } 326 } 327 328 342 public static void addText(Project project, Object target, char[] buf, 343 int start, int count) throws BuildException { 344 addText(project, target, new String (buf, start, count)); 345 } 346 347 360 public static void addText(Project project, Object target, String text) 361 throws BuildException { 362 363 if (text == null) { 364 return; 365 } 366 367 if (target instanceof TypeAdapter) { 368 target = ((TypeAdapter) target).getProxy(); 369 } 370 371 IntrospectionHelper.getHelper(project, target.getClass()).addText(project, 372 target, text); 373 } 374 375 388 public static void storeChild(Project project, Object parent, 389 Object child, String tag) { 390 IntrospectionHelper ih 391 = IntrospectionHelper.getHelper(project, parent.getClass()); 392 ih.storeElement(project, parent, child, tag); 393 } 394 395 415 public static String replaceProperties(Project project, String value) 416 throws BuildException { 417 return project.replaceProperties(value); 419 } 420 421 441 public static String replaceProperties(Project project, String value, 442 Hashtable keys) throws BuildException { 443 PropertyHelper ph = PropertyHelper.getPropertyHelper(project); 444 return ph.replaceProperties(null, value, keys); 445 } 446 447 466 public static void parsePropertyString(String value, Vector fragments, 467 Vector propertyRefs) 468 throws BuildException { 469 PropertyHelper.parsePropertyStringDefault(value, fragments, 470 propertyRefs); 471 } 472 481 public static String genComponentName(String uri, String name) { 482 if (uri == null || uri.equals("") || uri.equals(ANT_CORE_URI)) { 483 return name; 484 } 485 return uri + ":" + name; 486 } 487 488 494 public static String extractUriFromComponentName(String componentName) { 495 if (componentName == null) { 496 return ""; 497 } 498 int index = componentName.lastIndexOf(':'); 499 if (index == -1) { 500 return ""; 501 } 502 return componentName.substring(0, index); 503 } 504 505 511 public static String extractNameFromComponentName(String componentName) { 512 int index = componentName.lastIndexOf(':'); 513 if (index == -1) { 514 return componentName; 515 } 516 return componentName.substring(index + 1); 517 } 518 519 528 public static BuildException addLocationToBuildException( 529 BuildException ex, Location newLocation) { 530 if (ex.getLocation() == null || ex.getMessage() == null) { 531 return ex; 532 } 533 String errorMessage 534 = "The following error occurred while executing this line:" 535 + System.getProperty("line.separator") 536 + ex.getLocation().toString() 537 + ex.getMessage(); 538 if (newLocation == null) { 539 return new BuildException(errorMessage, ex); 540 } else { 541 return new BuildException( 542 errorMessage, ex, newLocation); 543 } 544 } 545 } 546 | Popular Tags |