some more javadoc work on the util and util.image packages

This commit is contained in:
Eric J. Bowersox 2001-11-13 04:53:26 +00:00
parent 7e42aa391c
commit 556f6d5861
12 changed files with 689 additions and 50 deletions

View File

@ -34,8 +34,8 @@ public final class AnyCharMatcher
*-------------------------------------------------------------------------------- *--------------------------------------------------------------------------------
*/ */
private char[] charset; private char[] charset; // the set of characters to look for
private int[] locs; private int[] locs; // a temporary array used for locations
/*-------------------------------------------------------------------------------- /*--------------------------------------------------------------------------------
* Constructors * Constructors

View File

@ -19,6 +19,14 @@ package com.silverwrist.util;
import org.w3c.dom.*; import org.w3c.dom.*;
/**
* A class which wraps around the DOM <CODE>Element</CODE> class, providing some
* additional functionality.
*
* @author Eric J. Bowersox &lt;erbo@silcom.com&gt;
* @version X
* @see org.w3c.dom.Element
*/
public class DOMElementHelper public class DOMElementHelper
{ {
/*-------------------------------------------------------------------------------- /*--------------------------------------------------------------------------------
@ -33,6 +41,11 @@ public class DOMElementHelper
*-------------------------------------------------------------------------------- *--------------------------------------------------------------------------------
*/ */
/**
* Constructs a new <CODE>DOMElementHelper</CODE> to wrap a specific <CODE>Element</CODE>.
*
* @param elt The <CODE>Element</CODE> to be wrapped.
*/
public DOMElementHelper(Element elt) public DOMElementHelper(Element elt)
{ {
this.elt = elt; this.elt = elt;
@ -44,6 +57,14 @@ public class DOMElementHelper
*-------------------------------------------------------------------------------- *--------------------------------------------------------------------------------
*/ */
/**
* Returns the content of all text nodes underneath a specified <CODE>Element</CODE>, concatenated
* together into a single string.
*
* @param e The <CODE>Element</CODE> to extract text from.
* @return The text content under this <CODE>Element</CODE> node. If the specified <CODE>Element</CODE>
* has no text nodes underneath it, returns <CODE>null.</CODE>
*/
private static String getTextOfElement(Element e) private static String getTextOfElement(Element e)
{ {
NodeList kids = e.getChildNodes(); NodeList kids = e.getChildNodes();
@ -78,12 +99,25 @@ public class DOMElementHelper
*-------------------------------------------------------------------------------- *--------------------------------------------------------------------------------
*/ */
/**
* Returns the <CODE>Element</CODE> wrapped by this object.
*
* @return See above.
*/
public Element getElement() public Element getElement()
{ {
return elt; return elt;
} // end getElement } // end getElement
/**
* Searches for the first sub-element of the wrapped <CODE>Element</CODE> with the given name.
*
* @param name Name of the sub-element to search for.
* @return The first sub-element of the wrapped <CODE>Element</CODE> with the specified name.
* If the <CODE>Element</CODE> has no child <CODE>Elements</CODE> with the given name,
* the method returns <CODE>null</CODE>.
*/
public Element getSubElement(String name) public Element getSubElement(String name)
{ {
NodeList kids = elt.getChildNodes(); NodeList kids = elt.getChildNodes();
@ -101,12 +135,28 @@ public class DOMElementHelper
} // end getSubElement } // end getSubElement
/**
* Returns the content of all text nodes underneath the wrapped <CODE>Element</CODE>, concatenated
* together into a single string.
*
* @return The text content under the wrapped <CODE>Element</CODE> node. If the wrapped <CODE>Element</CODE>
* has not text nodes underneath it, returns <CODE>null.</CODE>
*/
public String getElementText() public String getElementText()
{ {
return getTextOfElement(elt); return getTextOfElement(elt);
} // end getElementText } // end getElementText
/**
* Returns the content of all text nodes underneath the first sub-element of the wrapped
* <CODE>Element</CODE>, with the given name, concatenated together into a single string.
*
* @param name The name of the sub-element to search for.
* @return The text content under the specified sub-element of the wrapped <CODE>Element</CODE> node.
* If the wrapped <CODE>Element</CODE> does not have a sub-element with the given name, or
* that sub-element has no text nodes underneath it, returns <CODE>null.</CODE>
*/
public String getSubElementText(String name) public String getSubElementText(String name)
{ {
Element se = getSubElement(name); Element se = getSubElement(name);
@ -117,6 +167,13 @@ public class DOMElementHelper
} // end getSubElementText } // end getSubElementText
/**
* Determines whether the wrapped <CODE>Element</CODE> has a sub-element with the given name.
*
* @param name Name of the sub-element to search for.
* @return <CODE>true</CODE> if the wrapped <CODE>Element</CODE> has a sub-element with the
* specified name, <CODE>false</CODE> if not.
*/
public boolean hasChildElement(String name) public boolean hasChildElement(String name)
{ {
Element tmp = getSubElement(name); Element tmp = getSubElement(name);
@ -124,12 +181,28 @@ public class DOMElementHelper
} // end hasChildElement } // end hasChildElement
/**
* Determines whether the wrapped <CODE>Element</CODE> has an attribute with the given name.
*
* @param name Name of the attribute to search for.
* @return <CODE>true</CODE> if the wrapped <CODE>Element</CODE> has an attribute with the
* specified name, <CODE>false</CODE> if not.
*/
public boolean hasAttribute(String name) public boolean hasAttribute(String name)
{ {
return !(StringUtil.isStringEmpty(elt.getAttribute(name))); return !(StringUtil.isStringEmpty(elt.getAttribute(name)));
} // end hasAttribute } // end hasAttribute
/**
* Returns the value of a specified attribute of the wrapped <CODE>Element</CODE>, expressed as
* an integer.
*
* @param name Name of the attribute to search for.
* @return An <CODE>Integer</CODE> object containing the value of the specified attribute. If
* the wrapped <CODE>Element</CODE> has no such attribute, or if the attribute's value
* cannot be expressed as an integer, returns <CODE>null</CODE>.
*/
public Integer getAttributeInt(String name) public Integer getAttributeInt(String name)
{ {
String tmp = elt.getAttribute(name); String tmp = elt.getAttribute(name);

View File

@ -47,6 +47,7 @@ public class IOUtil
* Closes an input stream cleanly, without throwing an exception. * Closes an input stream cleanly, without throwing an exception.
* *
* @param stm The stream to be closed. * @param stm The stream to be closed.
* @see java.io.InputStream#close()
*/ */
public static void shutdown(InputStream stm) public static void shutdown(InputStream stm)
{ {
@ -65,6 +66,7 @@ public class IOUtil
* Closes an output stream cleanly, without throwing an exception. * Closes an output stream cleanly, without throwing an exception.
* *
* @param stm The stream to be closed. * @param stm The stream to be closed.
* @see java.io.OutputStream#close()
*/ */
public static void shutdown(OutputStream stm) public static void shutdown(OutputStream stm)
{ {
@ -83,6 +85,7 @@ public class IOUtil
* Closes an input reader cleanly, without throwing an exception. * Closes an input reader cleanly, without throwing an exception.
* *
* @param stm The stream to be closed. * @param stm The stream to be closed.
* @see java.io.Reader#close()
*/ */
public static void shutdown(Reader rdr) public static void shutdown(Reader rdr)
{ {
@ -101,6 +104,7 @@ public class IOUtil
* Closes an output reader cleanly, without throwing an exception. * Closes an output reader cleanly, without throwing an exception.
* *
* @param stm The stream to be closed. * @param stm The stream to be closed.
* @see java.io.Writer#close()
*/ */
public static void shutdown(Writer wr) public static void shutdown(Writer wr)
{ {

View File

@ -19,6 +19,15 @@ package com.silverwrist.util;
import java.util.*; import java.util.*;
/**
* A utility class which creates <CODE>Locale</CODE> objects from standard descriptor strings.
* The descriptor strings are of the form returned by <CODE>Locale.toString()</CODE>.
*
* @author Eric J. Bowersox &lt;erbo@silcom.com&gt;
* @version X
* @see java.util.Locale
* @see java.util.Locale#toString()
*/
public class LocaleFactory public class LocaleFactory
{ {
/*-------------------------------------------------------------------------------- /*--------------------------------------------------------------------------------
@ -35,27 +44,34 @@ public class LocaleFactory
*-------------------------------------------------------------------------------- *--------------------------------------------------------------------------------
*/ */
/**
* Creates a <CODE>Locale</CODE> from a standard descriptor string.
*
* @param streq The string equivalent of the Locale to be created.
* @return The corresponding <CODE>Locale</CODE>, or the default <CODE>Locale</CODE> if the parameter is
* <CODE>null</CODE> or the empty string.
*/
public static Locale createLocale(String streq) public static Locale createLocale(String streq)
{ {
if ((streq==null) || (streq.length()==0)) if ((streq==null) || (streq.length()==0))
return Locale.getDefault(); return Locale.getDefault(); // no locale
int p1 = streq.indexOf('_'); int p1 = streq.indexOf('_');
if (p1<0) if (p1<0)
return new Locale(streq,""); return new Locale(streq,""); // language but no country specified
String x_lang = streq.substring(0,p1); String x_lang = streq.substring(0,p1);
int p2 = streq.indexOf('_',p1+1); int p2 = streq.indexOf('_',p1+1);
if (p2<0) if (p2<0)
{ // there's only one underscore - figure out what part the last part is { // there's only one underscore - figure out what part the last part is
String lastpart = streq.substring(p1+1); String lastpart = streq.substring(p1+1);
if (lastpart.length()==2) if (lastpart.length()==2)
return new Locale(streq.substring(0,p1),lastpart); return new Locale(x_lang,lastpart); // language + country
else else
return new Locale(streq.substring(0,p1),"",lastpart); return new Locale(x_lang,"",lastpart); // language + country(null) + variant
} // end if } // end if
// do all three variants // do all three variants
return new Locale(streq.substring(0,p1),streq.substring(p1+1,p2),streq.substring(p2+1)); return new Locale(x_lang,streq.substring(p1+1,p2),streq.substring(p2+1));
} // end createLocale } // end createLocale

View File

@ -19,6 +19,14 @@ package com.silverwrist.util;
import java.util.BitSet; import java.util.BitSet;
/**
* A variant of the <CODE>BitSet</CODE> that can express itself as a character string, where each
* character represents a flag that is "set." Up to 91 flags can be specified pre <CODE>OptionSet</CODE>.
*
* @author Eric J. Bowersox &lt;erbo@silcom.com&gt;
* @version X
* @see java.util.BitSet
*/
public class OptionSet extends BitSet public class OptionSet extends BitSet
{ {
/*-------------------------------------------------------------------------------- /*--------------------------------------------------------------------------------
@ -26,6 +34,7 @@ public class OptionSet extends BitSet
*-------------------------------------------------------------------------------- *--------------------------------------------------------------------------------
*/ */
// The alphabet to use to store individual flags.
private static final String ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" private static final String ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
+ "!#$%&()*+,-./:;<=>?@[]^_`{|}~"; + "!#$%&()*+,-./:;<=>?@[]^_`{|}~";
@ -34,18 +43,34 @@ public class OptionSet extends BitSet
*-------------------------------------------------------------------------------- *--------------------------------------------------------------------------------
*/ */
/**
* Creates a new <CODE>OptionSet</CODE>. All bits are initially <CODE>false</CODE>.
*/
public OptionSet() public OptionSet()
{ {
super(); super();
} // end constructor } // end constructor
/**
* Creates an <CODE>OptionSet</CODE> whose initial size is large enough to explicitly represent bits with
* indices in the range 0 through nbits-1. All bits are initially <CODE>false</CODE>. The maximum
* size of the <CODE>OptionSet</CODE> is 91.
*
* @param nbits The initial size of the bit set.
* @exception java.lang.NegativeArraySizeException If the specified initial size is negative.
*/
public OptionSet(int nbits) public OptionSet(int nbits)
{ {
super(Math.min(nbits,ALPHA.length())); super(Math.min(nbits,ALPHA.length()));
} // end constructor } // end constructor
/**
* Creates an <CODE>OptionSet</CODE> from an array of characters representing "set" options.
*
* @param options The options to be set in the new <CODE>OptionSet</CODE>.
*/
public OptionSet(char[] options) public OptionSet(char[] options)
{ {
super(); // initialize all bits to 0 super(); // initialize all bits to 0
@ -59,12 +84,28 @@ public class OptionSet extends BitSet
} // end constructor } // end constructor
/**
* Creates an <CODE>OptionSet</CODE> from a string of characters representing "set" options.
*
* @param options The options to be set in the new <CODE>OptionSet</CODE>.
*/
public OptionSet(String options) public OptionSet(String options)
{ {
this(options.toCharArray()); this(options.toCharArray());
} // end constructor } // end constructor
/*--------------------------------------------------------------------------------
* Internal functions
*--------------------------------------------------------------------------------
*/
/**
* Returns a <CODE>StringBuffer</CODE> representing the current state of this <CODE>OptionSet</CODE>,
* with one character in it for each bit that is set.
*
* @return A <CODE>StringBuffer</CODE> containing the current state of the <CODE>OptionSet</CODE>.
*/
private StringBuffer asStringBuffer() private StringBuffer asStringBuffer()
{ {
StringBuffer b = new StringBuffer(); StringBuffer b = new StringBuffer();
@ -80,6 +121,13 @@ public class OptionSet extends BitSet
*-------------------------------------------------------------------------------- *--------------------------------------------------------------------------------
*/ */
/**
* Sets the bit specified by the index to <CODE>false</CODE>.
*
* @param bitIndex The index of the bit to be cleared.
* @exception java.lang.IndexOutOfBoundsException - If the specified index is negative or greater than the
* maximum option for an <CODE>OptionSet</CODE>.
*/
public void clear(int bitIndex) public void clear(int bitIndex)
{ {
if (bitIndex>=ALPHA.length()) if (bitIndex>=ALPHA.length())
@ -88,6 +136,16 @@ public class OptionSet extends BitSet
} // end clear } // end clear
/**
* Returns the value of the bit with the specified index. The value is <CODE>true</CODE> if the bit with
* the index <CODE>bitIndex</CODE> is currently set in this <CODE>OptionSet</CODE>; otherwise, the result
* is <CODE>false</CODE>.
*
* @param bitIndex The bit index.
* @return The value of the bit with the specified index.
* @exception java.lang.IndexOutOfBoundsException - If the specified index is negative or greater than the
* maximum option for an <CODE>OptionSet</CODE>.
*/
public boolean get(int bitIndex) public boolean get(int bitIndex)
{ {
if (bitIndex>=ALPHA.length()) if (bitIndex>=ALPHA.length())
@ -96,6 +154,13 @@ public class OptionSet extends BitSet
} // end get } // end get
/**
* Sets the bit specified by the index to <CODE>true</CODE>.
*
* @param bitIndex The index of the bit to be set.
* @exception java.lang.IndexOutOfBoundsException - If the specified index is negative or greater than the
* maximum option for an <CODE>OptionSet</CODE>.
*/
public void set(int bitIndex) public void set(int bitIndex)
{ {
if (bitIndex>=ALPHA.length()) if (bitIndex>=ALPHA.length())
@ -109,6 +174,17 @@ public class OptionSet extends BitSet
*-------------------------------------------------------------------------------- *--------------------------------------------------------------------------------
*/ */
/**
* Sets the value of the specified bit in the <CODE>OptionSet</CODE> to a specified Boolean
* value, and returns an indication of whether that value was changed.
*
* @param ndx The index of the bit to be assigned.
* @param val <CODE>true</CODE> to set the corresponding bit, <CODE>false</CODE> to clear it.
* @return <CODE>true</CODE> if the value of the bit in the <CODE>OptionSet</CODE> was changed by this
* operation, <CODE>false</CODE> if not.
* @exception java.lang.IndexOutOfBoundsException - If the specified index is negative or greater than the
* maximum option for an <CODE>OptionSet</CODE>.
*/
public boolean assign(int ndx, boolean val) public boolean assign(int ndx, boolean val)
{ {
if (ndx>=ALPHA.length()) if (ndx>=ALPHA.length())
@ -122,6 +198,12 @@ public class OptionSet extends BitSet
} // end assign } // end assign
/**
* Resets the state of this <CODE>OptionSet</CODE> from an array of characters representing "set" options.
*
* @param options The options to be set in the new <CODE>OptionSet</CODE>. All options not specified will
* be cleared.
*/
public void assign(char[] options) public void assign(char[] options)
{ {
int i; int i;
@ -138,6 +220,12 @@ public class OptionSet extends BitSet
} // end assign } // end assign
/**
* Resets the state of this <CODE>OptionSet</CODE> from a string of characters representing "set" options.
*
* @param options The options to be set in the new <CODE>OptionSet</CODE>. All options not specified will
* be cleared.
*/
public void assign(String options) public void assign(String options)
{ {
if (options!=null) if (options!=null)
@ -145,12 +233,24 @@ public class OptionSet extends BitSet
} // end assign } // end assign
/**
* Returns a character array representing the current state of this <CODE>OptionSet</CODE>,
* with one character in it for each bit that is set.
*
* @return A character array containing the current state of the <CODE>OptionSet</CODE>.
*/
public char[] asCharArray() public char[] asCharArray()
{ {
return asStringBuffer().toString().toCharArray(); return asStringBuffer().toString().toCharArray();
} // end asCharArray } // end asCharArray
/**
* Returns a string representing the current state of this <CODE>OptionSet</CODE>,
* with one character in it for each bit that is set.
*
* @return A string containing the current state of the <CODE>OptionSet</CODE>.
*/
public String asString() public String asString()
{ {
return asStringBuffer().toString(); return asStringBuffer().toString();

View File

@ -19,6 +19,15 @@ package com.silverwrist.util;
import java.util.Vector; import java.util.Vector;
/**
* A class which takes any number of <CODE>Runnable</CODE> objects, and executes them in parallel on
* multiple threads insofar as possible.
*
* @author Eric J. Bowersox &lt;erbo@silcom.com&gt;
* @version X
* @see java.lang.Runnable
* @see java.lang.Thread
*/
public class ParallelRunQueue implements Runnable public class ParallelRunQueue implements Runnable
{ {
/*-------------------------------------------------------------------------------- /*--------------------------------------------------------------------------------
@ -26,22 +35,27 @@ public class ParallelRunQueue implements Runnable
*-------------------------------------------------------------------------------- *--------------------------------------------------------------------------------
*/ */
private Thread[] thrds; private Thread[] thrds; // the current threads
private Vector queue; private Vector queue; // the queue of Runnables to be run
private int priority; private int priority; // the priority to use for all these threads
/*-------------------------------------------------------------------------------- /*--------------------------------------------------------------------------------
* Constructor * Constructor
*-------------------------------------------------------------------------------- *--------------------------------------------------------------------------------
*/ */
/**
* Creates a new <CODE>ParallelRunQueue</CODE>.
*
* @param nthread Number of threads to be executed in parallel with the current one by this run queue.
*/
public ParallelRunQueue(int nthread) public ParallelRunQueue(int nthread)
{ {
thrds = new Thread[nthread]; thrds = new Thread[nthread];
for (int i=0; i<nthread; i++) for (int i=0; i<nthread; i++)
thrds[i] = null; thrds[i] = null; // no threads to start with
queue = new Vector(); queue = new Vector();
priority = Thread.currentThread().getPriority(); priority = Thread.currentThread().getPriority(); // default the priority
} // end constructor } // end constructor
@ -50,6 +64,11 @@ public class ParallelRunQueue implements Runnable
*-------------------------------------------------------------------------------- *--------------------------------------------------------------------------------
*/ */
/**
* Executes all <CODE>Runnable</CODE> objects currently queued. When this method returns, the run queue
* is empty. The objects are executed on one of the internal worker threads, or on the current thread
* if no other threads are available.
*/
public void run() public void run()
{ {
while (queue.size()>0) while (queue.size()>0)
@ -72,6 +91,8 @@ public class ParallelRunQueue implements Runnable
} // end while } // end while
work(); // GC any further dead threads
} // end run } // end run
/*-------------------------------------------------------------------------------- /*--------------------------------------------------------------------------------
@ -79,6 +100,12 @@ public class ParallelRunQueue implements Runnable
*-------------------------------------------------------------------------------- *--------------------------------------------------------------------------------
*/ */
/**
* Enqueues a new <CODE>Runnable</CODE> object onto the queue. If a worker thread is available, the
* new object is started running immediately.
*
* @param r The <CODE>Runnable</CODE> object to enqueue.
*/
public void queue(Runnable r) public void queue(Runnable r)
{ {
for (int i=0; i<thrds.length; i++) for (int i=0; i<thrds.length; i++)
@ -88,6 +115,7 @@ public class ParallelRunQueue implements Runnable
thrds[i] = new Thread(r); thrds[i] = new Thread(r);
thrds[i].setPriority(priority); thrds[i].setPriority(priority);
thrds[i].start(); thrds[i].start();
work();
return; return;
} // end if } // end if
@ -98,4 +126,29 @@ public class ParallelRunQueue implements Runnable
} // end queue } // end queue
/**
* If any threads in the run queue are dead, replaces them with live threads running fresh elements out
* of the queue. If no more <CODE>Runnable</CODE> objects remain, then dead threads are nulled out to
* garbage-collect them.
*/
public void work()
{
for (int i=0; i<thrds.length; i++)
if ((thrds[i]==null) || !(thrds[i].isAlive()))
{ // found an empty slot or dead thread to replace
if (queue.size()==0)
thrds[i] = null; // let the dead thread be garbage-collected
else
{ // get the next Runnable and enqueue it
Runnable r = (Runnable)(queue.remove(0));
thrds[i] = new Thread(r);
thrds[i].setPriority(priority);
thrds[i].start();
} // end else
} // end if and for
} // end work
} // end class ParallelRunQueue } // end class ParallelRunQueue

View File

@ -7,7 +7,7 @@
* WARRANTY OF ANY KIND, either express or implied. See the License for the specific * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
* language governing rights and limitations under the License. * language governing rights and limitations under the License.
* *
* The Original Code is the Venice Web Community System. * The Original Code is the Venice Web Communities System.
* *
* The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>, * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
* for Silverwrist Design Studios. Portions created by Eric J. Bowersox are * for Silverwrist Design Studios. Portions created by Eric J. Bowersox are
@ -17,47 +17,141 @@
*/ */
package com.silverwrist.util; package com.silverwrist.util;
import java.io.PrintStream;
import java.io.PrintWriter;
/**
* An exception thrown by <CODE>ServletMultipartHandler</CODE> in certain circumstances.
*
* @author Eric J. Bowersox &lt;erbo@silcom.com&gt;
* @version X
* @see ServletMultipartHandler
*/
public class ServletMultipartException extends Exception public class ServletMultipartException extends Exception
{ {
// Attributes /*--------------------------------------------------------------------------------
private Exception e = null; // internal "root cause" exception * Attributes
*--------------------------------------------------------------------------------
*/
private Throwable inner = null; // internal "root cause" exception
/*--------------------------------------------------------------------------------
* Constructors
*--------------------------------------------------------------------------------
*/
/**
* Constructs a new <CODE>ServletMultipartException</CODE>.
*/
public ServletMultipartException() public ServletMultipartException()
{ {
super(); super();
} // end constructor } // end constructor
/**
* Constructs a new <CODE>ServletMultipartException</CODE> with a text message.
*
* @param msg The message to set in this exception.
*/
public ServletMultipartException(String msg) public ServletMultipartException(String msg)
{ {
super(msg); super(msg);
} // end constructor } // end constructor
public ServletMultipartException(Exception e) /**
* Constructs a new <CODE>ServletMultipartException</CODE> wrapping another exception.
*
* @param inner The exception wrapped by this one.
*/
public ServletMultipartException(Throwable inner)
{ {
super(e.getMessage()); super(inner.getMessage());
this.e = e; this.inner = inner;
} // end constructor } // end constructor
public ServletMultipartException(String msg, Exception e) /**
* Constructs a new <CODE>ServletMultipartException</CODE> wrapping another exception.
*
* @param msg The message to set in this exception.
* @param inner The exception wrapped by this one.
*/
public ServletMultipartException(String msg, Throwable inner)
{ {
super(msg); super(msg);
this.e = e; this.inner = inner;
} // end constructor } // end constructor
protected void finalize() throws Throwable /*--------------------------------------------------------------------------------
{ * Overrides from class Throwable
e = null; *--------------------------------------------------------------------------------
super.finalize(); */
} // end finalize /**
* Prints this exception and its backtrace to the standard error stream. Also prints the backtrace
public Exception getException() * of any "wrapped" exception.
*
* @see java.lang.System#err
*/
public void printStackTrace()
{ {
return e; this.printStackTrace(System.err);
} // end printStackTrace
/**
* Prints this exception and its backtrace to the specified <CODE>PrintStream</CODE>. Also prints the
* backtrace of any "wrapped" exception.
*
* @param s <CODE>PrintStream</CODE> to use for output.
*/
public void printStackTrace(PrintStream s)
{
super.printStackTrace(s);
if (inner!=null)
{ // print the inner stack trace
s.print("Root cause: ");
inner.printStackTrace(s);
} // end if
} // end printStackTrace
/**
* Prints this exception and its backtrace to the specified <CODE>PrintWriter</CODE>. Also prints the
* backtrace of any "wrapped" exception.
*
* @param s <CODE>PrintWriter</CODE> to use for output.
*/
public void printStackTrace(PrintWriter s)
{
super.printStackTrace(s);
if (inner!=null)
{ // print the inner stack trace
s.print("Root cause: ");
inner.printStackTrace(s);
} // end if
} // end printStackTrace
/*--------------------------------------------------------------------------------
* External operations
*--------------------------------------------------------------------------------
*/
/**
* Returns the exception wrapped by this exception, or <CODE>null</CODE> if there is none.
*
* @return See above.
*/
public Throwable getException()
{
return inner;
} // end getException } // end getException

View File

@ -31,6 +31,22 @@ import org.apache.log4j.*;
// a bunch of APIs in ways that their designers would never have anticipated. It's // a bunch of APIs in ways that their designers would never have anticipated. It's
// absolutely bodge-tastic! // absolutely bodge-tastic!
/**
* A class which parses servlet request data of the MIME type <CODE>multipart/form-data</CODE>. This is
* necessary because the standard Java Servlets API only handles the standard form data type of
* <CODE>application/x-www-form-urlencoded</CODE>, yet the <CODE>multipart/form-data</CODE> form
* data type is the only way to upload files using a standard Web browser (at least one that implements
* RFC 1867, as most modern browsers do).<P>
* When using this code, do <EM>not</EM> use the standard <CODE>ServletRequest.getParameter</CODE> API!
* Instead, if the datatype returned by <CODE>ServletRequest.getContentType</CODE> is "multipart/form-data",
* pass the request to the constructor of this class, and then use the methods of this class to extract both
* file and non-file parameters.
*
* @author Eric J. Bowersox &lt;erbo@silcom.com&gt;
* @version X
* @see ServletMultipartException
* @see javax.servlet.ServletRequest
*/
public class ServletMultipartHandler public class ServletMultipartHandler
{ {
/*-------------------------------------------------------------------------------- /*--------------------------------------------------------------------------------
@ -338,6 +354,13 @@ public class ServletMultipartHandler
*-------------------------------------------------------------------------------- *--------------------------------------------------------------------------------
*/ */
/**
* Constructs a new <CODE>ServletMultipartHandler</CODE>.
*
* @param request The <CODE>ServletRequest</CODE> which contains the request data of type
* <CODE>multipart/form-data</CODE>.
* @exception com.silverwrist.util.ServletMultipartException The request could not be correctly parsed.
*/
public ServletMultipartHandler(ServletRequest request) throws ServletMultipartException public ServletMultipartHandler(ServletRequest request) throws ServletMultipartException
{ {
try try
@ -398,6 +421,13 @@ public class ServletMultipartHandler
*-------------------------------------------------------------------------------- *--------------------------------------------------------------------------------
*/ */
/**
* Returns an <CODE>Enumeration</CODE> of all parameter names in this request.
*
* @return See above.
* @see #getParamNames()
* @see java.util.Enumeration
*/
public Enumeration getNames() public Enumeration getNames()
{ {
ArrayList tmp_v = new ArrayList(); ArrayList tmp_v = new ArrayList();
@ -413,6 +443,14 @@ public class ServletMultipartHandler
} // end getNames } // end getNames
/**
* Returns an <CODE>Iterator</CODE> over all parameter names in this request. This <CODE>Iterator</CODE>
* must be considered read-only.
*
* @return See above.
* @see #getNames()
* @see java.util.Iterator
*/
public Iterator getParamNames() public Iterator getParamNames()
{ {
ArrayList tmp_v = new ArrayList(); ArrayList tmp_v = new ArrayList();
@ -428,6 +466,13 @@ public class ServletMultipartHandler
} // end getParamNames } // end getParamNames
/**
* Returns whether or not the parameter with the specified name is a file parameter.
*
* @param name Name of the parameter to test.
* @return <CODE>true</CODE> if the specified parameter is a file parameter, <CODE>false</CODE> of not.
* If no parameter by this name exists in the request, the method returns <CODE>false</CODE>.
*/
public boolean isFileParam(String name) public boolean isFileParam(String name)
{ {
MultipartParameter parm = (MultipartParameter)(param_byname.get(name)); MultipartParameter parm = (MultipartParameter)(param_byname.get(name));
@ -442,6 +487,14 @@ public class ServletMultipartHandler
} // end isFileParam } // end isFileParam
/**
* Returns the value of the parameter with the specified name. In the case of file parameters, returns
* the name of the file that was uploaded.
*
* @param name Name of the parameter to return.
* @return Value of the parameter, which is a filename if this parameter is a file. If no parameter by
* this name exists in the request, the method returns <CODE>null</CODE>.
*/
public String getValue(String name) public String getValue(String name)
{ {
MultipartParameter parm = (MultipartParameter)(param_byname.get(name)); MultipartParameter parm = (MultipartParameter)(param_byname.get(name));
@ -456,6 +509,14 @@ public class ServletMultipartHandler
} // end getValue } // end getValue
/**
* Returns the MIME type of the file parameter with the specified name. Ordinary text parameters return
* the MIME type <CODE>text/plain</CODE>.
*
* @param name Name of the parameter to return.
* @return Content type of the parameter. If no parameter by this name exists in the request, the method
* returns <CODE>null</CODE>.
*/
public String getContentType(String name) public String getContentType(String name)
{ {
MultipartParameter parm = (MultipartParameter)(param_byname.get(name)); MultipartParameter parm = (MultipartParameter)(param_byname.get(name));
@ -470,6 +531,14 @@ public class ServletMultipartHandler
} // end getContentType } // end getContentType
/**
* Returns the size in bytes of the file parameter with the specified name. For ordinary text parameters,
* the return value is meaningless.
*
* @param name Name of the parameter to return.
* @return Size in bytes of the parameter. If no parameter by this name exists in the request, the method
* returns -1.
*/
public int getContentSize(String name) public int getContentSize(String name)
{ {
MultipartParameter parm = (MultipartParameter)(param_byname.get(name)); MultipartParameter parm = (MultipartParameter)(param_byname.get(name));
@ -484,6 +553,16 @@ public class ServletMultipartHandler
} // end getContentSize } // end getContentSize
/**
* Returns the contents of the specified file parameter expressed as a <CODE>Blob</CODE>.
*
* @param name Name of the parameter to return.
* @return <CODE>Blob</CODE> containing the file parameter data. If this parameter does not
* exist in the request or is not a file parameter, the method returns <CODE>null</CODE>.
* @exception com.silverwrist.util.ServletMultipartException Unable to prepare the file parameter data.
* @see #getFileContentStream()
* @see java.sql.Blob
*/
public Blob getFileContentBlob(String name) throws ServletMultipartException public Blob getFileContentBlob(String name) throws ServletMultipartException
{ {
MultipartParameter parm = (MultipartParameter)(param_byname.get(name)); MultipartParameter parm = (MultipartParameter)(param_byname.get(name));
@ -498,6 +577,16 @@ public class ServletMultipartHandler
} // end getFileContentBlob } // end getFileContentBlob
/**
* Returns the contents of the specified file parameter expressed as an <CODE>InputStream</CODE>.
*
* @param name Name of the parameter to return.
* @return <CODE>InputStream</CODE> containing the file parameter data. If this parameter does not
* exist in the request or is not a file parameter, the method returns <CODE>null</CODE>.
* @exception com.silverwrist.util.ServletMultipartException Unable to prepare the file parameter data.
* @see #getFileContentBlob()
* @see java.io.InputStream
*/
public InputStream getFileContentStream(String name) throws ServletMultipartException public InputStream getFileContentStream(String name) throws ServletMultipartException
{ {
MultipartParameter parm = (MultipartParameter)(param_byname.get(name)); MultipartParameter parm = (MultipartParameter)(param_byname.get(name));
@ -509,7 +598,7 @@ public class ServletMultipartHandler
} // end if } // end if
MultipartDataValue val = parm.getContent(); MultipartDataValue val = parm.getContent();
return val.getBinaryStream(); return ((val==null) ? null : val.getBinaryStream());
} // end getFileContentStream } // end getFileContentStream

View File

@ -19,6 +19,14 @@ package com.silverwrist.util.image;
import java.io.InputStream; import java.io.InputStream;
/**
* A class used to return both an image's length in bytes and an <CODE>InputStream</CODE> containing
* its data.
*
* @author Eric J. Bowersox &lt;erbo@silcom.com&gt;
* @version X
* @see ImageNormalizer
*/
public final class ImageLengthPair public final class ImageLengthPair
{ {
/*-------------------------------------------------------------------------------- /*--------------------------------------------------------------------------------
@ -26,14 +34,21 @@ public final class ImageLengthPair
*-------------------------------------------------------------------------------- *--------------------------------------------------------------------------------
*/ */
private int length; private int length; // image length
private InputStream data; private InputStream data; // image data
/*-------------------------------------------------------------------------------- /*--------------------------------------------------------------------------------
* Constructor * Constructor
*-------------------------------------------------------------------------------- *--------------------------------------------------------------------------------
*/ */
/**
* Constructs a new object; usually called upon return from the <CODE>ImageNormalizer</CODE>.
*
* @param length Image length to be returned.
* @param data Image data to be returned.
* @see java.io.InputStream
*/
public ImageLengthPair(int length, InputStream data) public ImageLengthPair(int length, InputStream data)
{ {
this.length = length; this.length = length;
@ -46,12 +61,23 @@ public final class ImageLengthPair
*-------------------------------------------------------------------------------- *--------------------------------------------------------------------------------
*/ */
/**
* Returns the length of the image data.
*
* @return See above.
*/
public final int getLength() public final int getLength()
{ {
return length; return length;
} // end getLength } // end getLength
/**
* Returns the iage data expressed as an <CODE>InputStream</CODE>.
*
* @return See above.
* @see java.io.InputStream
*/
public final InputStream getData() public final InputStream getData()
{ {
return data; return data;

View File

@ -24,6 +24,15 @@ import java.io.*;
import javax.media.jai.*; import javax.media.jai.*;
import com.sun.media.jai.codec.*; import com.sun.media.jai.codec.*;
/**
* A class that performs "normalization" on an image to fit within a bounding box of a specified pixel
* size. "Normalized" images are scaled down proportionally to fit the bounding box, then centered over
* a black backdrop (so the image will be "letterboxed" if it has a larger H:V aspect ratio than the bounding
* box, or "pillarboxed" if it has a smaller H:V aspect ratio than the bounding box).
*
* @author Eric J. Bowersox &lt;erbo@silcom.com&gt;
* @version X
*/
public class ImageNormalizer public class ImageNormalizer
{ {
/*-------------------------------------------------------------------------------- /*--------------------------------------------------------------------------------
@ -31,10 +40,26 @@ public class ImageNormalizer
*-------------------------------------------------------------------------------- *--------------------------------------------------------------------------------
*/ */
/**
* "Normalizes" an image to fit within a bounding box of a specified pixel size. "Normalized" images are
* scaled down proportionally to fit the bounding box, then centered over a black backdrop (so the image
* will be "letterboxed" if it has a larger H:V aspect ratio than the bounding box, or "pillarboxed" if it
* has a smaller H:V aspect ratio than the bounding box).
*
* @param raw_data Raw image data from the input, in any JAI-recognized format.
* @param width The width of the bounding box to use to normalize the image.
* @param height The height of the bounding box to use to normalize the image.
* @param out_format The desired format for output (such as "JPEG").
* @return The normalized image data and its length, as an <CODE>ImageLengthPair</CODE>.
* @exception com.silverwrist.util.image.ImageNormalizerException An error occurred in the image data or
* the image transformation process.
* @see ImageLengthPair
* @see ImageNormalizerException
*/
public static ImageLengthPair normalizeImage(InputStream raw_data, int width, int height, String out_format) public static ImageLengthPair normalizeImage(InputStream raw_data, int width, int height, String out_format)
throws ImageNormalizerException throws ImageNormalizerException
{ {
PlanarImage img1; PlanarImage img1; // the "initial" image we're loading
try try
{ // Start by loading the image we're working with. { // Start by loading the image we're working with.
SeekableStream istm = new ForwardSeekableStream(raw_data); SeekableStream istm = new ForwardSeekableStream(raw_data);
@ -75,7 +100,7 @@ public class ImageNormalizer
scale = scale_height; scale = scale_height;
// If we need to scale the image now, do so. // If we need to scale the image now, do so.
PlanarImage img2; PlanarImage img2; // image post-scaling
if (scale!=null) if (scale!=null)
{ // scale the image down! { // scale the image down!
ParameterBlock pb1 = new ParameterBlock(); ParameterBlock pb1 = new ParameterBlock();
@ -96,7 +121,7 @@ public class ImageNormalizer
int offset_y = (height - img2.getHeight()) / 2; int offset_y = (height - img2.getHeight()) / 2;
// If we need to translate the image now, do so. // If we need to translate the image now, do so.
PlanarImage img3; PlanarImage img3; // image after scaling and translation
if ((offset_x!=0) || (offset_y!=0)) if ((offset_x!=0) || (offset_y!=0))
{ // set up a translation to move the image to the right location { // set up a translation to move the image to the right location
ParameterBlock pb2 = new ParameterBlock(); ParameterBlock pb2 = new ParameterBlock();
@ -143,6 +168,21 @@ public class ImageNormalizer
} // end normalizeImage } // end normalizeImage
/**
* "Normalizes" an image to fit within a bounding box of a specified pixel size. "Normalized" images are
* scaled down proportionally to fit the bounding box, then centered over a black backdrop (so the image
* will be "letterboxed" if it has a larger H:V aspect ratio than the bounding box, or "pillarboxed" if it
* has a smaller H:V aspect ratio than the bounding box).
*
* @param raw_data Raw image data from the input, in any JAI-recognized format.
* @param dim The dimensions of the bounding box to use to normalize the image.
* @param out_format The desired format for output (such as "JPEG").
* @return The normalized image data and its length, as an <CODE>ImageLengthPair</CODE>.
* @exception com.silverwrist.util.image.ImageNormalizerException An error occurred in the image data or
* the image transformation process.
* @see ImageLengthPair
* @see ImageNormalizerException
*/
public static ImageLengthPair normalizeImage(InputStream raw_data, Dimension dim, String out_format) public static ImageLengthPair normalizeImage(InputStream raw_data, Dimension dim, String out_format)
throws ImageNormalizerException throws ImageNormalizerException
{ {

View File

@ -20,6 +20,14 @@ package com.silverwrist.util.image;
import java.io.PrintStream; import java.io.PrintStream;
import java.io.PrintWriter; import java.io.PrintWriter;
/**
* An exception thrown by <CODE>ImageNormalizer</CODE> when the normalizer encounters an error in
* its input image.
*
* @author Eric J. Bowersox &lt;erbo@silcom.com&gt;
* @version X
* @see ImageNormalizer
*/
public class ImageNormalizerException extends Exception public class ImageNormalizerException extends Exception
{ {
/*-------------------------------------------------------------------------------- /*--------------------------------------------------------------------------------
@ -34,18 +42,31 @@ public class ImageNormalizerException extends Exception
*-------------------------------------------------------------------------------- *--------------------------------------------------------------------------------
*/ */
/**
* Constructs a new <CODE>ImageNormalizerException</CODE>.
*/
public ImageNormalizerException() public ImageNormalizerException()
{ {
super(); super();
} // end constructor } // end constructor
/**
* Constructs a new <CODE>ImageNormalizerException</CODE> with a text message.
*
* @param msg The message to set in this exception.
*/
public ImageNormalizerException(String msg) public ImageNormalizerException(String msg)
{ {
super(msg); super(msg);
} // end constructor } // end constructor
/**
* Constructs a new <CODE>ImageNormalizerException</CODE> wrapping another exception.
*
* @param t The exception wrapped by this one.
*/
public ImageNormalizerException(Throwable t) public ImageNormalizerException(Throwable t)
{ {
super(t.getMessage()); super(t.getMessage());
@ -53,6 +74,12 @@ public class ImageNormalizerException extends Exception
} // end constructor } // end constructor
/**
* Constructs a new <CODE>ImageNormalizerException</CODE> wrapping another exception.
*
* @param msg The message to set in this exception.
* @param t The exception wrapped by this one.
*/
public ImageNormalizerException(String msg, Throwable t) public ImageNormalizerException(String msg, Throwable t)
{ {
super(msg); super(msg);
@ -65,12 +92,24 @@ public class ImageNormalizerException extends Exception
*-------------------------------------------------------------------------------- *--------------------------------------------------------------------------------
*/ */
/**
* Prints this exception and its backtrace to the standard error stream. Also prints the backtrace
* of any "wrapped" exception.
*
* @see java.lang.System#err
*/
public void printStackTrace() public void printStackTrace()
{ {
this.printStackTrace(System.err); this.printStackTrace(System.err);
} // end printStackTrace } // end printStackTrace
/**
* Prints this exception and its backtrace to the specified <CODE>PrintStream</CODE>. Also prints the
* backtrace of any "wrapped" exception.
*
* @param s <CODE>PrintStream</CODE> to use for output.
*/
public void printStackTrace(PrintStream s) public void printStackTrace(PrintStream s)
{ {
super.printStackTrace(s); super.printStackTrace(s);
@ -83,6 +122,12 @@ public class ImageNormalizerException extends Exception
} // end printStackTrace } // end printStackTrace
/**
* Prints this exception and its backtrace to the specified <CODE>PrintWriter</CODE>. Also prints the
* backtrace of any "wrapped" exception.
*
* @param s <CODE>PrintWriter</CODE> to use for output.
*/
public void printStackTrace(PrintWriter s) public void printStackTrace(PrintWriter s)
{ {
super.printStackTrace(s); super.printStackTrace(s);
@ -100,6 +145,11 @@ public class ImageNormalizerException extends Exception
*-------------------------------------------------------------------------------- *--------------------------------------------------------------------------------
*/ */
/**
* Returns the exception wrapped by this exception, or <CODE>null</CODE> if there is none.
*
* @return See above.
*/
public Throwable getException() public Throwable getException()
{ {
return inner; return inner;

View File

@ -7,7 +7,7 @@
* WARRANTY OF ANY KIND, either express or implied. See the License for the specific * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
* language governing rights and limitations under the License. * language governing rights and limitations under the License.
* *
* The Original Code is the Venice Web Community System. * The Original Code is the Venice Web Communities System.
* *
* The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>, * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
* for Silverwrist Design Studios. Portions created by Eric J. Bowersox are * for Silverwrist Design Studios. Portions created by Eric J. Bowersox are
@ -17,47 +17,141 @@
*/ */
package com.silverwrist.venice; package com.silverwrist.venice;
import java.io.PrintStream;
import java.io.PrintWriter;
/**
* The root exception of all exceptions thrown by the Venice core code. It is capable of "wrapping"
* another exception within it.
*
* @author Eric J. Bowersox &lt;erbo@silcom.com&gt;
* @version X
*/
public class VeniceException extends Exception public class VeniceException extends Exception
{ {
// Attributes /*--------------------------------------------------------------------------------
private Exception e = null; // internal "root cause" exception * Attributes
*--------------------------------------------------------------------------------
*/
private Throwable inner = null; // internal "root cause" exception
/*--------------------------------------------------------------------------------
* Constructors
*--------------------------------------------------------------------------------
*/
/**
* Constructs a new <CODE>VeniceException</CODE>.
*/
public VeniceException() public VeniceException()
{ {
super(); super();
} // end constructor } // end constructor
/**
* Constructs a new <CODE>VeniceException</CODE> with a text message.
*
* @param msg The message to set in this exception.
*/
public VeniceException(String msg) public VeniceException(String msg)
{ {
super(msg); super(msg);
} // end constructor } // end constructor
public VeniceException(Exception e) /**
* Constructs a new <CODE>VeniceException</CODE> wrapping another exception.
*
* @param inner The exception wrapped by this one.
*/
public VeniceException(Throwable inner)
{ {
super(e.getMessage()); super(inner.getMessage());
this.e = e; this.inner = inner;
} // end constructor } // end constructor
public VeniceException(String msg, Exception e) /**
* Constructs a new <CODE>VeniceException</CODE> wrapping another exception.
*
* @param msg The message to set in this exception.
* @param inner The exception wrapped by this one.
*/
public VeniceException(String msg, Throwable inner)
{ {
super(msg); super(msg);
this.e = e; this.inner = inner;
} // end constructor } // end constructor
protected void finalize() throws Throwable /*--------------------------------------------------------------------------------
{ * Overrides from class Throwable
e = null; *--------------------------------------------------------------------------------
super.finalize(); */
} // end finalize /**
* Prints this exception and its backtrace to the standard error stream. Also prints the backtrace
public Exception getException() * of any "wrapped" exception.
*
* @see java.lang.System#err
*/
public void printStackTrace()
{ {
return e; this.printStackTrace(System.err);
} // end printStackTrace
/**
* Prints this exception and its backtrace to the specified <CODE>PrintStream</CODE>. Also prints the
* backtrace of any "wrapped" exception.
*
* @param s <CODE>PrintStream</CODE> to use for output.
*/
public void printStackTrace(PrintStream s)
{
super.printStackTrace(s);
if (inner!=null)
{ // print the inner stack trace
s.print("Root cause: ");
inner.printStackTrace(s);
} // end if
} // end printStackTrace
/**
* Prints this exception and its backtrace to the specified <CODE>PrintWriter</CODE>. Also prints the
* backtrace of any "wrapped" exception.
*
* @param s <CODE>PrintWriter</CODE> to use for output.
*/
public void printStackTrace(PrintWriter s)
{
super.printStackTrace(s);
if (inner!=null)
{ // print the inner stack trace
s.print("Root cause: ");
inner.printStackTrace(s);
} // end if
} // end printStackTrace
/*--------------------------------------------------------------------------------
* External operations
*--------------------------------------------------------------------------------
*/
/**
* Returns the exception wrapped by this exception, or <CODE>null</CODE> if there is none.
*
* @return See above.
*/
public Throwable getException()
{
return inner;
} // end getException } // end getException