1 29 30 package nextapp.echo2.app.componentxml.propertypeer; 31 32 import java.util.Collections ; 33 import java.util.HashMap ; 34 import java.util.Map ; 35 36 import org.w3c.dom.Element ; 37 38 import nextapp.echo2.app.Extent; 39 import nextapp.echo2.app.componentxml.InvalidPropertyException; 40 import nextapp.echo2.app.componentxml.PropertyXmlPeer; 41 42 46 public class ExtentPeer 47 implements PropertyXmlPeer { 48 49 private static final Map UNITS_TEXT_TO_CONSTANT; 50 static { 51 Map constantMap = new HashMap (); 52 constantMap.put("cm", new Integer (Extent.CM)); 53 constantMap.put("em", new Integer (Extent.EM)); 54 constantMap.put("ex", new Integer (Extent.EX)); 55 constantMap.put("in", new Integer (Extent.IN)); 56 constantMap.put("mm", new Integer (Extent.MM)); 57 constantMap.put("pc", new Integer (Extent.PC)); 58 constantMap.put("pt", new Integer (Extent.PT)); 59 constantMap.put("px", new Integer (Extent.PX)); 60 constantMap.put("%", new Integer (Extent.PERCENT)); 61 UNITS_TEXT_TO_CONSTANT = Collections.unmodifiableMap(constantMap); 62 } 63 64 71 public static Extent toExtent(String value) { 72 int suffixIndex = -1; 73 for (int i = 0; i < value.length(); ++i) { 74 char ch = value.charAt(i); 75 if (!((i == 0 && ch == '-') || Character.isDigit(ch))) { 76 suffixIndex = i; 78 break; 79 } 80 } 81 if (suffixIndex < 1) { 82 throw new IllegalArgumentException ("Invalid Extent value: " + value); 83 } 84 85 int size = Integer.parseInt(value.substring(0, suffixIndex)); 86 String unitsText = value.substring(suffixIndex); 87 88 if (!UNITS_TEXT_TO_CONSTANT.containsKey(unitsText)) { 89 throw new IllegalArgumentException ("Invalid Extent units: " + value); 90 } 91 int units = ((Integer ) UNITS_TEXT_TO_CONSTANT.get(unitsText)).intValue(); 92 93 return new Extent(size, units); 94 } 95 96 100 public Object getValue(ClassLoader classLoader, Class objectClass, Element propertyElement) 101 throws InvalidPropertyException { 102 return(toExtent(propertyElement.getAttribute("value"))); 103 } 104 } 105 | Popular Tags |