From 314a73ff1e695e7d262dbd5dc226ccbacf8f84c6 Mon Sep 17 00:00:00 2001 From: Conor MacNeill Date: Mon, 11 Mar 2002 13:01:48 +0000 Subject: [PATCH] Cleanup of ProjectHelper bits - many to go. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271819 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/tools/ant/ProjectHelper.java | 104 ++++++++--------- .../tools/ant/helper/ProjectHelperImpl.java | 106 +++++++++--------- 2 files changed, 109 insertions(+), 101 deletions(-) diff --git a/src/main/org/apache/tools/ant/ProjectHelper.java b/src/main/org/apache/tools/ant/ProjectHelper.java index 5b9657464..506072ec2 100644 --- a/src/main/org/apache/tools/ant/ProjectHelper.java +++ b/src/main/org/apache/tools/ant/ProjectHelper.java @@ -55,9 +55,6 @@ 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; @@ -65,8 +62,6 @@ 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; import org.apache.tools.ant.helper.ProjectHelperImpl; @@ -92,6 +87,19 @@ import org.apache.tools.ant.util.LoaderUtils; * @author duncan@x180.com */ public class ProjectHelper { + /** + * Name of JVM system property which provides the name of the + * ProjectHelper class to use. + */ + public static final String HELPER_PROPERTY = + "org.apache.tools.ant.ProjectHelper"; + + /** + * The service identifier in jars which provide Project Helper + * implementations. + */ + public static final String SERVICE_ID = + "/META-INF/services/org.apache.tools.ant.ProjectHelper"; /** * Configures the project with the contents of the specified XML file. @@ -103,11 +111,13 @@ public class ProjectHelper { * @exception BuildException if the configuration is invalid or cannot * be read */ - public static void configureProject(Project project, File buildFile) throws BuildException { - ProjectHelper helper=ProjectHelper.getProjectHelper(); + public static void configureProject(Project project, File buildFile) + throws BuildException { + ProjectHelper helper = ProjectHelper.getProjectHelper(); helper.parse(project, buildFile); } + /** Default constructor */ public ProjectHelper() { } @@ -125,28 +135,20 @@ public class ProjectHelper { * be read */ public void parse(Project project, Object source) throws BuildException { - throw new BuildException("ProjectHelper.parse() must be implemented in a helper plugin " - + this.getClass().getName()); + throw new BuildException("ProjectHelper.parse() must be implemented " + + "in a helper plugin " + this.getClass().getName()); } - /* -------------------- Helper discovery -------------------- */ - 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"; - /** Discover a project helper instance. Uses the same patterns * as JAXP, commons-logging, etc: a system property, a JDK1.3 * service discovery, default. */ public static ProjectHelper getProjectHelper() - throws BuildException - { + throws BuildException { // Identify the class loader we will be using. Ant may be // in a webapp or embeded in a different app - ProjectHelper helper=null; + ProjectHelper helper = null; // First, try the system property try { @@ -161,18 +163,18 @@ public class ProjectHelper { // A JDK1.3 'service' ( like in JAXP ). That will plug a helper // automatically if in CLASSPATH, with the right META-INF/services. - if( helper==null ) { + if (helper == null) { try { - ClassLoader classLoader=getContextClassLoader(); - InputStream is=null; + ClassLoader classLoader = getContextClassLoader(); + InputStream is = null; if (classLoader != null) { - is=classLoader.getResourceAsStream( SERVICE_ID ); + is = classLoader.getResourceAsStream(SERVICE_ID); } - if( is==null ) { - is=ClassLoader.getSystemResourceAsStream( SERVICE_ID ); + if (is == null) { + is = ClassLoader.getSystemResourceAsStream(SERVICE_ID); } - if( is != null ) { + if (is != null) { // This code is needed by EBCDIC and other strange systems. // It's a fix for bugs reported in xerces BufferedReader rd; @@ -186,17 +188,17 @@ public class ProjectHelper { rd.close(); if (helperClassName != null && - ! "".equals(helperClassName)) { + !"".equals(helperClassName)) { - helper= newHelper( helperClassName ); + helper = newHelper( helperClassName ); } } - } catch( Exception ex ) { + } catch (Exception ex) { ; } } - if( helper!=null ) { + if (helper != null) { return helper; } else { // Default @@ -209,19 +211,18 @@ public class ProjectHelper { * loaded this class. */ private static ProjectHelper newHelper(String helperClass) - throws BuildException - { + throws BuildException { ClassLoader classLoader = getContextClassLoader(); try { Class clazz = null; if (classLoader != null) { try { clazz = classLoader.loadClass(helperClass); - } catch( ClassNotFoundException ex ) { + } catch (ClassNotFoundException ex) { // try next method } } - if( clazz==null ) { + if (clazz == null) { clazz = Class.forName(helperClass); } return ((ProjectHelper) clazz.newInstance()); @@ -235,8 +236,7 @@ public class ProjectHelper { * Cut&paste from Jaxp. */ public static ClassLoader getContextClassLoader() - throws BuildException - { + throws BuildException { if (!LoaderUtils.isContextLoaderAvailable()) { return null; } @@ -244,7 +244,7 @@ public class ProjectHelper { return LoaderUtils.getContextClassLoader(); } - // -------------------- Static utils, used by most helpers -------------------- + // -------------------- Static utils, used by most helpers ---------------- /** * Configures an object using an introspection handler. @@ -261,8 +261,8 @@ public class ProjectHelper { */ public static void configure(Object target, AttributeList attrs, Project project) throws BuildException { - if( target instanceof TaskAdapter ) { - target=((TaskAdapter)target).getProxy(); + if (target instanceof TaskAdapter) { + target = ((TaskAdapter)target).getProxy(); } IntrospectionHelper ih = @@ -272,7 +272,7 @@ public class ProjectHelper { for (int i = 0; i < attrs.getLength(); i++) { // reflect these into the target - String value=replaceProperties(project, attrs.getValue(i), + String value = replaceProperties(project, attrs.getValue(i), project.getProperties() ); try { ih.setAttribute(project, target, @@ -301,8 +301,8 @@ public class ProjectHelper { * * @exception BuildException if the target object doesn't accept text */ - public static void addText(Project project, Object target, char[] buf, int start, int count) - throws BuildException { + public static void addText(Project project, Object target, char[] buf, + int start, int count) throws BuildException { addText(project, target, new String(buf, start, count)); } @@ -326,11 +326,12 @@ public class ProjectHelper { return; } - if(target instanceof TaskAdapter) { + if (target instanceof TaskAdapter) { target = ((TaskAdapter) target).getProxy(); } - IntrospectionHelper.getHelper(target.getClass()).addText(project, target, text); + IntrospectionHelper.getHelper(target.getClass()).addText(project, + target, text); } /** @@ -346,7 +347,8 @@ public class ProjectHelper { * May be null, in which case * the child is not stored. */ - public static void storeChild(Project project, Object parent, Object child, String tag) { + public static void storeChild(Project project, Object parent, + Object child, String tag) { IntrospectionHelper ih = IntrospectionHelper.getHelper(parent.getClass()); ih.storeElement(project, parent, child, tag); } @@ -389,8 +391,8 @@ public class ProjectHelper { * @return the original string with the properties replaced, or * null if the original string is null. */ - public static String replaceProperties(Project project, String value, Hashtable keys) - throws BuildException { + public static String replaceProperties(Project project, String value, + Hashtable keys) throws BuildException { if (value == null) { return null; } @@ -407,7 +409,8 @@ public class ProjectHelper { if (fragment == null) { String propertyName = (String)j.nextElement(); if (!keys.containsKey(propertyName)) { - project.log("Property ${" + propertyName + "} has not been set", Project.MSG_VERBOSE); + project.log("Property ${" + propertyName + + "} has not been set", Project.MSG_VERBOSE); } fragment = (keys.containsKey(propertyName)) ? (String) keys.get(propertyName) : "${" + propertyName + "}"; @@ -444,11 +447,10 @@ public class ProjectHelper { fragments.addElement(value.substring(prev, pos)); } - if( pos == (value.length() - 1)) { + if (pos == (value.length() - 1)) { fragments.addElement("$"); prev = pos + 1; - } - else if (value.charAt(pos + 1) != '{' ) { + } else if (value.charAt(pos + 1) != '{' ) { fragments.addElement(value.substring(pos + 1, pos + 2)); prev = pos + 2; } else { diff --git a/src/main/org/apache/tools/ant/helper/ProjectHelperImpl.java b/src/main/org/apache/tools/ant/helper/ProjectHelperImpl.java index a7e019277..11bc0bf07 100644 --- a/src/main/org/apache/tools/ant/helper/ProjectHelperImpl.java +++ b/src/main/org/apache/tools/ant/helper/ProjectHelperImpl.java @@ -54,15 +54,10 @@ package org.apache.tools.ant.helper; -import org.apache.tools.ant.*; - import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; -import java.util.Hashtable; -import java.util.Vector; -import java.util.Enumeration; import java.util.Locale; import org.xml.sax.Locator; import org.xml.sax.InputSource; @@ -72,11 +67,22 @@ import org.xml.sax.SAXException; import org.xml.sax.DocumentHandler; import org.xml.sax.AttributeList; import org.xml.sax.helpers.XMLReaderAdapter; - import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.SAXParser; import javax.xml.parsers.ParserConfigurationException; +import org.apache.tools.ant.ProjectHelper; +import org.apache.tools.ant.UnknownElement; +import org.apache.tools.ant.Project; +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.Target; +import org.apache.tools.ant.Task; +import org.apache.tools.ant.RuntimeConfigurable; +import org.apache.tools.ant.IntrospectionHelper; +import org.apache.tools.ant.TaskContainer; +import org.apache.tools.ant.Location; +import org.apache.tools.ant.TaskAdapter; + /** * Original helper. * @@ -94,33 +100,36 @@ public class ProjectHelperImpl extends ProjectHelper { * SAX 1 style parser used to parse the given file. This may * in fact be a SAX 2 XMLReader wrapped in an XMLReaderAdapter. */ - org.xml.sax.Parser parser; + private org.xml.sax.Parser parser; /** The project to configure. */ - Project project; + private Project project; /** The configuration file to parse. */ - File buildFile; + private File buildFile; /** * Parent directory of the build file. Used for resolving entities * and setting the project's base directory. */ - File buildFileParent; + private File buildFileParent; /** * Locator for the configuration file parser. * Used for giving locations of errors etc. */ - Locator locator; + private Locator locator; /** * Parses the project file, configuring the project as it goes. * + * @param project project instance to be configured. + * @param source the source from which the project is read. * @exception BuildException if the configuration is invalid or cannot - * be read + * be read. */ public void parse(Project project, Object source) throws BuildException { - if( ! (source instanceof File) ) + if (!(source instanceof File)) { throw new BuildException( "Only File source supported by default plugin" ); - File buildFile=(File)source; + } + File buildFile = (File)source; FileInputStream inputStream = null; InputSource inputSource = null; @@ -136,28 +145,29 @@ public class ProjectHelperImpl extends ProjectHelper { parser = new XMLReaderAdapter(saxParser.getXMLReader()); } + String uri = "file:" + buildFile.getAbsolutePath().replace('\\', '/'); for (int index = uri.indexOf('#'); index != -1; index = uri.indexOf('#')) { - uri = uri.substring(0, index) + "%23" + uri.substring(index+1); + uri = uri.substring(0, index) + "%23" + uri.substring(index + 1); } inputStream = new FileInputStream(buildFile); inputSource = new InputSource(inputStream); inputSource.setSystemId(uri); - project.log("parsing buildfile " + buildFile + " with URI = " + uri, Project.MSG_VERBOSE); + project.log("parsing buildfile " + buildFile + " with URI = " + + uri, Project.MSG_VERBOSE); HandlerBase hb = new RootHandler(this); parser.setDocumentHandler(hb); parser.setEntityResolver(hb); parser.setErrorHandler(hb); parser.setDTDHandler(hb); parser.parse(inputSource); - } - catch(ParserConfigurationException exc) { + } catch (ParserConfigurationException exc) { throw new BuildException("Parser has not been configured correctly", exc); - } - catch(SAXParseException exc) { + } catch (SAXParseException exc) { Location location = - new Location(buildFile.toString(), exc.getLineNumber(), exc.getColumnNumber()); + new Location(buildFile.toString(), exc.getLineNumber(), + exc.getColumnNumber()); Throwable t = exc.getException(); if (t instanceof BuildException) { @@ -169,26 +179,21 @@ public class ProjectHelperImpl extends ProjectHelper { } throw new BuildException(exc.getMessage(), t, location); - } - catch(SAXException exc) { + } catch (SAXException exc) { Throwable t = exc.getException(); if (t instanceof BuildException) { throw (BuildException) t; } throw new BuildException(exc.getMessage(), t); - } - catch(FileNotFoundException exc) { + } catch (FileNotFoundException exc) { throw new BuildException(exc); - } - catch(IOException exc) { + } catch (IOException exc) { throw new BuildException("Error reading project file", exc); - } - finally { + } finally { if (inputStream != null) { try { inputStream.close(); - } - catch (IOException ioe) { + } catch (IOException ioe) { // ignore this } } @@ -227,13 +232,16 @@ public class ProjectHelperImpl extends ProjectHelper { * Creates a handler and sets the parser to use it * for the current element. * + * @param helperImpl the ProjectHelperImpl instance associated + * with this handler. + * * @param parentHandler The handler which should be restored to the * parser at the end of the element. * Must not be null. */ public AbstractHandler(ProjectHelperImpl helperImpl, DocumentHandler parentHandler) { this.parentHandler = parentHandler; - this.helperImpl=helperImpl; + this.helperImpl = helperImpl; // Start handling SAX events helperImpl.parser.setDocumentHandler(this); @@ -309,7 +317,7 @@ public class ProjectHelperImpl extends ProjectHelper { ProjectHelperImpl helperImpl; public RootHandler( ProjectHelperImpl helperImpl ) { - this.helperImpl=helperImpl; + this.helperImpl = helperImpl; } /** @@ -355,7 +363,7 @@ public class ProjectHelperImpl extends ProjectHelper { inputSource.setSystemId("file:" + entitySystemId); return inputSource; } catch (FileNotFoundException fne) { - helperImpl.project.log(file.getAbsolutePath()+" could not be found", + helperImpl.project.log(file.getAbsolutePath() + " could not be found", Project.MSG_WARN); } } @@ -490,7 +498,7 @@ public class ProjectHelperImpl extends ProjectHelper { * appropriate handler is created and initialised with the details * of the element. * - * @param tag The name of the element being started. + * @param name The name of the element being started. * Will not be null. * @param attrs Attributes of the element being started. * Will not be null. @@ -520,7 +528,7 @@ public class ProjectHelperImpl extends ProjectHelper { * Handles a task defintion element by creating a task handler * and initialising is with the details of the element. * - * @param tag The name of the element to be handled. + * @param name The name of the element to be handled. * Will not be null. * @param attrs Attributes of the element to be handled. * Will not be null. @@ -537,7 +545,7 @@ public class ProjectHelperImpl extends ProjectHelper { * Handles a type defintion element by creating a task handler * and initialising is with the details of the element. * - * @param tag The name of the element to be handled. + * @param name The name of the element to be handled. * Will not be null. * @param attrs Attributes of the element to be handled. * Will not be null. @@ -553,7 +561,7 @@ public class ProjectHelperImpl extends ProjectHelper { * Handles a property defintion element by creating a task handler * and initialising is with the details of the element. * - * @param tag The name of the element to be handled. + * @param name The name of the element to be handled. * Will not be null. * @param attrs Attributes of the element to be handled. * Will not be null. @@ -584,7 +592,7 @@ public class ProjectHelperImpl extends ProjectHelper { * Handles a data type defintion element by creating a data type * handler and initialising is with the details of the element. * - * @param tag The name of the element to be handled. + * @param name The name of the element to be handled. * Will not be null. * @param attrs Attributes of the element to be handled. * Will not be null. @@ -687,7 +695,7 @@ public class ProjectHelperImpl extends ProjectHelper { /** * Handles the start of an element within a target. * - * @param tag The name of the element being started. + * @param name The name of the element being started. * Will not be null. * @param attrs Attributes of the element being started. * Will not be null. @@ -852,7 +860,7 @@ public class ProjectHelperImpl extends ProjectHelper { * will always use another task handler, and all other tasks * will always use a nested element handler. * - * @param tag The name of the element being started. + * @param name The name of the element being started. * Will not be null. * @param attrs Attributes of the element being started. * Will not be null. @@ -864,8 +872,7 @@ public class ProjectHelperImpl extends ProjectHelper { if (task instanceof TaskContainer) { // task can contain other tasks - no other nested elements possible new TaskHandler(helperImpl, this, (TaskContainer)task, wrapper, target).init(name, attrs); - } - else { + } else { new NestedElementHandler(helperImpl, this, task, wrapper, target).init(name, attrs); } } @@ -933,7 +940,7 @@ public class ProjectHelperImpl extends ProjectHelper { * its parent container (if any). Nested elements are then * added later as the parser encounters them. * - * @param tag Name of the element which caused this handler + * @param propType Name of the element which caused this handler * to be created. Must not be null. * * @param attrs Attributes of the element which caused this @@ -1003,7 +1010,7 @@ public class ProjectHelperImpl extends ProjectHelper { * will always use a task handler, and all other elements * will always use another nested element handler. * - * @param tag The name of the element being started. + * @param name The name of the element being started. * Will not be null. * @param attrs Attributes of the element being started. * Will not be null. @@ -1016,8 +1023,7 @@ public class ProjectHelperImpl extends ProjectHelper { // taskcontainer nested element can contain other tasks - no other // nested elements possible new TaskHandler(helperImpl, this, (TaskContainer)child, childWrapper, target).init(name, attrs); - } - else { + } else { new NestedElementHandler(helperImpl, this, child, childWrapper, target).init(name, attrs); } } @@ -1067,7 +1073,7 @@ public class ProjectHelperImpl extends ProjectHelper { * its parent container (if any). Nested elements are then * added later as the parser encounters them. * - * @param tag Name of the element which caused this handler + * @param propType Name of the element which caused this handler * to be created. Must not be null. * * @param attrs Attributes of the element which caused this @@ -1080,7 +1086,7 @@ public class ProjectHelperImpl extends ProjectHelper { try { element = helperImpl.project.createDataType(propType); if (element == null) { - throw new BuildException("Unknown data type "+propType); + throw new BuildException("Unknown data type " + propType); } if (target != null) { @@ -1122,7 +1128,7 @@ public class ProjectHelperImpl extends ProjectHelper { * Handles the start of an element within this one. * This will always use a nested element handler. * - * @param tag The name of the element being started. + * @param name The name of the element being started. * Will not be null. * @param attrs Attributes of the element being started. * Will not be null.