/** * Parse a source xml input. * * @param project the current project * @param source the xml source * @exception BuildException if an error occurs */ public void parse(Project project, Object source) throws BuildException { getImportStack().addElement(source); AntXMLContext context = null; context = (AntXMLContext) project.getReference(REFID_CONTEXT); if (context == null) { context = new AntXMLContext(project); project.addReference(REFID_CONTEXT, context); project.addReference(REFID_TARGETS, context.getTargets()); } if (getImportStack().size() > 1) { // we are in an imported file. context.setIgnoreProjectTag(true); Target currentTarget = context.getCurrentTarget(); Target currentImplicit = context.getImplicitTarget(); Map<String, Target> currentTargets = context.getCurrentTargets(); try { Target newCurrent = new Target(); newCurrent.setProject(project); newCurrent.setName(""); context.setCurrentTarget(newCurrent); context.setCurrentTargets(new HashMap<String, Target>()); context.setImplicitTarget(newCurrent); parse(project, source, new RootHandler(context, mainHandler)); newCurrent.execute(); } finally { context.setCurrentTarget(currentTarget); context.setImplicitTarget(currentImplicit); context.setCurrentTargets(currentTargets); } } else { // top level file context.setCurrentTargets(new HashMap<String, Target>()); parse(project, source, new RootHandler(context, mainHandler)); // Execute the top-level target context.getImplicitTarget().execute(); // resolve extensionOf attributes resolveExtensionOfAttributes(project); } }
/** * Initialisation routine called after handler creation with the element name and attributes. * The attributes which this handler can deal with are: <code>"default"</code>, <code>"name" * </code>, <code>"id"</code> and <code>"basedir"</code>. * * @param uri The namespace URI for this element. * @param tag Name of the element which caused this handler to be created. Should not be <code> * null</code>. Ignored in this implementation. * @param qname The qualified name for this element. * @param attrs Attributes of the element which caused this handler to be created. Must not be * <code>null</code>. * @param context The current context. * @exception SAXParseException if an unexpected attribute is encountered or if the <code> * "default"</code> attribute is missing. */ public void onStartElement( String uri, String tag, String qname, Attributes attrs, AntXMLContext context) throws SAXParseException { String baseDir = null; boolean nameAttributeSet = false; Project project = context.getProject(); // Set the location of the implicit target associated with the project tag context.getImplicitTarget().setLocation(new Location(context.getLocator())); /** * XXX I really don't like this - the XML processor is still too 'involved' in the processing. * A better solution (IMO) would be to create UE for Project and Target too, and then process * the tree and have Project/Target deal with its attributes ( similar with Description ). * * <p>If we eventually switch to ( or add support for ) DOM, things will work smoothly - UE * can be avoided almost completely ( it could still be created on demand, for backward * compatibility ) */ for (int i = 0; i < attrs.getLength(); i++) { String attrUri = attrs.getURI(i); if (attrUri != null && !attrUri.equals("") && !attrUri.equals(uri)) { continue; // Ignore attributes from unknown uris } String key = attrs.getLocalName(i); String value = attrs.getValue(i); if (key.equals("default")) { if (value != null && !value.equals("")) { if (!context.isIgnoringProjectTag()) { project.setDefault(value); } } } else if (key.equals("name")) { if (value != null) { context.setCurrentProjectName(value); nameAttributeSet = true; if (!context.isIgnoringProjectTag()) { project.setName(value); project.addReference(value, project); } else if (isInIncludeMode()) { if (!"".equals(value) && (getCurrentTargetPrefix() == null || getCurrentTargetPrefix().length() == 0)) { // help nested include tasks setCurrentTargetPrefix(value); } } } } else if (key.equals("id")) { if (value != null) { // What's the difference between id and name ? if (!context.isIgnoringProjectTag()) { project.addReference(value, project); } } } else if (key.equals("basedir")) { if (!context.isIgnoringProjectTag()) { baseDir = value; } } else { // XXX ignore attributes in a different NS ( maybe store them ? ) throw new SAXParseException( "Unexpected attribute \"" + attrs.getQName(i) + "\"", context.getLocator()); } } // XXX Move to Project ( so it is shared by all helpers ) String antFileProp = MagicNames.ANT_FILE + "." + context.getCurrentProjectName(); String dup = project.getProperty(antFileProp); String typeProp = MagicNames.ANT_FILE_TYPE + "." + context.getCurrentProjectName(); String dupType = project.getProperty(typeProp); if (dup != null && nameAttributeSet) { Object dupFile = null; Object contextFile = null; if (MagicNames.ANT_FILE_TYPE_URL.equals(dupType)) { try { dupFile = new URL(dup); } catch (java.net.MalformedURLException mue) { throw new BuildException( "failed to parse " + dup + " as URL while looking" + " at a duplicate project" + " name.", mue); } contextFile = context.getBuildFileURL(); } else { dupFile = new File(dup); contextFile = context.getBuildFile(); } if (context.isIgnoringProjectTag() && !dupFile.equals(contextFile)) { project.log( "Duplicated project name in import. Project " + context.getCurrentProjectName() + " defined first in " + dup + " and again in " + contextFile, Project.MSG_WARN); } } if (nameAttributeSet) { if (context.getBuildFile() != null) { project.setUserProperty(antFileProp, context.getBuildFile().toString()); project.setUserProperty(typeProp, MagicNames.ANT_FILE_TYPE_FILE); } else if (context.getBuildFileURL() != null) { project.setUserProperty(antFileProp, context.getBuildFileURL().toString()); project.setUserProperty(typeProp, MagicNames.ANT_FILE_TYPE_URL); } } if (context.isIgnoringProjectTag()) { // no further processing return; } // set explicitly before starting ? if (project.getProperty("basedir") != null) { project.setBasedir(project.getProperty("basedir")); } else { // Default for baseDir is the location of the build file. if (baseDir == null) { project.setBasedir(context.getBuildFileParent().getAbsolutePath()); } else { // check whether the user has specified an absolute path if ((new File(baseDir)).isAbsolute()) { project.setBasedir(baseDir); } else { project.setBaseDir(FILE_UTILS.resolveFile(context.getBuildFileParent(), baseDir)); } } } project.addTarget("", context.getImplicitTarget()); context.setCurrentTarget(context.getImplicitTarget()); }
/** * Parse a source xml input. * * @param project the current project * @param source the xml source * @exception BuildException if an error occurs */ public void parse(Project project, Object source) throws BuildException { getImportStack().addElement(source); AntXMLContext context = null; context = (AntXMLContext) project.getReference(REFID_CONTEXT); if (context == null) { context = new AntXMLContext(project); project.addReference(REFID_CONTEXT, context); project.addReference(REFID_TARGETS, context.getTargets()); } if (getImportStack().size() > 1) { // we are in an imported file. context.setIgnoreProjectTag(true); Target currentTarget = context.getCurrentTarget(); Target currentImplicit = context.getImplicitTarget(); Map currentTargets = context.getCurrentTargets(); try { Target newCurrent = new Target(); newCurrent.setProject(project); newCurrent.setName(""); context.setCurrentTarget(newCurrent); context.setCurrentTargets(new HashMap()); context.setImplicitTarget(newCurrent); parse(project, source, new RootHandler(context, mainHandler)); newCurrent.execute(); } finally { context.setCurrentTarget(currentTarget); context.setImplicitTarget(currentImplicit); context.setCurrentTargets(currentTargets); } } else { // top level file context.setCurrentTargets(new HashMap()); parse(project, source, new RootHandler(context, mainHandler)); // Execute the top-level target context.getImplicitTarget().execute(); // resolve extensionOf attributes for (Iterator i = getExtensionStack().iterator(); i.hasNext(); ) { String[] extensionInfo = (String[]) i.next(); String tgName = extensionInfo[0]; String name = extensionInfo[1]; OnMissingExtensionPoint missingBehaviour = OnMissingExtensionPoint.valueOf(extensionInfo[2]); Hashtable projectTargets = project.getTargets(); if (!projectTargets.containsKey(tgName)) { String message = "can't add target " + name + " to extension-point " + tgName + " because the extension-point is unknown."; if (missingBehaviour == OnMissingExtensionPoint.FAIL) { throw new BuildException(message); } else if (missingBehaviour == OnMissingExtensionPoint.WARN) { Target target = (Target) projectTargets.get(name); context.getProject().log(target, "Warning: " + message, Project.MSG_WARN); } } else { Target t = (Target) projectTargets.get(tgName); if (!(t instanceof ExtensionPoint)) { throw new BuildException("referenced target " + tgName + " is not an extension-point"); } t.addDependency(name); } } } }
/** * Handle the end of the project, sets the current target of the context to be the implicit * target. * * @param uri The namespace URI of the element. * @param tag The name of the element. * @param context The current context. */ public void onEndElement(String uri, String tag, AntXMLContext context) { context.setCurrentTarget(context.getImplicitTarget()); }