Browse Source

adding if and unless namespaces allowing to put conditions on all tasks and nested elements. Code written by Peter Reilly. Bugzilla PR 43362.

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@1479422 13f79535-47bb-0310-9956-ffa450edef68
master
Antoine Levy-Lambert 12 years ago
parent
commit
46c940785a
20 changed files with 634 additions and 7 deletions
  1. +3
    -0
      WHATSNEW
  2. +4
    -0
      manual/Types/namespace.html
  3. +1
    -0
      manual/conceptstypeslist.html
  4. +64
    -0
      manual/ifunless.html
  5. +3
    -3
      src/etc/checkstyle/checkstyle-config
  6. +1
    -0
      src/main/org/apache/tools/ant/ComponentHelper.java
  7. +75
    -0
      src/main/org/apache/tools/ant/DefaultDefinitions.java
  8. +4
    -0
      src/main/org/apache/tools/ant/MagicNames.java
  9. +15
    -0
      src/main/org/apache/tools/ant/ProjectHelper.java
  10. +75
    -0
      src/main/org/apache/tools/ant/RuntimeConfigurable.java
  11. +16
    -4
      src/main/org/apache/tools/ant/UnknownElement.java
  12. +26
    -0
      src/main/org/apache/tools/ant/attribute/AttributeNamespace.java
  13. +85
    -0
      src/main/org/apache/tools/ant/attribute/BaseIfAttribute.java
  14. +34
    -0
      src/main/org/apache/tools/ant/attribute/EnableAttribute.java
  15. +38
    -0
      src/main/org/apache/tools/ant/attribute/IfBlankAttribute.java
  16. +38
    -0
      src/main/org/apache/tools/ant/attribute/IfSetAttribute.java
  17. +40
    -0
      src/main/org/apache/tools/ant/attribute/IfTrueAttribute.java
  18. +51
    -0
      src/main/org/apache/tools/ant/taskdefs/AttributeNamespaceDef.java
  19. +1
    -0
      src/main/org/apache/tools/ant/taskdefs/defaults.properties
  20. +60
    -0
      src/tests/antunit/core/ant-attribute-test.xml

+ 3
- 0
WHATSNEW View File

@@ -29,6 +29,9 @@ Other changes:


* simplifying Execute.getEnvironmentVariables since we are only running on Java 1.5 or higher now * simplifying Execute.getEnvironmentVariables since we are only running on Java 1.5 or higher now


* Add conditional attributes
Bugzilla report 43362

Changes from Ant 1.8.4 TO Ant 1.9.0 Changes from Ant 1.8.4 TO Ant 1.9.0
=================================== ===================================




+ 4
- 0
manual/Types/namespace.html View File

@@ -151,6 +151,10 @@
<li> they are in the same namespace as the element they belong to <li> they are in the same namespace as the element they belong to
</li> </li>
</ul> </ul>
<p>
In Ant 1.9.1 two attribute namespaces ant:if and ant:unless are added in order to allow to insert elements
conditionally
</p>
<p> <p>
Other attributes are simply ignored. Other attributes are simply ignored.
</p> </p>


+ 1
- 0
manual/conceptstypeslist.html View File

@@ -35,6 +35,7 @@
<li><a href="sysclasspath.html">build.sysclasspath</a></li> <li><a href="sysclasspath.html">build.sysclasspath</a></li>
<li><a href="javacprops.html">Ant properties controlling javac</a></li> <li><a href="javacprops.html">Ant properties controlling javac</a></li>
<li><a href="Tasks/common.html">Common Attributes</a></li> <li><a href="Tasks/common.html">Common Attributes</a></li>
<li><a href="ifunless.html">If and Unless Attributes</a></li>
</ul> </ul>


<h3>List of Types</h3> <h3>List of Types</h3>


+ 64
- 0
manual/ifunless.html View File

@@ -0,0 +1,64 @@
<!--
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.
-->
<html>

<head>
<meta http-equiv="Content-Language" content="en-us"/>
<link rel="stylesheet" type="text/css" href="stylesheets/style.css"/>
<title>If and Unless on all tasks/nested elements</title>
</head>

<body>
<h1><a name="if_and_unless">If And Unless</a></h1>

<p>Since Ant 1.9.1 it is possible to add if and unless attributes on all tasks and nested elements using special namespaces.</p>

<p>In order to use this feature you need to add the following namespace declarations</p>
<blockquote><pre>
xmlns:if=&quot;ant:if&quot;
xmlns:unless=&quot;ant:unless&quot;
</pre>
</blockquote>

<p>The if and unless namespaces support the following 3 conditions :
<ul>
<li>true</li>true if the value of the property evaluates to true
<li>blank</li>true if the value of the property is null or empty
<li>set</li>true if the property is set
</ul></p>

<blockquote>
<pre>
&lt;project name=&quot;tryit&quot;
xmlns:if=&quot;ant:if&quot;
xmlns:unless=&quot;ant:unless&quot;
&gt;
&lt;exec executable=&quot;java&quot;&gt;
&lt;arg line=&quot;-X&quot; if:true=&quot;showextendedparams&quot;/&gt;
&lt;arg line=&quot;-version&quot; unless:true=&quot;showextendedparams&quot;/&gt;
&lt;/exec&gt;
&lt;condition property=&quot;onmac&quot;&gt;
&lt;os family=&quot;mac&quot;/&gt;
&lt;/condition&gt;
&lt;echo if:set=&quot;onmac&quot;&gt;running on MacOS&lt;/echo&gt;
&lt;echo unless:set=&quot;onmac&quot;&gt;not running on MacOS&lt;/echo&gt;
&lt;/project&gt;
</pre>
</blockquote>

</body>
</html>

+ 3
- 3
src/etc/checkstyle/checkstyle-config View File

@@ -96,8 +96,8 @@
<module name="IllegalInstantiation"> <module name="IllegalInstantiation">
<property name="classes" value="java.lang.Boolean"/> <property name="classes" value="java.lang.Boolean"/>
</module> </module>
<module name="InnerAssignment"/>
<module name="MagicNumber"/>
<!-- <module name="InnerAssignment"/> -->
<!-- <module name="MagicNumber"/> -->
<module name="MissingSwitchDefault"/> <module name="MissingSwitchDefault"/>
<!-- Allow redundant throw declarations for doc purposes <!-- Allow redundant throw declarations for doc purposes
<module name="RedundantThrows"> <module name="RedundantThrows">
@@ -120,7 +120,7 @@
<property name="format" value="\s+$"/> <property name="format" value="\s+$"/>
<property name="message" value="Line has trailing spaces."/> <property name="message" value="Line has trailing spaces."/>
</module> </module>
<module name="TodoComment"/>
<!-- <module name="TodoComment"/> -->
<module name="UpperEll"/> <module name="UpperEll"/>
<!-- allow comment suppression of checks --> <!-- allow comment suppression of checks -->
<module name="FileContentsHolder"/> <module name="FileContentsHolder"/>


+ 1
- 0
src/main/org/apache/tools/ant/ComponentHelper.java View File

@@ -316,6 +316,7 @@ public class ComponentHelper {
public void initDefaultDefinitions() { public void initDefaultDefinitions() {
initTasks(); initTasks();
initTypes(); initTypes();
new DefaultDefinitions(this).execute();
} }


/** /**


+ 75
- 0
src/main/org/apache/tools/ant/DefaultDefinitions.java View File

@@ -0,0 +1,75 @@
/*
* 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;

/**
* Default definitions.
*/
public final class DefaultDefinitions {
private static final String IF_NAMESPACE = "ant:if";
private static final String UNLESS_NAMESPACE = "ant:unless";
private static final String OATA = "org.apache.tools.ant.";

private final ComponentHelper componentHelper;

/**
* Create a default definitions object.
* @param componentHelper the componenthelper to initialize.
*/
public DefaultDefinitions(ComponentHelper componentHelper) {
this.componentHelper = componentHelper;
}

/**
* Register the defintions.
*/
public void execute() {
attributeNamespaceDef(IF_NAMESPACE);
attributeNamespaceDef(UNLESS_NAMESPACE);

ifUnlessDef("true", "IfTrueAttribute");
ifUnlessDef("set", "IfSetAttribute");
ifUnlessDef("blank", "IfBlankAttribute");
}

private void attributeNamespaceDef(String ns) {
AntTypeDefinition def = new AntTypeDefinition();
def.setName(ProjectHelper.nsToComponentName(ns));
def.setClassName(OATA + "attribute.AttributeNamespace");
def.setClassLoader(getClass().getClassLoader());
def.setRestrict(true);
componentHelper.addDataTypeDefinition(def);
}

private void ifUnlessDef(String name, String base) {
String classname = OATA + "attribute." + base;
componentDef(IF_NAMESPACE, name, classname);
componentDef(UNLESS_NAMESPACE, name, classname + "$Unless");
}

private void componentDef(String ns, String name, String classname) {
AntTypeDefinition def = new AntTypeDefinition();
String n = ProjectHelper.genComponentName(ns, name);
def.setName(ProjectHelper.genComponentName(ns, name));
def.setClassName(classname);
def.setClassLoader(getClass().getClassLoader());
def.setRestrict(true);
componentHelper.addDataTypeDefinition(def);
}
}

+ 4
- 0
src/main/org/apache/tools/ant/MagicNames.java View File

@@ -276,5 +276,9 @@ public final class MagicNames {
* @since Ant 1.9.0 * @since Ant 1.9.0
*/ */
public static final String ANT_VM_LAUNCHER_REF_ID = "ant.vmLauncher"; public static final String ANT_VM_LAUNCHER_REF_ID = "ant.vmLauncher";
/** Name of the namespace "type" (note: cannot be used as an element)
* @since Ant 1.9.1
* */
public static final String ATTRIBUTE_NAMESPACE = "attribute namespace";
} }



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

@@ -43,6 +43,11 @@ public class ProjectHelper {
/** The URI for antlib current definitions */ /** The URI for antlib current definitions */
public static final String ANT_CURRENT_URI = "ant:current"; public static final String ANT_CURRENT_URI = "ant:current";


/** The URI for ant specific attributes
* @since Ant 1.9.1
* */
public static final String ANT_ATTRIBUTE_URI = "ant:attribute";

/** The URI for defined types/tasks - the format is antlib:<package> */ /** The URI for defined types/tasks - the format is antlib:<package> */
public static final String ANTLIB_URI = "antlib:"; public static final String ANTLIB_URI = "antlib:";


@@ -522,6 +527,16 @@ public class ProjectHelper {
return componentName.substring(index + 1); return componentName.substring(index + 1);
} }


/**
* Convert an attribute namespace to a "component name".
* @param ns the xml namespace uri.
* @return the converted value.
* @since Ant 1.9.1
*/
public static String nsToComponentName(String ns) {
return "attribute namespace:" + ns;
}

/** /**
* Add location to build exception. * Add location to build exception.
* @param ex the build exception, if the build exception * @param ex the build exception, if the build exception


+ 75
- 0
src/main/org/apache/tools/ant/RuntimeConfigurable.java View File

@@ -27,6 +27,8 @@ import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map.Entry; import java.util.Map.Entry;


import org.apache.tools.ant.attribute.EnableAttribute;

import org.apache.tools.ant.util.CollectionUtils; import org.apache.tools.ant.util.CollectionUtils;
import org.apache.tools.ant.taskdefs.MacroDef.Attribute; import org.apache.tools.ant.taskdefs.MacroDef.Attribute;
import org.apache.tools.ant.taskdefs.MacroInstance; import org.apache.tools.ant.taskdefs.MacroInstance;
@@ -64,6 +66,9 @@ public class RuntimeConfigurable implements Serializable {
*/ */
private transient AttributeList attributes; private transient AttributeList attributes;


// The following is set to true if any of the attributes are namespaced
private transient boolean namespacedAttribute = false;

/** Attribute names and values. While the XML spec doesn't require /** Attribute names and values. While the XML spec doesn't require
* preserving the order ( AFAIK ), some ant tests do rely on the * preserving the order ( AFAIK ), some ant tests do rely on the
* exact order. * exact order.
@@ -113,6 +118,73 @@ public class RuntimeConfigurable implements Serializable {
proxyConfigured = false; proxyConfigured = false;
} }


private static class EnableAttributeConsumer {
public void add(EnableAttribute b) {
// Ignore
}
}

/**
* Check if an UE is enabled.
* This looks tru the attributes and checks if there
* are any Ant attributes, and if so, the method calls the
* isEnabled() method on them.
* @param owner the UE that owns this RC.
* @return true if enabled, false if any of the ant attribures return
* false.
*/
public boolean isEnabled(UnknownElement owner) {
if (!namespacedAttribute) {
return true;
}
ComponentHelper componentHelper = ComponentHelper
.getComponentHelper(owner.getProject());

IntrospectionHelper ih
= IntrospectionHelper.getHelper(
owner.getProject(), EnableAttributeConsumer.class);
for (int i = 0; i < attributeMap.keySet().size(); ++i) {
String name = (String) attributeMap.keySet().toArray()[i];
if (name.indexOf(':') == -1) {
continue;
}
String componentName = attrToComponent(name);
String ns = ProjectHelper.extractUriFromComponentName(componentName);
if (componentHelper.getRestrictedDefinitions(
ProjectHelper.nsToComponentName(ns)) == null) {
continue;
}

String value = (String) attributeMap.get(name);

EnableAttribute enable = null;
try {
enable = (EnableAttribute)
ih.createElement(
owner.getProject(), new EnableAttributeConsumer(),
componentName);
} catch (BuildException ex) {
throw new BuildException(
"Unsupported attribute " + componentName);
}
if (enable == null) {
continue;
}
value = owner.getProject().replaceProperties(value); // FixMe: need to make config
if (!enable.isEnabled(owner, value)) {
return false;
}
}
return true;
}

private String attrToComponent(String a) {
// need to remove the prefix
int p1 = a.lastIndexOf(':');
int p2 = a.lastIndexOf(':', p1 - 1);
return a.substring(0, p2) + a.substring(p1);
}

/** /**
* Sets the creator of the element to be configured * Sets the creator of the element to be configured
* used to store the element in the parent. * used to store the element in the parent.
@@ -177,6 +249,9 @@ public class RuntimeConfigurable implements Serializable {
* @param value the attribute's value. * @param value the attribute's value.
*/ */
public synchronized void setAttribute(String name, String value) { public synchronized void setAttribute(String name, String value) {
if (name.indexOf(':') != -1) {
namespacedAttribute = true;
}
setAttribute(name, (Object) value); setAttribute(name, (Object) value);
} }




+ 16
- 4
src/main/org/apache/tools/ant/UnknownElement.java View File

@@ -170,6 +170,9 @@ public class UnknownElement extends Task {
* *
*/ */
public void configure(Object realObject) { public void configure(Object realObject) {
if (realObject == null) {
return;
}
realThing = realObject; realThing = realObject;


getWrapper().setProxy(realThing); getWrapper().setProxy(realThing);
@@ -281,10 +284,8 @@ public class UnknownElement extends Task {
*/ */
public void execute() { public void execute() {
if (realThing == null) { 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());
// Got here if the runtimeconfigurable is not enabled.
return;
} }
try { try {
if (realThing instanceof Task) { if (realThing instanceof Task) {
@@ -346,6 +347,14 @@ public class UnknownElement extends Task {
RuntimeConfigurable childWrapper = parentWrapper.getChild(i); RuntimeConfigurable childWrapper = parentWrapper.getChild(i);
UnknownElement child = it.next(); UnknownElement child = it.next();
try { try {
if (!childWrapper.isEnabled(child)) {
if (ih.supportsNestedElement(
parentUri, ProjectHelper.genComponentName(
child.getNamespace(), child.getTag()))) {
continue;
}
// fall tru and fail in handlechild (unsupported element)
}
if (!handleChild( if (!handleChild(
parentUri, ih, parent, child, childWrapper)) { parentUri, ih, parent, child, childWrapper)) {
if (!(parent instanceof TaskContainer)) { if (!(parent instanceof TaskContainer)) {
@@ -411,6 +420,9 @@ public class UnknownElement extends Task {
* @return the task or data type represented by the given unknown element. * @return the task or data type represented by the given unknown element.
*/ */
protected Object makeObject(UnknownElement ue, RuntimeConfigurable w) { protected Object makeObject(UnknownElement ue, RuntimeConfigurable w) {
if (!w.isEnabled(ue)) {
return null;
}
ComponentHelper helper = ComponentHelper.getComponentHelper( ComponentHelper helper = ComponentHelper.getComponentHelper(
getProject()); getProject());
String name = ue.getComponentName(); String name = ue.getComponentName();


+ 26
- 0
src/main/org/apache/tools/ant/attribute/AttributeNamespace.java View File

@@ -0,0 +1,26 @@
/*
* 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.attribute;

/**
* This class is used to indicate that the xml namespace (uri)
* can be used to look for namespace attributes.
*/
public final class AttributeNamespace {
}

+ 85
- 0
src/main/org/apache/tools/ant/attribute/BaseIfAttribute.java View File

@@ -0,0 +1,85 @@
/*
* 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.attribute;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.tools.ant.ProjectComponent;
import org.apache.tools.ant.RuntimeConfigurable;
import org.apache.tools.ant.UnknownElement;


/**
* An abstract class for if/unless attributes.
* This contains a boolean flag to specify whether this is an
* if or unless attribute.
*/
public abstract class BaseIfAttribute
extends ProjectComponent implements EnableAttribute {
private boolean positive = true;
/**
* Set the positive flag.
* @param positive the value to use.
*/
protected void setPositive(boolean positive) {
this.positive = positive;
}

/**
* Get the positive flag.
* @return the flag.
*/
protected boolean isPositive() {
return positive;
}

/**
* convert the result.
* @param val the result to convert
* @return val if positive or !val if not.
*/
protected boolean convertResult(boolean val) {
return positive ? val : !val;
}

/**
* Get all the attributes in the ant-attribute:param
* namespace and place them in a map.
* @param el the element this attribute is in.
* @return a map of attributes.
*/
protected Map getParams(UnknownElement el) {
Map ret = new HashMap();
RuntimeConfigurable rc = el.getWrapper();
Map attributes = rc.getAttributeMap(); // This does a copy!
for (Iterator i = attributes.entrySet().iterator(); i.hasNext();) {
Map.Entry entry = (Map.Entry) i.next();
String key = (String) entry.getKey();
String value = (String) entry.getValue();
if (key.startsWith("ant-attribute:param")) {
int pos = key.lastIndexOf(':');
ret.put(key.substring(pos + 1),
el.getProject().replaceProperties(value));
}
}
return ret;
}
}

+ 34
- 0
src/main/org/apache/tools/ant/attribute/EnableAttribute.java View File

@@ -0,0 +1,34 @@
/*
* 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.attribute;

import org.apache.tools.ant.UnknownElement;

/**
* This interface is used by ant attributes.
*/
public interface EnableAttribute {
/**
* is enabled.
* @param el the unknown element this attribute is in.
* @param value the value of the attribute.
* @return true if the attribute enables the element, false otherwise.
*/
boolean isEnabled(UnknownElement el, String value);
}

+ 38
- 0
src/main/org/apache/tools/ant/attribute/IfBlankAttribute.java View File

@@ -0,0 +1,38 @@
/*
* 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.attribute;

import org.apache.tools.ant.UnknownElement;

/**
* Check if an attribute is blank or not.
*/
public class IfBlankAttribute extends BaseIfAttribute {
/** The unless version */
public static class Unless extends IfBlankAttribute {
{ setPositive(false); }
}
/**
* check if the attribute value is blank or not
* {@inheritDoc}
*/
public boolean isEnabled(UnknownElement el, String value) {
return convertResult((value == null || "".equals(value)));
}
}

+ 38
- 0
src/main/org/apache/tools/ant/attribute/IfSetAttribute.java View File

@@ -0,0 +1,38 @@
/*
* 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.attribute;

import org.apache.tools.ant.UnknownElement;

/**
* Check if an attribute value as a property is set or not
*/
public class IfSetAttribute extends BaseIfAttribute {
/** The unless version */
public static class Unless extends IfSetAttribute {
{ setPositive(false); }
}
/**
* check if the attribute value is blank or not
* {@inheritDoc}
*/
public boolean isEnabled(UnknownElement el, String value) {
return convertResult(getProject().getProperty(value) != null);
}
}

+ 40
- 0
src/main/org/apache/tools/ant/attribute/IfTrueAttribute.java View File

@@ -0,0 +1,40 @@
/*
* 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.attribute;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.UnknownElement;

/**
* Check if an attribute value is true or not.
*/
public class IfTrueAttribute extends BaseIfAttribute {
/** The unless version */
public static class Unless extends IfTrueAttribute {
{ setPositive(false); }
}

/**
* check if the attribute value is true or not
* {@inheritDoc}
*/
public boolean isEnabled(UnknownElement el, String value) {
return convertResult(Project.toBoolean(value));
}
}

+ 51
- 0
src/main/org/apache/tools/ant/taskdefs/AttributeNamespaceDef.java View File

@@ -0,0 +1,51 @@
/*
* 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.taskdefs;

import org.apache.tools.ant.ProjectHelper;
import org.apache.tools.ant.ComponentHelper;
import org.apache.tools.ant.AntTypeDefinition;
import org.apache.tools.ant.attribute.AttributeNamespace;

/**
* Defintion to allow the uri to be considered for
* ant attributes.
*
* @since Ant 1.8.0
*/
public final class AttributeNamespaceDef extends AntlibDefinition {

/**
* Run the definition.
* This registers the xml namespace (uri) as a namepace for
* attributes.
*/
public void execute() {
String componentName = ProjectHelper.nsToComponentName(
getURI());
AntTypeDefinition def = new AntTypeDefinition();
def.setName(componentName);
def.setClassName(AttributeNamespace.class.getName());
def.setClass(AttributeNamespace.class);
def.setRestrict(true);
def.setClassLoader(AttributeNamespace.class.getClassLoader());
ComponentHelper.getComponentHelper(getProject())
.addDataTypeDefinition(def);
}
}

+ 1
- 0
src/main/org/apache/tools/ant/taskdefs/defaults.properties View File

@@ -20,6 +20,7 @@ antstructure=org.apache.tools.ant.taskdefs.AntStructure
antversion=org.apache.tools.ant.taskdefs.condition.AntVersion antversion=org.apache.tools.ant.taskdefs.condition.AntVersion
apply=org.apache.tools.ant.taskdefs.Transform apply=org.apache.tools.ant.taskdefs.Transform
apt=org.apache.tools.ant.taskdefs.Apt apt=org.apache.tools.ant.taskdefs.Apt
attributenamespacedef=org.apache.tools.ant.taskdefs.AttributeNamespaceDef
augment=org.apache.tools.ant.taskdefs.AugmentReference augment=org.apache.tools.ant.taskdefs.AugmentReference
available=org.apache.tools.ant.taskdefs.Available available=org.apache.tools.ant.taskdefs.Available
basename=org.apache.tools.ant.taskdefs.Basename basename=org.apache.tools.ant.taskdefs.Basename


+ 60
- 0
src/tests/antunit/core/ant-attribute-test.xml View File

@@ -0,0 +1,60 @@
<?xml version="1.0"?>
<!--
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.
-->
<project name="ant-attribute-test" default="antunit"
xmlns:au="antlib:org.apache.ant.antunit"
xmlns:if="ant:if"
xmlns:unless="ant:unless"
>

<import file="../antunit-base.xml" />

<target name="test-if-set">
<echo message="message1" if:set="not-set"/>
<au:assertLogDoesntContain text="message1"/>
</target>

<target name="test-unless-set">
<echo message="message2" unless:set="not-set"/>
<au:assertLogContains text="message2"/>
</target>

<target name="test-if-true">
<property name="sample" value="true"/>
<echo message="message3" if:true="${sample}"/>
<au:assertLogContains text="message3"/>
</target>

<target name="test-if-true-false">
<property name="sample-1" value="false"/>
<echo message="message4" if:true="${sample-1}"/>
<au:assertLogDoesntContain text="message4"/>
</target>

<target name="test-unless-true-false">
<property name="sample-1" value="false"/>
<echo message="message5" unless:true="${sample-1}"/>
<au:assertLogContains text="message5"/>
</target>

<target name="test-if-blank">
<property name="sample-1" value=""/>
<echo message="message6" if:blank="${sample-1}"/>
<au:assertLogContains text="message6"/>
</target>

</project>

Loading…
Cancel
Save