KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > fo > expr > RelativeNumericProperty


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: RelativeNumericProperty.java 488960 2006-12-20 08:34:28Z spepping $ */
19
20 package org.apache.fop.fo.expr;
21
22 import org.apache.fop.datatypes.Length;
23 import org.apache.fop.datatypes.PercentBaseContext;
24 import org.apache.fop.datatypes.Numeric;
25 import org.apache.fop.fo.properties.Property;
26
27
28 /**
29  * This class represent a node in a property expression tree.
30  * It is created when an operation involve relative expression and is used
31  * to delay evaluation of the operation until the time where getNumericValue()
32  * or getValue() is called.
33  */

34 public class RelativeNumericProperty extends Property implements Numeric, Length {
35     public static final int ADDITION = 1;
36     public static final int SUBTRACTION = 2;
37     public static final int MULTIPLY = 3;
38     public static final int DIVIDE = 4;
39     public static final int MODULO = 5;
40     public static final int NEGATE = 6;
41     public static final int ABS = 7;
42     public static final int MAX = 8;
43     public static final int MIN = 9;
44     
45     // Used in the toString() method, indexed by operation id.
46
private static String JavaDoc operations = " +-*/%";
47     
48     /**
49      * The operation identifier.
50      */

51     private int operation;
52     /**
53      * The first (or only) operand.
54      */

55     private Numeric op1;
56     /**
57      * The second operand.
58      */

59     private Numeric op2;
60     /**
61      * The dimension of the result.
62      */

63     private int dimension;
64     
65     /**
66      * Constructor for a two argument operation.
67      * @param operation the operation opcode: ADDITION, SUBTRACTION, ...
68      * @param op1 the first operand.
69      * @param op2 the second operand
70      */

71     public RelativeNumericProperty(int operation, Numeric op1, Numeric op2) {
72         this.operation = operation;
73         this.op1 = op1;
74         this.op2 = op2;
75         // Calculate the dimension. We can do now.
76
switch (operation) {
77         case MULTIPLY:
78             dimension = op1.getDimension() + op2.getDimension();
79             break;
80         case DIVIDE:
81             dimension = op1.getDimension() - op2.getDimension();
82             break;
83         default:
84             dimension = op1.getDimension();
85         }
86     }
87
88     /**
89      * Constructor for a one argument operation.
90      * @param operation the operation opcode: NEGATE, ABS
91      * @param op the operand.
92      */

93     public RelativeNumericProperty(int operation, Numeric op) {
94         this.operation = operation;
95         this.op1 = op;
96         this.dimension = op.getDimension();
97     }
98
99     /**
100      * Return a resolved (calculated) Numeric with the value of the expression.
101      * @param context Evaluation context
102      * @throws PropertyException when an exception occur during evaluation.
103      */

104     private Numeric getResolved(PercentBaseContext context) throws PropertyException {
105         switch (operation) {
106         case ADDITION:
107             return NumericOp.addition2(op1, op2, context);
108         case SUBTRACTION:
109             return NumericOp.subtraction2(op1, op2, context);
110         case MULTIPLY:
111             return NumericOp.multiply2(op1, op2, context);
112         case DIVIDE:
113             return NumericOp.divide2(op1, op2, context);
114         case MODULO:
115             return NumericOp.modulo2(op1, op2, context);
116         case NEGATE:
117             return NumericOp.negate2(op1, context);
118         case ABS:
119             return NumericOp.abs2(op1, context);
120         case MAX:
121             return NumericOp.max2(op1, op2, context);
122         case MIN:
123             return NumericOp.min2(op1, op2, context);
124         default:
125             throw new PropertyException("Unknown expr operation " + operation);
126         }
127     }
128
129     /**
130      * Return the resolved (calculated) value of the expression.
131      * @see org.apache.fop.datatypes.Numeric#getNumericValue()
132      */

133     public double getNumericValue() throws PropertyException {
134         return getResolved(null).getNumericValue(null);
135     }
136
137     /**
138      * @see org.apache.fop.datatypes.Numeric#getNumericValue(PercentBaseContext)
139      */

140     public double getNumericValue(PercentBaseContext context) throws PropertyException {
141         return getResolved(context).getNumericValue(context);
142     }
143
144     /**
145      * Return the dimension of the expression
146      */

147     public int getDimension() {
148         return dimension;
149     }
150
151     /**
152      * Return false since an expression is only created when there is relative
153      * numerics involved.
154      */

155     public boolean isAbsolute() {
156         return false;
157     }
158
159     /**
160      * Cast this numeric as a Length.
161      */

162     public Length getLength() {
163         if (dimension == 1) {
164             return this;
165         }
166         log.error("Can't create length with dimension " + dimension);
167         return null;
168     }
169
170     public Numeric getNumeric() {
171         return this;
172     }
173
174     /**
175      * @see org.apache.fop.datatypes.Numeric#getValue()
176      */

177     public int getValue() {
178         try {
179             return (int) getNumericValue();
180         } catch (PropertyException exc) {
181             log.error(exc);
182         }
183         return 0;
184     }
185
186     /**
187      * @see org.apache.fop.datatypes.Numeric#getValue(PercentBaseContext)
188      */

189     public int getValue(PercentBaseContext context) {
190         try {
191             return (int) getNumericValue(context);
192         } catch (PropertyException exc) {
193             log.error(exc);
194         }
195         return 0;
196     }
197
198     /**
199      * Return a string represention of the expression. Only used for debugging.
200      * @return the string representation.
201      */

202     public String JavaDoc toString() {
203         switch (operation) {
204         case ADDITION: case SUBTRACTION:
205         case DIVIDE: case MULTIPLY: case MODULO:
206             return "(" + op1 + " " + operations.charAt(operation) + op2 + ")";
207         case NEGATE:
208             return "-" + op1;
209         case MAX:
210             return "max(" + op1 + ", " + op2 + ")";
211         case MIN:
212            return "min(" + op1 + ", " + op2 + ")";
213         case ABS:
214            return "abs(" + op1 + ")";
215         }
216         return "unknown operation " + operation;
217     }
218 }
219
Popular Tags