org.eaglei.repository.util
Class Utils

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

public final class Utils
extends java.lang.Object

Utility methods

Version:
$Id: $
Author:
Larry Stone

Method Summary
static java.lang.String collectionDeepToString(java.util.Collection<? extends java.lang.Object> c)
          Returns a string representation of the "deep contents" of the specified Collection, rendering each element with its toString().
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 java.lang.String escapeHTMLAttribute(java.lang.String in)
          Escape a character string for HTML or XML attribute value: (assumes you're using double-quotes to delineate the value) 1.
static org.openrdf.model.URI getHomeGraph(org.openrdf.repository.RepositoryConnection rc, org.openrdf.model.URI resource)
          Get a resource instance's home graph, ideally where its *asserted* rdf:type statements live - inferred types don't count.
static java.lang.String getMavenVersionFromJar(java.lang.String jarURLspec)
          Get the Maven version string, if available, from a resource URL containing a Jar archive.
static java.lang.String getVersionInfo(org.openrdf.repository.RepositoryConnection rc, org.openrdf.model.URI subject, org.openrdf.model.URI graph)
          Get the owl:versionInfo value (as String) for the given subject and named graph.
static java.lang.String indent(java.lang.String source, int indent)
          Create a string where every line is indented by the specified number of spaces.
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 join(java.lang.String glue, java.lang.String[] as)
          Like perl join()
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 java.lang.String makePreciseHTTPDate(java.util.Date d)
          Returns a Date formatted in a more precise version of the standard HTTP date format - the same as the conventional standard format but with milliseconds added to the time.
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)
          Parse a date string in the XML date format, return a Calendar
static java.lang.String prettyPrint(org.openrdf.query.Dataset ds)
          Prettyprint the dataset with no ident.
static java.lang.String prettyPrint(org.openrdf.query.Dataset ds, int indent)
          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.
static java.lang.String valueAsString(org.openrdf.model.Value val)
          Return the "most readable" string representation of a Sesame RDF Value object - Value can be URI, BNode, or Literal; in the case of a Literal it just returns hte string content, no datatype or language annotations.
 
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

prettyPrint

public static java.lang.String prettyPrint(org.openrdf.query.Dataset ds,
                                           int indent)
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.
indent - number of spaces to indent
Returns:
multi-line prettyprint in a String object.

prettyPrint

public static java.lang.String prettyPrint(org.openrdf.query.Dataset ds)
Prettyprint the dataset with no ident.

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)
Parse a date string in the XML date format, return a Calendar

Parameters:
raw - the string to parse
Returns:
Calendar representing the given date, never null (throws on error)

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.

Parameters:
mimeType - internet media type string
charset - string representation of default characer set.
Returns:
a more completely qualified Internet Media Type (MIME Type) string

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

Parameters:
ct - content type string (MIME type)
dflt - default value of charset
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

Parameters:
ct - fully qualified content type (MIME type)
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.

Parameters:
ba - byte array to print
Returns:
hex representation of bytes in ba

valueAsString

public static java.lang.String valueAsString(org.openrdf.model.Value val)
Return the "most readable" string representation of a Sesame RDF Value object - Value can be URI, BNode, or Literal; in the case of a Literal it just returns hte string content, no datatype or language annotations.

Parameters:
val - incoming Value object
Returns:
String rendition of the value optimized for clarity

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

Parameters:
in - string to be URLencoded
Returns:
encoding of string, never null
Throws:
java.io.IOException

escapeHTMLAttribute

public static java.lang.String escapeHTMLAttribute(java.lang.String in)
Escape a character string for HTML or XML attribute value: (assumes you're using double-quotes to delineate the value) 1. Escape the ampersands '&' to '&' 2. Escape double-quotes '"' to '"'

Parameters:
in - the source string
Returns:
escaped string

getMavenVersionFromJar

public static java.lang.String getMavenVersionFromJar(java.lang.String jarURLspec)
                                               throws java.io.IOException
Get the Maven version string, if available, from a resource URL containing a Jar archive. Depends on the Maven conventions that: (a) the jar contains a file named along the pattenr "META-INF/maven/GROUPID/ARTIFACTID/pom.properties", and (b) this file contains a serialized Properties list, which (c) has a property named "version". Note that upon failure, there will be an explanation in the log (level WARN) of why it failed.

Parameters:
jarURLspec - the URL where the jar may be found
Returns:
version string or null if not found for various reasons.
Throws:
java.io.IOException

getVersionInfo

public static java.lang.String getVersionInfo(org.openrdf.repository.RepositoryConnection rc,
                                              org.openrdf.model.URI subject,
                                              org.openrdf.model.URI graph)
                                       throws org.openrdf.repository.RepositoryException
Get the owl:versionInfo value (as String) for the given subject and named graph. Named graph can be null to search all graphs. Try searching on both the OWL and repo-internal version predicates.

Parameters:
rc - sesame repository connection
subject - subject URI about which to query
graph - named graph on which to look
Returns:
string value of owl:versionInfo statement or null if none found.
Throws:
org.openrdf.repository.RepositoryException

getHomeGraph

public static org.openrdf.model.URI getHomeGraph(org.openrdf.repository.RepositoryConnection rc,
                                                 org.openrdf.model.URI resource)
                                          throws org.openrdf.repository.RepositoryException
Get a resource instance's home graph, ideally where its *asserted* rdf:type statements live - inferred types don't count. NOte this does NOT impose access controls.

Parameters:
rc - the sesame repository connection
resource - - the URI of the resorce
Returns:
URI of home graph or null if not found for any reason
Throws:
org.openrdf.repository.RepositoryException

collectionDeepToString

public static java.lang.String collectionDeepToString(java.util.Collection<? extends java.lang.Object> c)
Returns a string representation of the "deep contents" of the specified Collection, rendering each element with its toString(). String elements are enclosed in double-quotes. The whole collection is surrounded in curly braces and elements are separated by comma.

Parameters:
c - the collection to print
Returns:
string representation of the collection.

makePreciseHTTPDate

public static java.lang.String makePreciseHTTPDate(java.util.Date d)
Returns a Date formatted in a more precise version of the standard HTTP date format - the same as the conventional standard format but with milliseconds added to the time.

Parameters:
d - the Date to format
Returns:
formatted string representation of the date

join

public static java.lang.String join(java.lang.String glue,
                                    java.lang.String[] as)
Like perl join()

Parameters:
glue -
as -
Returns:

indent

public static java.lang.String indent(java.lang.String source,
                                      int indent)
Create a string where every line is indented by the specified number of spaces. The newline (\n) character is assumed to break to a new line.

Parameters:
source - string to indent
indent - number of spaces
Returns:
a new string containing the indented text


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