1 17 18 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 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 private static String operations = " +-*/%"; 47 48 51 private int operation; 52 55 private Numeric op1; 56 59 private Numeric op2; 60 63 private int dimension; 64 65 71 public RelativeNumericProperty(int operation, Numeric op1, Numeric op2) { 72 this.operation = operation; 73 this.op1 = op1; 74 this.op2 = op2; 75 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 93 public RelativeNumericProperty(int operation, Numeric op) { 94 this.operation = operation; 95 this.op1 = op; 96 this.dimension = op.getDimension(); 97 } 98 99 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 133 public double getNumericValue() throws PropertyException { 134 return getResolved(null).getNumericValue(null); 135 } 136 137 140 public double getNumericValue(PercentBaseContext context) throws PropertyException { 141 return getResolved(context).getNumericValue(context); 142 } 143 144 147 public int getDimension() { 148 return dimension; 149 } 150 151 155 public boolean isAbsolute() { 156 return false; 157 } 158 159 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 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 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 202 public String 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 |