com.aspose.words

Class ComparisonExpression

  • java.lang.Object
    • com.aspose.words.ComparisonExpression
public class ComparisonExpression 
extends java.lang.Object

The comparison expression.

Example:

Shows how to implement custom evaluation for the IF and COMPARE fields.
public void conditionEvaluationExtensionPoint(String fieldCode, byte comparisonResult, String comparisonError,
                                              String expectedResult) throws Exception {
    final String LEFT = "\"left expression\"";
    final String _OPERATOR = "<>";
    final String RIGHT = "\"right expression\"";

    DocumentBuilder builder = new DocumentBuilder();

    // Field codes that we use in this example:
    // 1.   " IF %s %s %s \"true argument\" \"false argument\" ".
    // 2.   " COMPARE %s %s %s ".
    Field field = builder.insertField(String.format(fieldCode, LEFT, _OPERATOR, RIGHT), null);

    // If the "comparisonResult" is undefined, we create "ComparisonEvaluationResult" with string, instead of bool.
    ComparisonEvaluationResult result = comparisonResult != -1
            ? new ComparisonEvaluationResult(comparisonResult == 1)
            : comparisonError != null ? new ComparisonEvaluationResult(comparisonError) : null;

    ComparisonExpressionEvaluator evaluator = new ComparisonExpressionEvaluator(result);
    builder.getDocument().getFieldOptions().setComparisonExpressionEvaluator(evaluator);

    builder.getDocument().updateFields();

    Assert.assertEquals(expectedResult, field.getResult());
    evaluator.assertInvocationsCount(1).assertInvocationArguments(0, LEFT, _OPERATOR, RIGHT);
}

@DataProvider(name = "conditionEvaluationExtensionPointDataProvider")
public static Object[][] conditionEvaluationExtensionPointDataProvider() {
    return new Object[][]
            {
                    {" IF %s %s %s \"true argument\" \"false argument\" ", (byte) 1, null, "true argument"},
                    {" IF %s %s %s \"true argument\" \"false argument\" ", (byte) 0, null, "false argument"},
                    {" IF %s %s %s \"true argument\" \"false argument\" ", (byte) -1, "Custom Error", "Custom Error"},
                    {" IF %s %s %s \"true argument\" \"false argument\" ", (byte) -1, null, "true argument"},
                    {" COMPARE %s %s %s ", (byte) 1, null, "1"},
                    {" COMPARE %s %s %s ", (byte) 0, null, "0"},
                    {" COMPARE %s %s %s ", (byte) -1, "Custom Error", "Custom Error"},
                    {" COMPARE %s %s %s ", (byte) -1, null, "1"},
            };
}

/// <summary>
/// Comparison expressions evaluation for the FieldIf and FieldCompare.
/// </summary>
private static class ComparisonExpressionEvaluator implements IComparisonExpressionEvaluator {
    public ComparisonExpressionEvaluator(ComparisonEvaluationResult result) {
        mResult = result;
    }

    public ComparisonEvaluationResult evaluate(Field field, ComparisonExpression expression) {
        mInvocations.add(new String[]
                {
                        expression.getLeftExpression(),
                        expression.getComparisonOperator(),
                        expression.getRightExpression()
                });

        return mResult;
    }

    public ComparisonExpressionEvaluator assertInvocationsCount(int expected) {
        Assert.assertEquals(expected, mInvocations.size());
        return this;
    }

    public ComparisonExpressionEvaluator assertInvocationArguments(
            int invocationIndex,
            String expectedLeftExpression,
            String expectedComparisonOperator,
            String expectedRightExpression) {
        String[] arguments = mInvocations.get(invocationIndex);

        Assert.assertEquals(expectedLeftExpression, arguments[0]);
        Assert.assertEquals(expectedComparisonOperator, arguments[1]);
        Assert.assertEquals(expectedRightExpression, arguments[2]);

        return this;
    }

    private final ComparisonEvaluationResult mResult;
    private final ArrayList<String[]> mInvocations = new ArrayList<>();
}

Property Getters/Setters Summary
java.lang.StringgetComparisonOperator()
Gets the comparison operator.
java.lang.StringgetLeftExpression()
Gets the left expression.
java.lang.StringgetRightExpression()
Gets the right expression.
 

    • Property Getters/Setters Detail

      • getComparisonOperator

        public java.lang.String getComparisonOperator()
        
        Gets the comparison operator.

        Example:

        Shows how to implement custom evaluation for the IF and COMPARE fields.
        public void conditionEvaluationExtensionPoint(String fieldCode, byte comparisonResult, String comparisonError,
                                                      String expectedResult) throws Exception {
            final String LEFT = "\"left expression\"";
            final String _OPERATOR = "<>";
            final String RIGHT = "\"right expression\"";
        
            DocumentBuilder builder = new DocumentBuilder();
        
            // Field codes that we use in this example:
            // 1.   " IF %s %s %s \"true argument\" \"false argument\" ".
            // 2.   " COMPARE %s %s %s ".
            Field field = builder.insertField(String.format(fieldCode, LEFT, _OPERATOR, RIGHT), null);
        
            // If the "comparisonResult" is undefined, we create "ComparisonEvaluationResult" with string, instead of bool.
            ComparisonEvaluationResult result = comparisonResult != -1
                    ? new ComparisonEvaluationResult(comparisonResult == 1)
                    : comparisonError != null ? new ComparisonEvaluationResult(comparisonError) : null;
        
            ComparisonExpressionEvaluator evaluator = new ComparisonExpressionEvaluator(result);
            builder.getDocument().getFieldOptions().setComparisonExpressionEvaluator(evaluator);
        
            builder.getDocument().updateFields();
        
            Assert.assertEquals(expectedResult, field.getResult());
            evaluator.assertInvocationsCount(1).assertInvocationArguments(0, LEFT, _OPERATOR, RIGHT);
        }
        
        @DataProvider(name = "conditionEvaluationExtensionPointDataProvider")
        public static Object[][] conditionEvaluationExtensionPointDataProvider() {
            return new Object[][]
                    {
                            {" IF %s %s %s \"true argument\" \"false argument\" ", (byte) 1, null, "true argument"},
                            {" IF %s %s %s \"true argument\" \"false argument\" ", (byte) 0, null, "false argument"},
                            {" IF %s %s %s \"true argument\" \"false argument\" ", (byte) -1, "Custom Error", "Custom Error"},
                            {" IF %s %s %s \"true argument\" \"false argument\" ", (byte) -1, null, "true argument"},
                            {" COMPARE %s %s %s ", (byte) 1, null, "1"},
                            {" COMPARE %s %s %s ", (byte) 0, null, "0"},
                            {" COMPARE %s %s %s ", (byte) -1, "Custom Error", "Custom Error"},
                            {" COMPARE %s %s %s ", (byte) -1, null, "1"},
                    };
        }
        
        /// <summary>
        /// Comparison expressions evaluation for the FieldIf and FieldCompare.
        /// </summary>
        private static class ComparisonExpressionEvaluator implements IComparisonExpressionEvaluator {
            public ComparisonExpressionEvaluator(ComparisonEvaluationResult result) {
                mResult = result;
            }
        
            public ComparisonEvaluationResult evaluate(Field field, ComparisonExpression expression) {
                mInvocations.add(new String[]
                        {
                                expression.getLeftExpression(),
                                expression.getComparisonOperator(),
                                expression.getRightExpression()
                        });
        
                return mResult;
            }
        
            public ComparisonExpressionEvaluator assertInvocationsCount(int expected) {
                Assert.assertEquals(expected, mInvocations.size());
                return this;
            }
        
            public ComparisonExpressionEvaluator assertInvocationArguments(
                    int invocationIndex,
                    String expectedLeftExpression,
                    String expectedComparisonOperator,
                    String expectedRightExpression) {
                String[] arguments = mInvocations.get(invocationIndex);
        
                Assert.assertEquals(expectedLeftExpression, arguments[0]);
                Assert.assertEquals(expectedComparisonOperator, arguments[1]);
                Assert.assertEquals(expectedRightExpression, arguments[2]);
        
                return this;
            }
        
            private final ComparisonEvaluationResult mResult;
            private final ArrayList<String[]> mInvocations = new ArrayList<>();
        }
      • getLeftExpression

        public java.lang.String getLeftExpression()
        
        Gets the left expression.

        Example:

        Shows how to implement custom evaluation for the IF and COMPARE fields.
        public void conditionEvaluationExtensionPoint(String fieldCode, byte comparisonResult, String comparisonError,
                                                      String expectedResult) throws Exception {
            final String LEFT = "\"left expression\"";
            final String _OPERATOR = "<>";
            final String RIGHT = "\"right expression\"";
        
            DocumentBuilder builder = new DocumentBuilder();
        
            // Field codes that we use in this example:
            // 1.   " IF %s %s %s \"true argument\" \"false argument\" ".
            // 2.   " COMPARE %s %s %s ".
            Field field = builder.insertField(String.format(fieldCode, LEFT, _OPERATOR, RIGHT), null);
        
            // If the "comparisonResult" is undefined, we create "ComparisonEvaluationResult" with string, instead of bool.
            ComparisonEvaluationResult result = comparisonResult != -1
                    ? new ComparisonEvaluationResult(comparisonResult == 1)
                    : comparisonError != null ? new ComparisonEvaluationResult(comparisonError) : null;
        
            ComparisonExpressionEvaluator evaluator = new ComparisonExpressionEvaluator(result);
            builder.getDocument().getFieldOptions().setComparisonExpressionEvaluator(evaluator);
        
            builder.getDocument().updateFields();
        
            Assert.assertEquals(expectedResult, field.getResult());
            evaluator.assertInvocationsCount(1).assertInvocationArguments(0, LEFT, _OPERATOR, RIGHT);
        }
        
        @DataProvider(name = "conditionEvaluationExtensionPointDataProvider")
        public static Object[][] conditionEvaluationExtensionPointDataProvider() {
            return new Object[][]
                    {
                            {" IF %s %s %s \"true argument\" \"false argument\" ", (byte) 1, null, "true argument"},
                            {" IF %s %s %s \"true argument\" \"false argument\" ", (byte) 0, null, "false argument"},
                            {" IF %s %s %s \"true argument\" \"false argument\" ", (byte) -1, "Custom Error", "Custom Error"},
                            {" IF %s %s %s \"true argument\" \"false argument\" ", (byte) -1, null, "true argument"},
                            {" COMPARE %s %s %s ", (byte) 1, null, "1"},
                            {" COMPARE %s %s %s ", (byte) 0, null, "0"},
                            {" COMPARE %s %s %s ", (byte) -1, "Custom Error", "Custom Error"},
                            {" COMPARE %s %s %s ", (byte) -1, null, "1"},
                    };
        }
        
        /// <summary>
        /// Comparison expressions evaluation for the FieldIf and FieldCompare.
        /// </summary>
        private static class ComparisonExpressionEvaluator implements IComparisonExpressionEvaluator {
            public ComparisonExpressionEvaluator(ComparisonEvaluationResult result) {
                mResult = result;
            }
        
            public ComparisonEvaluationResult evaluate(Field field, ComparisonExpression expression) {
                mInvocations.add(new String[]
                        {
                                expression.getLeftExpression(),
                                expression.getComparisonOperator(),
                                expression.getRightExpression()
                        });
        
                return mResult;
            }
        
            public ComparisonExpressionEvaluator assertInvocationsCount(int expected) {
                Assert.assertEquals(expected, mInvocations.size());
                return this;
            }
        
            public ComparisonExpressionEvaluator assertInvocationArguments(
                    int invocationIndex,
                    String expectedLeftExpression,
                    String expectedComparisonOperator,
                    String expectedRightExpression) {
                String[] arguments = mInvocations.get(invocationIndex);
        
                Assert.assertEquals(expectedLeftExpression, arguments[0]);
                Assert.assertEquals(expectedComparisonOperator, arguments[1]);
                Assert.assertEquals(expectedRightExpression, arguments[2]);
        
                return this;
            }
        
            private final ComparisonEvaluationResult mResult;
            private final ArrayList<String[]> mInvocations = new ArrayList<>();
        }
      • getRightExpression

        public java.lang.String getRightExpression()
        
        Gets the right expression.

        Example:

        Shows how to implement custom evaluation for the IF and COMPARE fields.
        public void conditionEvaluationExtensionPoint(String fieldCode, byte comparisonResult, String comparisonError,
                                                      String expectedResult) throws Exception {
            final String LEFT = "\"left expression\"";
            final String _OPERATOR = "<>";
            final String RIGHT = "\"right expression\"";
        
            DocumentBuilder builder = new DocumentBuilder();
        
            // Field codes that we use in this example:
            // 1.   " IF %s %s %s \"true argument\" \"false argument\" ".
            // 2.   " COMPARE %s %s %s ".
            Field field = builder.insertField(String.format(fieldCode, LEFT, _OPERATOR, RIGHT), null);
        
            // If the "comparisonResult" is undefined, we create "ComparisonEvaluationResult" with string, instead of bool.
            ComparisonEvaluationResult result = comparisonResult != -1
                    ? new ComparisonEvaluationResult(comparisonResult == 1)
                    : comparisonError != null ? new ComparisonEvaluationResult(comparisonError) : null;
        
            ComparisonExpressionEvaluator evaluator = new ComparisonExpressionEvaluator(result);
            builder.getDocument().getFieldOptions().setComparisonExpressionEvaluator(evaluator);
        
            builder.getDocument().updateFields();
        
            Assert.assertEquals(expectedResult, field.getResult());
            evaluator.assertInvocationsCount(1).assertInvocationArguments(0, LEFT, _OPERATOR, RIGHT);
        }
        
        @DataProvider(name = "conditionEvaluationExtensionPointDataProvider")
        public static Object[][] conditionEvaluationExtensionPointDataProvider() {
            return new Object[][]
                    {
                            {" IF %s %s %s \"true argument\" \"false argument\" ", (byte) 1, null, "true argument"},
                            {" IF %s %s %s \"true argument\" \"false argument\" ", (byte) 0, null, "false argument"},
                            {" IF %s %s %s \"true argument\" \"false argument\" ", (byte) -1, "Custom Error", "Custom Error"},
                            {" IF %s %s %s \"true argument\" \"false argument\" ", (byte) -1, null, "true argument"},
                            {" COMPARE %s %s %s ", (byte) 1, null, "1"},
                            {" COMPARE %s %s %s ", (byte) 0, null, "0"},
                            {" COMPARE %s %s %s ", (byte) -1, "Custom Error", "Custom Error"},
                            {" COMPARE %s %s %s ", (byte) -1, null, "1"},
                    };
        }
        
        /// <summary>
        /// Comparison expressions evaluation for the FieldIf and FieldCompare.
        /// </summary>
        private static class ComparisonExpressionEvaluator implements IComparisonExpressionEvaluator {
            public ComparisonExpressionEvaluator(ComparisonEvaluationResult result) {
                mResult = result;
            }
        
            public ComparisonEvaluationResult evaluate(Field field, ComparisonExpression expression) {
                mInvocations.add(new String[]
                        {
                                expression.getLeftExpression(),
                                expression.getComparisonOperator(),
                                expression.getRightExpression()
                        });
        
                return mResult;
            }
        
            public ComparisonExpressionEvaluator assertInvocationsCount(int expected) {
                Assert.assertEquals(expected, mInvocations.size());
                return this;
            }
        
            public ComparisonExpressionEvaluator assertInvocationArguments(
                    int invocationIndex,
                    String expectedLeftExpression,
                    String expectedComparisonOperator,
                    String expectedRightExpression) {
                String[] arguments = mInvocations.get(invocationIndex);
        
                Assert.assertEquals(expectedLeftExpression, arguments[0]);
                Assert.assertEquals(expectedComparisonOperator, arguments[1]);
                Assert.assertEquals(expectedRightExpression, arguments[2]);
        
                return this;
            }
        
            private final ComparisonEvaluationResult mResult;
            private final ArrayList<String[]> mInvocations = new ArrayList<>();
        }