Project Report: fawkez

Packagesummary org.jcoderz.commons.util

org.jcoderz.commons.util.XsdUtil

LineHitsNoteSource
1  /*
2   * $Id: XsdUtil.java 1392 2009-04-04 13:16:54Z amandel $
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 ()
510    {
52        // utility class -- only static methods.
530    }
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     {
68100       if (str.length() == 0)
69        {
70100          throw new NumberFormatException();
71        }
72100       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.
75100       if ((startChar == '+' || startChar == '-')
76              && str.length() > 1
77              && (str.charAt(1) < '0' || str.charAt(1) > '9'))
78        {
79100          throw new NumberFormatException();
80        }
81        final String argument;
82100       if (startChar == '+')
83        {
84100          argument = str.substring(1);
85        }
86        else
87        {
88100          argument = str;
89        }
90  
91100       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;
108100(1)      if (str.startsWith("+") && str.length() > 1
109              && str.charAt(1) != '-' && str.charAt(1) != '+')
110        {
111100          argument = str.substring(1);
112        }
113        else
114        {
115100          argument = str;
116        }
117100       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;
134100(2)      if (str.startsWith("+") && str.length() > 1
135              && str.charAt(1) != '-' && str.charAt(1) != '+')
136        {
137100          argument = str.substring(1);
138        }
139        else
140        {
141100          argument = str;
142        }
143100       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     {
164100       boolean result = true;
165100       if (token == null)
166        {
167100          result = false;
168        }
169        else
170        {
171100          char lastChar = Constants.SPACE_CHAR;
172100          for (int i = 0; i < token.length() && result; ++i)
173           {
174100             final char currentChar = token.charAt(i);
175100             result = isValidTokenCharacter(lastChar, currentChar);
176100             lastChar = currentChar;
177           }
178           // trailing spaces?
179100          if (lastChar == Constants.SPACE_CHAR && token.length() != 0)
180           {
181100             result = false;
182           }
183        }
184100       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     {
203100       boolean result = true;
204100       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:
211100             result = false;
212100             break;
213           case Constants.SPACE_CHAR:
214              // leading spaces or sequence of two or more spaces?
215100             if (lastChar == Constants.SPACE_CHAR)
216              {
217100                result = false;
218              }
219              break;
220           default:
221              /* valid character */
222              break;
223        }
224100       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     {
2380       final Calendar cal = DatatypeConverter.parseDateTime(date);
2390       cal.setLenient(false);
2400       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     {
2540       final Calendar cal = DatatypeConverter.parseDate(date);
2550       cal.setLenient(false);
2560       return new Date(cal.getTimeInMillis());
257     }
258     
259  }

Findings in this File

c (1) 108 : 11 This call to String.startsWith can be rewritten using String.charAt(0)
c (2) 134 : 11 This call to String.startsWith can be rewritten using String.charAt(0)