use of org.apache.tools.ant.UnknownElement in project ant by apache.
the class MacroInstance method execute.
/**
* Execute the templates instance.
* Copies the unknown element, substitutes the attributes,
* and calls perform on the unknown element.
*/
@Override
public void execute() {
presentElements = new HashMap<>();
getNsElements();
processTasks();
localAttributes = new Hashtable<>();
Set<String> copyKeys = new HashSet<>(map.keySet());
for (Attribute attribute : macroDef.getAttributes()) {
String value = map.get(attribute.getName());
if (value == null && "description".equals(attribute.getName())) {
value = getDescription();
}
if (value == null) {
value = attribute.getDefault();
value = macroSubs(value, localAttributes);
}
if (value == null) {
throw new BuildException("required attribute %s not set", attribute.getName());
}
localAttributes.put(attribute.getName(), value);
copyKeys.remove(attribute.getName());
}
copyKeys.remove("id");
if (macroDef.getText() != null) {
if (text == null) {
String defaultText = macroDef.getText().getDefault();
if (!macroDef.getText().getOptional() && defaultText == null) {
throw new BuildException("required text missing");
}
text = defaultText == null ? "" : defaultText;
}
if (macroDef.getText().getTrim()) {
text = text.trim();
}
localAttributes.put(macroDef.getText().getName(), text);
} else if (text != null && !text.trim().isEmpty()) {
throw new BuildException("The \"%s\" macro does not support nested text data.", getTaskName());
}
if (!copyKeys.isEmpty()) {
throw new BuildException("Unknown attribute" + (copyKeys.size() > 1 ? "s " : " ") + copyKeys);
}
// need to set the project on unknown element
UnknownElement c = copy(macroDef.getNestedTask(), false);
c.init();
LocalProperties localProperties = LocalProperties.get(getProject());
localProperties.enterScope();
try {
c.perform();
} catch (BuildException ex) {
if (macroDef.getBackTrace()) {
throw ProjectHelper.addLocationToBuildException(ex, getLocation());
} else {
ex.setLocation(getLocation());
throw ex;
}
} finally {
presentElements = null;
localAttributes = null;
localProperties.exitScope();
}
}
use of org.apache.tools.ant.UnknownElement in project ant by apache.
the class ProjectHelper2 method parseUnknownElement.
/**
* Parse an unknown element from a url
*
* @param project the current project
* @param source the url containing the task
* @return a configured task
* @exception BuildException if an error occurs
*/
public UnknownElement parseUnknownElement(Project project, URL source) throws BuildException {
Target dummyTarget = new Target();
dummyTarget.setProject(project);
AntXMLContext context = new AntXMLContext(project);
context.addTarget(dummyTarget);
context.setImplicitTarget(dummyTarget);
parse(context.getProject(), source, new RootHandler(context, elementHandler));
Task[] tasks = dummyTarget.getTasks();
if (tasks.length != 1) {
throw new BuildException("No tasks defined");
}
return (UnknownElement) tasks[0];
}
use of org.apache.tools.ant.UnknownElement in project ant by apache.
the class DispatchUtils method execute.
/**
* Determines and Executes the action method for the task.
* @param task the task to execute.
* @throws BuildException on error.
*/
public static final void execute(Object task) throws BuildException {
String methodName = "execute";
Dispatchable dispatchable = null;
try {
if (task instanceof Dispatchable) {
dispatchable = (Dispatchable) task;
} else if (task instanceof UnknownElement) {
UnknownElement ue = (UnknownElement) task;
Object realThing = ue.getRealThing();
if (realThing instanceof Dispatchable && realThing instanceof Task) {
dispatchable = (Dispatchable) realThing;
}
}
if (dispatchable != null) {
String mName = null;
try {
final String name = dispatchable.getActionParameterName();
if (name != null && name.trim().length() > 0) {
mName = "get" + name.trim().substring(0, 1).toUpperCase();
if (name.length() > 1) {
mName += name.substring(1);
}
final Class<? extends Dispatchable> c = dispatchable.getClass();
final Method actionM = c.getMethod(mName);
if (actionM != null) {
final Object o = actionM.invoke(dispatchable, (Object[]) null);
if (o != null) {
final String s = o.toString();
if (s.trim().length() > 0) {
methodName = s.trim();
Method executeM = null;
executeM = dispatchable.getClass().getMethod(methodName);
if (executeM == null) {
throw new BuildException("No public " + methodName + "() in " + dispatchable.getClass());
}
executeM.invoke(dispatchable, (Object[]) null);
if (task instanceof UnknownElement) {
((UnknownElement) task).setRealThing(null);
}
} else {
throw new BuildException("Dispatchable Task attribute '" + name.trim() + "' not set or value is empty.");
}
} else {
throw new BuildException("Dispatchable Task attribute '" + name.trim() + "' not set or value is empty.");
}
}
} else {
throw new BuildException("Action Parameter Name must not be empty for Dispatchable Task.");
}
} catch (NoSuchMethodException nsme) {
throw new BuildException("No public " + mName + "() in " + task.getClass());
}
} else {
Method executeM = null;
executeM = task.getClass().getMethod(methodName);
if (executeM == null) {
throw new BuildException("No public " + methodName + "() in " + task.getClass());
}
executeM.invoke(task);
if (task instanceof UnknownElement) {
((UnknownElement) task).setRealThing(null);
}
}
} catch (InvocationTargetException ie) {
Throwable t = ie.getTargetException();
if (t instanceof BuildException) {
throw ((BuildException) t);
} else {
throw new BuildException(t);
}
} catch (NoSuchMethodException e) {
throw new BuildException(e);
} catch (IllegalAccessException e) {
throw new BuildException(e);
}
}
use of org.apache.tools.ant.UnknownElement in project ant by apache.
the class Antlib method createAntlib.
/**
* Static method to read an ant lib definition from
* a url.
*
* @param project the current project
* @param antlibUrl the url to read the definitions from
* @param uri the uri that the antlib is to be placed in
* @return the ant lib task
*/
public static Antlib createAntlib(Project project, URL antlibUrl, String uri) {
// Check if we can contact the URL
try {
URLConnection conn = antlibUrl.openConnection();
conn.setUseCaches(false);
conn.connect();
} catch (IOException ex) {
throw new BuildException("Unable to find " + antlibUrl, ex);
}
ComponentHelper helper = ComponentHelper.getComponentHelper(project);
helper.enterAntLib(uri);
URLResource antlibResource = new URLResource(antlibUrl);
try {
// Should be safe to parse
ProjectHelper parser = null;
Object p = project.getReference(ProjectHelper.PROJECTHELPER_REFERENCE);
if (p instanceof ProjectHelper) {
parser = (ProjectHelper) p;
if (!parser.canParseAntlibDescriptor(antlibResource)) {
parser = null;
}
}
if (parser == null) {
ProjectHelperRepository helperRepository = ProjectHelperRepository.getInstance();
parser = helperRepository.getProjectHelperForAntlib(antlibResource);
}
UnknownElement ue = parser.parseAntlibDescriptor(project, antlibResource);
// Check name is "antlib"
if (!TAG.equals(ue.getTag())) {
throw new BuildException("Unexpected tag " + ue.getTag() + " expecting " + TAG, ue.getLocation());
}
Antlib antlib = new Antlib();
antlib.setProject(project);
antlib.setLocation(ue.getLocation());
antlib.setTaskName("antlib");
antlib.init();
ue.configure(antlib);
return antlib;
} finally {
helper.exitAntLib();
}
}
use of org.apache.tools.ant.UnknownElement in project ant by apache.
the class Antlib method execute.
/**
* Execute the nested tasks, setting the classloader for
* any tasks that derive from Definer.
*/
@Override
public void execute() {
for (Task task : tasks) {
UnknownElement ue = (UnknownElement) task;
setLocation(ue.getLocation());
ue.maybeConfigure();
Object configuredObject = ue.getRealThing();
if (configuredObject == null) {
continue;
}
if (!(configuredObject instanceof AntlibDefinition)) {
throw new BuildException("Invalid task in antlib %s %s does not extend %s", ue.getTag(), configuredObject.getClass(), AntlibDefinition.class.getName());
}
AntlibDefinition def = (AntlibDefinition) configuredObject;
def.setURI(uri);
def.setAntlibClassLoader(getClassLoader());
def.init();
def.execute();
}
}
Aggregations