001 /*
002 * This file is part of the Echo Point Project. This project is a collection
003 * of Components that have extended the Echo Web Application Framework.
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 package echopoint.util;
030
031 import nextapp.echo.app.Extent;
032
033 /**
034 * <code>ExtentKit</code> is a utility class to help with Extent
035 * manipulation
036 */
037
038 public class ExtentKit {
039
040 /**
041 * Returns an new Extent object that represents the given Extent string
042 * in the form : <code> [integer][unit] </code>
043 * where unit is one of <code>[px | % | pt | cm | mm | in | em | ex | pc]</code>
044 * and no spaces are allowed betwwen the integer value and the
045 * unit. For example '5em', '12pt', '50%' or '22cm'.
046 *
047 * @param extentString - a valid extent string
048 * @return a new Extent
049 *
050 * @see ExtentKit#isExtent(String)
051 */
052 public static Extent makeExtent(String extentString) {
053 if (extentString == null)
054 throw new IllegalArgumentException("The extentString must be non null");
055
056 if (! isExtent(extentString))
057 throw new IllegalArgumentException("The extentString is invalid : " + extentString);
058
059 extentString = extentString.trim().toLowerCase();
060 String digitBit = _parseIntegerPrefix(extentString);
061 String unitBit = _parseIntegerPostfix(extentString);
062
063 int size = Integer.parseInt(digitBit);
064 int units = Extent.PX;
065 if (unitBit.equals("px"))
066 units = Extent.PX;
067 else if (unitBit.equals("%"))
068 units = Extent.PERCENT;
069 else if (unitBit.equals("pt"))
070 units = Extent.PT;
071 else if (unitBit.equals("cm"))
072 units = Extent.CM;
073 else if (unitBit.equals("mm"))
074 units = Extent.MM;
075 else if (unitBit.equals("in"))
076 units = Extent.IN;
077 else if (unitBit.equals("em"))
078 units = Extent.EM;
079 else if (unitBit.equals("ex"))
080 units = Extent.EX;
081 else if (unitBit.equals("pc"))
082 units = Extent.PC;
083
084 return new Extent(size,units);
085 }
086
087 /**
088 * Returns true if the string is in an extent in
089 * the form : <code> [integer][unit] </code>
090 * where unit is one of <code>[px | % | pt | cm | mm | in | em | ex | pc]</code>
091 * and no spaces are allowed betwwen the integer value and the
092 * unit. For example '5em', '12pt', '50%' or '22cm'.
093 */
094 public static boolean isExtent(String extentString) {
095 if (extentString == null)
096 return false;
097 extentString = extentString.trim().toLowerCase();
098 String digitBit = _parseIntegerPrefix(extentString);
099 String unitBit = _parseIntegerPostfix(extentString);
100 if (! _isInteger(digitBit))
101 return false;
102 if (! _isExtentUnit(unitBit))
103 return false;
104
105 return true;
106 }
107
108 private static boolean _isExtentUnit(String propertyValue) {
109 if (propertyValue.equals("px"))
110 return true;
111 if (propertyValue.equals("%"))
112 return true;
113 if (propertyValue.equals("pt"))
114 return true;
115 if (propertyValue.equals("cm"))
116 return true;
117 if (propertyValue.equals("mm"))
118 return true;
119 if (propertyValue.equals("in"))
120 return true;
121 if (propertyValue.equals("em"))
122 return true;
123 if (propertyValue.equals("ex"))
124 return true;
125 if (propertyValue.equals("pc"))
126 return true;
127 return false;
128 }
129
130 /*
131 * Well is it an integer?
132 */
133 private static boolean _isInteger(String propertyValue) {
134 try {
135 Integer.parseInt(propertyValue.trim());
136 } catch (NumberFormatException nfe) {
137 return false;
138 }
139 return true;
140 }
141
142 //
143 // returns the integer part of a string that starts with digits
144 //
145 private static String _parseIntegerPrefix(String propertyValue) {
146 int i = 0;
147 while (i < propertyValue.length()) {
148 if (! Character.isDigit(propertyValue.charAt(i))) {
149 break;
150 }
151 i++;
152 }
153 return propertyValue.substring(0,i);
154 }
155
156 //
157 // returns the string part of a string that starts with digits
158 //
159 private static String _parseIntegerPostfix(String propertyValue) {
160 int i = 0;
161 while (i < propertyValue.length()) {
162 if (! Character.isDigit(propertyValue.charAt(i))) {
163 break;
164 }
165 i++;
166 }
167 return propertyValue.substring(i,propertyValue.length());
168 }
169
170 }