KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > app > componentxml > propertypeer > ExtentPeer


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.app.componentxml.propertypeer;
31
32 import java.util.Collections JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Map JavaDoc;
35
36 import org.w3c.dom.Element JavaDoc;
37
38 import nextapp.echo2.app.Extent;
39 import nextapp.echo2.app.componentxml.InvalidPropertyException;
40 import nextapp.echo2.app.componentxml.PropertyXmlPeer;
41
42 /**
43  * <code>PropertyXmlPeer</code> implementation for
44  * <code>nextapp.echo2.app.Extent</code> properties.
45  */

46 public class ExtentPeer
47 implements PropertyXmlPeer {
48     
49     private static final Map JavaDoc UNITS_TEXT_TO_CONSTANT;
50     static {
51         Map JavaDoc constantMap = new HashMap JavaDoc();
52         constantMap.put("cm", new Integer JavaDoc(Extent.CM));
53         constantMap.put("em", new Integer JavaDoc(Extent.EM));
54         constantMap.put("ex", new Integer JavaDoc(Extent.EX));
55         constantMap.put("in", new Integer JavaDoc(Extent.IN));
56         constantMap.put("mm", new Integer JavaDoc(Extent.MM));
57         constantMap.put("pc", new Integer JavaDoc(Extent.PC));
58         constantMap.put("pt", new Integer JavaDoc(Extent.PT));
59         constantMap.put("px", new Integer JavaDoc(Extent.PX));
60         constantMap.put("%", new Integer JavaDoc(Extent.PERCENT));
61         UNITS_TEXT_TO_CONSTANT = Collections.unmodifiableMap(constantMap);
62     }
63     
64     /**
65      * Converts the given string value to an <code>Extent</code> equivalent.
66      *
67      * @param value a string representation of an <code>Extent</code>
68      * @return the <code>Extent</code>
69      * @throws IllegalArgumentException if the string is improperly formatted.
70      */

71     public static Extent toExtent(String JavaDoc 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                 // Not a digit (nor a minus at first position)
77
suffixIndex = i;
78                 break;
79             }
80         }
81         if (suffixIndex < 1) {
82             throw new IllegalArgumentException JavaDoc("Invalid Extent value: " + value);
83         }
84         
85         int size = Integer.parseInt(value.substring(0, suffixIndex));
86         String JavaDoc unitsText = value.substring(suffixIndex);
87         
88         if (!UNITS_TEXT_TO_CONSTANT.containsKey(unitsText)) {
89             throw new IllegalArgumentException JavaDoc("Invalid Extent units: " + value);
90         }
91         int units = ((Integer JavaDoc) UNITS_TEXT_TO_CONSTANT.get(unitsText)).intValue();
92         
93         return new Extent(size, units);
94     }
95
96     /**
97      * @see nextapp.echo2.app.componentxml.PropertyXmlPeer#getValue(java.lang.ClassLoader,
98      * java.lang.Class, org.w3c.dom.Element)
99      */

100     public Object JavaDoc getValue(ClassLoader JavaDoc classLoader, Class JavaDoc objectClass, Element JavaDoc propertyElement)
101     throws InvalidPropertyException {
102         return(toExtent(propertyElement.getAttribute("value")));
103     }
104 }
105
Popular Tags