Browse Source

DynamicConfiguratorNS

Initial code for dynamicConfiguratorNS.
Change from patch the qualified name is given and
not the prefix.
PR: 28426


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@276473 13f79535-47bb-0310-9956-ffa450edef68
master
Peter Reilly 21 years ago
parent
commit
476678d4a6
5 changed files with 86 additions and 32 deletions
  1. +55
    -6
      src/main/org/apache/tools/ant/IntrospectionHelper.java
  2. +3
    -0
      src/main/org/apache/tools/ant/ProjectHelper.java
  3. +2
    -7
      src/main/org/apache/tools/ant/RuntimeConfigurable.java
  4. +6
    -3
      src/main/org/apache/tools/ant/helper/ProjectHelper2.java
  5. +20
    -16
      src/main/org/apache/tools/ant/util/XMLFragment.java

+ 55
- 6
src/main/org/apache/tools/ant/IntrospectionHelper.java View File

@@ -486,13 +486,30 @@ public final class IntrospectionHelper implements BuildListener {
public void setAttribute(Project p, Object element, String attributeName,
String value) throws BuildException {
AttributeSetter as
= (AttributeSetter) attributeSetters.get(attributeName);
= (AttributeSetter) attributeSetters.get(
attributeName.toLowerCase(Locale.US));
if (as == null) {
if (element instanceof DynamicConfigurator) {
if (element instanceof DynamicConfiguratorNS) {
DynamicConfiguratorNS dc = (DynamicConfiguratorNS) element;
String uriPlusPrefix =
ProjectHelper.extractUriFromComponentName(attributeName);
String uri =
ProjectHelper.extractUriFromComponentName(uriPlusPrefix);
String localName =
ProjectHelper.extractNameFromComponentName(attributeName);
String qName = ("".equals(uri)
? localName : (uri + ":" + localName));

dc.setDynamicAttribute(uri, localName, qName, value);
return;
} else if (element instanceof DynamicConfigurator) {
DynamicConfigurator dc = (DynamicConfigurator) element;
dc.setDynamicAttribute(attributeName, value);
dc.setDynamicAttribute(attributeName.toLowerCase(Locale.US), value);
return;
} else {
if (attributeName.indexOf(':') != -1) {
return; // Ignore attribute from unknown uri's
}
String msg = getElementName(p, element)
+ " doesn't support the \"" + attributeName
+ "\" attribute.";
@@ -512,6 +529,7 @@ public final class IntrospectionHelper implements BuildListener {
throw new BuildException(t);
}
}

/**
* Adds PCDATA to an element, using the element's
@@ -574,7 +592,7 @@ public final class IntrospectionHelper implements BuildListener {

private NestedCreator getNestedCreator(
Project project, String parentUri, Object parent,
String elementName) throws BuildException {
String elementName, UnknownElement child) throws BuildException {

String uri = ProjectHelper.extractUriFromComponentName(elementName);
String name = ProjectHelper.extractNameFromComponentName(elementName);
@@ -593,6 +611,35 @@ public final class IntrospectionHelper implements BuildListener {
if (nc == null) {
nc = createAddTypeCreator(project, parent, elementName);
}
if (nc == null && parent instanceof DynamicConfiguratorNS) {
DynamicConfiguratorNS dc = (DynamicConfiguratorNS) parent;
String qName = (child == null ? name : child.getQName());
final Object nestedElement =
dc.createDynamicElement(
(child == null ? "" : child.getNamespace()),
name, qName);
if (nestedElement != null) {
nc = new NestedCreator() {
public boolean isPolyMorphic() {
return false;
}
public Class getElementClass() {
return null;
}

public Object getRealObject() {
return null;
}

public Object create(
Project project, Object parent, Object ignore) {
return nestedElement;
}
public void store(Object parent, Object child) {
}
};
}
}
if (nc == null && parent instanceof DynamicConfigurator) {
DynamicConfigurator dc = (DynamicConfigurator) parent;
final Object nestedElement =
@@ -649,7 +696,7 @@ public final class IntrospectionHelper implements BuildListener {
*/
public Object createElement(Project project, Object parent,
String elementName) throws BuildException {
NestedCreator nc = getNestedCreator(project, "", parent, elementName);
NestedCreator nc = getNestedCreator(project, "", parent, elementName, null);
try {
Object nestedElement = nc.create(project, parent, null);
if (project != null) {
@@ -688,7 +735,7 @@ public final class IntrospectionHelper implements BuildListener {
Project project, String parentUri, Object parent, String elementName,
UnknownElement ue) {
NestedCreator nc = getNestedCreator(
project, parentUri, parent, elementName);
project, parentUri, parent, elementName, ue);
return new Creator(project, parent, nc);
}

@@ -703,6 +750,7 @@ public final class IntrospectionHelper implements BuildListener {
public boolean supportsNestedElement(String elementName) {
return nestedCreators.containsKey(elementName.toLowerCase(Locale.US))
|| DynamicConfigurator.class.isAssignableFrom(bean)
|| DynamicConfiguratorNS.class.isAssignableFrom(bean)
|| addTypeMethods.size() != 0;
}

@@ -729,6 +777,7 @@ public final class IntrospectionHelper implements BuildListener {
nestedCreators.containsKey(name.toLowerCase(Locale.US))
&& (uri.equals(parentUri))) // || uri.equals("")))
|| DynamicConfigurator.class.isAssignableFrom(bean)
|| DynamicConfiguratorNS.class.isAssignableFrom(bean)
|| addTypeMethods.size() != 0;
}



+ 3
- 0
src/main/org/apache/tools/ant/ProjectHelper.java View File

@@ -56,6 +56,9 @@ public class ProjectHelper {
/** The URI for defined types/tasks - the format is antlib:<package> */
public static final String ANTLIB_URI = "antlib:";

/** Polymorphic attribute */
public static final String ANT_TYPE = "ant-type";

/**
* Name of JVM system property which provides the name of the
* ProjectHelper class to use.


+ 2
- 7
src/main/org/apache/tools/ant/RuntimeConfigurable.java View File

@@ -40,9 +40,6 @@ import org.xml.sax.helpers.AttributeListImpl;
*/
public class RuntimeConfigurable implements Serializable {

/** Polymorphic attribute (May be XML NS attribute later) */
private static final String ANT_TYPE = "ant-type";

/** Name of the element to configure. */
private String elementTag = null;

@@ -168,7 +165,7 @@ public class RuntimeConfigurable implements Serializable {
* @param value the attribute's value.
*/
public void setAttribute(String name, String value) {
if (name.equalsIgnoreCase(ANT_TYPE)) {
if (name.equalsIgnoreCase(ProjectHelper.ANT_TYPE)) {
this.polyType = value;
} else {
if (attributeNames == null) {
@@ -353,7 +350,6 @@ public class RuntimeConfigurable implements Serializable {
Object target = (wrappedObject instanceof TypeAdapter)
? ((TypeAdapter) wrappedObject).getProxy() : wrappedObject;

//PropertyHelper ph=PropertyHelper.getPropertyHelper(p);
IntrospectionHelper ih =
IntrospectionHelper.getHelper(p, target.getClass());

@@ -365,8 +361,7 @@ public class RuntimeConfigurable implements Serializable {
// reflect these into the target
value = p.replaceProperties(value);
try {
ih.setAttribute(p, target,
name.toLowerCase(Locale.US), value);
ih.setAttribute(p, target, name, value);
} catch (BuildException be) {
// id attribute must be set externally
if (!name.equals("id")) {


+ 6
- 3
src/main/org/apache/tools/ant/helper/ProjectHelper2.java View File

@@ -942,19 +942,22 @@ public class ProjectHelper2 extends ProjectHelper {
= new RuntimeConfigurable(task, task.getTaskName());

for (int i = 0; i < attrs.getLength(); i++) {
String name = attrs.getLocalName(i);
String attrUri = attrs.getURI(i);
if (attrUri != null
&& !attrUri.equals("")
&& !attrUri.equals(uri)) {
continue; // Ignore attributes from unknown uris
name = attrUri + ":" + attrs.getQName(i);
}
String name = attrs.getLocalName(i);
String value = attrs.getValue(i);
// PR: Hack for ant-type value
// an ant-type is a component name which can
// be namespaced, need to extract the name
// and convert from qualified name to uri/name
if (name.equals("ant-type")) {
if (ANT_TYPE.equals(name)
|| (ANT_CORE_URI.equals(attrUri)
&& ANT_TYPE.equals(attrs.getLocalName(i)))) {
name = ANT_TYPE;
int index = value.indexOf(":");
if (index != -1) {
String prefix = value.substring(0, index);


+ 20
- 16
src/main/org/apache/tools/ant/util/XMLFragment.java View File

@@ -24,7 +24,7 @@ import org.w3c.dom.Node;
import org.w3c.dom.Text;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DynamicConfigurator;
import org.apache.tools.ant.DynamicConfiguratorNS;
import org.apache.tools.ant.ProjectHelper;

/**
@@ -38,7 +38,7 @@ import org.apache.tools.ant.ProjectHelper;
*
* @since Ant 1.7
*/
public class XMLFragment implements DynamicConfigurator {
public class XMLFragment implements DynamicConfiguratorNS {

private Document doc;
private DocumentFragment fragment;
@@ -66,7 +66,7 @@ public class XMLFragment implements DynamicConfigurator {
/**
* No attributes for the wrapping element.
*/
public void setDynamicAttribute(String name, String value)
public void setDynamicAttribute(String uri, String name, String qName, String value)
throws BuildException {
throw new BuildException("Attribute " + name + " is not supported.");
}
@@ -74,10 +74,8 @@ public class XMLFragment implements DynamicConfigurator {
/**
* Creates a nested element.
*/
public Object createDynamicElement(String name) {
Element e = doc
.createElementNS(ProjectHelper.extractUriFromComponentName(name),
ProjectHelper.extractNameFromComponentName(name));
public Object createDynamicElement(String uri, String name, String qName) {
Element e = doc.createElementNS(uri, qName);
fragment.appendChild(e);
return new Child(e);
}
@@ -89,7 +87,7 @@ public class XMLFragment implements DynamicConfigurator {
}
}

public class Child implements DynamicConfigurator {
public class Child implements DynamicConfiguratorNS {
private Element e;

Child(Element e) {
@@ -106,19 +104,25 @@ public class XMLFragment implements DynamicConfigurator {
/**
* Sets the attribute
*/
public void setDynamicAttribute(String name, String value) {
e.setAttribute(name, value);
public void setDynamicAttribute(
String uri, String name, String qName, String value) {
if (uri.equals("")) {
e.setAttribute(name, value);
} else {
e.setAttributeNS(uri, qName, value);
}
}

/**
* Creates a nested element.
*/
public Object createDynamicElement(String name) {
Element e2 = doc
.createElementNS(ProjectHelper
.extractUriFromComponentName(name),
ProjectHelper
.extractNameFromComponentName(name));
public Object createDynamicElement(String uri, String name, String qName) {
Element e2 = null;
if (uri.equals("")) {
e2 = doc.createElement(name);
} else {
e2 = doc.createElementNS(uri, qName);
}
e.appendChild(e2);
return new Child(e2);
}


Loading…
Cancel
Save