@@ -5,6 +5,7 @@
package org.apache.ant;
package org.apache.ant;
import java.io.*;
import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.*;
@@ -16,46 +17,86 @@ import org.xml.sax.*;
*
*
* @author James Duncan Davidson (duncan@apache.org)
* @author James Duncan Davidson (duncan@apache.org)
*/
*/
class ProjectBuilder {
public class ProjectBuilder {
// -----------------------------------------------------------------
// PRIVATE MEMBERS
// -----------------------------------------------------------------
/**
*
*/
//private Ant ant;
/**
*
*/
private FrontEnd frontEnd;
/**
*
*/
private SAXParserFactory parserFactory;
private SAXParserFactory parserFactory;
// -----------------------------------------------------------------
// -----------------------------------------------------------------
// CONSTRUCTORS
// CONSTRUCTORS
// -----------------------------------------------------------------
// -----------------------------------------------------------------
ProjectBuilder() {
/**
* Creates a new project builder that will build projects for the given
* Ant.
*/
public ProjectBuilder(FrontEnd frontEnd) {
this.frontEnd = frontEnd;
parserFactory = SAXParserFactory.newInstance();
parserFactory = SAXParserFactory.newInstance();
parserFactory.setValidating(false);
parserFactory.setValidating(false);
}
}
Project buildFromFile(File file) throws AntException {
/**
* Builds a project from the given file.
*/
public Project buildFromFile(File file) throws AntException {
try {
try {
SAXParser parser = parserFactory.newSAXParser();
SAXParser parser = parserFactory.newSAXParser();
BuilderHandlerBase bhb = new BuilderHandlerBase();
BuilderHandlerBase bhb = new BuilderHandlerBase();
bhb.setProjectFileLocation(file);
parser.parse(file, bhb);
parser.parse(file, bhb);
return bhb.getProject();
Project project = bhb.getProject();
project.setFrontEnd(frontEnd);
return project;
} catch (ParserConfigurationException pce) {
} catch (ParserConfigurationException pce) {
throw new AntException(pce.getMessage());
throw new AntException(pce);
} catch (SAXException se) {
} catch (SAXException se) {
System.out.println(se);
System.out.println(se.getMessage());
throw new AntException(se.getMessage());
Exception e = se.getException();
if (e != null && e instanceof AntException) {
// it's one of our own thrown from inside the parser to stop it
throw (AntException)e;
}
throw new AntException(se);
} catch (IOException ioe) {
} catch (IOException ioe) {
throw new AntException(ioe.getMessage());
throw new AntException(ioe);
}
}
}
}
/**
* Inner class that implements the needed SAX methods to get all the
* data needed out of a build file.
*/
class BuilderHandlerBase extends HandlerBase {
class BuilderHandlerBase extends HandlerBase {
private static final int STATE_START = 0;
private static final int STATE_START = 0;
private static final int STATE_PROJECT = 1;
private static final int STATE_PROJECT = 1;
private static final int STATE_TARGET = 2;
private static final int STATE_TARGET = 2;
private static final int STATE_TASK = 3;
private static final int STATE_TASK = 3;
private static final int STATE_DESCRIPTION = 4;
private static final int STATE_PROPERTY = 5;
private static final int STATE_FINISHED = 99;
private static final int STATE_FINISHED = 99;
private int state = STATE_START;
private int state = STATE_START;
private Vector tagCharDataStack = new Vector();
private Target currentTarget;
private Target currentTarget;
private Task currentTask;
private Task currentTask;
@@ -65,42 +106,98 @@ class ProjectBuilder {
return project;
return project;
}
}
void setProjectFileLocation(File file) {
project.setBaseDir(file.getParentFile());
}
public void startElement(String name, AttributeList atts) throws SAXException {
public void startElement(String name, AttributeList atts) throws SAXException {
//System.out.println("element: " + name);
StringBuffer tagCharData = new StringBuffer();
tagCharDataStack.insertElementAt(tagCharData, 0);
switch (state) {
switch (state) {
case STATE_START:
case STATE_START:
if (name.equals("project")) {
if (name.equals("project")) {
state = STATE_PROJECT;
state = STATE_PROJECT;
String projectName = atts.getValue("name");
String projectName = atts.getValue("name");
if (projectName == null) {
System.out.println("Projects *must* have names");
// XXX exception out
if (projectName != null) {
project.setName(projectName);
} else {
String msg = "Project element doesn't contain a name attribute";
AntException ae = new AntException(msg);
throw new SAXException(ae);
}
String defaultTarget = atts.getValue("default");
if (defaultTarget != null) {
project.setDefaultTargetName(defaultTarget);
}
String baseDirName = atts.getValue("basedir");
if (baseDirName != null) {
// XXX need to check to see if base dir exists
project.setBaseDir(new File(baseDirName));
}
}
project.setName(projectName);
} else {
} else {
System.out.println("Expecting project, got: " + name);
// XXX exception out
String msg = "Project file doesn't contain a project element as " +
"its root node";
AntException ae = new AntException(msg);
throw new SAXException(ae);
}
}
break;
break;
case STATE_PROJECT:
case STATE_PROJECT:
if (name.equals("target")) {
// valid tags in a project object are: description, property, and target
if (name.equals("description")) {
state = STATE_DESCRIPTION;
} else if (name.equals("property")) {
state = STATE_PROPERTY;
String propertyName = atts.getValue("name");
String propertyValue = atts.getValue("value");
if (propertyName == null) {
String msg = "Name attribute must be present on property";
AntException ae = new AntException(msg);
throw new SAXException(ae);
} else if (propertyValue == null) {
String msg = "Value attribute must be present on property";
AntException ae = new AntException(msg);
throw new SAXException(ae);
} else {
project.setProperty(propertyName, propertyValue);
}
} else if (name.equals("target")) {
state = STATE_TARGET;
state = STATE_TARGET;
String targetName = atts.getValue("name");
String targetName = atts.getValue("name");
if (targetName == null) {
System.out.println("Targets *must* have names");
// XXX exception out
if (targetName != null) {
currentTarget = new Target(targetName);
project.addTarget(currentTarget);
} else {
// XXX figure out which target we're talking about!
// Like a location
String msg = "Target element doesn't contain a name attribute";
AntException ae = new AntException(msg);
throw new SAXException(ae);
}
String depends = atts.getValue("depends");
if (depends != null) {
StringTokenizer tok = new StringTokenizer(depends, ",", false);
while(tok.hasMoreTokens()) {
currentTarget.addDependancy(tok.nextToken().trim());
}
}
}
currentTarget = new Target(targetName);
project.addTarget(currentTarget);
// XXX add dependency checks
// XXX add dependency checks
} else {
} else {
System.out.println("Expecting target, got: " + name);
System.out.println("Expecting target, got: " + name);
// XXX exception out
// XXX exception out
}
}
break;
break;
case STATE_TARGET:
case STATE_TARGET:
// Valid tags inside target: task
state = STATE_TASK;
state = STATE_TASK;
//System.out.println("Getting task: " + name + " for target " +
//System.out.println("Getting task: " + name + " for target " +
// currentTarget);
// currentTarget);
@@ -114,6 +211,16 @@ class ProjectBuilder {
currentTask.addAttribute(atName, atValue);
currentTask.addAttribute(atName, atValue);
}
}
break;
break;
case STATE_TASK:
// data in here needs to be reflected into tasks
System.out.println("Not yet supporting tags inside of tasks!");
System.out.println("The project build will probably bust right here");
break;
default:
default:
System.out.println("I'm not sure, but we're off base here: " + name);
System.out.println("I'm not sure, but we're off base here: " + name);
// XXX exception out
// XXX exception out
@@ -121,14 +228,21 @@ class ProjectBuilder {
}
}
public void characters(char ch[], int start, int length) throws SAXException {
public void characters(char ch[], int start, int length) throws SAXException {
StringBuffer buf = (StringBuffer)tagCharDataStack.elementAt(0);
buf.append(ch, start, length);
}
}
public void endElement(String name) throws SAXException {
public void endElement(String name) throws SAXException {
// System.out.println("end: " + name);
StringBuffer elementData = (StringBuffer)tagCharDataStack.elementAt(0);
tagCharDataStack.removeElementAt(0);
switch (state) {
switch (state) {
case STATE_TASK:
case STATE_TASK:
state = STATE_TARGET;
state = STATE_TARGET;
break;
break;
case STATE_TARGET:
case STATE_TARGET:
if (name.equals("target")) {
if (name.equals("target")) {
state = STATE_PROJECT;
state = STATE_PROJECT;
@@ -137,6 +251,27 @@ class ProjectBuilder {
// XXX exception out.
// XXX exception out.
}
}
break;
break;
case STATE_DESCRIPTION:
if (name.equals("description")) {
state = STATE_PROJECT;
project.setDescription(elementData.toString().trim());
} else {
System.out.println("Expecting to get an end of description, got: " +
name);
// XXX exception out.
}
break;
case STATE_PROPERTY:
if (name.equals("property")) {
state = STATE_PROJECT;
} else {
System.out.println("Expecting to get end of property, got: " + name);
// XXX exception out
}
break;
case STATE_PROJECT:
case STATE_PROJECT:
if (name.equals("project")) {
if (name.equals("project")) {
state = STATE_FINISHED;
state = STATE_FINISHED;
@@ -145,6 +280,7 @@ class ProjectBuilder {
// XXX exception out;
// XXX exception out;
}
}
break;
break;
default:
default:
System.out.println("I'm not sure what we are ending here: " + name);
System.out.println("I'm not sure what we are ending here: " + name);
// XXX exception out;
// XXX exception out;