ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Robert Dockins <rdock...@coral.tufts.edu>
Subject Re: AW: parsing ant build scripts from unusual sources
Date Fri, 15 Oct 2004 12:19:07 GMT
I've taken a stab at doing this.  Basicly I've sublcassed ProjectHelper2
to add support for InputStream's and InputSource's (code below, its 
mostly a cut-n-paste job from ProjectHelper2).  Everything appears to 
just work.  If you don't use Project.setBaseDir() then it appears to 
simply use PWD; obviously, if you setBaseDir() it does stuff there.  I 
haven't run into any problems with error reporting or otherwise.  I'll 
be doing some more testing on bigger-than-toy build scripts soon.

It seems to me that the restriction is mostly in people's heads :)

===================================================

import java.net.URL;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import org.apache.tools.ant.util.JAXPUtils;
import org.apache.tools.ant.*;
import org.apache.tools.ant.helper.*;

public class TestMain
{
     public static class MyHelper extends ProjectHelper2
     {
	public void parse( Project project, Object source, RootHandler handler )
	    throws BuildException
	{
	    InputStream inputStream = null;
	    InputSource inputSource = null;

	    if (source instanceof File) {
		super.parse( project, source, handler );
	    } else if (source instanceof URL) {
		super.parse( project, source, handler );
	    } else if (source instanceof InputStream ) {
		inputStream = (InputStream) source;
		inputSource = new InputSource(inputStream);
	    } else if (source instanceof InputSource ) {
		inputSource = (InputSource) source;
	    } else {
		throw new BuildException("Source " + source.getClass().getName()
					 + " not supported by this plugin");
	    }
	
	    try {
		XMLReader parser = JAXPUtils.getNamespaceXMLReader();

		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;
		}
		
		throw new BuildException(exc.getMessage(), t, location);
	    } catch (SAXException exc) {
		Throwable t = exc.getException();
		if (t instanceof BuildException) {
		    throw (BuildException) t;
		}
		throw new BuildException(exc.getMessage(), t);
	    } catch (IOException exc) {
             throw new BuildException("Error reading project file : " + 
exc.getMessage(),
                                      exc);
	    }
	}
     }


     public static void main( String args[] )
	throws Exception
     {
	Project proj = new Project();
	ProjectHelper helper = new MyHelper();

	helper.parse( proj, new FileInputStream( new File( "test.xml")) );

	proj.setBaseDir( new File("/path/to/whereever") );
	proj.init();
	proj.executeTarget( "test" );

     }
}





Peter Reilly wrote:
> Jan.Materne@rzf.fin-nrw.de wrote:
> 
>>> The problem lies in the fact that so much revolves around the build
>>> file's
>>> basedir, which can only be a File in Ant as of today. Even importing a
>>> build file from a URL or resources didn't reach consensus, for the same
>>> reasons. It seems that Ant is pretty much strictly file based for build
>>> files, unless so major restructuring happens, which would probably not
>>> be
>>> backward compatible. At least that's my current understanding. --DD
>>>   
>>
>>
>> Basically thats the same I think. But it should be possible to work with
>> a buildfile from another datasource, if specify the basedir. So something
>> like
>>  setBuildfile(File file)
>>  setBuildfile(InputStream is, File basedir)
>>  setBuildfile(Document dom, File basedir)
>> may be possible.
>>  
>>
> 
> One also need to know (or does one?), the source name for
> error reporting.
> This is something that I had to do for the antlib.xml processing (which
> is retrieved from a URL).
> 
> Peter
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Mime
View raw message