1 12 13 package org.eclipse.ant.internal.core.ant; 14 15 import java.io.IOException ; 16 import java.io.InputStream ; 17 import java.lang.reflect.Constructor ; 18 import java.lang.reflect.InvocationTargetException ; 19 import java.text.MessageFormat ; import java.util.Enumeration ; 21 import java.util.Hashtable ; 22 import java.util.Properties ; 23 24 import org.apache.tools.ant.BuildException; 25 import org.apache.tools.ant.Project; 26 import org.apache.tools.ant.ProjectComponent; 27 28 40 public class InternalProject extends Project { 41 42 private Hashtable typeNameToClass = null; 43 44 public InternalProject() { 45 super(); 46 } 47 48 51 public void init() throws BuildException { 52 setJavaVersionProperty(); 53 54 try { 55 Class taskClass = Class.forName("org.apache.tools.ant.taskdefs.Property"); addTaskDefinition("property", taskClass); taskClass = Class.forName("org.apache.tools.ant.taskdefs.Typedef"); addTaskDefinition("typedef", taskClass); taskClass = Class.forName("org.apache.tools.ant.taskdefs.Taskdef"); addTaskDefinition("taskdef", taskClass); } catch (NoClassDefFoundError e) { 62 throw new BuildException(InternalAntMessages.InternalAntRunner_Missing_Class, e); 63 } catch (ClassNotFoundException c) { 64 throw new BuildException(InternalAntMessages.InternalAntRunner_Missing_Class, c); 65 } 66 67 setSystemProperties(); 68 } 69 70 73 public Object createDataType(String typeName) throws BuildException { 74 if (typeNameToClass == null) { 75 initializeTypes(); 76 } 77 Class typeClass = (Class ) typeNameToClass.get(typeName); 78 79 if (typeClass == null) { 80 return null; 81 } 82 83 Throwable thrown = null; 84 try { 85 Constructor ctor = null; 86 boolean noArg = false; 87 try { 90 ctor = typeClass.getConstructor(new Class [0]); 91 noArg = true; 92 } catch (NoSuchMethodException nse) { 93 ctor = typeClass.getConstructor(new Class [] { Project.class }); 94 noArg = false; 95 } 96 97 Object o = null; 98 if (noArg) { 99 o = ctor.newInstance(new Object [0]); 100 } else { 101 o = ctor.newInstance(new Object [] { this }); 102 } 103 if (o instanceof ProjectComponent) { 104 ((ProjectComponent) o).setProject(this); 105 } 106 return o; 107 } catch (InvocationTargetException ite) { 108 thrown = ite.getTargetException(); 109 } catch (IllegalArgumentException e) { 110 thrown = e; 111 } catch (InstantiationException e) { 112 thrown = e; 113 } catch (IllegalAccessException e) { 114 thrown = e; 115 } catch (NoSuchMethodException nse) { 116 thrown = nse; 117 } catch (NoClassDefFoundError ncdfe) { 118 thrown = ncdfe; 119 } 120 if (thrown != null) { 121 String message= MessageFormat.format(InternalAntMessages.InternalProject_0, new String []{typeName, thrown.toString()}); 122 throw new BuildException(message, thrown); 123 } 124 return null; 126 } 127 128 131 private void initializeTypes() { 132 typeNameToClass = new Hashtable (18); 133 String dataDefs = "/org/apache/tools/ant/types/defaults.properties"; try { 135 Properties props = new Properties (); 136 InputStream in = Project.class.getResourceAsStream(dataDefs); 137 if (in == null) { 138 return; 139 } 140 props.load(in); 141 in.close(); 142 143 Enumeration enumeration = props.propertyNames(); 144 while (enumeration.hasMoreElements()) { 145 String typeName = (String ) enumeration.nextElement(); 146 String className = props.getProperty(typeName); 147 try { 148 Class typeClass= Class.forName(className); 149 typeNameToClass.put(typeName, typeClass); 150 } catch (NoClassDefFoundError e) { 151 } catch (ClassNotFoundException c) { 153 } 155 } 156 } catch (IOException ioe) { 157 return; 158 } 159 160 } 161 162 165 public Hashtable getDataTypeDefinitions() { 166 if (typeNameToClass == null) { 167 initializeTypes(); 168 } 169 return typeNameToClass; 170 } 171 172 175 public void addDataTypeDefinition(String typeName, Class typeClass) { 176 getDataTypeDefinitions(); 177 typeNameToClass.put(typeName, typeClass); 178 } 179 } 180 | Popular Tags |