|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750 |
-
-
- package 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 org.xml.sax.Locator;
- import org.xml.sax.InputSource;
- import org.xml.sax.HandlerBase;
- import org.xml.sax.SAXParseException;
- import org.xml.sax.SAXException;
- import org.xml.sax.DocumentHandler;
- import org.xml.sax.AttributeList;
- import org.xml.sax.helpers.XMLReaderAdapter;
-
- import javax.xml.parsers.SAXParserFactory;
- import javax.xml.parsers.SAXParser;
- import javax.xml.parsers.ParserConfigurationException;
-
-
-
-
-
-
-
-
-
- public class ProjectHelperImpl extends ProjectHelper {
- private static SAXParserFactory parserFactory = null;
-
- protected Project project;
- protected Object source;
- protected File buildFile;
- protected File buildFileParent;
- private org.xml.sax.Parser parser;
- private Locator locator;
-
-
-
- public HandlerBase defaultProjectHandler( Project project,
- org.xml.sax.Parser parser,
- String tag, AttributeList attrs,
- DocumentHandler parent )
- throws SAXParseException
- {
- this.project=project;
- this.parser=parser;
- ProjectHandler h=new ProjectHandler(this, parent);
- h.init( tag, attrs );
- return h;
- }
-
-
-
- public void parse(Project project, Object source) throws BuildException {
- if( ! (source instanceof File) )
- throw new BuildException( "Only File source is supported by the default helper");
-
- File buildFile=(File)source;
- this.project = project;
- this.buildFile = new File(buildFile.getAbsolutePath());
- buildFileParent = new File(this.buildFile.getParent());
-
- FileInputStream inputStream = null;
- InputSource inputSource = null;
-
- try {
- SAXParser saxParser = getParserFactory().newSAXParser();
- try {
- parser = saxParser.getParser();
- } catch (SAXException exc) {
- parser = new XMLReaderAdapter(saxParser.getXMLReader());
- }
-
- String uri = "file:" + 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(buildFile);
- inputSource = new InputSource(inputStream);
- inputSource.setSystemId(uri);
- project.log("parsing buildfile " + buildFile + " with URI = " + uri, Project.MSG_VERBOSE);
-
- HandlerBase hb = new RootHandler(this);
- parser.setDocumentHandler(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(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) {
-
- }
- }
- }
- }
-
-
-
- private static class AbstractHandler extends HandlerBase {
- protected DocumentHandler parentHandler;
- protected ProjectHelperImpl helper;
-
- public AbstractHandler(ProjectHelperImpl helper, DocumentHandler parentHandler) {
- this.parentHandler = parentHandler;
- this.helper=helper;
-
-
- helper.parser.setDocumentHandler(this);
- }
-
- public void startElement(String tag, AttributeList attrs) throws SAXParseException {
- throw new SAXParseException("Unexpected element \"" + tag + "\"", helper.locator);
- }
-
- public void characters(char[] buf, int start, int end) throws SAXParseException {
- String s = new String(buf, start, end).trim();
-
- if (s.length() > 0) {
- throw new SAXParseException("Unexpected text \"" + s + "\"", helper.locator);
- }
- }
-
-
-
- protected void finished() {}
-
- public void endElement(String name) throws SAXException {
-
- finished();
-
- helper.parser.setDocumentHandler(parentHandler);
- }
- }
-
-
-
- private static class RootHandler extends HandlerBase {
- private ProjectHelperImpl helper;
-
- public RootHandler( ProjectHelperImpl helper ) {
- this.helper=helper;
- }
-
-
-
- public InputSource resolveEntity(String publicId,
- String systemId) {
-
- helper.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(helper.buildFileParent, path);
- }
-
- try {
- InputSource inputSource = new InputSource(new FileInputStream(file));
- inputSource.setSystemId("file:" + entitySystemId);
- return inputSource;
- } catch (FileNotFoundException fne) {
- helper.project.log(file.getAbsolutePath()+" could not be found",
- Project.MSG_WARN);
- }
- }
-
- return null;
- }
-
- public void startElement(String tag, AttributeList attrs) throws SAXParseException {
- if (tag.equals("project")) {
- new ProjectHandler(helper, this).init(tag, attrs);
- } else {
- throw new SAXParseException("Config file is not of expected XML type", helper.locator);
- }
- }
-
- public void setDocumentLocator(Locator locator) {
- helper.locator = locator;
- }
- }
-
-
-
- private static class ProjectHandler extends AbstractHandler {
- public ProjectHandler(ProjectHelperImpl helper, DocumentHandler parentHandler) {
- super(helper, parentHandler);
- }
-
- public void init(String tag, AttributeList attrs) throws SAXParseException {
- String def = null;
- String name = null;
- String id = null;
- String baseDir = null;
-
- for (int i = 0; i < attrs.getLength(); i++) {
- String key = attrs.getName(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.getName(i) + "\"", helper.locator);
- }
- }
-
- if (def == null) {
- throw new SAXParseException("The default attribute of project is required",
- helper.locator);
- }
-
-
- helper.project.setDefaultTarget(def);
-
- if (name != null) {
- helper.project.setName(name);
- helper.project.addReference(name, helper.project);
- }
-
- if (id != null) {
- helper.project.addReference(id, helper.project);
- }
-
- if (helper.project.getProperty("basedir") != null) {
- helper.project.setBasedir(helper.project.getProperty("basedir"));
- } else {
- if (baseDir == null) {
- helper.project.setBasedir(helper.buildFileParent.getAbsolutePath());
- } else {
-
- if ((new File(baseDir)).isAbsolute()) {
- helper.project.setBasedir(baseDir);
- } else {
- helper.project.setBaseDir(helper.project.resolveFile(baseDir, helper.buildFileParent));
- }
- }
- }
-
- }
-
- public void startElement(String name, AttributeList attrs) throws SAXParseException {
- if (name.equals("taskdef")) {
- handleTaskdef(name, attrs);
- } else if (name.equals("typedef")) {
- handleTypedef(name, attrs);
- } else if (name.equals("property")) {
- handleProperty(name, attrs);
- } else if (name.equals("target")) {
- handleTarget(name, attrs);
- } else if (helper.project.getDataTypeDefinitions().get(name) != null) {
- handleDataType(name, attrs);
- } else {
- throw new SAXParseException("Unexpected element \"" + name + "\"", helper.locator);
- }
- }
-
- private void handleTaskdef(String name, AttributeList attrs) throws SAXParseException {
- (new TaskHandler(helper, this, null, null, null)).init(name, attrs);
- }
-
- private void handleTypedef(String name, AttributeList attrs) throws SAXParseException {
- (new TaskHandler(helper, this, null, null, null)).init(name, attrs);
- }
-
- private void handleProperty(String name, AttributeList attrs) throws SAXParseException {
- (new TaskHandler(helper, this, null, null, null)).init(name, attrs);
- }
-
- private void handleTarget(String tag, AttributeList attrs) throws SAXParseException {
- new TargetHandler(helper, this).init(tag, attrs);
- }
-
- private void handleDataType(String name, AttributeList attrs) throws SAXParseException {
- new DataTypeHandler(helper, this).init(name, attrs);
- }
-
- }
-
-
-
- private static class TargetHandler extends AbstractHandler {
- private Target target;
-
- public TargetHandler(ProjectHelperImpl helper, DocumentHandler parentHandler) {
- super(helper, parentHandler);
- }
-
- public void init(String tag, AttributeList attrs) 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.getName(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 + "\"", helper.locator);
- }
- }
-
- if (name == null) {
- throw new SAXParseException("target element appears without a name attribute", helper.locator);
- }
-
- target = new Target();
- target.setName(name);
- target.setIf(ifCond);
- target.setUnless(unlessCond);
- target.setDescription(description);
- helper.project.addTarget(name, target);
-
- if (id != null && !id.equals("")) {
- helper.project.addReference(id, target);
- }
-
-
-
- if (depends.length() > 0) {
- target.setDepends(depends);
- }
- }
-
- public void startElement(String name, AttributeList attrs) throws SAXParseException {
- if (helper.project.getDataTypeDefinitions().get(name) != null) {
- new DataTypeHandler(helper, this, target).init(name, attrs);
- } else {
- new TaskHandler(helper, this, target, null, target).init(name, attrs);
- }
- }
- }
-
-
-
- private static class TaskHandler extends AbstractHandler {
- private Target target;
- private TaskContainer container;
- private Task task;
- private RuntimeConfigurable parentWrapper;
- private RuntimeConfigurable wrapper = null;
-
- public TaskHandler(ProjectHelperImpl helper,
- DocumentHandler parentHandler,
- TaskContainer container,
- RuntimeConfigurable parentWrapper,
- Target target)
- {
- super(helper, parentHandler);
- this.container = container;
- this.parentWrapper = parentWrapper;
- this.target = target;
- }
-
- public void init(String tag, AttributeList attrs) throws SAXParseException {
- try {
- task = helper.project.createTask(tag);
- } catch (BuildException e) {
-
-
- }
-
- if (task == null) {
- task = new UnknownElement(tag);
- task.setProject(helper.project);
- task.setTaskType(tag);
- task.setTaskName(tag);
- }
-
- task.setLocation(new Location(helper.buildFile.toString(),
- helper.locator.getLineNumber(),
- helper.locator.getColumnNumber()));
- helper.configureId(task, attrs);
-
-
- if (target != null) {
- task.setOwningTarget(target);
- container.addTask(task);
- task.init();
- wrapper = task.getRuntimeConfigurableWrapper();
- wrapper.setAttributes(attrs);
- if (parentWrapper != null) {
- parentWrapper.addChild(wrapper);
- }
- } else {
- task.init();
- configure(task, attrs, helper.project);
- }
- }
-
- protected void finished() {
- if (task != null && target == null) {
- task.execute();
- }
- }
-
- public void characters(char[] buf, int start, int end) throws SAXParseException {
- if (wrapper == null) {
- try {
- addText(helper.project, task, buf, start, end);
- } catch (BuildException exc) {
- throw new SAXParseException(exc.getMessage(), helper.locator, exc);
- }
- } else {
- wrapper.addText(buf, start, end);
- }
- }
-
- public void startElement(String name, AttributeList attrs) throws SAXParseException {
- if (task instanceof TaskContainer) {
-
- new TaskHandler(helper, this, (TaskContainer)task, wrapper, target).init(name, attrs);
- }
- else {
- new NestedElementHandler(helper, this, task, wrapper, target).init(name, attrs);
- }
- }
- }
-
-
-
- private static class NestedElementHandler extends AbstractHandler {
- private Object parent;
- private Object child;
- private RuntimeConfigurable parentWrapper;
- private RuntimeConfigurable childWrapper = null;
- private TaskAdapter adapter=null;
- private Target target;
-
- public NestedElementHandler(ProjectHelperImpl helper,
- DocumentHandler parentHandler,
- Object parent,
- RuntimeConfigurable parentWrapper,
- Target target) {
- super(helper, parentHandler);
-
- if (parent instanceof TaskAdapter) {
- this.adapter= (TaskAdapter)parent;
- this.parent = adapter.getProxy();
- } else {
- this.parent = parent;
- }
- this.parentWrapper = parentWrapper;
- this.target = target;
- }
-
- public void init(String propType, AttributeList attrs) throws SAXParseException {
- Class parentClass = parent.getClass();
- IntrospectionHelper ih =
- IntrospectionHelper.getHelper(parentClass);
- if( adapter!=null ) {
- adapter.setIntrospectionHelper( ih );
- }
-
- try {
- String elementName = propType.toLowerCase(Locale.US);
- if (parent instanceof UnknownElement) {
- UnknownElement uc = new UnknownElement(elementName);
- uc.setProject(helper.project);
- ((UnknownElement) parent).addChild(uc);
- child = uc;
- } else {
- child = ih.createElement(helper.project, parent, elementName);
- }
-
- helper.configureId(child, attrs);
-
- if (parentWrapper != null) {
- childWrapper = new RuntimeConfigurable(child, propType);
- childWrapper.setAttributes(attrs);
- parentWrapper.addChild(childWrapper);
- } else {
- configure(child, attrs, helper.project);
- ih.storeElement(helper.project, parent, child, elementName);
- }
- } catch (BuildException exc) {
- throw new SAXParseException(exc.getMessage(), helper.locator, exc);
- }
- }
-
- public void characters(char[] buf, int start, int end) throws SAXParseException {
- if (parentWrapper == null) {
- try {
- addText(helper.project, child, buf, start, end);
- } catch (BuildException exc) {
- throw new SAXParseException(exc.getMessage(), helper.locator, exc);
- }
- } else {
- childWrapper.addText(buf, start, end);
- }
- }
-
- public void startElement(String name, AttributeList attrs) throws SAXParseException {
- if (child instanceof TaskContainer) {
-
-
- new TaskHandler(helper, this, (TaskContainer)child, childWrapper, target).init(name, attrs);
- }
- else {
- new NestedElementHandler(helper, this, child, childWrapper, target).init(name, attrs);
- }
- }
- }
-
-
-
- private static class DataTypeHandler extends AbstractHandler {
- private Target target;
- private Object element;
- private RuntimeConfigurable wrapper = null;
-
- public DataTypeHandler(ProjectHelperImpl helper, DocumentHandler parentHandler) {
- this(helper, parentHandler, null);
- }
-
- public DataTypeHandler(ProjectHelperImpl helper,
- DocumentHandler parentHandler,
- Target target)
- {
- super(helper, parentHandler);
- this.target = target;
- }
-
- public void init(String propType, AttributeList attrs) throws SAXParseException {
- try {
- element = helper.project.createDataType(propType);
- if (element == null) {
- throw new BuildException("Unknown data type "+propType);
- }
-
- if (target != null) {
- wrapper = new RuntimeConfigurable(element, propType);
- wrapper.setAttributes(attrs);
- target.addDataType(wrapper);
- } else {
- configure(element, attrs, helper.project);
- helper.configureId(element, attrs);
- }
- } catch (BuildException exc) {
- throw new SAXParseException(exc.getMessage(), helper.locator, exc);
- }
- }
-
- public void characters(char[] buf, int start, int end) throws SAXParseException {
- try {
- addText(helper.project, element, buf, start, end);
- } catch (BuildException exc) {
- throw new SAXParseException(exc.getMessage(), helper.locator, exc);
- }
- }
-
- public void startElement(String name, AttributeList attrs) throws SAXParseException {
- new NestedElementHandler(helper, this, element, wrapper, target).init(name, attrs);
- }
- }
-
- public ProjectHelperImpl() {
- }
-
- private static SAXParserFactory getParserFactory() {
- if (parserFactory == null) {
- parserFactory = SAXParserFactory.newInstance();
- }
-
- return parserFactory;
- }
-
-
-
- private void configureId(Object target, AttributeList attr) {
- String id = attr.getValue("id");
- if (id != null) {
- project.addReference(id, target);
- }
- }
-
- }
|