add LogicalOperationEvaluator

git-svn-id: https://svn.apache.org/repos/asf/ant/antlibs/props/trunk@916861 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/org/apache/ant/props/LogicalOperationEvaluator.java b/src/main/org/apache/ant/props/LogicalOperationEvaluator.java
new file mode 100644
index 0000000..fa414af
--- /dev/null
+++ b/src/main/org/apache/ant/props/LogicalOperationEvaluator.java
@@ -0,0 +1,86 @@
+/*
+ *  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.ant.props;
+
+import java.util.regex.Pattern;
+
+import org.apache.tools.ant.PropertyHelper;
+import org.apache.tools.ant.PropertyHelper.PropertyEvaluator;
+
+/**
+ * {@link PropertyEvaluator} that processes combinations of <code>!</code>,
+ * <code>&</code> (<code>+</code> is also accepted for XML ease-of-use),
+ * <code>^</code> (XOR), and <code>|</code>, in descending priority.
+ * Probably only useful when used with {@link NestedPropertyExpander} which, when
+ * taken in conjunction with {@link ConditionTypeEvaluator}, will resolve nested
+ * conditions to <code>true</code> or <code>false</code>, allowing them to be
+ * processed by this little fellow.
+ * 
+ * Grouping can be accomplished by means of nested property expressions.
+ */
+public class LogicalOperationEvaluator extends RegexBasedEvaluator {
+    private static final String BOOL = "(?:true|false)";
+    private static final String NEGATED_BOOL = "!" + BOOL;
+    private static final String EXPR = "(?:" + BOOL + "|" + NEGATED_BOOL + ")";
+    private static final String AND = "[\\s]*(?:&|\\+)[\\s]*";
+    private static final Pattern AND_PATTERN = Pattern.compile(AND);
+    private static final String XOR = "[\\s]*\\^[\\s]*";
+    private static final Pattern XOR_PATTERN = Pattern.compile(XOR);
+    private static final String OR = "[\\s]*\\|[\\s]*";
+    private static final Pattern OR_PATTERN = Pattern.compile(OR);
+    private static final String OP = "(?:" + AND + "|" + XOR + "|" + OR + ")" + EXPR;
+
+    // we accept a pattern of either EXPR OP EXPR (OP EXPR)* | NEGATED_BOOL (without op)
+    private static final String PATTERN = "^" + "(?:" + EXPR + OP + "(?:" + OP + ")*)|"
+            + NEGATED_BOOL + "$";
+
+    /**
+     * Create a new LogicalOperationEvaluator instance.
+     */
+    public LogicalOperationEvaluator() {
+        super(PATTERN);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    protected Object evaluate(String[] groups, PropertyHelper propertyHelper) {
+        boolean result = false;
+        String[] or = OR_PATTERN.split(groups[0]);
+        for (int o = 0; !result && o < or.length; o++) {
+            boolean accumXor = false;
+            String[] xor = XOR_PATTERN.split(or[o]);
+            for (int x = 0; x < xor.length; x++) {
+                boolean accumAnd = true;
+                String[] and = AND_PATTERN.split(xor[x]);
+                for (int a = 0; accumAnd && a < and.length; a++) {
+                    boolean negate = false;
+                    String expr = and[a];
+                    if (expr.charAt(0) == '!') {
+                        negate = true;
+                        expr = expr.substring(1);
+                    }
+                    boolean b = Boolean.valueOf(expr.trim().toLowerCase()).booleanValue() ^ negate;
+                    accumAnd &= b;
+                }
+                accumXor ^= accumAnd;
+            }
+            result |= accumXor;
+        }
+        return Boolean.valueOf(result);
+    }
+}
diff --git a/src/tests/antunit/logical-ops-test.xml b/src/tests/antunit/logical-ops-test.xml
new file mode 100644
index 0000000..f2a2ae0
--- /dev/null
+++ b/src/tests/antunit/logical-ops-test.xml
@@ -0,0 +1,216 @@
+<?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 xmlns:au="antlib:org.apache.ant.antunit"
+         xmlns:props="antlib:org.apache.ant.props" default="antunit">
+
+  <typedef name="logical-ops" classname="org.apache.ant.props.LogicalOperationEvaluator" />
+  <logical-ops id="logical-ops" />
+
+  <propertyhelper>
+    <delegate refid="logical-ops" />
+    <props:nested />
+  </propertyhelper>
+
+  <target name="antunit">
+    <antunit xmlns="antlib:org.apache.ant.antunit">
+      <plainlistener />
+      <file file="${ant.file}" xmlns="antlib:org.apache.tools.ant" />
+    </antunit>
+  </target>
+
+  <target name="test-amp">
+    <au:assertTrue>
+      <istrue value="${true &amp; true}" />
+    </au:assertTrue>
+    <au:assertFalse>
+      <istrue value="${false &amp; true}" />
+    </au:assertFalse>
+    <au:assertFalse>
+      <istrue value="${false &amp; false}" />
+    </au:assertFalse>
+    <au:assertFalse>
+      <istrue value="${true &amp; false}" />
+    </au:assertFalse>
+  </target>
+
+  <target name="test-plus">
+    <au:assertTrue>
+      <istrue value="${true + true}" />
+    </au:assertTrue>
+    <au:assertFalse>
+      <istrue value="${true + false}" />
+    </au:assertFalse>
+    <au:assertFalse>
+      <istrue value="${false + false}" />
+    </au:assertFalse>
+    <au:assertFalse>
+      <istrue value="${false + true}" />
+    </au:assertFalse>
+  </target>
+
+  <target name="test-xor">
+    <au:assertTrue>
+      <istrue value="${true ^ false}" />
+    </au:assertTrue>
+    <au:assertTrue>
+      <istrue value="${false ^ true}" />
+    </au:assertTrue>
+    <au:assertFalse>
+      <istrue value="${true ^ true}" />
+    </au:assertFalse>
+    <au:assertFalse>
+      <istrue value="${false ^ false}" />
+    </au:assertFalse>
+  </target>
+
+  <target name="test-or">
+    <au:assertTrue>
+      <istrue value="${true | false}" />
+    </au:assertTrue>
+    <au:assertTrue>
+      <istrue value="${true | true}" />
+    </au:assertTrue>
+    <au:assertTrue>
+      <istrue value="${false | true}" />
+    </au:assertTrue>
+    <au:assertFalse>
+      <istrue value="${false | false}" />
+    </au:assertFalse>
+  </target>
+
+  <target name="test-multi-amp">
+    <au:assertTrue>
+      <istrue value="${true &amp; true &amp; true &amp; true}" />
+    </au:assertTrue>
+    <au:assertFalse>
+      <istrue value="${false &amp; true &amp; true &amp; true}" />
+    </au:assertFalse>
+    <au:assertFalse>
+      <istrue value="${true &amp; false &amp; true &amp; true}" />
+    </au:assertFalse>
+    <au:assertFalse>
+      <istrue value="${true &amp; true &amp; false &amp; true}" />
+    </au:assertFalse>
+    <au:assertFalse>
+      <istrue value="${true &amp; true &amp; true &amp; false}" />
+    </au:assertFalse>
+  </target>
+
+  <target name="test-multi-plus">
+    <au:assertTrue>
+      <istrue value="${true + true + true + true}" />
+    </au:assertTrue>
+    <au:assertFalse>
+      <istrue value="${false + true + true + true}" />
+    </au:assertFalse>
+    <au:assertFalse>
+      <istrue value="${true + false + true + true}" />
+    </au:assertFalse>
+    <au:assertFalse>
+      <istrue value="${true + true + false + true}" />
+    </au:assertFalse>
+    <au:assertFalse>
+      <istrue value="${true + true + true + false}" />
+    </au:assertFalse>
+  </target>
+
+  <target name="test-multi-xor">
+    <au:assertFalse>
+      <!-- true ^ false == true, ^ true == false, ^ false == false -->
+      <istrue value="${true ^ false ^ true ^ false}" />
+    </au:assertFalse>
+    <au:assertTrue>
+      <!-- true ^ false == true, ^ false == true, ^ false == true -->
+      <istrue value="${true ^ false ^ false ^ false}" />
+    </au:assertTrue>
+    <au:assertTrue>
+      <!-- false ^ true == true, ^ false == true, ^ false == true -->
+      <istrue value="${false ^ true ^ false ^ false}" />
+    </au:assertTrue>
+    <au:assertFalse>
+      <istrue value="${false ^ false ^ false ^ false}" />
+    </au:assertFalse>
+    <au:assertTrue>
+      <istrue value="${false ^ false ^ false ^ true}" />
+    </au:assertTrue>
+    <au:assertTrue>
+      <istrue value="${true ^ false ^ true ^ true}" />
+    </au:assertTrue>
+    <au:assertTrue>
+      <istrue value="${true ^ true ^ true ^ true ^ true}" />
+    </au:assertTrue>
+  </target>
+
+  <target name="test-multi-or">
+    <au:assertTrue>
+      <istrue value="${false | true | false | false}" />
+    </au:assertTrue>
+    <au:assertTrue>
+      <istrue value="${true | false | false | false}" />
+    </au:assertTrue>
+    <au:assertTrue>
+      <istrue value="${false | false | false | true}" />
+    </au:assertTrue>
+    <au:assertTrue>
+      <istrue value="${false | false | true | false}" />
+    </au:assertTrue>
+    <au:assertTrue>
+      <istrue value="${true | false | true | false}" />
+    </au:assertTrue>
+    <au:assertTrue>
+      <istrue value="${true | true | true | false}" />
+    </au:assertTrue>
+    <au:assertTrue>
+      <istrue value="${true | true | true | false}" />
+    </au:assertTrue>
+  </target>
+
+  <target name="test-precedence">
+    <au:assertTrue>
+      <istrue value="${true | false + true}" />
+    </au:assertTrue>
+    <au:assertTrue>
+      <istrue value="${true + false | true}" />
+    </au:assertTrue>
+    <au:assertTrue>
+      <istrue value="${false + true ^ true | true + false}" />
+    </au:assertTrue>
+  </target>
+
+  <target name="test-negation">
+    <au:assertTrue>
+      <istrue value="${!false}" />
+    </au:assertTrue>
+    <au:assertFalse>
+      <istrue value="${!true}" />
+    </au:assertFalse>
+  </target>
+
+  <target name="test-nested-negation">
+    <propertyhelper>
+      <props:stringops />
+    </propertyhelper>
+    <au:assertTrue>
+      <istrue value="${!${foo:-false}}" />
+    </au:assertTrue>
+    <au:assertFalse>
+      <istrue value="${!${foo:-true}}" />
+    </au:assertFalse>
+  </target>
+
+</project>