use of org.apache.tools.ant.taskdefs.MacroDef.Attribute 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.taskdefs.MacroDef.Attribute in project ant by apache.
the class RuntimeConfigurable method maybeConfigure.
/**
* Configures the wrapped element. The attributes and text for
* the wrapped element are configured. Each time the wrapper is
* configured, the attributes and text for it are reset.
* <p>
* If the element has an <code>id</code> attribute, a reference
* is added to the project as well.
* </p>
*
* @param p The project containing the wrapped element.
* Must not be <code>null</code>.
*
* @param configureChildren ignored.
*
* @exception BuildException if the configuration fails, for instance due
* to invalid attributes, or text being added to
* an element which doesn't accept it.
*/
public synchronized void maybeConfigure(Project p, boolean configureChildren) throws BuildException {
if (proxyConfigured) {
return;
}
// Configure the object
Object target = (wrappedObject instanceof TypeAdapter) ? ((TypeAdapter) wrappedObject).getProxy() : wrappedObject;
IntrospectionHelper ih = IntrospectionHelper.getHelper(p, target.getClass());
ComponentHelper componentHelper = ComponentHelper.getComponentHelper(p);
if (attributeMap != null) {
for (Map.Entry<String, Object> entry : attributeMap.entrySet()) {
String name = entry.getKey();
// skip restricted attributes such as if:set
AttributeComponentInformation attributeComponentInformation = isRestrictedAttribute(name, componentHelper);
if (attributeComponentInformation.isRestricted()) {
continue;
}
Object value = entry.getValue();
// reflect these into the target, defer for
// MacroInstance where properties are expanded for the
// nested sequential
Object attrValue;
if (value instanceof Evaluable<?>) {
attrValue = ((Evaluable<?>) value).eval();
} else {
attrValue = PropertyHelper.getPropertyHelper(p).parseProperties(value.toString());
}
if (target instanceof MacroInstance) {
for (Attribute attr : ((MacroInstance) target).getMacroDef().getAttributes()) {
if (attr.getName().equals(name)) {
if (!attr.isDoubleExpanding()) {
attrValue = value;
}
break;
}
}
}
try {
ih.setAttribute(p, target, name, attrValue);
} catch (UnsupportedAttributeException be) {
// id attribute must be set externally
if ("id".equals(name)) {
// Do nothing
} else if (getElementTag() == null) {
throw be;
} else {
throw new BuildException(getElementTag() + " doesn't support the \"" + be.getAttribute() + "\" attribute", be);
}
} catch (BuildException be) {
if ("id".equals(name)) {
// Assume that this is an not supported attribute type
// thrown for example by a dynamic attribute task
// Do nothing
} else {
throw be;
}
}
}
}
if (characters != null) {
ProjectHelper.addText(p, wrappedObject, characters.substring(0));
}
if (id != null) {
p.addReference(id, wrappedObject);
}
proxyConfigured = true;
}
Aggregations