org.eaglei.repository.util
Class Utils

java.lang.Object
  extended by org.eaglei.repository.util.Utils

public class Utils
extends java.lang.Object

Utility methods

Version:
$Id: $
Author:
Larry Stone

Method Summary
static java.lang.String contentTypeGetCharset(java.lang.String ct, java.lang.String dflt)
          Extracts the character set specified with the content-type value.
static java.lang.String contentTypeGetMIMEType(java.lang.String ct)
          Extracts the MIME type specified with the content-type value.
static void copyStream(java.io.InputStream in, java.io.OutputStream out)
          The canonical file copy loop.
static boolean isValidURI(java.lang.String s)
          Test whether a string is a well-formed-enough absolute URI that it will not cause createURI to fail.
static java.lang.String makeContentType(java.lang.String mimeType, java.lang.String charset)
          Combine MIME type expression with charset string (IFF not already present), and return the result.
static boolean parseBooleanParameter(java.lang.String name, java.lang.String argName, boolean required, boolean defaultValue)
          Special query arg parser for args with boolean values.
static java.lang.Enum parseKeywordArg(java.lang.Class et, java.lang.String name, java.lang.String argName, boolean required, java.lang.Enum defaultValue)
          Parse a query arg value that is supposed to belong to an Enum set.
static org.openrdf.model.URI parseURI(java.lang.String s, java.lang.String argName, boolean required)
          Translates string to URI with sanity check.
static javax.xml.datatype.XMLGregorianCalendar parseXMLDate(java.lang.String raw)
           
static java.lang.String prettyPrint(org.openrdf.query.Dataset ds)
          Formatted human-readable view of Dataset contents which works even when some members are null.
static java.lang.String toString(byte[] ba)
          Like Arrays.toString() but prints bytes in hex instead of signed decimal for easier debugging of char strings.
static java.lang.String urlEncode(java.lang.String in)
          Returns an URL-encoded UTF8 encoding of the string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

copyStream

public static void copyStream(java.io.InputStream in,
                              java.io.OutputStream out)
                       throws java.io.IOException
The canonical file copy loop. Closes streams when done.

Parameters:
in - source, a InputStream object.
out - destination, a OutputStream object.
Throws:
java$io$IOException - if anything goes wrong.
java.io.IOException

prettyPrint

public static java.lang.String prettyPrint(org.openrdf.query.Dataset ds)
Formatted human-readable view of Dataset contents which works even when some members are null. Needed because Sesame's toString() breaks on a null URI, even though that is allowed and necessary(!) for temporary fix of inferencing context problem..

Parameters:
ds - a Dataset object.
Returns:
multi-line prettyprint in a String object.

isValidURI

public static boolean isValidURI(java.lang.String s)
Test whether a string is a well-formed-enough absolute URI that it will not cause createURI to fail.

Parameters:
s - possible URI
Returns:
a boolean, true if s is a valid URI.

parseURI

public static org.openrdf.model.URI parseURI(java.lang.String s,
                                             java.lang.String argName,
                                             boolean required)
Translates string to URI with sanity check.

Parameters:
s - possible URI
argName - name of parameter for error messages
required - true if arg is required
Returns:
a URI

parseKeywordArg

public static java.lang.Enum parseKeywordArg(java.lang.Class et,
                                             java.lang.String name,
                                             java.lang.String argName,
                                             boolean required,
                                             java.lang.Enum defaultValue)
Parse a query arg value that is supposed to belong to an Enum set. If a value is required, throw the BadRequest exception if not present, and in any case throw BadRequest if it is not a valid enumerated value. NOTE: This _ought_ to be shared in utils or some such but then all the arg enums would haev to be public, and they shouldn't be.

Parameters:
et - the enumerated type class
name - value of the argument
argName - name of the argument, e.g. parameter name
required - when true this arg MUST have a value.
defaultValue - default to subsitute when arg is not required and missing
Returns:
the enum or null only if required is false and there is no value.
Throws:
BadRequestException - if arg is missing or illegal

parseBooleanParameter

public static boolean parseBooleanParameter(java.lang.String name,
                                            java.lang.String argName,
                                            boolean required,
                                            boolean defaultValue)
Special query arg parser for args with boolean values. If a value is "required", throw the BadRequest exception if not present, and in any case throw BadRequest if it is not a valid enumerated value. Use the enum as a convenient table of strings and values.

Parameters:
name - value of the argument
argName - name of the argument, e.g. parameter name
required - when true this arg MUST have a value.
defaultValue - default to subsitute when arg is not required and missing
Returns:
the boolean result of parsed or defaulted arg
Throws:
BadRequestException - if arg is missing or illegal

parseXMLDate

public static javax.xml.datatype.XMLGregorianCalendar parseXMLDate(java.lang.String raw)

makeContentType

public static java.lang.String makeContentType(java.lang.String mimeType,
                                               java.lang.String charset)
Combine MIME type expression with charset string (IFF not already present), and return the result.


contentTypeGetCharset

public static java.lang.String contentTypeGetCharset(java.lang.String ct,
                                                     java.lang.String dflt)
Extracts the character set specified with the content-type value. E.g. ``text/plain; charset="UTF-8"'' => UTF-8

Returns:
the charset (without quotes) or null if none specified

contentTypeGetMIMEType

public static java.lang.String contentTypeGetMIMEType(java.lang.String ct)
Extracts the MIME type specified with the content-type value. E.g. ``text/plain; charset="UTF-8"'' => text/plain

Returns:
the MIME type portion of the content-type string.

toString

public static java.lang.String toString(byte[] ba)
Like Arrays.toString() but prints bytes in hex instead of signed decimal for easier debugging of char strings.


urlEncode

public static java.lang.String urlEncode(java.lang.String in)
                                  throws java.io.IOException
Returns an URL-encoded UTF8 encoding of the string. Beware, many web servers (incl. Tomcat) default to assuming URLs are ISO-8859-1

Throws:
java.io.IOException


Copyright © 2009-2010 Eagle-I. All Rights Reserved.