Wrapper class that holds all the information necessary to create a task
or data type that did not exist when Ant started, or one which
has had its definition updated to use a different implementation class.
| Method from org.apache.tools.ant.UnknownElement Detail: |
public void addChild(UnknownElement child) {
if (children == null) {
children = new ArrayList();
}
children.add(child);
}
Adds a child element to this element. |
public void applyPreSet(UnknownElement u) {
if (presetDefed) {
return;
}
// Do the runtime
getWrapper().applyPreSet(u.getWrapper());
if (u.children != null) {
List newChildren = new ArrayList();
newChildren.addAll(u.children);
if (children != null) {
newChildren.addAll(children);
}
children = newChildren;
}
presetDefed = true;
}
This is used then the realobject of the UE is a PreSetDefinition.
This is also used when a presetdef is used on a presetdef
The attributes, elements and text are applied to this
UE. |
public void configure(Object realObject) {
realThing = realObject;
getWrapper().setProxy(realThing);
Task task = null;
if (realThing instanceof Task) {
task = (Task) realThing;
task.setRuntimeConfigurableWrapper(getWrapper());
// For Script example that modifies id'ed tasks in other
// targets to work. *very* Ugly
// The reference is replaced by RuntimeConfigurable
if (getWrapper().getId() != null) {
this.getOwningTarget().replaceChild(this, (Task) realThing);
}
}
// configure attributes of the object and it's children. If it is
// a task container, defer the configuration till the task container
// attempts to use the task
if (task != null) {
task.maybeConfigure();
} else {
getWrapper().maybeConfigure(getProject());
}
handleChildren(realThing, getWrapper());
}
Configure the given object from this UnknownElement |
public UnknownElement copy(Project newProject) {
UnknownElement ret = new UnknownElement(getTag());
ret.setNamespace(getNamespace());
ret.setProject(newProject);
ret.setQName(getQName());
ret.setTaskType(getTaskType());
ret.setTaskName(getTaskName());
ret.setLocation(getLocation());
if (getOwningTarget() == null) {
Target t = new Target();
t.setProject(getProject());
ret.setOwningTarget(t);
} else {
ret.setOwningTarget(getOwningTarget());
}
RuntimeConfigurable copyRC = new RuntimeConfigurable(
ret, getTaskName());
copyRC.setPolyType(getWrapper().getPolyType());
Map m = getWrapper().getAttributeMap();
for (Iterator i = m.entrySet().iterator(); i.hasNext();) {
Map.Entry entry = (Map.Entry) i.next();
copyRC.setAttribute(
(String) entry.getKey(), (String) entry.getValue());
}
copyRC.addText(getWrapper().getText().toString());
for (Enumeration e = getWrapper().getChildren(); e.hasMoreElements();) {
RuntimeConfigurable r = (RuntimeConfigurable) e.nextElement();
UnknownElement ueChild = (UnknownElement) r.getProxy();
UnknownElement copyChild = ueChild.copy(newProject);
copyRC.addChild(copyChild.getWrapper());
ret.addChild(copyChild);
}
return ret;
}
Make a copy of the unknown element and set it in the new project. |
public void execute() {
if (realThing == null) {
// plain impossible to get here, maybeConfigure should
// have thrown an exception.
throw new BuildException("Could not create task of type: "
+ elementName, getLocation());
}
try {
if (realThing instanceof Task) {
((Task) realThing).execute();
}
} finally {
// Finished executing the task
// null it (unless it has an ID) to allow
// GC do its job
// If this UE is used again, a new "realthing" will be made
if (getWrapper().getId() == null) {
realThing = null;
getWrapper().setProxy(null);
}
}
}
Executes the real object if it's a task. If it's not a task
(e.g. a data type) then this method does nothing. |
public List getChildren() {
return children;
}
|
protected String getComponentName() {
return ProjectHelper.genComponentName(getNamespace(), getTag());
}
|
public String getNamespace() {
return namespace;
}
Return the namespace of the XML element associated with this component. |
protected BuildException getNotFoundException(String what,
String name) {
ComponentHelper helper = ComponentHelper.getComponentHelper(getProject());
String msg = helper.diagnoseCreationFailure(name, what);
return new BuildException(msg, getLocation());
}
Returns a very verbose exception for when a task/data type cannot
be found. |
public String getQName() {
return qname;
}
Return the qname of the XML element associated with this component. |
public Object getRealThing() {
return realThing;
}
Return the configured object |
public String getTag() {
return elementName;
}
Returns the name of the XML element which generated this unknown
element. |
public Task getTask() {
if (realThing instanceof Task) {
return (Task) realThing;
}
return null;
}
Returns the task instance after it has been created and if it is a task. |
public String getTaskName() {
//return elementName;
return realThing == null
|| !(realThing instanceof Task) ? super.getTaskName()
: ((Task) realThing).getTaskName();
}
Returns the name to use in logging messages. |
public RuntimeConfigurable getWrapper() {
return super.getWrapper();
}
Get the RuntimeConfigurable instance for this UnknownElement, containing
the configuration information. |
protected void handleChildren(Object parent,
RuntimeConfigurable parentWrapper) throws BuildException {
if (parent instanceof TypeAdapter) {
parent = ((TypeAdapter) parent).getProxy();
}
String parentUri = getNamespace();
Class parentClass = parent.getClass();
IntrospectionHelper ih = IntrospectionHelper.getHelper(getProject(), parentClass);
if (children != null) {
Iterator it = children.iterator();
for (int i = 0; it.hasNext(); i++) {
RuntimeConfigurable childWrapper = parentWrapper.getChild(i);
UnknownElement child = (UnknownElement) it.next();
try {
if (!handleChild(
parentUri, ih, parent, child, childWrapper)) {
if (!(parent instanceof TaskContainer)) {
ih.throwNotSupported(getProject(), parent,
child.getTag());
} else {
// a task container - anything could happen - just add the
// child to the container
TaskContainer container = (TaskContainer) parent;
container.addTask(child);
}
}
} catch (UnsupportedElementException ex) {
throw new BuildException(
parentWrapper.getElementTag()
+ " doesn't support the nested \"" + ex.getElement()
+ "\" element.", ex);
}
}
}
}
Creates child elements, creates children of the children
(recursively), and sets attributes of the child elements. |
protected void handleErrorFlush(String output) {
if (realThing instanceof Task) {
((Task) realThing).handleErrorOutput(output);
} else {
super.handleErrorOutput(output);
}
}
Handles error output sent to System.err by this task or its real task. |
protected void handleErrorOutput(String output) {
if (realThing instanceof Task) {
((Task) realThing).handleErrorOutput(output);
} else {
super.handleErrorOutput(output);
}
}
Handles error output sent to System.err by this task or its real task. |
protected void handleFlush(String output) {
if (realThing instanceof Task) {
((Task) realThing).handleFlush(output);
} else {
super.handleFlush(output);
}
}
Handles output sent to System.out by this task or its real task. |
protected int handleInput(byte[] buffer,
int offset,
int length) throws IOException {
if (realThing instanceof Task) {
return ((Task) realThing).handleInput(buffer, offset, length);
} else {
return super.handleInput(buffer, offset, length);
}
}
Delegate to realThing if present and if it as task. |
protected void handleOutput(String output) {
if (realThing instanceof Task) {
((Task) realThing).handleOutput(output);
} else {
super.handleOutput(output);
}
}
Handles output sent to System.out by this task or its real task. |
protected Object makeObject(UnknownElement ue,
RuntimeConfigurable w) {
ComponentHelper helper = ComponentHelper.getComponentHelper(
getProject());
String name = ue.getComponentName();
Object o = helper.createComponent(ue, ue.getNamespace(), name);
if (o == null) {
throw getNotFoundException("task or type", name);
}
if (o instanceof PreSetDef.PreSetDefinition) {
PreSetDef.PreSetDefinition def = (PreSetDef.PreSetDefinition) o;
o = def.createObject(ue.getProject());
if (o == null) {
throw getNotFoundException(
"preset " + name,
def.getPreSets().getComponentName());
}
ue.applyPreSet(def.getPreSets());
if (o instanceof Task) {
Task task = (Task) o;
task.setTaskType(ue.getTaskType());
task.setTaskName(ue.getTaskName());
task.init();
}
}
if (o instanceof UnknownElement) {
o = ((UnknownElement) o).makeObject((UnknownElement) o, w);
}
if (o instanceof Task) {
((Task) o).setOwningTarget(getOwningTarget());
}
if (o instanceof ProjectComponent) {
((ProjectComponent) o).setLocation(getLocation());
}
return o;
}
Creates a named task or data type. If the real object is a task,
it is configured up to the init() stage. |
protected Task makeTask(UnknownElement ue,
RuntimeConfigurable w) {
Task task = getProject().createTask(ue.getTag());
if (task != null) {
task.setLocation(getLocation());
// UnknownElement always has an associated target
task.setOwningTarget(getOwningTarget());
task.init();
}
return task;
}
Creates a named task and configures it up to the init() stage. |
public void maybeConfigure() throws BuildException {
if (realThing != null) {
return;
}
configure(makeObject(this, getWrapper()));
}
Creates the real object instance and child elements, then configures
the attributes and text of the real object. This unknown element
is then replaced with the real object in the containing target's list
of children. |
public void setNamespace(String namespace) {
if (namespace.equals(ProjectHelper.ANT_CURRENT_URI)) {
ComponentHelper helper = ComponentHelper.getComponentHelper(
getProject());
namespace = helper.getCurrentAntlibUri();
}
this.namespace = namespace == null ? "" : namespace;
}
Set the namespace of the XML element associated with this component.
This method is typically called by the XML processor.
If the namespace is "ant:current", the component helper
is used to get the current antlib uri. |
public void setQName(String qname) {
this.qname = qname;
}
Set the namespace qname of the XML element.
This method is typically called by the XML processor. |
public void setRealThing(Object realThing) {
this.realThing = realThing;
}
Set the configured object |
public boolean similar(Object obj) {
if (obj == null) {
return false;
}
if (!getClass().getName().equals(obj.getClass().getName())) {
return false;
}
UnknownElement other = (UnknownElement) obj;
// Are the names the same ?
if (!equalsString(elementName, other.elementName)) {
return false;
}
if (!namespace.equals(other.namespace)) {
return false;
}
if (!qname.equals(other.qname)) {
return false;
}
// Are attributes the same ?
if (!getWrapper().getAttributeMap().equals(
other.getWrapper().getAttributeMap())) {
return false;
}
// Is the text the same?
// Need to use equals on the string and not
// on the stringbuffer as equals on the string buffer
// does not compare the contents.
if (!getWrapper().getText().toString().equals(
other.getWrapper().getText().toString())) {
return false;
}
// Are the sub elements the same ?
if (children == null || children.size() == 0) {
return other.children == null || other.children.size() == 0;
}
if (other.children == null) {
return false;
}
if (children.size() != other.children.size()) {
return false;
}
for (int i = 0; i < children.size(); ++i) {
UnknownElement child = (UnknownElement) children.get(i);
if (!child.similar(other.children.get(i))) {
return false;
}
}
return true;
}
like contents equals, but ignores project |