001: /*--
002: $Id: Util.java,v 1.1 2004/03/01 07:56:03 wolfpaulus Exp $
003:
004: Copyright (C) 2003-2007 Wolf Paulus.
005: All rights reserved.
006:
007: Redistribution and use in source and binary forms, with or without
008: modification, are permitted provided that the following conditions
009: are met:
010:
011: 1. Redistributions of source code must retain the above copyright
012: notice, this list of conditions, and the following disclaimer.
013:
014: 2. Redistributions in binary form must reproduce the above copyright
015: notice, this list of conditions, and the disclaimer that follows
016: these conditions in the documentation and/or other materials provided
017: with the distribution.
018:
019: 3. The end-user documentation included with the redistribution,
020: if any, must include the following acknowledgment:
021: "This product includes software developed by the
022: SWIXML Project (http://www.swixml.org/)."
023: Alternately, this acknowledgment may appear in the software itself,
024: if and wherever such third-party acknowledgments normally appear.
025:
026: 4. The name "Swixml" must not be used to endorse or promote products
027: derived from this software without prior written permission. For
028: written permission, please contact <info_AT_swixml_DOT_org>
029:
030: 5. Products derived from this software may not be called "Swixml",
031: nor may "Swixml" appear in their name, without prior written
032: permission from the Swixml Project Management.
033:
034: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
035: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
036: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
037: DISCLAIMED. IN NO EVENT SHALL THE SWIXML PROJECT OR ITS
038: CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
039: SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
040: LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
041: USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
042: ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
043: OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
044: OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
045: SUCH DAMAGE.
046: ====================================================================
047:
048: This software consists of voluntary contributions made by many
049: individuals on behalf of the Swixml Project and was originally
050: created by Wolf Paulus <wolf_AT_swixml_DOT_org>. For more information
051: on the Swixml Project, please see <http://www.swixml.org/>.
052: */
053:
054: package org.swixml.converters;
055:
056: import java.util.StringTokenizer;
057:
058: import org.jdom.Element;
059:
060: /**
061: * Util. Class with static helper methods
062: * @author <a href="mailto:wolf@paulus.com">Wolf Paulus</a>
063: * @version $Revision: 1.1 $
064:
065: */
066: public final class Util {
067: /**
068: * Returns the remaining tokens of a StringTokenizer in an int-Array
069: * @param st <code>StringTokenizer</code>
070: * @return <code>int[]</code> array containing the remaining tokens converted into int(s)
071: */
072: public static int[] ia(StringTokenizer st) {
073: int size = st != null ? st.countTokens() : 0;
074: int[] a = new int[size];
075: int i = 0;
076: while (st != null && st.hasMoreTokens()) {
077: try {
078: a[i] = Integer.parseInt(st.nextToken().trim());
079: i++;
080: } catch (NumberFormatException e) {
081: // no exceptiion handling required
082: }
083: }
084: int[] b = new int[i];
085: System.arraycopy(a, 0, b, 0, i);
086: return b;
087: }
088:
089: /**
090: * Returns the remaining tokens of a StringTokenizer in a double-Array
091: * @param st <code>StringTokenizer</code>
092: * @return <code>double[]</code> array containing the remaining tokens converted into double(s)
093: */
094: public static double[] da(StringTokenizer st) {
095: int size = st != null ? st.countTokens() : 0;
096: double[] a = new double[size];
097: int i = 0;
098: while (st != null && st.hasMoreTokens()) {
099: try {
100: a[i] = Double.parseDouble(st.nextToken().trim());
101: i++;
102: } catch (NumberFormatException e) {
103: // no exceptiion handling required
104: }
105: }
106: double[] b = new double[i];
107: System.arraycopy(a, 0, b, 0, i);
108: return b;
109: }
110:
111: /**
112: * Return a capitalized version of the specified property name.
113: *
114: * @param s <code>String</code> The property name
115: * @return <code>String</code> given String with 1st letter capitalized
116: */
117: public static final String capitalize(final String s) {
118: String cs = null;
119: if (s != null && 0 < s.length()) {
120: final char[] chars = s.toCharArray();
121: chars[0] = Character.toUpperCase(chars[0]);
122: cs = new String(chars);
123: }
124: return cs;
125: }
126:
127: /**
128: * Returns the integer value of the given XML attribute; or the default value.
129: */
130: public static final int getInteger(final Element element,
131: final String attr, int def) {
132: String value = element.getAttributeValue(attr);
133: if (value == null)
134: return def;
135:
136: try {
137: return Integer.parseInt(value.trim());
138: } catch (NumberFormatException e) {
139: // no exceptiion handling required
140: return def;
141: }
142: }
143: }
|