|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418 |
-
-
- package org.apache.tools.ant;
-
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.Hashtable;
- import java.util.Vector;
- import java.util.Enumeration;
- import java.util.Locale;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
-
- import org.xml.sax.AttributeList;
-
-
-
- public class ProjectHelper {
-
-
-
- public static void configureProject(Project project, File buildFile)
- throws BuildException
- {
- ProjectHelper helper=ProjectHelper.getProjectHelper();
- helper.parse(project, buildFile);
- }
-
- public ProjectHelper() {
- }
-
-
-
- private ProjectHelper(Project project, File buildFile) {
-
-
-
- }
-
- public Project createProject(ClassLoader coreLoader) {
- return new Project();
- }
-
-
-
- public void parse(Project project, Object source)
- throws BuildException
- {
- throw new BuildException("You must use a real ProjectHelper implementation");
- }
-
-
- public static final String HELPER_PROPERTY =
- "org.apache.tools.ant.ProjectHelper";
-
- public static final String SERVICE_ID =
- "/META-INF/services/org.apache.tools.ant.ProjectHelper";
-
-
-
-
- public static ProjectHelper getProjectHelper()
- throws BuildException
- {
-
-
- ClassLoader classLoader = getContextClassLoader();
- ProjectHelper helper=null;
-
-
- try {
- String helperClass = System.getProperty(HELPER_PROPERTY);
- if (helperClass != null) {
- helper = newHelper(helperClass, classLoader);
- }
- } catch (SecurityException e) {
-
- ;
- }
-
-
-
- if( helper==null ) {
- try {
- InputStream is=null;
- if (classLoader == null) {
- is=ClassLoader.getSystemResourceAsStream( SERVICE_ID );
- } else {
- is=classLoader.getResourceAsStream( SERVICE_ID );
- }
-
- if( is != null ) {
-
-
- BufferedReader rd;
- try {
- rd = new BufferedReader(new InputStreamReader(is, "UTF-8"));
- } catch (java.io.UnsupportedEncodingException e) {
- rd = new BufferedReader(new InputStreamReader(is));
- }
-
- String helperClassName = rd.readLine();
- rd.close();
-
- if (helperClassName != null &&
- ! "".equals(helperClassName)) {
-
- helper= newHelper( helperClassName, classLoader );
- }
- }
- } catch( Exception ex ) {
- ;
- }
- }
-
-
- return new ProjectHelperImpl();
- }
-
- private static ProjectHelper newHelper(String helperClass,
- ClassLoader classLoader)
- throws BuildException
- {
-
- try {
- Class clazz = null;
- if (classLoader == null) {
- clazz = Class.forName(helperClass);
- } else {
- clazz = classLoader.loadClass(helperClass);
- }
- return ((ProjectHelper) clazz.newInstance());
- } catch (Exception e) {
- throw new BuildException(e);
- }
- }
-
-
-
- public static ClassLoader getContextClassLoader()
- throws BuildException
- {
-
- Method method = null;
- try {
- method = Thread.class.getMethod("getContextClassLoader", null);
- } catch (NoSuchMethodException e) {
-
- return null;
- }
-
-
- ClassLoader classLoader = null;
- try {
- classLoader = (ClassLoader)
- method.invoke(Thread.currentThread(), null);
- } catch (IllegalAccessException e) {
- throw new BuildException
- ("Unexpected IllegalAccessException", e);
- } catch (InvocationTargetException e) {
- throw new BuildException
- ("Unexpected InvocationTargetException", e);
- }
-
-
- return (classLoader);
- }
-
-
-
-
-
-
- public static void configure(Object target, AttributeList attrs,
- Project project) throws BuildException {
- TaskAdapter adapter=null;
- if( target instanceof TaskAdapter ) {
- adapter=(TaskAdapter)target;
- target=adapter.getProxy();
- }
-
- IntrospectionHelper ih =
- IntrospectionHelper.getHelper(target.getClass());
- if( adapter != null )
- adapter.setIntrospectionHelper( ih );
-
-
- project.addBuildListener(ih);
-
- for (int i = 0; i < attrs.getLength(); i++) {
-
- String value=replaceProperties(project, attrs.getValue(i),
- project.getProperties() );
- String name=attrs.getName(i).toLowerCase(Locale.US);
- try {
- if (adapter!=null ) {
- adapter.setAttribute( name, value );
- } else {
- ih.setAttribute(project, target,
- name, value);
- }
- } catch (BuildException be) {
-
-
- if (!attrs.getName(i).equals("id")) {
- throw be;
- }
- }
- }
- }
-
-
-
- public static void addText(Project project, Object target, char[] buf, int start, int end)
- throws BuildException {
- addText(project, target, new String(buf, start, end));
- }
-
-
-
- public static void addText(Project project, Object target, String text)
- throws BuildException {
-
- if (text == null ) {
- return;
- }
-
- if(target instanceof TaskAdapter) {
- target = ((TaskAdapter) target).getProxy();
- }
-
- IntrospectionHelper.getHelper(target.getClass()).addText(project, target, text);
- }
-
-
-
- public static void storeChild(Project project, Object parent, Object child, String tag) {
- IntrospectionHelper ih = IntrospectionHelper.getHelper(parent.getClass());
- ih.storeElement(project, parent, child, tag);
- }
-
-
-
- public static String replaceProperties(Project project, String value)
- throws BuildException {
- return project.replaceProperties(value);
- }
-
-
-
- public static String replaceProperties(Project project, String value, Hashtable keys)
- throws BuildException {
- if (value == null) {
- return null;
- }
-
- Vector fragments = new Vector();
- Vector propertyRefs = new Vector();
- parsePropertyString(value, fragments, propertyRefs);
-
- StringBuffer sb = new StringBuffer();
- Enumeration i = fragments.elements();
- Enumeration j = propertyRefs.elements();
- while (i.hasMoreElements()) {
- String fragment = (String)i.nextElement();
- if (fragment == null) {
- String propertyName = (String)j.nextElement();
- if (!keys.containsKey(propertyName)) {
- project.log("Property ${" + propertyName + "} has not been set", Project.MSG_VERBOSE);
- }
- fragment = (keys.containsKey(propertyName)) ? (String) keys.get(propertyName)
- : "${" + propertyName + "}";
- }
- sb.append(fragment);
- }
-
- return sb.toString();
- }
-
-
-
- public static void parsePropertyString(String value, Vector fragments, Vector propertyRefs)
- throws BuildException {
- int prev = 0;
- int pos;
- while ((pos = value.indexOf("$", prev)) >= 0) {
- if (pos > 0) {
- fragments.addElement(value.substring(prev, pos));
- }
-
- if( pos == (value.length() - 1)) {
- fragments.addElement("$");
- prev = pos + 1;
- }
- else if (value.charAt(pos + 1) != '{' ) {
- fragments.addElement(value.substring(pos + 1, pos + 2));
- prev = pos + 2;
- } else {
- int endName = value.indexOf('}', pos);
- if (endName < 0) {
- throw new BuildException("Syntax error in property: "
- + value );
- }
- String propertyName = value.substring(pos + 2, endName);
- fragments.addElement(null);
- propertyRefs.addElement(propertyName);
- prev = endName + 1;
- }
- }
-
- if (prev < value.length()) {
- fragments.addElement(value.substring(prev));
- }
- }
-
- }
|