001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.app.componentxml.propertypeer;
031:
032: import java.util.Collections;
033: import java.util.HashMap;
034: import java.util.Map;
035:
036: import org.w3c.dom.Element;
037:
038: import nextapp.echo2.app.Extent;
039: import nextapp.echo2.app.componentxml.InvalidPropertyException;
040: import nextapp.echo2.app.componentxml.PropertyXmlPeer;
041:
042: /**
043: * <code>PropertyXmlPeer</code> implementation for
044: * <code>nextapp.echo2.app.Extent</code> properties.
045: */
046: public class ExtentPeer implements PropertyXmlPeer {
047:
048: private static final Map UNITS_TEXT_TO_CONSTANT;
049: static {
050: Map constantMap = new HashMap();
051: constantMap.put("cm", new Integer(Extent.CM));
052: constantMap.put("em", new Integer(Extent.EM));
053: constantMap.put("ex", new Integer(Extent.EX));
054: constantMap.put("in", new Integer(Extent.IN));
055: constantMap.put("mm", new Integer(Extent.MM));
056: constantMap.put("pc", new Integer(Extent.PC));
057: constantMap.put("pt", new Integer(Extent.PT));
058: constantMap.put("px", new Integer(Extent.PX));
059: constantMap.put("%", new Integer(Extent.PERCENT));
060: UNITS_TEXT_TO_CONSTANT = Collections
061: .unmodifiableMap(constantMap);
062: }
063:
064: /**
065: * Converts the given string value to an <code>Extent</code> equivalent.
066: *
067: * @param value a string representation of an <code>Extent</code>
068: * @return the <code>Extent</code>
069: * @throws IllegalArgumentException if the string is improperly formatted.
070: */
071: public static Extent toExtent(String value) {
072: int suffixIndex = -1;
073: for (int i = 0; i < value.length(); ++i) {
074: char ch = value.charAt(i);
075: if (!((i == 0 && ch == '-') || Character.isDigit(ch))) {
076: // Not a digit (nor a minus at first position)
077: suffixIndex = i;
078: break;
079: }
080: }
081: if (suffixIndex < 1) {
082: throw new IllegalArgumentException("Invalid Extent value: "
083: + value);
084: }
085:
086: int size = Integer.parseInt(value.substring(0, suffixIndex));
087: String unitsText = value.substring(suffixIndex);
088:
089: if (!UNITS_TEXT_TO_CONSTANT.containsKey(unitsText)) {
090: throw new IllegalArgumentException("Invalid Extent units: "
091: + value);
092: }
093: int units = ((Integer) UNITS_TEXT_TO_CONSTANT.get(unitsText))
094: .intValue();
095:
096: return new Extent(size, units);
097: }
098:
099: /**
100: * @see nextapp.echo2.app.componentxml.PropertyXmlPeer#getValue(java.lang.ClassLoader,
101: * java.lang.Class, org.w3c.dom.Element)
102: */
103: public Object getValue(ClassLoader classLoader, Class objectClass,
104: Element propertyElement) throws InvalidPropertyException {
105: return (toExtent(propertyElement.getAttribute("value")));
106: }
107: }
|