|
-
-
- 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 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 javax.xml.parsers.SAXParserFactory;
- import javax.xml.parsers.SAXParser;
- import javax.xml.parsers.ParserConfigurationException;
-
-
- public class ProjectHelperImpl2 extends ProjectHelper {
-
-
-
-
- private static SAXParserFactory parserFactory = null;
-
-
-
- public void parse(Project project, Object source) throws BuildException {
- AntXmlContext context=new AntXmlContext();
- 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.project = project;
- context.buildFile = new File(context.buildFile.getAbsolutePath());
- context.buildFileParent = new File(context.buildFile.getParent());
-
- try {
-
-
- org.xml.sax.XMLReader parser;
-
- if (parserFactory == null) {
- parserFactory = SAXParserFactory.newInstance();
- }
-
- SAXParser saxParser = parserFactory.newSAXParser();
- parser =saxParser.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 = new RootHandler(context);
-
- parser.setContentHandler(hb);
- parser.setEntityResolver(hb);
- parser.setErrorHandler(hb);
- parser.setDTDHandler(hb);
- parser.parse(inputSource);
- }
- catch(ParserConfigurationException exc) {
- throw new BuildException("Parser has not been configured correctly", exc);
- }
- catch(SAXParseException exc) {
- Location location =
- new Location(context.buildFile.toString(), 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(IOException exc) {
- throw new BuildException("Error reading project file", 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
- {
- throw new SAXParseException("Unexpected element \" " + qname + "\"", context.locator);
- }
-
-
-
- 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 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 {
-
- Project project;
-
- File buildFile;
-
-
- File buildFileParent;
-
-
- Locator locator;
-
-
-
- void configureId(Object target, Attributes attr) {
- String id = attr.getValue("id");
- if (id != null) {
- project.addReference(id, target);
- }
- }
-
- }
-
-
-
-
- public static class RootHandler extends DefaultHandler {
- Stack antHandlers=new Stack();
- AntHandler currentHandler;
- AntXmlContext context;
-
- public RootHandler(AntXmlContext context) {
- currentHandler=new MainHandler();
- antHandlers.push( currentHandler );
- this.context=context;
- }
-
-
-
- public InputSource resolveEntity(String publicId,
- String systemId) {
-
- context.project.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.project.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;
- }
-
- public void characters(char[] buf, int start, int count)
- throws SAXParseException
- {
- currentHandler.characters( buf, start, count, context );
- }
- }
-
- public static class MainHandler extends AntHandler {
-
- public void onStartElement(String uri, String tag, String qname,
- Attributes attrs,
- AntXmlContext context)
- throws SAXParseException
- {
- }
-
- public AntHandler onStartChild(String uri, String name, String qname,
- Attributes attrs,
- AntXmlContext context)
- throws SAXParseException
- {
- if (qname.equals("project")) {
- return new 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 def = null;
- String name = null;
- String id = null;
- String baseDir = null;
-
- if (! qname.equals("project")) {
- throw new SAXParseException("Config file is not of expected XML type", context.locator);
- }
-
- for (int i = 0; i < attrs.getLength(); i++) {
- String key = attrs.getQName(i);
- String value = attrs.getValue(i);
-
- if (key.equals("default")) {
- def = value;
- } else if (key.equals("name")) {
- name = value;
- } else if (key.equals("id")) {
- id = value;
- } else if (key.equals("basedir")) {
- baseDir = value;
- } else {
- throw new SAXParseException("Unexpected attribute \"" + attrs.getQName(i) + "\"", context.locator);
- }
- }
-
- if (def == null) {
- throw new SAXParseException("The default attribute of project is required",
- context.locator);
- }
-
- Project project=context.project;
- project.setDefaultTarget(def);
-
- if (name != null) {
- project.setName(name);
- project.addReference(name, project);
- }
-
- if (id != null) {
- project.addReference(id, project);
- }
-
- 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));
- }
- }
- }
-
- }
-
-
-
- public AntHandler onStartChild(String uri, String name, String qname,
- Attributes attrs,
- AntXmlContext context)
- throws SAXParseException
- {
- if (qname.equals("taskdef")) {
- return new TaskHandler(null, null, null);
- } else if (qname.equals("typedef")) {
- return new TaskHandler(null, null, null);
- } else if (qname.equals("property")) {
- return new TaskHandler(null, null, null);
- } else if (qname.equals("target")) {
- return new TargetHandler();
- } else if (context.project.getDataTypeDefinitions().get(qname) != null) {
- return new DataTypeHandler(null);
- } else {
- throw new SAXParseException("Unexpected element \"" + qname + "\" " + name, context.locator);
- }
- }
- }
-
-
-
- public static class TargetHandler extends AntHandler {
- private Target target;
-
-
-
- public void onStartElement(String uri, String tag, String qname,
- Attributes attrs,
- AntXmlContext context)
- throws SAXParseException
- {
- String name = null;
- String depends = "";
- String ifCond = null;
- String unlessCond = null;
- String id = null;
- String description = null;
-
- for (int i = 0; i < attrs.getLength(); i++) {
- String key = attrs.getQName(i);
- String value = attrs.getValue(i);
-
- if (key.equals("name")) {
- name = value;
- } else if (key.equals("depends")) {
- depends = value;
- } else if (key.equals("if")) {
- ifCond = value;
- } else if (key.equals("unless")) {
- unlessCond = value;
- } else if (key.equals("id")) {
- id = value;
- } else if (key.equals("description")) {
- description = 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);
- }
-
- target = new Target();
- target.setName(name);
- target.setIf(ifCond);
- target.setUnless(unlessCond);
- target.setDescription(description);
- context.project.addTarget(name, target);
-
- if (id != null && !id.equals("")) {
- context.project.addReference(id, target);
- }
-
-
-
- if (depends.length() > 0) {
- target.setDepends(depends);
- }
- }
-
-
-
- public AntHandler onStartChild(String uri, String name, String qname,
- Attributes attrs,
- AntXmlContext context)
- throws SAXParseException
- {
- if (context.project.getDataTypeDefinitions().get(qname) != null) {
- return new DataTypeHandler(target);
- } else {
- return new TaskHandler(target, null, target);
- }
- }
- }
-
-
-
- public static class TaskHandler extends AntHandler {
-
- private Target target;
-
-
- private TaskContainer container;
-
-
- private Task task;
-
-
- private RuntimeConfigurable parentWrapper;
-
-
- private RuntimeConfigurable wrapper = null;
-
-
-
- public TaskHandler(TaskContainer container, RuntimeConfigurable parentWrapper, Target target) {
- this.container = container;
- this.parentWrapper = parentWrapper;
- this.target = target;
- }
-
-
-
- public void onStartElement(String uri, String tag, String qname,
- Attributes attrs,
- AntXmlContext context)
- throws SAXParseException
- {
- try {
- task = context.project.createTask(qname);
- } catch (BuildException e) {
-
-
- }
-
- if (task == null) {
- task = new UnknownElement(qname);
- task.setProject(context.project);
-
- task.setTaskName(qname);
- }
-
- task.setLocation(new Location(context.buildFile.toString(),
- context.locator.getLineNumber(),
- context.locator.getColumnNumber()));
- context.configureId(task, attrs);
-
-
- if (target != null) {
- task.setOwningTarget(target);
- container.addTask(task);
- task.init();
- wrapper = task.getRuntimeConfigurableWrapper();
- wrapper.setAttributes(sax1Attributes(attrs));
- if (parentWrapper != null) {
- parentWrapper.addChild(wrapper);
- }
- } else {
- task.init();
- ProjectHelper.configure(task, sax1Attributes(attrs), context.project);
- }
- }
-
-
-
- public void onEndElement(String uri, String tag, AntXmlContext context) {
- if (task != null && target == null) {
- task.execute();
- }
- }
-
-
-
- public void characters(char[] buf, int start, int count,
- AntXmlContext context)
- throws SAXParseException
- {
- if (wrapper == null) {
- try {
- ProjectHelper.addText(context.project, task, buf, start, count);
- } catch (BuildException exc) {
- throw new SAXParseException(exc.getMessage(), context.locator, exc);
- }
- } else {
- wrapper.addText(buf, start, count);
- }
- }
-
-
-
- public AntHandler onStartChild(String uri, String tag, String qname,
- Attributes attrs,
- AntXmlContext context)
- throws SAXParseException
- {
- if (task instanceof TaskContainer) {
-
- return new TaskHandler((TaskContainer)task, wrapper, target);
- }
- else {
- return new NestedElementHandler(task, wrapper, target);
- }
- }
- }
-
-
-
- public static class NestedElementHandler extends AntHandler {
-
- private Object parent;
-
- private Object child;
-
-
- private RuntimeConfigurable parentWrapper;
-
-
- private RuntimeConfigurable childWrapper = null;
-
- private Target target;
-
-
-
- public NestedElementHandler(Object parent,
- RuntimeConfigurable parentWrapper,
- Target target) {
- if (parent instanceof TaskAdapter) {
- this.parent = ((TaskAdapter) parent).getProxy();
- } else {
- this.parent = parent;
- }
- this.parentWrapper = parentWrapper;
- this.target = target;
- }
-
-
-
- public void onStartElement(String uri, String propType, String qname,
- Attributes attrs,
- AntXmlContext context)
- throws SAXParseException
- {
- Class parentClass = parent.getClass();
- IntrospectionHelper ih =
- IntrospectionHelper.getHelper(parentClass);
-
- try {
- String elementName = qname.toLowerCase(Locale.US);
- if (parent instanceof UnknownElement) {
- UnknownElement uc = new UnknownElement(elementName);
- uc.setProject(context.project);
- ((UnknownElement) parent).addChild(uc);
- child = uc;
- } else {
- child = ih.createElement(context.project, parent, elementName);
- }
-
- context.configureId(child, attrs);
-
- if (parentWrapper != null) {
- childWrapper = new RuntimeConfigurable(child, qname);
- childWrapper.setAttributes(sax1Attributes(attrs));
- parentWrapper.addChild(childWrapper);
- } else {
- ProjectHelper.configure(child, sax1Attributes(attrs), context.project);
- ih.storeElement(context.project, parent, child, elementName);
- }
- } catch (BuildException exc) {
- throw new SAXParseException(exc.getMessage(), context.locator, exc);
- }
- }
-
-
-
- public void characters(char[] buf, int start, int count,
- AntXmlContext context)
- throws SAXParseException
- {
- if (parentWrapper == null) {
- try {
- ProjectHelper.addText(context.project, child, buf, start, count);
- } catch (BuildException exc) {
- throw new SAXParseException(exc.getMessage(), context.locator, exc);
- }
- } else {
- childWrapper.addText(buf, start, count);
- }
- }
-
-
-
- public AntHandler onStartChild(String uri, String tag, String qname,
- Attributes attrs,
- AntXmlContext context)
- throws SAXParseException
- {
- if (child instanceof TaskContainer) {
-
-
- return new TaskHandler((TaskContainer)child, childWrapper, target);
- }
- else {
- return new NestedElementHandler(child, childWrapper, target);
- }
- }
- }
-
-
-
- public static class DataTypeHandler extends AntHandler {
-
- private Target target;
-
- private Object element;
-
- private RuntimeConfigurable wrapper = null;
-
-
-
- public DataTypeHandler( Target target) {
- this.target = target;
- }
-
-
-
- public void onStartElement(String uri, String propType, String qname,
- Attributes attrs,
- AntXmlContext context)
- throws SAXParseException
- {
- try {
- element = context.project.createDataType(qname);
- if (element == null) {
- throw new BuildException("Unknown data type "+qname);
- }
-
- if (target != null) {
- wrapper = new RuntimeConfigurable(element, qname);
- wrapper.setAttributes(sax1Attributes(attrs));
- target.addDataType(wrapper);
- } else {
- ProjectHelper.configure(element, sax1Attributes(attrs), context.project);
- context.configureId(element, attrs);
- }
- } catch (BuildException exc) {
- throw new SAXParseException(exc.getMessage(), context.locator, exc);
- }
- }
-
-
-
-
-
- public void characters(char[] buf, int start, int count,
- AntXmlContext context)
- throws SAXParseException
- {
- try {
- ProjectHelper.addText(context.project, element, buf, start, count);
- } catch (BuildException exc) {
- throw new SAXParseException(exc.getMessage(), context.locator, exc);
- }
- }
-
-
-
- public AntHandler onStartChild(String uri, String tag, String qname,
- Attributes attrs, AntXmlContext context)
- throws SAXParseException
- {
- return new NestedElementHandler(element, wrapper, target);
- }
- }
-
- public static AttributeList sax1Attributes( Attributes sax2Att ) {
- AttributeListImpl sax1Att=new AttributeListImpl();
- int length = sax2Att.getLength();
- if (length > 0) {
- for (int i = 0; i < length; i++) {
-
-
- sax1Att.addAttribute( sax2Att.getQName(i),
- sax2Att.getType(i),
- sax2Att.getValue(i));
- }
- }
- return sax1Att;
- }
- }
|