Update of /cvsroot/webmacro/webmacro/src/org/webmacro/util In directory sc8-pr-cvs1:/tmp/cvs-serv6675/src/org/webmacro/util Modified Files: AbstractLogFile.java AbstractScalableMap.java ArrayIterator.java Atomizer.java Bag.java Base64.java ByteBufferOutputStream.java CastUtil.java Clock.java ComponentMap.java DictionaryTool.java Encoder.java EncoderProvider.java EncodingCache.java EnumIterator.java Eval.java HTMLEscaper.java IntMap.java IntStack.java InvalidArgumentException.java LogFile.java LogSource.java LogSystem.java LogTarget.java LogTargetFactory.java Named.java NativeAsciiReader.java Pool.java PrimitiveArrayIterator.java PropertyMethod.java QueueWriter.java ScalableIdentityMap.java ScalableMap.java ScalablePool.java SelectList.java Settings.java SharedObject.java SharedReference.java SimpleHashMap.java SimpleIdentityMap.java SimpleMap.java SimpleMapFactory.java SimpleStack.java SparseArrayIterator.java SparseProperties.java StringArray.java SubSettings.java ThreadScheduler.java TimeLoop.java UPool.java WMEval.java Log Message: If this doesn't drive our SF "activity" stats through the roof, I don't know what will. Mass re-formatting of all code, following (to the best of my interpertation) the Sun (w/ jakarta tweaks) coding style guidelines defined here: http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html and here: http://jakarta.apache.org/turbine/common/code-standards.html I did this reformatting with IDEA 3.0.5, and I am going to commit the style configuration for IDEA (if I can find it). Index: AbstractLogFile.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/src/org/webmacro/util/AbstractLogFile.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** AbstractLogFile.java 11 Jun 2002 17:43:23 -0000 1.3 --- AbstractLogFile.java 12 Jun 2003 00:47:48 -0000 1.4 *************** *** 32,131 **** */ ! abstract public class AbstractLogFile implements LogTarget { ! protected Map _levels = new HashMap(); ! protected boolean _trace = false; ! protected int _defaultLevel = LogSystem.NOTICE; ! protected String _defaultFormatString = "{0,time}\t{1}\t{2}\t{3}"; ! protected String _formatString = _defaultFormatString; ! protected MessageFormat _mf; ! protected List _observers = new LinkedList(); ! protected String _name; ! /** ! * Create a new LogFile instance reading properties from the ! * supplied Settings object: <pre> ! * LogTraceExceptions: true|false|yes|no|on|off ! * LogLevel: ! */ ! public AbstractLogFile(Settings s) { ! _trace = s.getBooleanSetting("LogTraceExceptions"); ! String slevel = s.getSetting("LogLevel", "NOTICE"); ! _defaultLevel = LogSystem.getLevel(slevel); ! String format = s.getSetting("LogFormat"); ! if (format != null) ! _formatString = format; ! _mf = new MessageFormat(_formatString); ! Settings levels = new SubSettings(s, "LogLevel"); ! String[] keys = levels.getKeys(); ! for (int i = 0; i < keys.length; i++) ! _levels.put(keys[i], new Integer(LogSystem.getLevel( ! levels.getSetting(keys[i])))); ! } ! public AbstractLogFile() { ! _mf = new MessageFormat(_formatString); ! } ! public String toString() { ! return "LogFile(name=" + _name + ", level=" + _defaultLevel + ", trace=" + _trace + ")"; ! } ! /** ! * Set the log level for this Logfile. The default is LogSystem.NOTICE ! */ ! public void setLogLevel(int level) { ! _defaultLevel = level; ! Iterator i = _observers.iterator(); ! while (i.hasNext()) { ! LogSystem ls = (LogSystem) i.next(); ! ls.update(this, null); ! } ! } ! /** ! * Set the log level for a specific category name. ! */ ! public void setLogLevel(String name, int level) { ! _levels.put(name, new Integer(level)); ! Iterator i = _observers.iterator(); ! while (i.hasNext()) { ! LogSystem ls = (LogSystem) i.next(); ! ls.update(this, name); ! } ! } ! /** ! * Set whether this LogFile traces exceptions. The ! * default is false. ! */ ! public void setTraceExceptions(boolean trace) { ! _trace = trace; ! } ! public boolean subscribe(String category, String name, int level) { ! Integer ilevel = (Integer) _levels.get(name); ! boolean sub; ! if (ilevel != null) { ! sub = (ilevel.intValue() <= level); ! } ! else { ! sub = (_defaultLevel <= level); ! } ! return sub; ! } ! public void addObserver(LogSystem ls) { ! _observers.add(ls); ! } ! public void removeObserver(LogSystem ls) { ! _observers.remove(ls); ! } } --- 32,145 ---- */ ! abstract public class AbstractLogFile implements LogTarget ! { ! protected Map _levels = new HashMap(); ! protected boolean _trace = false; ! protected int _defaultLevel = LogSystem.NOTICE; ! protected String _defaultFormatString = "{0,time}\t{1}\t{2}\t{3}"; ! protected String _formatString = _defaultFormatString; ! protected MessageFormat _mf; ! protected List _observers = new LinkedList(); ! protected String _name; ! /** ! * Create a new LogFile instance reading properties from the ! * supplied Settings object: <pre> ! * LogTraceExceptions: true|false|yes|no|on|off ! * LogLevel: ! */ ! public AbstractLogFile (Settings s) ! { ! _trace = s.getBooleanSetting("LogTraceExceptions"); ! String slevel = s.getSetting("LogLevel", "NOTICE"); ! _defaultLevel = LogSystem.getLevel(slevel); ! String format = s.getSetting("LogFormat"); ! if (format != null) ! _formatString = format; ! _mf = new MessageFormat(_formatString); ! Settings levels = new SubSettings(s, "LogLevel"); ! String[] keys = levels.getKeys(); ! for (int i = 0; i < keys.length; i++) ! _levels.put(keys[i], new Integer(LogSystem.getLevel( ! levels.getSetting(keys[i])))); ! } ! public AbstractLogFile () ! { ! _mf = new MessageFormat(_formatString); ! } ! public String toString () ! { ! return "LogFile(name=" + _name + ", level=" + _defaultLevel + ", trace=" + _trace + ")"; ! } ! /** ! * Set the log level for this Logfile. The default is LogSystem.NOTICE ! */ ! public void setLogLevel (int level) ! { ! _defaultLevel = level; ! Iterator i = _observers.iterator(); ! while (i.hasNext()) ! { ! LogSystem ls = (LogSystem) i.next(); ! ls.update(this, null); ! } ! } ! /** ! * Set the log level for a specific category name. ! */ ! public void setLogLevel (String name, int level) ! { ! _levels.put(name, new Integer(level)); ! Iterator i = _observers.iterator(); ! while (i.hasNext()) ! { ! LogSystem ls = (LogSystem) i.next(); ! ls.update(this, name); ! } ! } ! /** ! * Set whether this LogFile traces exceptions. The ! * default is false. ! */ ! public void setTraceExceptions (boolean trace) ! { ! _trace = trace; ! } ! public boolean subscribe (String category, String name, int level) ! { ! Integer ilevel = (Integer) _levels.get(name); ! boolean sub; ! if (ilevel != null) ! { ! sub = (ilevel.intValue() <= level); ! } ! else ! { ! sub = (_defaultLevel <= level); ! } ! return sub; ! } ! public void addObserver (LogSystem ls) ! { ! _observers.add(ls); ! } ! public void removeObserver (LogSystem ls) ! { ! _observers.remove(ls); ! } } Index: AbstractScalableMap.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/src/org/webmacro/util/AbstractScalableMap.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** AbstractScalableMap.java 11 Jun 2002 17:43:23 -0000 1.2 --- AbstractScalableMap.java 12 Jun 2003 00:47:48 -0000 1.3 *************** *** 33,87 **** * as real maps. */ ! public class AbstractScalableMap implements SimpleMap { ! public static final int DEFAULT_FACTOR = 5; ! private final int factor; ! private final SimpleMap[] _maps; ! private int pos = 0; ! /** ! * Create a new scalable map, that uses ! * factor SimpleMaps. SimpleMap object are ! * created by mapFactory. ! * @param factor number of SimpleMaps to create ! * @param mapFactory object to create SimpleMap objects ! **/ ! public AbstractScalableMap(int factor, SimpleMapFactory mapFactory) { ! super(); ! this.factor = factor; ! _maps = new SimpleMap[factor]; ! for (int i = 0; i < factor; i++) { ! _maps[i] = mapFactory.createSimpleMap(); ! } ! } ! public AbstractScalableMap(SimpleMapFactory mapFactory) { ! this(DEFAULT_FACTOR, mapFactory); ! } ! public void put(final Object key, final Object value) { ! for (int i = 0; i < factor; i++) { ! _maps[i].put(key, value); ! } ! } ! public Object get(final Object key) { ! pos = (pos + 1) % factor; ! return _maps[pos].get(key); ! } ! public Object remove(final Object key) { ! Object o = null; ! for (int i = 0; i < _maps.length; i++) { ! o = _maps[i].remove(key); ! } ! return o; ! } ! public void clear() { ! for (int i = 0; i < _maps.length; i++) { ! _maps[i].clear(); ! } ! } } --- 33,98 ---- * as real maps. */ ! public class AbstractScalableMap implements SimpleMap ! { ! public static final int DEFAULT_FACTOR = 5; ! private final int factor; ! private final SimpleMap[] _maps; ! private int pos = 0; ! /** ! * Create a new scalable map, that uses ! * factor SimpleMaps. SimpleMap object are ! * created by mapFactory. ! * @param factor number of SimpleMaps to create ! * @param mapFactory object to create SimpleMap objects ! **/ ! public AbstractScalableMap (int factor, SimpleMapFactory mapFactory) ! { ! super(); ! this.factor = factor; ! _maps = new SimpleMap[factor]; ! for (int i = 0; i < factor; i++) ! { ! _maps[i] = mapFactory.createSimpleMap(); ! } ! } ! public AbstractScalableMap (SimpleMapFactory mapFactory) ! { ! this(DEFAULT_FACTOR, mapFactory); ! } ! public void put (final Object key, final Object value) ! { ! for (int i = 0; i < factor; i++) ! { ! _maps[i].put(key, value); ! } ! } ! public Object get (final Object key) ! { ! pos = (pos + 1) % factor; ! return _maps[pos].get(key); ! } ! public Object remove (final Object key) ! { ! Object o = null; ! for (int i = 0; i < _maps.length; i++) ! { ! o = _maps[i].remove(key); ! } ! return o; ! } ! public void clear () ! { ! for (int i = 0; i < _maps.length; i++) ! { ! _maps[i].clear(); ! } ! } } Index: ArrayIterator.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/src/org/webmacro/util/ArrayIterator.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** ArrayIterator.java 11 Jun 2002 17:43:23 -0000 1.6 --- ArrayIterator.java 12 Jun 2003 00:47:48 -0000 1.7 *************** *** 24,28 **** package org.webmacro.util; ! import java.util.*; --- 24,29 ---- package org.webmacro.util; ! import java.util.Iterator; ! import java.util.NoSuchElementException; *************** *** 30,88 **** * This provides an iterator interface to an array */ ! final public class ArrayIterator implements Iterator { ! final Object[] a; ! int pos; ! /** ! * Construct an iterator given an enumeration ! */ ! public ArrayIterator(Object[] array) { ! this.a = array; ! pos = 0; ! } ! /** ! * Return true if we have not yet reached the end of the enumeration ! */ ! final public boolean hasNext() { ! return (pos < a.length); ! } ! /** ! * Advance the iterator and return the next value. Return null if we ! * reach the end of the enumeration. ! */ ! final public Object next() throws NoSuchElementException { ! if (pos < a.length) { ! return a[pos++]; ! } ! else { ! throw new NoSuchElementException("Advanced beyond end of array"); ! } ! } ! /** ! * Unsupported ! */ ! final public void remove() throws UnsupportedOperationException { ! throw new UnsupportedOperationException(); ! } ! /** ! * Test harness ! */ ! static public void main(String arg[]) { ! try { ! Iterator i = new ArrayIterator(arg); ! while (i.hasNext()) { ! System.out.println("item: " + i.next()); ! } ! } ! catch (Exception e) { ! e.printStackTrace(); ! } ! } } --- 31,100 ---- * This provides an iterator interface to an array */ ! final public class ArrayIterator implements Iterator ! { ! final Object[] a; ! int pos; ! /** ! * Construct an iterator given an enumeration ! */ ! public ArrayIterator (Object[] array) ! { ! this.a = array; ! pos = 0; ! } ! /** ! * Return true if we have not yet reached the end of the enumeration ! */ ! final public boolean hasNext () ! { ! return (pos < a.length); ! } ! /** ! * Advance the iterator and return the next value. Return null if we ! * reach the end of the enumeration. ! */ ! final public Object next () throws NoSuchElementException ! { ! if (pos < a.length) ! { ! return a[pos++]; ! } ! else ! { ! throw new NoSuchElementException("Advanced beyond end of array"); ! } ! } ! /** ! * Unsupported ! */ ! final public void remove () throws UnsupportedOperationException ! { ! throw new UnsupportedOperationException(); ! } ! /** ! * Test harness ! */ ! static public void main (String arg[]) ! { ! try ! { ! Iterator i = new ArrayIterator(arg); ! while (i.hasNext()) ! { ! System.out.println("item: " + i.next()); ! } ! } ! catch (Exception e) ! { ! e.printStackTrace(); ! } ! } } Index: Atomizer.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/src/org/webmacro/util/Atomizer.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** Atomizer.java 11 Jun 2002 17:43:23 -0000 1.7 --- Atomizer.java 12 Jun 2003 00:47:48 -0000 1.8 *************** *** 24,28 **** package org.webmacro.util; ! import java.util.*; /** --- 24,29 ---- package org.webmacro.util; ! import java.util.HashMap; ! import java.util.Iterator; /** *************** *** 30,204 **** * that atomic number back into the original object. */ ! final public class Atomizer implements Cloneable { ! private HashMap _atoms; ! private Object[] _values; ! private Pool _freeAtoms = new ScalablePool(); ! private int _max = 0; ! /** ! * Create an atomizer with space for 100 atoms ! */ ! public Atomizer() { ! this(100); ! } ! /** ! * Create an atomizer with space for the specified number ! * of atoms. ! */ ! public Atomizer(int size) { ! _atoms = new HashMap((int) (size / .75 + 1)); ! _values = new Object[size]; ! } ! /** ! * Clone this Atomizer ! */ ! public Object clone() { ! try { ! Atomizer c = (Atomizer) super.clone(); ! c._atoms = (HashMap) _atoms.clone(); ! c._values = (Object[]) _values.clone(); ! c._freeAtoms = new ScalablePool(); ! return c; ! } ! catch (CloneNotSupportedException e) { ! // never gonna happen ! return null; ! } ! } ! /** ! * Get the atomic number for o. If o does not have an atomic ! * number return -1. ! */ ! public int lookup(Object o) { ! Integer atom = (Integer) _atoms.get(o); ! return (atom != null) ? atom.intValue() : -1; ! } ! /** ! * Put an object in the atomizer, return its atomic number. ! * You will need this atomic number later to access the object. ! * If the object is already in the atomizer you will get back ! * the same atomic number as last time. ! */ ! public int atomize(Object o) { ! Integer atom = (Integer) _atoms.get(o); ! int i; ! if (atom == null) { ! atom = (Integer) _freeAtoms.get(); ! if (atom == null) { ! atom = new Integer(_max++); ! } ! i = atom.intValue(); ! if (_max == _values.length) { ! int newM = _max * 2 + 1; ! Object[] newV = new Object[newM]; ! System.arraycopy(_values, 0, newV, 0, _max); ! _values = newV; ! } ! _values[i] = o; ! _atoms.put(o, atom); ! } ! else { ! i = atom.intValue(); ! } ! return i; ! } ! /** ! * Get the Object matching this atom ! */ ! public Object get(int atom) { ! try { ! return _values[atom]; ! } ! catch (ArrayIndexOutOfBoundsException e) { ! return null; ! } ! } ! /** ! * Remove an entry by atomic number, returning its former value. ! */ ! public Object remove(int atom) { ! try { ! Object o = _values[atom]; ! remove(o); ! return o; ! } ! catch (ArrayIndexOutOfBoundsException e) { ! return null; ! } ! } ! /** ! * Remove an entry by value ! */ ! public void remove(Object o) { ! if (o == null) { ! return; ! } ! Integer atom = (Integer) _atoms.remove(o); ! if (atom == null) { ! return; ! } ! _values[atom.intValue()] = null; ! _freeAtoms.put(atom); ! } ! /** ! * Get an iterator capable of walking through all the values ! * in the atomizer. ! */ ! public Iterator iterator() { ! return new Iter(_values); ! } ! /** ! * Test based on command line args ! */ ! public static void main(String arg[]) { ! Atomizer a = new Atomizer(3); ! int atoms[] = new int[arg.length]; ! for (int i = 0; i < arg.length; i++) { ! atoms[i] = a.atomize(arg[i]); ! System.out.println("Atomized " + arg[i] + " to " + atoms[i]); ! } ! for (int i = 0; i < atoms.length; i++) { ! System.out.println("Atom " + atoms[i] + " is " + a.get(atoms[i])); ! } ! Iterator iter = a.iterator(); ! while (iter.hasNext()) { ! Object o = iter.next(); ! System.out.println("RM: Object " + o + " was atom " + a.atomize(o)); ! iter.remove(); ! } ! atoms = new int[arg.length]; ! for (int i = 0; i < arg.length; i++) { ! atoms[i] = a.atomize(arg[i]); ! System.out.println("Atomized " + arg[i] + " to " + atoms[i]); ! } ! } ! class Iter extends SparseArrayIterator { ! Iter(Object values[]) { ! super(values); ! } ! public void remove() throws IllegalStateException { ! if (_last < 0) { ! throw new IllegalStateException("Remove called before next()"); ! } ! Atomizer.this.remove(this._values[this._last]); ! } ! } } --- 31,236 ---- * that atomic number back into the original object. */ ! final public class Atomizer implements Cloneable ! { ! private HashMap _atoms; ! private Object[] _values; ! private Pool _freeAtoms = new ScalablePool(); ! private int _max = 0; ! /** ! * Create an atomizer with space for 100 atoms ! */ ! public Atomizer () ! { ! this(100); ! } ! /** ! * Create an atomizer with space for the specified number ! * of atoms. ! */ ! public Atomizer (int size) ! { ! _atoms = new HashMap((int) (size / .75 + 1)); ! _values = new Object[size]; ! } ! /** ! * Clone this Atomizer ! */ ! public Object clone () ! { ! try ! { ! Atomizer c = (Atomizer) super.clone(); ! c._atoms = (HashMap) _atoms.clone(); ! c._values = (Object[]) _values.clone(); ! c._freeAtoms = new ScalablePool(); ! return c; ! } ! catch (CloneNotSupportedException e) ! { ! // never gonna happen ! return null; ! } ! } ! /** ! * Get the atomic number for o. If o does not have an atomic ! * number return -1. ! */ ! public int lookup (Object o) ! { ! Integer atom = (Integer) _atoms.get(o); ! return (atom != null) ? atom.intValue() : -1; ! } ! /** ! * Put an object in the atomizer, return its atomic number. ! * You will need this atomic number later to access the object. ! * If the object is already in the atomizer you will get back ! * the same atomic number as last time. ! */ ! public int atomize (Object o) ! { ! Integer atom = (Integer) _atoms.get(o); ! int i; ! if (atom == null) ! { ! atom = (Integer) _freeAtoms.get(); ! if (atom == null) ! { ! atom = new Integer(_max++); ! } ! i = atom.intValue(); ! if (_max == _values.length) ! { ! int newM = _max * 2 + 1; ! Object[] newV = new Object[newM]; ! System.arraycopy(_values, 0, newV, 0, _max); ! _values = newV; ! } ! _values[i] = o; ! _atoms.put(o, atom); ! } ! else ! { ! i = atom.intValue(); ! } ! return i; ! } ! /** ! * Get the Object matching this atom ! */ ! public Object get (int atom) ! { ! try ! { ! return _values[atom]; ! } ! catch (ArrayIndexOutOfBoundsException e) ! { ! return null; ! } ! } ! /** ! * Remove an entry by atomic number, returning its former value. ! */ ! public Object remove (int atom) ! { ! try ! { ! Object o = _values[atom]; ! remove(o); ! return o; ! } ! catch (ArrayIndexOutOfBoundsException e) ! { ! return null; ! } ! } ! /** ! * Remove an entry by value ! */ ! public void remove (Object o) ! { ! if (o == null) ! { ! return; ! } ! Integer atom = (Integer) _atoms.remove(o); ! if (atom == null) ! { ! return; ! } ! _values[atom.intValue()] = null; ! _freeAtoms.put(atom); ! } ! /** ! * Get an iterator capable of walking through all the values ! * in the atomizer. ! */ ! public Iterator iterator () ! { ! return new Iter(_values); ! } ! /** ! * Test based on command line args ! */ ! public static void main (String arg[]) ! { ! Atomizer a = new Atomizer(3); ! int atoms[] = new int[arg.length]; ! for (int i = 0; i < arg.length; i++) ! { ! atoms[i] = a.atomize(arg[i]); ! System.out.println("Atomized " + arg[i] + " to " + atoms[i]); ! } ! for (int i = 0; i < atoms.length; i++) ! { ! System.out.println("Atom " + atoms[i] + " is " + a.get(atoms[i])); ! } ! Iterator iter = a.iterator(); ! while (iter.hasNext()) ! { ! Object o = iter.next(); ! System.out.println("RM: Object " + o + " was atom " + a.atomize(o)); ! iter.remove(); ! } ! atoms = new int[arg.length]; ! for (int i = 0; i < arg.length; i++) ! { ! atoms[i] = a.atomize(arg[i]); ! System.out.println("Atomized " + arg[i] + " to " + atoms[i]); ! } ! } ! class Iter extends SparseArrayIterator ! { ! Iter (Object values[]) ! { ! super(values); ! } ! public void remove () throws IllegalStateException ! { ! if (_last < 0) ! { ! throw new IllegalStateException("Remove called before next()"); ! } ! Atomizer.this.remove(this._values[this._last]); ! } ! } } Index: Bag.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/src/org/webmacro/util/Bag.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** Bag.java 11 Jun 2002 17:43:23 -0000 1.5 --- Bag.java 12 Jun 2003 00:47:48 -0000 1.6 *************** *** 31,50 **** * a dictionary, but similar in nature. */ ! public interface Bag { ! /** ! * Add an item to the bag ! */ ! public void put(String itemName, Object item) throws UnsettableException; ! /** ! * Get an item from the bag ! */ ! public Object get(String itemName); ! /** ! * Remove an item from the bag ! */ ! public void remove(String itemName) throws UnsettableException; --- 31,51 ---- * a dictionary, but similar in nature. */ ! public interface Bag ! { ! /** ! * Add an item to the bag ! */ ! public void put (String itemName, Object item) throws UnsettableException; ! /** ! * Get an item from the bag ! */ ! public Object get (String itemName); ! /** ! * Remove an item from the bag ! */ ! public void remove (String itemName) throws UnsettableException; Index: Base64.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/src/org/webmacro/util/Base64.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** Base64.java 12 Jun 2002 17:17:28 -0000 1.4 --- Base64.java 12 Jun 2003 00:47:48 -0000 1.5 *************** *** 24,289 **** package org.webmacro.util; ! final public class Base64 { ! ! private static final byte UPPER_FOUR = (byte) (16 + 32 + 64 + 128); ! private static final byte LOWER_FOUR = (byte) (1 + 2 + 4 + 8); ! private static final byte UPPER_SIX = (byte) (4 + 8 + 16 + 32 + 64 + 128); ! private static final byte LOWER_TWO = (byte) (1 + 2); ! private static final byte UPPER_TWO = (byte) (64 + 128); ! private static final byte LOWER_SIX = (byte) (1 + 2 + 4 + 8 + 16 + 32); ! ! /** ! * Get the plain text version of a base64 encoded string ! */ - final public static String decode(String encoded) { - return decode(encoded.getBytes()); - } ! /** ! * Get the base64 encoded version of a plain text String ! */ ! final public static String encode(String plainText) { ! return encode(plainText.getBytes()); ! } ! /** ! * Ge tthe plain text version of a base64 encoded byte array ! */ ! final public static String decode(byte[] encoded) { ! byte[] plain = new byte[(int) (encoded.length * 0.75) + 2]; ! byte code, ptext; ! int ppos = 0; ! int epos = 0; ! boolean cutShort = false; ! while (epos < encoded.length) { ! /* ! * base64 decoding: turn 4*6 bits into 3*8 bits via the pattern: ! * xx111111 xx112222 xx222233 xx333333 (xx: high bits unused) ! */ ! code = sixBits(encoded[epos]); ! // 1st byte: lower six bits from 1st char ! ptext = (byte) (code << 2); - // 1st byte: plus upper two (of six) bits from 2nd char - epos++; - try { code = sixBits(encoded[epos]); - } - catch (Exception e) { - code = 0; // too few chars, assume missing pad - cutShort = true; - } - ptext |= (code & UPPER_FOUR) >>> 4; - plain[ppos++] = ptext; - if (cutShort) { - break; // loop - } ! // 2nd byte: lower four bytes from 2nd char ! ptext = (byte) ((code & LOWER_FOUR) << 4); - // 2nd byte: plus upper four (of six) bits from 3rd char - epos++; - try { - code = sixBits(encoded[epos]); - } - catch (Exception e) { - code = 0; // too few chars, assume missing pad - } - ptext |= (code & UPPER_SIX) >>> 2; - plain[ppos++] = ptext; - if (cutShort) { - break; // loop - } ! // 3rd byte: lower two bits of 3rd char ! ptext = (byte) ((code & LOWER_TWO) << 6); ! // 3rd byte: lower six bits of fourth char ! epos++; ! try { ! code = sixBits(encoded[epos]); ! } ! catch (Exception e) { ! code = 0; // too few chars, assume missing pad ! } ! ptext |= code; ! plain[ppos++] = ptext; ! // advance loop ! epos++; ! } ! return new String(plain); ! } - /** - * Get the base64 encoded version of a plain text byte array - */ - final static public String encode(byte[] plain) { ! /* ! * base64 encoding: turn 3*8 bits into 4*6 bits via the pattern: ! * 111111 112222 222233 333333 (xx: high bits unused) ! * UPPER6 LOWER2 LOWER4 LOWER6 ! * UPPER4 UPPER2 ! **/ ! StringBuffer encoded = new StringBuffer((int) (plain.length * 1.34) + 1); ! byte ptext; ! byte sixbits; ! int ppos = 0; ! boolean cutShort = false; ! while (ppos < plain.length) { ! // first char: upper 6 bytes ! ptext = plain[ppos]; ! sixbits = (byte) ((ptext & UPPER_SIX) >>> 2); ! encoded.append(base64(sixbits)); ! // second char: lower 2, upper 4 ! sixbits = (byte) ((ptext & LOWER_TWO) << 4); ! ppos++; ! try { ptext = plain[ppos]; ! } ! catch (ArrayIndexOutOfBoundsException e) { ! ptext = 0; ! cutShort = true; ! } ! sixbits |= (byte) ((ptext & UPPER_FOUR) >>> 4); ! encoded.append(base64(sixbits)); ! if (cutShort) { ! encoded.append("=="); ! return encoded.toString(); ! } ! // third char: lower four, upper 2 ! sixbits = (byte) ((ptext & LOWER_FOUR) << 2); ! ppos++; ! try { ! ptext = plain[ppos]; ! } ! catch (ArrayIndexOutOfBoundsException e) { ! ptext = 0; ! cutShort = true; ! } ! sixbits |= (byte) ((ptext & UPPER_TWO) >>> 6); ! encoded.append(base64(sixbits)); ! if (cutShort) { ! encoded.append("="); ! return encoded.toString(); ! } ! // fourth char: lower six ! sixbits = (byte) (ptext & LOWER_SIX); ! encoded.append(base64(sixbits)); ! // increment loop ! ppos++; ! } ! return encoded.toString(); ! } ! /** ! * Translate a character in the base64 alphabet into a byte with ! * the corresponding bits set (ie: a number from 0 to 64). ! * @return the base64 value, or 0 for the special '=' pad character ! * @param base64 the character to be translated ! * @exception NumberFormatException if base64 is not a base64 character ! */ ! private static byte sixBits(byte base64) ! throws NumberFormatException { - if ((base64 >= 'A') && (base64 <= 'Z')) { - return (byte) (base64 - 'A'); - } ! if ((base64 >= 'a') && (base64 <= 'z')) { ! return (byte) (base64 - 'a' + 26); ! } ! if ((base64 >= '0') && (base64 <= '9')) { ! return (byte) (base64 - '0' + 52); ! } ! if (base64 == '+') { ! return 62; ! } ! if (base64 == '/') { ! return 63; ! } ! if (base64 == '=') { ! return 0; // pad means zeroed bits FIXME: i am not sure, really? ! } ! throw new NumberFormatException("Not a base64 character: " + base64); ! } ! /** ! * Turn a six-bit value into a base64 digit ! */ ! static private char base64(byte sixBits) { ! if (sixBits <= 25) { // less/equal base64 'Z' ! return (char) ('A' + sixBits); ! } ! if (sixBits <= 51) { // less/equal base64 'z' ! return (char) ('a' + sixBits - 26); // count from base64 'a' ! } ! if (sixBits <= 61) { // less/equal base64 '9' ! return (char) ('0' + sixBits - 52); // count from base64 '0' ! } ! if (sixBits == 62) { ! return '+'; ! } ! if (sixBits == 63) { ! return '/'; ! } ! throw new NumberFormatException("Not a base64 digit: " + sixBits); ! } ! /** ! * Test harness ! */ ! public static void main(String arg[]) { - boolean encode; ! if (arg.length < 2) { ! System.out.println("Usage: Base64 encode|decode string"); ! return; ! } ! if (arg[0].equals("encode")) { ! encode = true; ! } ! else if (arg[0].equals("decode")) { ! encode = false; ! } ! else { ! System.out.println("Unrecognized argument: " + arg[0]); ! return; ! } ! System.out.println(encode ? encode(arg[1]) : decode(arg[1])); ! } } --- 24,328 ---- package org.webmacro.util; ! final public class Base64 ! { ! private static final byte UPPER_FOUR = (byte) (16 + 32 + 64 + 128); ! private static final byte LOWER_FOUR = (byte) (1 + 2 + 4 + 8); ! private static final byte UPPER_SIX = (byte) (4 + 8 + 16 + 32 + 64 + 128); ! private static final byte LOWER_TWO = (byte) (1 + 2); ! private static final byte UPPER_TWO = (byte) (64 + 128); ! private static final byte LOWER_SIX = (byte) (1 + 2 + 4 + 8 + 16 + 32); ! /** ! * Get the plain text version of a base64 encoded string ! */ ! final public static String decode (String encoded) ! { ! return decode(encoded.getBytes()); ! } ! /** ! * Get the base64 encoded version of a plain text String ! */ ! final public static String encode (String plainText) ! { ! return encode(plainText.getBytes()); ! } ! /** ! * Ge tthe plain text version of a base64 encoded byte array ! */ ! final public static String decode (byte[] encoded) ! { ! byte[] plain = new byte[(int) (encoded.length * 0.75) + 2]; ! byte code, ptext; ! int ppos = 0; ! int epos = 0; ! boolean cutShort = false; ! while (epos < encoded.length) ! { ! /* ! * base64 decoding: turn 4*6 bits into 3*8 bits via the pattern: ! * xx111111 xx112222 xx222233 xx333333 (xx: high bits unused) ! */ code = sixBits(encoded[epos]); + // 1st byte: lower six bits from 1st char + ptext = (byte) (code << 2); ! // 1st byte: plus upper two (of six) bits from 2nd char ! epos++; ! try ! { ! code = sixBits(encoded[epos]); ! } ! catch (Exception e) ! { ! code = 0; // too few chars, assume missing pad ! cutShort = true; ! } ! ptext |= (code & UPPER_FOUR) >>> 4; ! plain[ppos++] = ptext; ! if (cutShort) ! { ! break; // loop ! } ! // 2nd byte: lower four bytes from 2nd char ! ptext = (byte) ((code & LOWER_FOUR) << 4); ! // 2nd byte: plus upper four (of six) bits from 3rd char ! epos++; ! try ! { ! code = sixBits(encoded[epos]); ! } ! catch (Exception e) ! { ! code = 0; // too few chars, assume missing pad ! } ! ptext |= (code & UPPER_SIX) >>> 2; ! plain[ppos++] = ptext; ! if (cutShort) ! { ! break; // loop ! } + // 3rd byte: lower two bits of 3rd char + ptext = (byte) ((code & LOWER_TWO) << 6); ! // 3rd byte: lower six bits of fourth char ! epos++; ! try ! { ! code = sixBits(encoded[epos]); ! } ! catch (Exception e) ! { ! code = 0; // too few chars, assume missing pad ! } ! ptext |= code; ! plain[ppos++] = ptext; ! // advance loop ! epos++; ! } ! return new String(plain); ! } ! /** ! * Get the base64 encoded version of a plain text byte array ! */ ! final static public String encode (byte[] plain) ! { ! /* ! * base64 encoding: turn 3*8 bits into 4*6 bits via the pattern: ! * 111111 112222 222233 333333 (xx: high bits unused) ! * UPPER6 LOWER2 LOWER4 LOWER6 ! * UPPER4 UPPER2 ! **/ ! StringBuffer encoded = new StringBuffer((int) (plain.length * 1.34) + 1); ! byte ptext; ! byte sixbits; ! int ppos = 0; ! boolean cutShort = false; ! while (ppos < plain.length) ! { ! // first char: upper 6 bytes ptext = plain[ppos]; ! sixbits = (byte) ((ptext & UPPER_SIX) >>> 2); ! encoded.append(base64(sixbits)); ! // second char: lower 2, upper 4 ! sixbits = (byte) ((ptext & LOWER_TWO) << 4); ! ppos++; ! try ! { ! ptext = plain[ppos]; ! } ! catch (ArrayIndexOutOfBoundsException e) ! { ! ptext = 0; ! cutShort = true; ! } ! sixbits |= (byte) ((ptext & UPPER_FOUR) >>> 4); ! encoded.append(base64(sixbits)); ! if (cutShort) ! { ! encoded.append("=="); ! return encoded.toString(); ! } ! // third char: lower four, upper 2 ! sixbits = (byte) ((ptext & LOWER_FOUR) << 2); ! ppos++; ! try ! { ! ptext = plain[ppos]; ! } ! catch (ArrayIndexOutOfBoundsException e) ! { ! ptext = 0; ! cutShort = true; ! } ! sixbits |= (byte) ((ptext & UPPER_TWO) >>> 6); ! encoded.append(base64(sixbits)); ! if (cutShort) ! { ! encoded.append("="); ! return encoded.toString(); ! } + // fourth char: lower six + sixbits = (byte) (ptext & LOWER_SIX); + encoded.append(base64(sixbits)); ! // increment loop ! ppos++; ! } ! return encoded.toString(); ! } ! /** ! * Translate a character in the base64 alphabet into a byte with ! * the corresponding bits set (ie: a number from 0 to 64). ! * @return the base64 value, or 0 for the special '=' pad character ! * @param base64 the character to be translated ! * @exception NumberFormatException if base64 is not a base64 character ! */ ! private static byte sixBits (byte base64) ! throws NumberFormatException ! { ! if ((base64 >= 'A') && (base64 <= 'Z')) ! { ! return (byte) (base64 - 'A'); ! } ! if ((base64 >= 'a') && (base64 <= 'z')) ! { ! return (byte) (base64 - 'a' + 26); ! } ! if ((base64 >= '0') && (base64 <= '9')) ! { ! return (byte) (base64 - '0' + 52); ! } ! if (base64 == '+') ! { ! return 62; ! } ! if (base64 == '/') ! { ! return 63; ! } ! if (base64 == '=') ! { ! return 0; // pad means zeroed bits FIXME: i am not sure, really? ! } ! throw new NumberFormatException("Not a base64 character: " + base64); ! } ! /** ! * Turn a six-bit value into a base64 digit ! */ ! static private char base64 (byte sixBits) ! { ! if (sixBits <= 25) ! { // less/equal base64 'Z' ! return (char) ('A' + sixBits); ! } ! if (sixBits <= 51) ! { // less/equal base64 'z' ! return (char) ('a' + sixBits - 26); // count from base64 'a' ! } ! if (sixBits <= 61) ! { // less/equal base64 '9' ! return (char) ('0' + sixBits - 52); // count from base64 '0' ! } ! if (sixBits == 62) ! { ! return '+'; ! } + if (sixBits == 63) + { + return '/'; + } ! throw new NumberFormatException("Not a base64 digit: " + sixBits); ! } ! /** ! * Test harness ! */ ! public static void main (String arg[]) ! { ! boolean encode; ! if (arg.length < 2) ! { ! System.out.println("Usage: Base64 encode|decode string"); ! return; ! } ! if (arg[0].equals("encode")) ! { ! encode = true; ! } ! else if (arg[0].equals("decode")) ! { ! encode = false; ! } ! else ! { ! System.out.println("Unrecognized argument: " + arg[0]); ! return; ! } ! ! System.out.println(encode ? encode(arg[1]) : decode(arg[1])); ! ! } } Index: ByteBufferOutputStream.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/src/org/webmacro/util/ByteBufferOutputStream.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** ByteBufferOutputStream.java 11 Jun 2002 17:43:23 -0000 1.6 --- ByteBufferOutputStream.java 12 Jun 2003 00:47:48 -0000 1.7 *************** *** 24,192 **** package org.webmacro.util; ! import java.io.*; ! final public class ByteBufferOutputStream extends OutputStream { ! private byte[] _buf; ! private int _pos; ! private final int _initialSize; ! /** ! * Create a new ByteBuffer with the specified capacity ! */ ! public ByteBufferOutputStream(int size) { ! _buf = new byte[size]; ! _pos = 0; ! _initialSize = size; ! } ! /** ! * Clear the contents of the byte buffer. Also shrinks the byte buffer ! * to the size specified during construction of this ByteBufferOutputStream ! */ ! public void reset() { ! _pos = 0; ! _buf = new byte[_initialSize]; ! } ! public void write(int i) { ! write((byte) i); ! } ! /** ! * Copy an array of bytes on to the end of the buffer ! */ ! public void write(byte[] b) { ! int len = b.length; ! ensureCapacity(len); ! System.arraycopy(b, 0, _buf, _pos, len); ! _pos += len; ! } ! /** ! * Copy an array of bytes on to the end of the buffer ! */ ! public void write(byte[] b, int offset, int len) { ! ensureCapacity(len); ! System.arraycopy(b, 0, _buf, _pos, len); ! _pos += len; ! } ! /** ! * Append a single byte ! */ ! public void write(byte b) { ! ensureCapacity(1); ! _buf[_pos] = b; ! _pos++; ! } ! /** ! * Make sure the buffer contains space for len more bytes. ! */ ! public final void ensureCapacity(int len) { ! if (_buf.length < _pos + len) { ! int blen = _buf.length; ! while (blen < _pos + len) { ! blen *= 2; ! } ! byte[] tmp = new byte[blen]; ! System.arraycopy(_buf, 0, tmp, 0, _pos); ! _buf = tmp; ! } ! } ! /** ! * How many bytes currently in the buffer ! */ ! public int size() { ! return _pos; ! } ! /** ! * Get the bytes in the buffer. Note that you are getting the ! * live buffer. You also need to call size() to find out how ! * many of these bytes are significant. If you just want a ! * byte array call getBytes() instead--that will allocate a ! * new one for you. ! */ ! public byte[] getBuffer() { ! return _buf; ! } ! /** ! * Allocate a new byte[] and fill it with the contents of the ! * current byte buffer. If you want the live byte buffer instead ! * of this newly allocated copy call getBuffer() instead. ! */ ! public byte[] getBytes() { ! byte[] ret = new byte[_pos]; ! System.arraycopy(_buf, 0, ret, 0, _pos); ! return ret; ! } ! /** ! * Convert the bytes to a String using the default encoding ! */ ! public String toString() { ! return new String(_buf, 0, _pos); ! } ! /** ! * Convert the bytes to a String using the specified encodign ! */ ! public String toString(String encoding) throws UnsupportedEncodingException { ! return new String(_buf, 0, _pos, encoding); ! } ! /** ! * Write the bytes to the specified output stream ! */ ! public void writeTo(OutputStream out) throws IOException { ! out.write(_buf, 0, _pos); ! } ! public static void main(String arg[]) { ! try { ! ByteBufferOutputStream bb = new ByteBufferOutputStream(5); ! bb.write((byte) 'T'); ! bb.write((byte) 'h'); ! bb.write((byte) 'i'); ! bb.write((byte) 's'); ! bb.write((byte) ' '); ! bb.write((byte) 'i'); ! bb.write((byte) 's'); ! bb.write((byte) ' '); ! bb.write((byte) 'a'); ! bb.write((byte) ' '); ! bb.write((byte) 't'); ! bb.write((byte) 'e'); ! bb.write((byte) 's'); ! bb.write((byte) 't'); ! bb.write((byte) '\n'); ! bb.write("This is the second line of the byte buffer".getBytes()); ! bb.write((byte) '\n'); ! bb.write("This is the third line\n".getBytes()); ! bb.write("This is the fourth line\n".getBytes()); ! bb.write((byte) 'E'); ! bb.write((byte) 'N'); ! bb.write((byte) 'D'); ! bb.write((byte) '.'); ! bb.write((byte) '\n'); ! System.out.println("Byte buffer as a string: [" + bb + "]"); ! System.out.print("Byte buffer written to a stream: ["); ! bb.writeTo(System.out); ! System.out.print("]"); ! System.out.println("DONE"); ! } ! catch (Exception e) { ! e.printStackTrace(); ! } ! } } --- 24,213 ---- package org.webmacro.util; ! import java.io.IOException; ! import java.io.OutputStream; ! import java.io.UnsupportedEncodingException; ! final public class ByteBufferOutputStream extends OutputStream ! { ! private byte[] _buf; ! private int _pos; ! private final int _initialSize; ! /** ! * Create a new ByteBuffer with the specified capacity ! */ ! public ByteBufferOutputStream (int size) ! { ! _buf = new byte[size]; ! _pos = 0; ! _initialSize = size; ! } ! /** ! * Clear the contents of the byte buffer. Also shrinks the byte buffer ! * to the size specified during construction of this ByteBufferOutputStream ! */ ! public void reset () ! { ! _pos = 0; ! _buf = new byte[_initialSize]; ! } ! public void write (int i) ! { ! write((byte) i); ! } ! /** ! * Copy an array of bytes on to the end of the buffer ! */ ! public void write (byte[] b) ! { ! int len = b.length; ! ensureCapacity(len); ! System.arraycopy(b, 0, _buf, _pos, len); ! _pos += len; ! } ! /** ! * Copy an array of bytes on to the end of the buffer ! */ ! public void write (byte[] b, int offset, int len) ! { ! ensureCapacity(len); ! System.arraycopy(b, 0, _buf, _pos, len); ! _pos += len; ! } ! /** ! * Append a single byte ! */ ! public void write (byte b) ! { ! ensureCapacity(1); ! _buf[_pos] = b; ! _pos++; ! } ! /** ! * Make sure the buffer contains space for len more bytes. ! */ ! public final void ensureCapacity (int len) ! { ! if (_buf.length < _pos + len) ! { ! int blen = _buf.length; ! while (blen < _pos + len) ! { ! blen *= 2; ! } ! byte[] tmp = new byte[blen]; ! System.arraycopy(_buf, 0, tmp, 0, _pos); ! _buf = tmp; ! } ! } ! /** ! * How many bytes currently in the buffer ! */ ! public int size () ! { ! return _pos; ! } ! /** ! * Get the bytes in the buffer. Note that you are getting the ! * live buffer. You also need to call size() to find out how ! * many of these bytes are significant. If you just want a ! * byte array call getBytes() instead--that will allocate a ! * new one for you. ! */ ! public byte[] getBuffer () ! { ! return _buf; ! } ! /** ! * Allocate a new byte[] and fill it with the contents of the ! * current byte buffer. If you want the live byte buffer instead ! * of this newly allocated copy call getBuffer() instead. ! */ ! public byte[] getBytes () ! { ! byte[] ret = new byte[_pos]; ! System.arraycopy(_buf, 0, ret, 0, _pos); ! return ret; ! } ! /** ! * Convert the bytes to a String using the default encoding ! */ ! public String toString () ! { ! return new String(_buf, 0, _pos); ! } ! /** ! * Convert the bytes to a String using the specified encodign ! */ ! public String toString (String encoding) throws UnsupportedEncodingException ! { ! return new String(_buf, 0, _pos, encoding); ! } ! /** ! * Write the bytes to the specified output stream ! */ ! public void writeTo (OutputStream out) throws IOException ! { ! out.write(_buf, 0, _pos); ! } ! public static void main (String arg[]) ! { ! try ! { ! ByteBufferOutputStream bb = new ByteBufferOutputStream(5); ! bb.write((byte) 'T'); ! bb.write((byte) 'h'); ! bb.write((byte) 'i'); ! bb.write((byte) 's'); ! bb.write((byte) ' '); ! bb.write((byte) 'i'); ! bb.write((byte) 's'); ! bb.write((byte) ' '); ! bb.write((byte) 'a'); ! bb.write((byte) ' '); ! bb.write((byte) 't'); ! bb.write((byte) 'e'); ! bb.write((byte) 's'); ! bb.write((byte) 't'); ! bb.write((byte) '\n'); ! bb.write("This is the second line of the byte buffer".getBytes()); ! bb.write((byte) '\n'); ! bb.write("This is the third line\n".getBytes()); ! bb.write("This is the fourth line\n".getBytes()); ! bb.write((byte) 'E'); ! bb.write((byte) 'N'); ! bb.write((byte) 'D'); ! bb.write((byte) '.'); ! bb.write((byte) '\n'); ! System.out.println("Byte buffer as a string: [" + bb + "]"); ! System.out.print("Byte buffer written to a stream: ["); ! bb.writeTo(System.out); ! System.out.print("]"); ! System.out.println("DONE"); ! } ! catch (Exception e) ! { ! e.printStackTrace(); ! } ! } } Index: CastUtil.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/src/org/webmacro/util/CastUtil.java,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** CastUtil.java 13 May 2003 05:33:29 -0000 1.8 --- CastUtil.java 12 Jun 2003 00:47:48 -0000 1.9 *************** *** 27,227 **** * @since 0.96 */ ! public class CastUtil extends java.lang.Object { ! static private CastUtil _singleton = new CastUtil(); ! /** private constructor for singleton */ ! private CastUtil() { ! } ! static public CastUtil getInstance() { ! return _singleton; ! } ! public static Boolean toBoolean(java.lang.Object o) { ! if (o == null) return Boolean.FALSE; ! return Boolean.valueOf(o.toString()); ! } ! public static Character toChar(Object o) throws org.webmacro.PropertyException { ! Character c = null; ! if (o == null || o == "") return null; ! if (o instanceof Character) return (Character) o; ! if (o instanceof Number) { ! try { ! c = new Character((char) ((Number) o).intValue()); ! } ! catch (Exception e) { ! throw new IllegalArgumentException("Not a valid char: " + ((o==null) ? "null" : (o.toString() + "; type=" + o.getClass().getName())) ); ! } ! } ! else if (o instanceof String) { ! String s = (String) o; ! if (s.startsWith("\\u") && s.length() == 6) { ! // unicode char ! int i = Integer.parseInt(s.substring(2), 16); ! c = new Character((char) i); ! } ! else { // use the first character of the string ! char ch = s.charAt(0); ! c = new Character(ch); ! } ! } ! else { // throw exception ! throw new org.webmacro.PropertyException( ! "$Type.toChar() is unable to convert the supplied value to a Character. The argument" ! + " must be a number or a String. The supplied argument was " ! + o + " of type " + o.getClass().getName()); ! } ! return c; ! } ! public static Byte toByte(java.lang.Object o) { ! if (o instanceof Byte) return (Byte) o; ! Byte v = null; ! try { ! int i = Integer.parseInt(o.toString()); ! v = new Byte((byte) i); ! } ! catch (Exception e) { ! throw new IllegalArgumentException("Not a valid byte: " + ((o==null) ? "null" : (o.toString() + "; type=" + o.getClass().getName())) ); ! } ! return v; ! } ! public static Short toShort(java.lang.Object o) { ! if (o instanceof Short) return (Short) o; ! Short v = null; ! try { ! int i = Integer.parseInt(o.toString()); ! v = new Short((short) i); ! } ! catch (Exception e) { ! throw new IllegalArgumentException("Not a valid short: " + ((o==null) ? "null" : (o.toString() + "; type=" + o.getClass().getName())) ); ! } ! return v; ! } ! public static Integer toInt(java.lang.Object o) { ! Integer i = null; ! if (o instanceof Integer) return (Integer) o; ! try { ! i = new Integer(o.toString()); ! } ! catch (Exception e) { ! throw new IllegalArgumentException("Not a valid int: " + ((o==null) ? "null" : (o.toString() + "; type=" + o.getClass().getName())) ); ! } ! return i; ! } ! public static Long toLong(java.lang.Object o) { ! Long l = null; ! if (o instanceof Long) return (Long) o; ! try { ! l = new Long(o.toString()); ! } ! catch (Exception e) { ! throw new IllegalArgumentException("Not a valid long: " + ((o==null) ? "null" : (o.toString() + "; type=" + o.getClass().getName())) ); ! } ! return l; ! } ! public static Float toFloat(java.lang.Object o) { ! Float f = null; ! if (o instanceof Float) return (Float) o; ! try { ! f = new Float(o.toString()); ! } ! catch (Exception e) { ! throw new IllegalArgumentException("Not a valid float: " + ((o==null) ? "null" : (o.toString() + "; type=" + o.getClass().getName())) ); ! } ! return f; ! } ! public static Double toDouble(java.lang.Object o) { ! Double d = null; ! if (o instanceof Double) return (Double) o; ! try { ! d = new Double(o.toString()); ! } ! catch (Exception e) { ! throw new IllegalArgumentException("Not a valid double: " + ((o==null) ? "null" : (o.toString() + "; type=" + o.getClass().getName())) ); ! } ! return d; ! } ! static public void main(String[] args) { ! CastUtil cu = CastUtil.getInstance(); ! println("77" ! + test(BOOLEAN, "77") ! + test(CHAR, "77") ! + test(BYTE, "77") ! + test(SHORT, "77") ! + test(INTEGER, "77") ! + test(LONG, "77") ! + test(FLOAT, "77") ! + test(DOUBLE, "77") ! + "."); ! println("true: " + test(BOOLEAN, "true") + "."); ! println("FalSE: " + test(BOOLEAN, "FalSE") + "."); ! println("null: " + test(BOOLEAN, null) + "."); ! } ! static private void println(String s) { ! System.out.println(s); ! } ! static private void print(Object o) { ! System.out.print(o); ! } ! final static public int BOOLEAN = 1; ! final static public int CHAR = 2; ! final static public int BYTE = 3; ! final static public int SHORT = 4; ! final static public int INTEGER = 5; ! final static public int LONG = 6; ! final static public int FLOAT = 7; ! final static public int DOUBLE = 8; ! static private String test(int type, Object val) { ! String s = null; ! try { ! Object o = null; ! switch (type) { ! case BOOLEAN: ! o = toBoolean(val); ! break; ! case CHAR: ! o = toChar(val); ! break; ! case BYTE: ! o = toByte(val); ! break; ! case SHORT: ! o = toShort(val); ! break; ! case INTEGER: ! o = toInt(val); ! break; ! case LONG: ! o = toLong(val); ! break; ! case FLOAT: ! o = toFloat(val); ! break; ! case DOUBLE: ! o = toDouble(val); ! break; ! default: ! throw new IllegalArgumentException("Unknown type: " + type); ! } ! s = ", " + o + " (" + o.getClass().getName() + ")"; ! } ! catch (Exception e) { ! e.printStackTrace(); ! } ! return s; ! } } --- 27,264 ---- * @since 0.96 */ ! public class CastUtil extends java.lang.Object ! { ! static private CastUtil _singleton =... [truncated message content] |