| 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 | |
|---|
| 37 | /** |
|---|
| 38 | * This class encapsulates util methods used for schema data type handling. |
|---|
| 39 | * |
|---|
| 40 | * @author Andreas Mandel |
|---|
| 41 | */ |
|---|
| 42 | public final class XsdUtil |
|---|
| 43 | { |
|---|
| 44 | /** No instances allowed. */ |
|---|
| 45 | private XsdUtil () |
|---|
| 46 | { |
|---|
| 47 | // utility class -- only static methods. |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | /** |
|---|
| 51 | * Parses the given string into a BigInteger. |
|---|
| 52 | * This method uses {@link BigInteger#BigInteger(java.lang.String)} to |
|---|
| 53 | * parse the string, but allows a optional leading '+' for positive |
|---|
| 54 | * values. |
|---|
| 55 | * @param str the string to be parsed. |
|---|
| 56 | * @return a BigInteger representing the same value as the string. |
|---|
| 57 | * @throws NumberFormatException if the string can not be parsed. |
|---|
| 58 | * @throws NullPointerException if the string is null. |
|---|
| 59 | */ |
|---|
| 60 | public static BigInteger integerFromString (String str) |
|---|
| 61 | throws NumberFormatException, NullPointerException |
|---|
| 62 | { |
|---|
| 63 | if (str.length() == 0) |
|---|
| 64 | { |
|---|
| 65 | throw new NumberFormatException(); |
|---|
| 66 | } |
|---|
| 67 | final char startChar = str.charAt(0); |
|---|
| 68 | // this is different in JDK1.4.2 vs. 1.5.0! |
|---|
| 69 | // we need to do it consistent, as defined with the schema spec. |
|---|
| 70 | if ((startChar == '+' || startChar == '-') |
|---|
| 71 | && str.length() > 1 |
|---|
| 72 | && (str.charAt(1) < '0' || str.charAt(1) > '9')) |
|---|
| 73 | { |
|---|
| 74 | throw new NumberFormatException(); |
|---|
| 75 | } |
|---|
| 76 | final String argument; |
|---|
| 77 | if (startChar == '+') |
|---|
| 78 | { |
|---|
| 79 | argument = str.substring(1); |
|---|
| 80 | } |
|---|
| 81 | else |
|---|
| 82 | { |
|---|
| 83 | argument = str; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | return new BigInteger(argument); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | /** |
|---|
| 90 | * Parses the given string into a int. |
|---|
| 91 | * This method uses {@link Integer#parseInt(java.lang.String)} to |
|---|
| 92 | * parse the string, but allows a optional leading '+' for positive |
|---|
| 93 | * values. |
|---|
| 94 | * @param str the string to be parsed. |
|---|
| 95 | * @return a int representing the same value as the string. |
|---|
| 96 | * @throws NumberFormatException if the string can not be parsed. |
|---|
| 97 | * @throws NullPointerException if the string is null. |
|---|
| 98 | */ |
|---|
| 99 | public static int intFromString (String str) |
|---|
| 100 | throws NumberFormatException, NullPointerException |
|---|
| 101 | { |
|---|
| 102 | final String argument; |
|---|
| 103 | if (str.startsWith("+") && str.length() > 1 |
|---|
| 104 | && str.charAt(1) != '-' && str.charAt(1) != '+') |
|---|
| 105 | { |
|---|
| 106 | argument = str.substring(1); |
|---|
| 107 | } |
|---|
| 108 | else |
|---|
| 109 | { |
|---|
| 110 | argument = str; |
|---|
| 111 | } |
|---|
| 112 | return Integer.parseInt(argument); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | /** |
|---|
| 116 | * Parses the given string into a long. |
|---|
| 117 | * This method uses {@link Long#parseLong(java.lang.String)} to |
|---|
| 118 | * parse the string, but allows a optional leading '+' for positive |
|---|
| 119 | * values. |
|---|
| 120 | * @param str the string to be parsed. |
|---|
| 121 | * @return a long representing the same value as the string. |
|---|
| 122 | * @throws NumberFormatException if the string can not be parsed. |
|---|
| 123 | * @throws NullPointerException if the string is null. |
|---|
| 124 | */ |
|---|
| 125 | public static long longFromString (String str) |
|---|
| 126 | throws NumberFormatException, NullPointerException |
|---|
| 127 | { |
|---|
| 128 | final String argument; |
|---|
| 129 | if (str.startsWith("+") && str.length() > 1 |
|---|
| 130 | && str.charAt(1) != '-' && str.charAt(1) != '+') |
|---|
| 131 | { |
|---|
| 132 | argument = str.substring(1); |
|---|
| 133 | } |
|---|
| 134 | else |
|---|
| 135 | { |
|---|
| 136 | argument = str; |
|---|
| 137 | } |
|---|
| 138 | return Long.parseLong(argument); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | /** |
|---|
| 142 | * Checks if the given string complies to the XML schema token restrictions. |
|---|
| 143 | * <p> |
|---|
| 144 | * <b>XML Schema Definition:</b> token represents tokenized strings. |
|---|
| 145 | * The <i>value space</i> of token is the set of strings that do not |
|---|
| 146 | * contain the carriage return (#xD), line feed (#xA) nor tab (#x9) |
|---|
| 147 | * characters, that have no leading or trailing spaces (#x20) and |
|---|
| 148 | * that have no internal sequences of two or more spaces. |
|---|
| 149 | * For more information about the XML Schema datatype definition of |
|---|
| 150 | * a <code>token</code> see |
|---|
| 151 | * <a href="http://www.w3.org/TR/xmlschema-2/datatypes.html#token"> |
|---|
| 152 | * XML Schema datatype: token</a> |
|---|
| 153 | * @param token the string to be checked |
|---|
| 154 | * @return <code>true</code> if the given string complies to the |
|---|
| 155 | * XML schema token restrictions; <code>false</code> otherwise. |
|---|
| 156 | */ |
|---|
| 157 | public static boolean isValidToken (String token) |
|---|
| 158 | { |
|---|
| 159 | boolean result = true; |
|---|
| 160 | if (token == null) |
|---|
| 161 | { |
|---|
| 162 | result = false; |
|---|
| 163 | } |
|---|
| 164 | else |
|---|
| 165 | { |
|---|
| 166 | char lastChar = Constants.SPACE_CHAR; |
|---|
| 167 | for (int i = 0; i < token.length() && result; ++i) |
|---|
| 168 | { |
|---|
| 169 | final char currentChar = token.charAt(i); |
|---|
| 170 | result = isValidTokenCharacter(lastChar, currentChar); |
|---|
| 171 | lastChar = currentChar; |
|---|
| 172 | } |
|---|
| 173 | // trailing spaces? |
|---|
| 174 | if (lastChar == Constants.SPACE_CHAR && token.length() != 0) |
|---|
| 175 | { |
|---|
| 176 | result = false; |
|---|
| 177 | } |
|---|
| 178 | } |
|---|
| 179 | return result; |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | /** |
|---|
| 183 | * Checks if the given character complies to the XML schema token |
|---|
| 184 | * restrictions. |
|---|
| 185 | * The character is <b>not</b> valid XML schema token character if it |
|---|
| 186 | * is a carriage return (#xD), line feed (#xA) or tab (#x9) |
|---|
| 187 | * characters. This method also checks that there are no leading |
|---|
| 188 | * spaces (#x20) and that there are no internal sequences of |
|---|
| 189 | * two spaces. |
|---|
| 190 | * @return <code>true</code> if the given character complies to the |
|---|
| 191 | * XML schema token restrictions; <code>false</code> otherwise. |
|---|
| 192 | * @param lastChar the character before the current character. |
|---|
| 193 | * @param currentChar the current character in the character sequence. |
|---|
| 194 | */ |
|---|
| 195 | private static boolean isValidTokenCharacter ( |
|---|
| 196 | final char lastChar, final char currentChar) |
|---|
| 197 | { |
|---|
| 198 | boolean result = true; |
|---|
| 199 | switch (currentChar) |
|---|
| 200 | { |
|---|
| 201 | case Constants.CARRIAGE_RETURN_CHAR: |
|---|
| 202 | /* falls through */ |
|---|
| 203 | case Constants.LINE_FEED_CHAR: |
|---|
| 204 | /* falls through */ |
|---|
| 205 | case Constants.TAB_CHAR: |
|---|
| 206 | result = false; |
|---|
| 207 | break; |
|---|
| 208 | case Constants.SPACE_CHAR: |
|---|
| 209 | // leading spaces or sequence of two or more spaces? |
|---|
| 210 | if (lastChar == Constants.SPACE_CHAR) |
|---|
| 211 | { |
|---|
| 212 | result = false; |
|---|
| 213 | } |
|---|
| 214 | break; |
|---|
| 215 | default: |
|---|
| 216 | /* valid character */ |
|---|
| 217 | break; |
|---|
| 218 | } |
|---|
| 219 | return result; |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | } |
|---|