FileDocCategorySizeDatePackage
UnknownElement.javaAPI DocApache Ant 1.7022845Wed Dec 13 06:16:20 GMT 2006org.apache.tools.ant

UnknownElement

public class UnknownElement extends Task
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.

Fields Summary
private String
elementName
Holds the name of the task/type or nested child element of a task/type that hasn't been defined at parser time or has been redefined since original creation.
private String
namespace
Holds the namespace of the element.
private String
qname
Holds the namespace qname of the element.
private Object
realThing
The real object after it has been loaded.
private List
children
List of child elements (UnknownElements).
private boolean
presetDefed
Specifies if a predefined definition has been done
Constructors Summary
public UnknownElement(String elementName)
Creates an UnknownElement for the given element name.

param
elementName The name of the unknown element. Must not be null.


                                                
        
        this.elementName = elementName;
    
Methods Summary
public voidaddChild(org.apache.tools.ant.UnknownElement child)
Adds a child element to this element.

param
child The child element to add. Must not be null.

        if (children == null) {
            children = new ArrayList();
        }
        children.add(child);
    
public voidapplyPreSet(org.apache.tools.ant.UnknownElement u)
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.

param
u an UnknownElement containing the attributes, elements and text

        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;
    
public voidconfigure(java.lang.Object realObject)
Configure the given object from this UnknownElement

param
realObject the real object this UnknownElement is representing.

        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());
    
public org.apache.tools.ant.UnknownElementcopy(Project newProject)
Make a copy of the unknown element and set it in the new project.

param
newProject the project to create the UE in.
return
the copied UE.

        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;
    
private static booleanequalsString(java.lang.String a, java.lang.String b)

        return (a == null) ? (b == null) : a.equals(b);
    
public voidexecute()
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.

        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());
        }

        if (realThing instanceof Task) {
            ((Task) realThing).execute();
        }

        // Finished executing the task, null it to allow
        // GC do its job
        // If this UE is used again, a new "realthing" will be made
        realThing = null;
        getWrapper().setProxy(null);

    
public java.util.ListgetChildren()

return
the list of nested UnknownElements for this UnknownElement.

        return children;
    
protected java.lang.StringgetComponentName()

return
the component name - uses ProjectHelper#genComponentName()

        return ProjectHelper.genComponentName(getNamespace(), getTag());
    
public java.lang.StringgetNamespace()
Return the namespace of the XML element associated with this component.

return
Namespace URI used in the xmlns declaration.

        return namespace;
    
protected BuildExceptiongetNotFoundException(java.lang.String what, java.lang.String name)
Returns a very verbose exception for when a task/data type cannot be found.

param
what The kind of thing being created. For example, when a task name could not be found, this would be "task". Should not be null.
param
name The name of the element which could not be found. Should not be null.
return
a detailed description of what might have caused the problem.

        ComponentHelper helper = ComponentHelper.getComponentHelper(getProject());
        String msg = helper.diagnoseCreationFailure(name, what);
        return new BuildException(msg, getLocation());
    
public java.lang.StringgetQName()
Return the qname of the XML element associated with this component.

return
namespace Qname used in the element declaration.

        return qname;
    
public java.lang.ObjectgetRealThing()
Return the configured object

return
the real thing whatever it is
since
ant 1.6

        return realThing;
    
public java.lang.StringgetTag()
Returns the name of the XML element which generated this unknown element.

return
the name of the XML element which generated this unknown element.

        return elementName;
    
public TaskgetTask()
Returns the task instance after it has been created and if it is a task.

return
a task instance or null if the real object is not a task.

        if (realThing instanceof Task) {
            return (Task) realThing;
        }
        return null;
    
public java.lang.StringgetTaskName()
Returns the name to use in logging messages.

return
the name to use in logging messages.

        //return elementName;
        return realThing == null
            || !(realThing instanceof Task) ? super.getTaskName()
                                            : ((Task) realThing).getTaskName();
    
public RuntimeConfigurablegetWrapper()
Get the RuntimeConfigurable instance for this UnknownElement, containing the configuration information.

return
the configuration info.

        return super.getWrapper();
    
private booleanhandleChild(java.lang.String parentUri, IntrospectionHelper ih, java.lang.Object parent, org.apache.tools.ant.UnknownElement child, RuntimeConfigurable childWrapper)
Try to create a nested element of parent for the given tag.

return
whether the creation has been successful

        String childName = ProjectHelper.genComponentName(
            child.getNamespace(), child.getTag());
        if (ih.supportsNestedElement(parentUri, childName)) {
            IntrospectionHelper.Creator creator =
                ih.getElementCreator(
                    getProject(), parentUri, parent, childName, child);
            creator.setPolyType(childWrapper.getPolyType());
            Object realChild = creator.create();
            if (realChild instanceof PreSetDef.PreSetDefinition) {
                PreSetDef.PreSetDefinition def =
                    (PreSetDef.PreSetDefinition) realChild;
                realChild = creator.getRealObject();
                child.applyPreSet(def.getPreSets());
            }
            childWrapper.setCreator(creator);
            childWrapper.setProxy(realChild);
            if (realChild instanceof Task) {
                Task childTask = (Task) realChild;
                childTask.setRuntimeConfigurableWrapper(childWrapper);
                childTask.setTaskName(childName);
                childTask.setTaskType(childName);
            }
            if (realChild instanceof ProjectComponent) {
                ((ProjectComponent) realChild).setLocation(child.getLocation());
            }
            childWrapper.maybeConfigure(getProject());
            child.handleChildren(realChild, childWrapper);
            creator.store();
            return true;
        }
        return false;
    
protected voidhandleChildren(java.lang.Object parent, RuntimeConfigurable parentWrapper)
Creates child elements, creates children of the children (recursively), and sets attributes of the child elements.

param
parent The configured object for the parent. Must not be null.
param
parentWrapper The wrapper containing child wrappers to be configured. Must not be null if there are any children.
exception
BuildException if the children cannot be configured.

        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);
                }
            }
        }
    
protected voidhandleErrorFlush(java.lang.String output)
Handles error output sent to System.err by this task or its real task.

param
output The error output to log. Should not be null.

        if (realThing instanceof Task) {
            ((Task) realThing).handleErrorOutput(output);
        } else {
            super.handleErrorOutput(output);
        }
    
protected voidhandleErrorOutput(java.lang.String output)
Handles error output sent to System.err by this task or its real task.

param
output The error output to log. Should not be null.

        if (realThing instanceof Task) {
            ((Task) realThing).handleErrorOutput(output);
        } else {
            super.handleErrorOutput(output);
        }
    
protected voidhandleFlush(java.lang.String output)
Handles output sent to System.out by this task or its real task.

param
output The output to log. Should not be null.

        if (realThing instanceof Task) {
            ((Task) realThing).handleFlush(output);
        } else {
            super.handleFlush(output);
        }
    
protected inthandleInput(byte[] buffer, int offset, int length)
Delegate to realThing if present and if it as task.

see
Task#handleInput(byte[], int, int)
param
buffer the buffer into which data is to be read.
param
offset the offset into the buffer at which data is stored.
param
length the amount of data to read.
return
the number of bytes read.
exception
IOException if the data cannot be read.
since
Ant 1.6

        if (realThing instanceof Task) {
            return ((Task) realThing).handleInput(buffer, offset, length);
        } else {
            return super.handleInput(buffer, offset, length);
        }

    
protected voidhandleOutput(java.lang.String output)
Handles output sent to System.out by this task or its real task.

param
output The output to log. Should not be null.

        if (realThing instanceof Task) {
            ((Task) realThing).handleOutput(output);
        } else {
            super.handleOutput(output);
        }
    
protected java.lang.ObjectmakeObject(org.apache.tools.ant.UnknownElement ue, RuntimeConfigurable w)
Creates a named task or data type. If the real object is a task, it is configured up to the init() stage.

param
ue The unknown element to create the real object for. Must not be null.
param
w Ignored in this implementation.
return
the task or data type represented by the given unknown element.

        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;
    
protected TaskmakeTask(org.apache.tools.ant.UnknownElement ue, RuntimeConfigurable w)
Creates a named task and configures it up to the init() stage.

param
ue The UnknownElement to create the real task for. Must not be null.
param
w Ignored.
return
the task specified by the given unknown element, or null if the task name is not recognised.

        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;
    
public voidmaybeConfigure()
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.

exception
BuildException if the configuration fails

        if (realThing != null) {
            return;
        }
        configure(makeObject(this, getWrapper()));
    
public voidsetNamespace(java.lang.String 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.

param
namespace URI used in the xmlns declaration.

        if (namespace.equals(ProjectHelper.ANT_CURRENT_URI)) {
            ComponentHelper helper = ComponentHelper.getComponentHelper(
                getProject());
            namespace = helper.getCurrentAntlibUri();
        }
        this.namespace = namespace == null ? "" : namespace;
    
public voidsetQName(java.lang.String qname)
Set the namespace qname of the XML element. This method is typically called by the XML processor.

param
qname the qualified name of the element

        this.qname = qname;
    
public voidsetRealThing(java.lang.Object realThing)
Set the configured object

param
realThing the configured object
since
ant 1.7

        this.realThing = realThing;
    
public booleansimilar(java.lang.Object obj)
like contents equals, but ignores project

param
obj the object to check against
return
true if this unknownelement has the same contents the other

        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;