| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. |
|---|
| 5 | * |
|---|
| 6 | * Redistribution and use in source and binary forms, with or without |
|---|
| 7 | * modification, are permitted provided that the following conditions are |
|---|
| 8 | * met: |
|---|
| 9 | * |
|---|
| 10 | * * Redistributions of source code must retain the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer. |
|---|
| 12 | * * Redistributions in binary form must reproduce the above |
|---|
| 13 | * copyright notice, this list of conditions and the following |
|---|
| 14 | * disclaimer in the documentation and/or other materials |
|---|
| 15 | * provided with the distribution. |
|---|
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of |
|---|
| 17 | * its contributors may be used to endorse or promote products |
|---|
| 18 | * derived from this software without specific prior written |
|---|
| 19 | * permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|---|
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 32 | */ |
|---|
| 33 | package org.jcoderz.commons.util; |
|---|
| 34 | |
|---|
| 35 | import java.math.BigInteger; |
|---|
| 36 | import java.util.Calendar; |
|---|
| 37 | |
|---|
| 38 | import javax.xml.bind.DatatypeConverter; |
|---|
| 39 | |
|---|
| 40 | import org.jcoderz.commons.types.Date; |
|---|
| 41 | |
|---|
| 42 | /** |
|---|
| 43 | * This class encapsulates util methods used for schema data type handling. |
|---|
| 44 | * |
|---|
| 45 | * @author Andreas Mandel |
|---|
| 46 | */ |
|---|
| 47 | public final class XsdUtil |
|---|
| 48 | { |
|---|
| 49 | /** No instances allowed. */ |
|---|
| 50 | private XsdUtil () |
|---|
| 51 | { |
|---|
| 52 | // utility class -- only static methods. |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | /** |
|---|
| 56 | * Parses the given string into a BigInteger. |
|---|
| 57 | * This method uses {@link BigInteger#BigInteger(java.lang.String)} to |
|---|
| 58 | * parse the string, but allows a optional leading '+' for positive |
|---|
| 59 | * values. |
|---|
| 60 | * @param str the string to be parsed. |
|---|
| 61 | * @return a BigInteger representing the same value as the string. |
|---|
| 62 | * @throws NumberFormatException if the string can not be parsed. |
|---|
| 63 | * @throws NullPointerException if the string is null. |
|---|
| 64 | */ |
|---|
| 65 | public static BigInteger integerFromString (String str) |
|---|
| 66 | throws NumberFormatException, NullPointerException |
|---|
| 67 | { |
|---|
| 68 | if (str.length() == 0) |
|---|
| 69 | { |
|---|
| 70 | throw new NumberFormatException(); |
|---|
| 71 | } |
|---|
| 72 | final char startChar = str.charAt(0); |
|---|
| 73 | // this is different in JDK1.4.2 vs. 1.5.0! |
|---|
| 74 | // we need to do it consistent, as defined with the schema spec. |
|---|
| 75 | if ((startChar == '+' || startChar == '-') |
|---|
| 76 | && str.length() > 1 |
|---|
| 77 | && (str.charAt(1) < '0' || str.charAt(1) > '9')) |
|---|
| 78 | { |
|---|
| 79 | throw new NumberFormatException(); |
|---|
| 80 | } |
|---|
| 81 | final String argument; |
|---|
| 82 | if (startChar == '+') |
|---|
| 83 | { |
|---|
| 84 | argument = str.substring(1); |
|---|
| 85 | } |
|---|
| 86 | else |
|---|
| 87 | { |
|---|
| 88 | argument = str; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | return new BigInteger(argument); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | /** |
|---|
| 95 | * Parses the given string into a int. |
|---|
| 96 | * This method uses {@link Integer#parseInt(java.lang.String)} to |
|---|
| 97 | * parse the string, but allows a optional leading '+' for positive |
|---|
| 98 | * values. |
|---|
| 99 | * @param str the string to be parsed. |
|---|
| 100 | * @return a int representing the same value as the string. |
|---|
| 101 | * @throws NumberFormatException if the string can not be parsed. |
|---|
| 102 | * @throws NullPointerException if the string is null. |
|---|
| 103 | */ |
|---|
| 104 | public static int intFromString (String str) |
|---|
| 105 | throws NumberFormatException, NullPointerException |
|---|
| 106 | { |
|---|
| 107 | final String argument; |
|---|
| 108 | if (str.startsWith("+") && str.length() > 1 |
|---|
| 109 | && str.charAt(1) != '-' && str.charAt(1) != '+') |
|---|
| 110 | { |
|---|
| 111 | argument = str.substring(1); |
|---|
| 112 | } |
|---|
| 113 | else |
|---|
| 114 | { |
|---|
| 115 | argument = str; |
|---|
| 116 | } |
|---|
| 117 | return Integer.parseInt(argument); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | /** |
|---|
| 121 | * Parses the given string into a long. |
|---|
| 122 | * This method uses {@link Long#parseLong(java.lang.String)} to |
|---|
| 123 | * parse the string, but allows a optional leading '+' for positive |
|---|
| 124 | * values. |
|---|
| 125 | * @param str the string to be parsed. |
|---|
| 126 | * @return a long representing the same value as the string. |
|---|
| 127 | * @throws NumberFormatException if the string can not be parsed. |
|---|
| 128 | * @throws NullPointerException if the string is null. |
|---|
| 129 | */ |
|---|
| 130 | public static long longFromString (String str) |
|---|
| 131 | throws NumberFormatException, NullPointerException |
|---|
| 132 | { |
|---|
| 133 | final String argument; |
|---|
| 134 | if (str.startsWith("+") && str.length() > 1 |
|---|
| 135 | && str.charAt(1) != '-' && str.charAt(1) != '+') |
|---|
| 136 | { |
|---|
| 137 | argument = str.substring(1); |
|---|
| 138 | } |
|---|
| 139 | else |
|---|
| 140 | { |
|---|
| 141 | argument = str; |
|---|
| 142 | } |
|---|
| 143 | return Long.parseLong(argument); |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | /** |
|---|
| 147 | * Checks if the given string complies to the XML schema token restrictions. |
|---|
| 148 | * <p> |
|---|
| 149 | * <b>XML Schema Definition:</b> token represents tokenized strings. |
|---|
| 150 | * The <i>value space</i> of token is the set of strings that do not |
|---|
| 151 | * contain the carriage return (#xD), line feed (#xA) nor tab (#x9) |
|---|
| 152 | * characters, that have no leading or trailing spaces (#x20) and |
|---|
| 153 | * that have no internal sequences of two or more spaces. |
|---|
| 154 | * For more information about the XML Schema datatype definition of |
|---|
| 155 | * a <code>token</code> see |
|---|
| 156 | * <a href="http://www.w3.org/TR/xmlschema-2/datatypes.html#token"> |
|---|
| 157 | * XML Schema datatype: token</a> |
|---|
| 158 | * @param token the string to be checked |
|---|
| 159 | * @return <code>true</code> if the given string complies to the |
|---|
| 160 | * XML schema token restrictions; <code>false</code> otherwise. |
|---|
| 161 | */ |
|---|
| 162 | public static boolean isValidToken (String token) |
|---|
| 163 | { |
|---|
| 164 | boolean result = true; |
|---|
| 165 | if (token == null) |
|---|
| 166 | { |
|---|
| 167 | result = false; |
|---|
| 168 | } |
|---|
| 169 | else |
|---|
| 170 | { |
|---|
| 171 | char lastChar = Constants.SPACE_CHAR; |
|---|
| 172 | for (int i = 0; i < token.length() && result; ++i) |
|---|
| 173 | { |
|---|
| 174 | final char currentChar = token.charAt(i); |
|---|
| 175 | result = isValidTokenCharacter(lastChar, currentChar); |
|---|
| 176 | lastChar = currentChar; |
|---|
| 177 | } |
|---|
| 178 | // trailing spaces? |
|---|
| 179 | if (lastChar == Constants.SPACE_CHAR && token.length() != 0) |
|---|
| 180 | { |
|---|
| 181 | result = false; |
|---|
| 182 | } |
|---|
| 183 | } |
|---|
| 184 | return result; |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | /** |
|---|
| 188 | * Checks if the given character complies to the XML schema token |
|---|
| 189 | * restrictions. |
|---|
| 190 | * The character is <b>not</b> valid XML schema token character if it |
|---|
| 191 | * is a carriage return (#xD), line feed (#xA) or tab (#x9) |
|---|
| 192 | * characters. This method also checks that there are no leading |
|---|
| 193 | * spaces (#x20) and that there are no internal sequences of |
|---|
| 194 | * two spaces. |
|---|
| 195 | * @return <code>true</code> if the given character complies to the |
|---|
| 196 | * XML schema token restrictions; <code>false</code> otherwise. |
|---|
| 197 | * @param lastChar the character before the current character. |
|---|
| 198 | * @param currentChar the current character in the character sequence. |
|---|
| 199 | */ |
|---|
| 200 | private static boolean isValidTokenCharacter ( |
|---|
| 201 | final char lastChar, final char currentChar) |
|---|
| 202 | { |
|---|
| 203 | boolean result = true; |
|---|
| 204 | switch (currentChar) |
|---|
| 205 | { |
|---|
| 206 | case Constants.CARRIAGE_RETURN_CHAR: |
|---|
| 207 | /* falls through */ |
|---|
| 208 | case Constants.LINE_FEED_CHAR: |
|---|
| 209 | /* falls through */ |
|---|
| 210 | case Constants.TAB_CHAR: |
|---|
| 211 | result = false; |
|---|
| 212 | break; |
|---|
| 213 | case Constants.SPACE_CHAR: |
|---|
| 214 | // leading spaces or sequence of two or more spaces? |
|---|
| 215 | if (lastChar == Constants.SPACE_CHAR) |
|---|
| 216 | { |
|---|
| 217 | result = false; |
|---|
| 218 | } |
|---|
| 219 | break; |
|---|
| 220 | default: |
|---|
| 221 | /* valid character */ |
|---|
| 222 | break; |
|---|
| 223 | } |
|---|
| 224 | return result; |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | /** |
|---|
| 228 | * Parses the given String as schema date time representation and returns |
|---|
| 229 | * a Date object holding the given time. |
|---|
| 230 | * This method must only be used after a JAXBContext has been initialized, |
|---|
| 231 | * otherwise it is likely that a NullpointerException is thrown. |
|---|
| 232 | * @param date the date time in schema dateTime |
|---|
| 233 | * @return a newly generated Date object representing the time given in the |
|---|
| 234 | * date. |
|---|
| 235 | */ |
|---|
| 236 | public static Date fromDateTimeString (String date) |
|---|
| 237 | { |
|---|
| 238 | final Calendar cal = DatatypeConverter.parseDateTime(date); |
|---|
| 239 | cal.setLenient(false); |
|---|
| 240 | return new Date(cal.getTimeInMillis()); |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | /** |
|---|
| 244 | * Parses the given String as schema date representation and returns |
|---|
| 245 | * a Date object holding the given time. |
|---|
| 246 | * This method must only be used after a JAXBContext has been initialized, |
|---|
| 247 | * otherwise it is likely that a NullpointerException is thrown. |
|---|
| 248 | * @param date the date in schema date representation |
|---|
| 249 | * @return a newly generated Date object representing the time given in the |
|---|
| 250 | * date. |
|---|
| 251 | */ |
|---|
| 252 | public static Date fromDateString (String date) |
|---|
| 253 | { |
|---|
| 254 | final Calendar cal = DatatypeConverter.parseDate(date); |
|---|
| 255 | cal.setLenient(false); |
|---|
| 256 | return new Date(cal.getTimeInMillis()); |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | } |
|---|