📄 unknownelement.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */package org.apache.tools.ant;import java.util.ArrayList;import java.util.Enumeration;import java.util.Iterator;import java.util.List;import java.util.Map;import java.io.IOException;import org.apache.tools.ant.taskdefs.PreSetDef;/** * 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. * */public class UnknownElement extends Task { /** * 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 elementName; /** * Holds the namespace of the element. */ private String namespace = ""; /** * Holds the namespace qname of the element. */ private String qname; /** * The real object after it has been loaded. */ private Object realThing; /** * List of child elements (UnknownElements). */ private List/*<UnknownElement>*/ children = null; /** Specifies if a predefined definition has been done */ private boolean presetDefed = false; /** * Creates an UnknownElement for the given element name. * * @param elementName The name of the unknown element. * Must not be <code>null</code>. */ public UnknownElement (String elementName) { this.elementName = elementName; } /** * @return the list of nested UnknownElements for this UnknownElement. */ public List getChildren() { return children; } /** * Returns the name of the XML element which generated this unknown * element. * * @return the name of the XML element which generated this unknown * element. */ public String getTag() { return elementName; } /** Return the namespace of the XML element associated with this component. * * @return Namespace URI used in the xmlns declaration. */ public String getNamespace() { return 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. */ 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; } /** Return the qname of the XML element associated with this component. * * @return namespace Qname used in the element declaration. */ public String getQName() { return 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 */ public void setQName(String qname) { this.qname = qname; } /** * Get the RuntimeConfigurable instance for this UnknownElement, containing * the configuration information. * * @return the configuration info. */ public RuntimeConfigurable getWrapper() { return super.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. * * @exception BuildException if the configuration fails */ public void maybeConfigure() throws BuildException { if (realThing != null) { return; } configure(makeObject(this, getWrapper())); } /** * Configure the given object from this UnknownElement * * @param realObject the real object this UnknownElement is representing. * */ 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()); } /** * Handles output sent to System.out by this task or its real task. * * @param output The output to log. Should not be <code>null</code>. */ protected void handleOutput(String output) { if (realThing instanceof Task) { ((Task) realThing).handleOutput(output); } else { super.handleOutput(output); } } /** * 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 */ 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); } } /** * Handles output sent to System.out by this task or its real task. * * @param output The output to log. Should not be <code>null</code>. */ protected void handleFlush(String output) { if (realThing instanceof Task) { ((Task) realThing).handleFlush(output); } else { super.handleFlush(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 <code>null</code>. */ 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. * * @param output The error output to log. Should not be <code>null</code>. */ protected void handleErrorFlush(String output) { if (realThing instanceof Task) { ((Task) realThing).handleErrorOutput(output); } else { super.handleErrorOutput(output); } } /** * 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 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); } } } /** * Adds a child element to this element. * * @param child The child element to add. Must not be <code>null</code>. */ public void addChild(UnknownElement child) { if (children == null) { children = new ArrayList(); } children.add(child); } /** * 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 <code>null</code>. * * @param parentWrapper The wrapper containing child wrappers * to be configured. Must not be <code>null</code> * if there are any children. * * @exception BuildException if the children cannot be configured. */ 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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -