FileDocCategorySizeDatePackage
ProjectHelper2.javaAPI DocApache Ant 1.7045162Wed Dec 13 06:16:18 GMT 2006org.apache.tools.ant.helper

ProjectHelper2

public class ProjectHelper2 extends org.apache.tools.ant.ProjectHelper
Sax2 based project reader

Fields Summary
private static AntHandler
elementHandler
private static AntHandler
targetHandler
private static AntHandler
mainHandler
private static AntHandler
projectHandler
private static final org.apache.tools.ant.util.FileUtils
FILE_UTILS
helper for path -> URI and URI -> path conversions.
Constructors Summary
Methods Summary
protected static org.apache.tools.ant.helper.ProjectHelper2$AntHandlergetElementHandler()
Returns element handler

return
element handler

        return elementHandler;
    
protected static org.apache.tools.ant.helper.ProjectHelper2$AntHandlergetMainHandler()
Returns main handler

return
main handler

        return mainHandler;
    
protected static org.apache.tools.ant.helper.ProjectHelper2$AntHandlergetProjectHandler()
Returns project handler

return
project handler

        return projectHandler;
    
protected static org.apache.tools.ant.helper.ProjectHelper2$AntHandlergetTargetHandler()
Returns target handler

return
target handler

        return targetHandler;
    
public voidparse(org.apache.tools.ant.Project project, java.lang.Object source)
Parse a source xml input.

param
project the current project
param
source the xml source
exception
BuildException if an error occurs

        getImportStack().addElement(source);
        //System.out.println("Adding " + source);
        AntXMLContext context = null;
        context = (AntXMLContext) project.getReference("ant.parsing.context");
//        System.out.println("Parsing " + getImportStack().size() + " " +
//                context+ " " + getImportStack() );
        if (context == null) {
            context = new AntXMLContext(project);
            project.addReference("ant.parsing.context", context);
            project.addReference("ant.targets", context.getTargets());
        }

        if (getImportStack().size() > 1) {
            // we are in an imported file.
            context.setIgnoreProjectTag(true);
            Target currentTarget = context.getCurrentTarget();
            Target currentImplicit = context.getImplicitTarget();
            Map    currentTargets = context.getCurrentTargets();
            try {
                Target newCurrent = new Target();
                newCurrent.setProject(project);
                newCurrent.setName("");
                context.setCurrentTarget(newCurrent);
                context.setCurrentTargets(new HashMap());
                context.setImplicitTarget(newCurrent);
                parse(project, source, new RootHandler(context, mainHandler));
                newCurrent.execute();
            } finally {
                context.setCurrentTarget(currentTarget);
                context.setImplicitTarget(currentImplicit);
                context.setCurrentTargets(currentTargets);
            }
        } else {
            // top level file
            context.setCurrentTargets(new HashMap());
            parse(project, source, new RootHandler(context, mainHandler));
            // Execute the top-level target
            context.getImplicitTarget().execute();
        }
    
public voidparse(org.apache.tools.ant.Project project, java.lang.Object source, org.apache.tools.ant.helper.ProjectHelper2$RootHandler handler)
Parses the project file, configuring the project as it goes.

param
project the current project
param
source the xml source
param
handler the root handler to use (contains the current context)
exception
BuildException if the configuration is invalid or cannot be read


        AntXMLContext context = handler.context;

        File buildFile = null;
        URL  url = null;
        String buildFileName = null;

        if (source instanceof File) {
            buildFile = (File) source;
            buildFile = FILE_UTILS.normalize(buildFile.getAbsolutePath());
            context.setBuildFile(buildFile);
            buildFileName = buildFile.toString();
//         } else if (source instanceof InputStream ) {
        } else if (source instanceof URL) {
            url = (URL) source;
            buildFileName = url.toString();
//         } else if (source instanceof InputSource ) {
        } else {
            throw new BuildException("Source " + source.getClass().getName()
                                     + " not supported by this plugin");
        }

        InputStream inputStream = null;
        InputSource inputSource = null;


        try {
            /**
             * SAX 2 style parser used to parse the given file.
             */
            XMLReader parser = JAXPUtils.getNamespaceXMLReader();

            String uri = null;
            if (buildFile != null) {
                uri = FILE_UTILS.toURI(buildFile.getAbsolutePath());
                inputStream = new FileInputStream(buildFile);
            } else {
                inputStream = url.openStream();
                uri = url.toString(); // ?? OK ??
            }

            inputSource = new InputSource(inputStream);
            if (uri != null) {
                inputSource.setSystemId(uri);
            }
            project.log("parsing buildfile " + buildFileName
                        + " with URI = " + uri, Project.MSG_VERBOSE);

            DefaultHandler hb = handler;

            parser.setContentHandler(hb);
            parser.setEntityResolver(hb);
            parser.setErrorHandler(hb);
            parser.setDTDHandler(hb);
            parser.parse(inputSource);
        } catch (SAXParseException exc) {
            Location location = new Location(exc.getSystemId(),
                exc.getLineNumber(), exc.getColumnNumber());

            Throwable t = exc.getException();
            if (t instanceof BuildException) {
                BuildException be = (BuildException) t;
                if (be.getLocation() == Location.UNKNOWN_LOCATION) {
                    be.setLocation(location);
                }
                throw be;
            } else if (t == null) {
                t = exc;
            }

            throw new BuildException(exc.getMessage(), t, location);
        } catch (SAXException exc) {
            Throwable t = exc.getException();
            if (t instanceof BuildException) {
                throw (BuildException) t;
            } else if (t == null) {
                t = exc;
            }
            throw new BuildException(exc.getMessage(), t);
        } catch (FileNotFoundException exc) {
            throw new BuildException(exc);
        } catch (UnsupportedEncodingException exc) {
              throw new BuildException("Encoding of project file "
                                       + buildFileName + " is invalid.",
                                       exc);
        } catch (IOException exc) {
            throw new BuildException("Error reading project file "
                                     + buildFileName + ": " + exc.getMessage(),
                                     exc);
        } finally {
            FileUtils.close(inputStream);
        }
    
public org.apache.tools.ant.UnknownElementparseUnknownElement(org.apache.tools.ant.Project project, java.net.URL source)
Parse an unknown element from a url

param
project the current project
param
source the url containing the task
return
a configured task
exception
BuildException if an error occurs


                                       
         
          
        Target dummyTarget = new Target();
        dummyTarget.setProject(project);

        AntXMLContext context = new AntXMLContext(project);
        context.addTarget(dummyTarget);
        context.setImplicitTarget(dummyTarget);

        parse(context.getProject(), source,
              new RootHandler(context, elementHandler));
        Task[] tasks = dummyTarget.getTasks();
        if (tasks.length != 1) {
            throw new BuildException("No tasks defined");
        }
        return (UnknownElement) tasks[0];
    
protected static voidsetElementHandler(org.apache.tools.ant.helper.ProjectHelper2$AntHandler handler)
Sets element handler

param
handler new element handler

        elementHandler = handler;
    
protected static voidsetMainHandler(org.apache.tools.ant.helper.ProjectHelper2$AntHandler handler)
Sets main handler

param
handler new main handler

        mainHandler = handler;
    
protected static voidsetProjectHandler(org.apache.tools.ant.helper.ProjectHelper2$AntHandler handler)
Sets project handler

param
handler new project handler

        projectHandler = handler;
    
protected static voidsetTargetHandler(org.apache.tools.ant.helper.ProjectHelper2$AntHandler handler)
Sets target handler

param
handler new target handler

        targetHandler = handler;