public void onStartElement()

in src/main/org/apache/tools/ant/helper/ProjectHelper2.java [898:1050]


        public void onStartElement(String uri, String tag, String qname, Attributes attrs,
                                   AntXMLContext context) throws SAXParseException {
            String name = null;
            String depends = "";
            String extensionPoint = null;
            OnMissingExtensionPoint extensionPointMissing = null;

            Project project = context.getProject();
            Target target = "target".equals(tag)
                ? new Target() : new ExtensionPoint();
            target.setProject(project);
            target.setLocation(new Location(context.getLocator()));
            context.addTarget(target);

            for (int i = 0; i < attrs.getLength(); i++) {
                String attrUri = attrs.getURI(i);
                if (attrUri != null && !attrUri.isEmpty() && !attrUri.equals(uri)) {
                    continue; // Ignore attributes from unknown uris
                }
                String value = attrs.getValue(i);
                switch (attrs.getLocalName(i)) {
                    case "name":
                        name = value;
                        if (name.isEmpty()) {
                            throw new BuildException("name attribute must not be empty");
                        }
                        break;
                    case "depends":
                        depends = value;
                        break;
                    case "if":
                        target.setIf(value);
                        break;
                    case "unless":
                        target.setUnless(value);
                        break;
                    case "id":
                        if (value != null && !value.isEmpty()) {
                            context.getProject().addReference(value, target);
                        }
                        break;
                    case "description":
                        target.setDescription(value);
                        break;
                    case "extensionOf":
                        extensionPoint = value;
                        break;
                    case "onMissingExtensionPoint":
                        try {
                            extensionPointMissing = OnMissingExtensionPoint.valueOf(value);
                        } catch (IllegalArgumentException e) {
                            throw new BuildException("Invalid onMissingExtensionPoint " + value);
                        }
                        break;
                    default:
                        throw new SAXParseException("Unexpected attribute \"" + attrs.getQName(i)
                                + "\"", context.getLocator());
                }
            }

            if (name == null) {
                throw new SAXParseException("target element appears without a name attribute",
                                            context.getLocator());
            }

            String prefix = null;
            boolean isInIncludeMode =
                context.isIgnoringProjectTag() && isInIncludeMode();
            String sep = getCurrentPrefixSeparator();

            if (isInIncludeMode) {
                prefix = getTargetPrefix(context);
                if (prefix == null) {
                    throw new BuildException("can't include build file "
                                             + context.getBuildFileURL()
                                             + ", no as attribute has been given"
                                             + " and the project tag doesn't"
                                             + " specify a name attribute");
                }
                name = prefix + sep + name;
            }

            // Check if this target is in the current build file
            if (context.getCurrentTargets().get(name) != null) {
                throw new BuildException("Duplicate target '" + name + "'",
                                         target.getLocation());
            }
            Hashtable<String, Target> projectTargets = project.getTargets();
            boolean usedTarget = false;
            // If the name has not already been defined define it
            if (projectTargets.containsKey(name)) {
                project.log("Already defined in main or a previous import, ignore " + name,
                            Project.MSG_VERBOSE);
            } else {
                target.setName(name);
                context.getCurrentTargets().put(name, target);
                project.addOrReplaceTarget(name, target);
                usedTarget = true;
            }

            if (!depends.isEmpty()) {
                if (!isInIncludeMode) {
                    target.setDepends(depends);
                } else {
                    for (String string : Target.parseDepends(depends, name, "depends")) {
                        target.addDependency(prefix + sep + string);
                   }
                }
            }
            if (!isInIncludeMode && context.isIgnoringProjectTag()
                && (prefix = getTargetPrefix(context)) != null) {
                // In an imported file (and not completely
                // ignoring the project tag or having a preconfigured prefix)
                String newName = prefix + sep + name;
                Target newTarget = target;
                if (usedTarget) {
                    newTarget = "target".equals(tag)
                            ? new Target(target) : new ExtensionPoint(target);
                }
                newTarget.setName(newName);
                context.getCurrentTargets().put(newName, newTarget);
                project.addOrReplaceTarget(newName, newTarget);
            }
            if (extensionPointMissing != null && extensionPoint == null) {
                throw new BuildException("onMissingExtensionPoint attribute cannot " +
                                         "be specified unless extensionOf is specified",
                                         target.getLocation());

            }
            if (extensionPoint != null) {
                ProjectHelper helper =
                        context.getProject().getReference(MagicNames.REFID_PROJECT_HELPER);
                for (String extPointName : Target.parseDepends(extensionPoint, name, "extensionOf")) {
                    if (extensionPointMissing == null) {
                        extensionPointMissing = OnMissingExtensionPoint.FAIL;
                    }
                    // defer extensionpoint resolution until the full
                    // import stack has been processed
                    if (isInIncludeMode()) {
                        // if in include mode, provide prefix we're including by
                        // so that we can try and resolve extension point from
                        // the local file first
                        helper.getExtensionStack().add(
                                new String[] {extPointName, target.getName(),
                                        extensionPointMissing.name(), prefix + sep});
                    } else {
                        helper.getExtensionStack().add(
                                new String[] {extPointName, target.getName(),
                                        extensionPointMissing.name()});
                    }
                }
            }
        }