|
-
-
- 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.io.UnsupportedEncodingException;
- import java.util.Hashtable;
- import java.util.Vector;
- import java.util.Enumeration;
- import java.util.Locale;
- import java.util.Stack;
- import org.xml.sax.Locator;
- import org.xml.sax.InputSource;
- import org.xml.sax.SAXParseException;
- import org.xml.sax.XMLReader;
- import org.xml.sax.SAXException;
- import org.xml.sax.DocumentHandler;
- import org.xml.sax.Attributes;
- import org.xml.sax.AttributeList;
- import org.xml.sax.helpers.XMLReaderAdapter;
- import org.xml.sax.helpers.DefaultHandler;
- import org.xml.sax.helpers.AttributeListImpl;
-
- import org.apache.tools.ant.util.JAXPUtils;
-
-
- public class ProjectHelperImpl2 extends ProjectHelper {
-
-
-
- static AntHandler elementHandler=new ElementHandler();
- static AntHandler targetHandler=new TargetHandler();
- static AntHandler nestedElementHandler=new NestedElementHandler();
- static AntHandler mainHandler=new MainHandler();
- static AntHandler projectHandler=new ProjectHandler();
-
-
-
- private void hookSpecialTasks(Project project) {
- try {
- Class c=Class.forName("org.apache.tools.ant.types.SystemPath");
- project.addDataTypeDefinition( "systemPath" , c );
- c=Class.forName("org.apache.tools.ant.tasks.Import");
- project.addTaskDefinition( "import" , c );
- } catch (Exception ex ) {
- }
- }
-
-
- public void parse(Project project, Object source) throws BuildException {
- hookSpecialTasks(project);
- AntXmlContext context=new AntXmlContext(project, this);
-
- project.addReference( "ant.parsing.context", context );
-
- parse(project, source,new RootHandler(context));
-
-
- context.implicitTarget.execute();
- }
-
-
-
- public void parse(Project project, Object source, RootHandler handler) throws BuildException {
-
- AntXmlContext context=handler.context;
-
- if(source instanceof File) {
- context.buildFile=(File)source;
-
-
-
- } else {
- throw new BuildException( "Source " + source.getClass().getName() +
- " not supported by this plugin" );
- }
-
- FileInputStream inputStream = null;
- InputSource inputSource = null;
-
- context.buildFile = new File(context.buildFile.getAbsolutePath());
- context.buildFileParent = new File(context.buildFile.getParent());
-
- try {
-
-
- context.parser =JAXPUtils.getXMLReader();
-
- String uri = "file:" + context.buildFile.getAbsolutePath().replace('\\', '/');
- for (int index = uri.indexOf('#'); index != -1; index = uri.indexOf('#')) {
- uri = uri.substring(0, index) + "%23" + uri.substring(index+1);
- }
-
- inputStream = new FileInputStream(context.buildFile);
- inputSource = new InputSource(inputStream);
- inputSource.setSystemId(uri);
- project.log("parsing buildfile " + context.buildFile + " with URI = " + uri, Project.MSG_VERBOSE);
-
- DefaultHandler hb = handler;
-
- context.parser.setContentHandler(hb);
- context.parser.setEntityResolver(hb);
- context.parser.setErrorHandler(hb);
- context.parser.setDTDHandler(hb);
- context.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(FileNotFoundException exc) {
- throw new BuildException(exc);
- }
- catch(UnsupportedEncodingException exc) {
- throw new BuildException("Encoding of project file is invalid.",exc);
- }
- catch(IOException exc) {
- throw new BuildException("Error reading project file: " +exc.getMessage(), exc);
- }
- finally {
- if (inputStream != null) {
- try {
- inputStream.close();
- }
- catch (IOException ioe) {
-
- }
- }
- }
- }
-
-
-
- public static class AntHandler {
-
-
- public void onStartElement(String uri, String tag, String qname,
- Attributes attrs,
- AntXmlContext context)
- throws SAXParseException
- {
- }
-
-
-
- public AntHandler onStartChild(String uri, String tag, String qname,
- Attributes attrs,
- AntXmlContext context)
- throws SAXParseException
- {
- throw new SAXParseException("Unexpected element \"" + qname + " \"", context.locator);
- }
-
- public void onEndChild(String uri, String tag, String qname,
- AntXmlContext context)
- throws SAXParseException
- {
- }
-
-
-
- public void onEndElement(String uri, String tag, AntXmlContext context) {
- }
-
-
-
- public void characters(char[] buf, int start, int count, AntXmlContext context)
- throws SAXParseException
- {
- String s = new String(buf, start, count).trim();
-
- if (s.length() > 0) {
- throw new SAXParseException("Unexpected text \"" + s + "\"", context.locator);
- }
- }
- }
-
-
-
- public static class AntXmlContext {
-
- private Project project;
-
-
- public File buildFile;
-
-
-
- public File buildFileParent;
-
-
- public String currentProjectName;
-
-
-
- Locator locator;
-
-
- public ProjectHelperImpl2 helper;
- org.xml.sax.XMLReader parser;
-
-
-
- Target implicitTarget = new Target();
-
-
-
- public Target currentTarget=null;
-
-
-
- Vector wStack=new Vector();
-
-
- public boolean ignoreProjectTag=false;
- public static Hashtable importedFiles = new Hashtable();
- public static int importlevel = 0;
-
- public AntXmlContext(Project project, ProjectHelperImpl2 helper) {
- this.project=project;
- implicitTarget.setName("");
- this.helper=helper;
- }
-
- public Project getProject() {
- return project;
- }
-
- public RuntimeConfigurable2 currentWrapper() {
- if( wStack.size() < 1 ) return null;
- return (RuntimeConfigurable2)wStack.elementAt( wStack.size() - 1 );
- }
-
- public RuntimeConfigurable2 parentWrapper() {
- if( wStack.size() < 2 ) return null;
- return (RuntimeConfigurable2)wStack.elementAt( wStack.size() - 2 );
- }
-
- public void pushWrapper( RuntimeConfigurable2 wrapper ) {
- wStack.addElement(wrapper);
- }
-
- public void popWrapper() {
- if( wStack.size() > 0 )
- wStack.removeElementAt( wStack.size() - 1 );
- }
-
- public Vector getWrapperStack() {
- return wStack;
- }
-
-
-
- void configureId(Object element, Attributes attr) {
- String id = attr.getValue("id");
- if (id != null) {
- project.addReference(id, element);
- }
- }
-
- }
-
-
-
- public static class RootHandler extends DefaultHandler {
- Stack antHandlers=new Stack();
- AntHandler currentHandler=null;
- AntXmlContext context;
-
- public RootHandler(AntXmlContext context) {
- currentHandler=ProjectHelperImpl2.mainHandler;
- antHandlers.push( currentHandler );
- this.context=context;
- }
-
-
-
- public InputSource resolveEntity(String publicId,
- String systemId) {
-
- context.getProject().log("resolving systemId: " + systemId, Project.MSG_VERBOSE);
-
- if (systemId.startsWith("file:")) {
- String path = systemId.substring(5);
- int index = path.indexOf("file:");
-
-
-
- while (index != -1) {
- path = path.substring(0, index) + path.substring(index + 5);
- index = path.indexOf("file:");
- }
-
- String entitySystemId = path;
- index = path.indexOf("%23");
-
- while (index != -1) {
- path = path.substring(0, index) + "#" + path.substring(index + 3);
- index = path.indexOf("%23");
- }
-
- File file = new File(path);
- if (!file.isAbsolute()) {
- file = new File(context.buildFileParent, path);
- }
-
- try {
- InputSource inputSource = new InputSource(new FileInputStream(file));
- inputSource.setSystemId("file:" + entitySystemId);
- return inputSource;
- } catch (FileNotFoundException fne) {
- context.getProject().log(file.getAbsolutePath()+" could not be found",
- Project.MSG_WARN);
- }
- }
-
- return null;
- }
-
-
-
- public void startElement(String uri, String tag, String qname, Attributes attrs)
- throws SAXParseException
- {
- AntHandler next=currentHandler.onStartChild(uri, tag, qname, attrs, context);
- antHandlers.push( currentHandler );
- currentHandler=next;
- currentHandler.onStartElement( uri, tag, qname, attrs, context );
- }
-
-
-
- public void setDocumentLocator(Locator locator) {
- context.locator = locator;
- }
-
-
-
- public void endElement(String uri, String name, String qName) throws SAXException {
- currentHandler.onEndElement(uri, name, context);
- AntHandler prev=(AntHandler)antHandlers.pop();
- currentHandler=prev;
- if( currentHandler!=null )
- currentHandler.onEndChild( uri, name, qName, context );
- }
-
- public void characters(char[] buf, int start, int count)
- throws SAXParseException
- {
- currentHandler.characters( buf, start, count, context );
- }
- }
-
- public static class MainHandler extends AntHandler {
-
- public AntHandler onStartChild(String uri, String name, String qname,
- Attributes attrs,
- AntXmlContext context)
- throws SAXParseException
- {
- if (qname.equals("project")) {
- return ProjectHelperImpl2.projectHandler;
- } else {
- throw new SAXParseException("Unexpected element \"" + qname + "\" " + name, context.locator);
- }
- }
- }
-
-
-
- public static class ProjectHandler extends AntHandler {
-
-
-
- public void onStartElement(String uri, String tag, String qname,
- Attributes attrs,
- AntXmlContext context)
- throws SAXParseException
- {
- String id = null;
- String baseDir = null;
-
- Project project=context.getProject();
-
- for (int i = 0; i < attrs.getLength(); i++) {
- String key = attrs.getQName(i);
- String value = attrs.getValue(i);
-
- if (key.equals("default")) {
- if ( value != null && !value.equals("")) {
- if( !context.ignoreProjectTag )
- project.setDefaultTarget(value);
- }
- } else if (key.equals("name")) {
- if (value != null) {
- context.currentProjectName=value;
-
- if( !context.ignoreProjectTag ) {
- project.setName(value);
- project.addReference(value, project);
- }
- }
- } else if (key.equals("id")) {
- if (value != null) {
-
- if( !context.ignoreProjectTag ) {
- project.addReference(value, project);
- }
- }
- } else if (key.equals("basedir")) {
- if( !context.ignoreProjectTag )
- baseDir = value;
- } else {
-
- throw new SAXParseException("Unexpected attribute \"" + attrs.getQName(i) + "\"", context.locator);
- }
- }
-
- if( context.ignoreProjectTag ) {
-
- return;
- }
-
- if (project.getProperty("basedir") != null) {
- project.setBasedir(project.getProperty("basedir"));
- } else {
-
- if (baseDir == null) {
- project.setBasedir(context.buildFileParent.getAbsolutePath());
- } else {
-
- if ((new File(baseDir)).isAbsolute()) {
- project.setBasedir(baseDir);
- } else {
- project.setBaseDir(project.resolveFile(baseDir,
- context.buildFileParent));
- }
- }
- }
-
- project.addTarget("", context.implicitTarget);
- context.currentTarget=context.implicitTarget;
- }
-
-
-
- public AntHandler onStartChild(String uri, String name, String qname,
- Attributes attrs,
- AntXmlContext context)
- throws SAXParseException
- {
- if (qname.equals("target")) {
- return ProjectHelperImpl2.targetHandler;
- } else {
- return ProjectHelperImpl2.elementHandler;
- }
- }
-
- }
-
-
-
- public static class TargetHandler extends AntHandler {
-
-
-
- public void onStartElement(String uri, String tag, String qname,
- Attributes attrs,
- AntXmlContext context)
- throws SAXParseException
- {
- String name = null;
- String depends = "";
-
- Project project=context.getProject();
- Target target = new Target();
- context.currentTarget=target;
-
- for (int i = 0; i < attrs.getLength(); i++) {
- String key = attrs.getQName(i);
- String value = attrs.getValue(i);
-
- if (key.equals("name")) {
- name = value;
- if( "".equals( name ) )
- throw new BuildException("name attribute must not be empty");
- } else if (key.equals("depends")) {
- depends = value;
- } else if (key.equals("if")) {
- target.setIf(value);
- } else if (key.equals("unless")) {
- target.setUnless(value);
- } else if (key.equals("id")) {
- if (value != null && !value.equals("")) {
- context.getProject().addReference(value, target);
- }
- } else if (key.equals("description")) {
- target.setDescription(value);
- } else {
- throw new SAXParseException("Unexpected attribute \"" + key + "\"", context.locator);
- }
- }
-
- if (name == null) {
- throw new SAXParseException("target element appears without a name attribute",
- context.locator);
- }
-
- Hashtable currentTargets = project.getTargets();
-
-
- if(currentTargets.containsKey(name)) {
-
- if( context.currentProjectName != null ) {
- String newName=context.currentProjectName + "." + name;
- project.log("Already defined in main or a previous import, define "
- + name + " as " + newName,
- Project.MSG_VERBOSE);
- name=newName;
- } else {
- project.log("Already defined in main or a previous import, ignore "
- + name,
- Project.MSG_VERBOSE);
- name=null;
- }
- }
-
- if( name != null ) {
- target.setName(name);
- project.addOrReplaceTarget(name, target);
- }
-
-
- project.log("Targets are now: "+ currentTargets ,
- Project.MSG_VERBOSE);
-
-
- if (depends.length() > 0) {
- target.setDepends(depends);
- }
- }
-
-
-
- public AntHandler onStartChild(String uri, String name, String qname,
- Attributes attrs,
- AntXmlContext context)
- throws SAXParseException
- {
- return ProjectHelperImpl2.elementHandler;
- }
- public void onEndElement(String uri, String tag, AntXmlContext context) {
- context.currentTarget=context.implicitTarget;
- }
- }
-
-
-
- public static class ElementHandler extends AntHandler {
-
-
-
- public ElementHandler() {
- }
-
-
-
- public void onStartElement(String uri, String tag, String qname,
- Attributes attrs,
- AntXmlContext context)
- throws SAXParseException
- {
- RuntimeConfigurable2 parentWrapper=context.currentWrapper();
- RuntimeConfigurable2 wrapper=null;
-
- if (context.getProject().getDataTypeDefinitions().get(qname) != null) {
- try {
- Object element = context.getProject().createDataType(qname);
- if (element == null) {
-
- throw new BuildException("Unknown data type "+qname);
- }
-
- wrapper = new RuntimeConfigurable2(element, qname);
- wrapper.setAttributes2(attrs);
- context.currentTarget.addDataType(wrapper);
- } catch (BuildException exc) {
- throw new SAXParseException(exc.getMessage(), context.locator, exc);
- }
- } else {
- Task task=null;
- try {
- task = context.getProject().createTask(qname);
- } catch (BuildException e) {
-
-
- }
-
- if (task == null) {
- task = new UnknownElement(qname);
- task.setProject(context.getProject());
-
- task.setTaskName(qname);
- }
-
- task.setLocation(new Location(context.locator.getSystemId(),
- context.locator.getLineNumber(),
- context.locator.getColumnNumber()));
- context.configureId(task, attrs);
-
- task.setOwningTarget(context.currentTarget);
-
- Object parent=null;
- if( parentWrapper!=null ) {
- parent=parentWrapper.getProxy();
- }
-
- if( parent instanceof TaskContainer ) {
-
- ((TaskContainer)parent).addTask( task );
- } else {
-
- context.currentTarget.addTask( task );
- }
-
- task.init();
-
- wrapper=new RuntimeConfigurable2(task, task.getTaskName());
- wrapper.setAttributes2(attrs);
-
- if (parentWrapper != null) {
- parentWrapper.addChild(wrapper);
- }
- }
-
- context.pushWrapper( wrapper );
- }
-
-
-
-
- public void characters(char[] buf, int start, int count,
- AntXmlContext context)
- throws SAXParseException
- {
- RuntimeConfigurable2 wrapper=context.currentWrapper();
- wrapper.addText(buf, start, count);
- }
-
-
-
- public AntHandler onStartChild(String uri, String tag, String qname,
- Attributes attrs,
- AntXmlContext context)
- throws SAXParseException
- {
-
- RuntimeConfigurable2 wrapper=context.currentWrapper();
-
- Object element=wrapper.getProxy();
- if (element instanceof TaskContainer) {
-
- return ProjectHelperImpl2.elementHandler;
- }
- else {
- return ProjectHelperImpl2.nestedElementHandler;
- }
- }
-
- public void onEndElement(String uri, String tag, AntXmlContext context) {
- context.popWrapper();
- }
-
- public void onEndChild(String uri, String tag, String qname,
- AntXmlContext context)
- throws SAXParseException
- {
- }
- }
-
-
-
- public static class NestedElementHandler extends ElementHandler {
-
-
- public NestedElementHandler() {
- }
-
-
-
- public void onStartElement(String uri, String propType, String qname,
- Attributes attrs,
- AntXmlContext context)
- throws SAXParseException
- {
- RuntimeConfigurable2 parentWrapper=context.currentWrapper();
- RuntimeConfigurable2 wrapper=null;
- try {
- Object element;
- Object parent=parentWrapper.getProxy();
- if (parent instanceof TaskAdapter) {
- parent = ((TaskAdapter) parent).getProxy();
- }
-
- String elementName = qname.toLowerCase(Locale.US);
- if (parent instanceof UnknownElement) {
- UnknownElement uc = new UnknownElement(elementName);
- uc.setProject(context.getProject());
- ((UnknownElement) parent).addChild(uc);
- element = uc;
- } else {
- Class parentClass = parent.getClass();
- IntrospectionHelper ih =
- IntrospectionHelper.getHelper(parentClass);
- element = ih.createElement(context.getProject(), parent, elementName);
- }
-
- context.configureId(element, attrs);
-
- wrapper = new RuntimeConfigurable2(element, qname);
- wrapper.setAttributes2(attrs);
- parentWrapper.addChild(wrapper);
- } catch (BuildException exc) {
- throw new SAXParseException(exc.getMessage(), context.locator, exc);
- }
- context.pushWrapper( wrapper );
- }
- }
- }
|