org.apache.stanbol.enhancer.servicesapi.helper
Class ContentItemHelper

java.lang.Object
  extended by org.apache.stanbol.enhancer.servicesapi.helper.ContentItemHelper

public class ContentItemHelper
extends java.lang.Object

Helper class to factorize common code for ContentItem handling.

Author:
ogrisel

Field Summary
static int MAX_BUF_SIZE
           
static int MIN_BUF_SIZE
           
static java.lang.String SHA1
           
static java.lang.String UTF8
           
 
Constructor Summary
ContentItemHelper()
           
 
Method Summary
static org.apache.clerezza.rdf.core.UriRef ensureUri(ContentItem ci)
          Check that ContentItem#getId returns a valid URI or make an urn out of it.
static java.util.Map.Entry<org.apache.clerezza.rdf.core.UriRef,Blob> getBlob(ContentItem ci, java.util.Set<java.lang.String> mimeTypes)
          Searches an content part of the type Blob with one of the the parsed mimeTypes.
static
<T> java.util.LinkedHashMap<org.apache.clerezza.rdf.core.UriRef,T>
getContentParts(ContentItem ci, java.lang.Class<T> clazz)
          Returns a Map with the current content parts of the parsed type.
static java.lang.String getMimeTypeWithParameters(Blob blob)
          Creates the "{type}/{subtime}; [{param}={value}]+" mime type representation for the Blob.getMimeType() and Blob.getParameter() values
static java.lang.String getText(Blob blob)
          Getter for the Text of an Blob.
static org.apache.clerezza.rdf.core.UriRef makeDefaultUri(java.lang.String baseUri, Blob blob)
           
static org.apache.clerezza.rdf.core.UriRef makeDefaultUri(java.lang.String baseUri, byte[] data)
           
static org.apache.clerezza.rdf.core.UriRef makeDefaultUri(java.lang.String baseUri, java.io.InputStream in)
           
static org.apache.clerezza.rdf.core.UriRef makeDefaultUrn(Blob blob)
           
static org.apache.clerezza.rdf.core.UriRef makeDefaultUrn(byte[] data)
           
static org.apache.clerezza.rdf.core.UriRef makeDefaultUrn(java.io.InputStream in)
           
static java.util.Map<java.lang.String,java.lang.String> parseMimeType(java.lang.String mimeTypeString)
          This parses and validates the mime-type and parameters from the parsed mimetype string based on the definition as defined in rfc2046.
static java.lang.String streamDigest(java.io.InputStream in, java.io.OutputStream out, java.lang.String digestAlgorithm)
          Pass the binary content from in to out (if not null) while computing the digest.
static java.lang.String toHexString(byte[] data)
           
static java.lang.String urlEncode(java.lang.String uriPart)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SHA1

public static final java.lang.String SHA1
See Also:
Constant Field Values

MIN_BUF_SIZE

public static final int MIN_BUF_SIZE
See Also:
Constant Field Values

MAX_BUF_SIZE

public static final int MAX_BUF_SIZE
See Also:
Constant Field Values

UTF8

public static final java.lang.String UTF8
See Also:
Constant Field Values
Constructor Detail

ContentItemHelper

public ContentItemHelper()
Method Detail

ensureUri

public static org.apache.clerezza.rdf.core.UriRef ensureUri(ContentItem ci)
Check that ContentItem#getId returns a valid URI or make an urn out of it.


urlEncode

public static java.lang.String urlEncode(java.lang.String uriPart)

streamDigest

public static java.lang.String streamDigest(java.io.InputStream in,
                                            java.io.OutputStream out,
                                            java.lang.String digestAlgorithm)
                                     throws java.io.IOException
Pass the binary content from in to out (if not null) while computing the digest. Digest can typically be used to build ContentItem ids that map the binary content of the array.

Parameters:
in - stream to read the data from
out - optional output stream to
digestAlgorithm - MD5 or SHA1 for instance
Returns:
an hexadecimal representation of the digest
Throws:
java.io.IOException

toHexString

public static java.lang.String toHexString(byte[] data)

makeDefaultUrn

public static org.apache.clerezza.rdf.core.UriRef makeDefaultUrn(Blob blob)

makeDefaultUrn

public static org.apache.clerezza.rdf.core.UriRef makeDefaultUrn(java.io.InputStream in)

makeDefaultUrn

public static org.apache.clerezza.rdf.core.UriRef makeDefaultUrn(byte[] data)

makeDefaultUri

public static org.apache.clerezza.rdf.core.UriRef makeDefaultUri(java.lang.String baseUri,
                                                                 Blob blob)

makeDefaultUri

public static org.apache.clerezza.rdf.core.UriRef makeDefaultUri(java.lang.String baseUri,
                                                                 byte[] data)

makeDefaultUri

public static org.apache.clerezza.rdf.core.UriRef makeDefaultUri(java.lang.String baseUri,
                                                                 java.io.InputStream in)

parseMimeType

public static java.util.Map<java.lang.String,java.lang.String> parseMimeType(java.lang.String mimeTypeString)
This parses and validates the mime-type and parameters from the parsed mimetype string based on the definition as defined in rfc2046.

The mime-type is stored as value for the null key. Parameter keys are converted to lower case. Values are stored as defined in the parsed media type. Parameters with empty key, empty or no values are ignored.

Parameters:
mimeTypeString - the media type formatted as defined by rfc2046
Returns:
A map containing the mime-type under the null key and all parameters with lower case keys and values.
Throws:
java.lang.IllegalArgumentException - if the parsed mimeTypeString is null, empty or the parsed mime-type is empty, does not define non empty '{type}/{sub-type}' or uses a wildcard for the type or sub-type.

getBlob

public static java.util.Map.Entry<org.apache.clerezza.rdf.core.UriRef,Blob> getBlob(ContentItem ci,
                                                                                    java.util.Set<java.lang.String> mimeTypes)
Searches an content part of the type Blob with one of the the parsed mimeTypes.

NOTE:

In contrast to the contentPart related methods of the ContentItem this method does NOT throw NoSuchPartException.

Parameters:
ci - the contentITem
mimeTypes - List of possible mimeTypes
Returns:
the URI and the content of the content part or null if not found
Throws:
java.lang.IllegalArgumentException - If the parsed ContentItem is null or the parsed Set with the mimeTypes is null or empty.

getContentParts

public static <T> java.util.LinkedHashMap<org.apache.clerezza.rdf.core.UriRef,T> getContentParts(ContentItem ci,
                                                                                                 java.lang.Class<T> clazz)
Returns a Map with the current content parts of the parsed type. future changes to the contentParts of the content item will NOT be reflected within the returned map. The ordering of the Iterators over the returned map is consistent with the ordering of the contentPart within the ContentItem.

When parsing Object as class the number of the element will be equals to the index of that content part.

In contrast to the contentPart related methods of the ContentItem this method does NOT throw NoSuchPartException.

Parameters:
ci - the content item
clazz - the class of the content part
Returns:
the Map with the ids and the content as entries.

getText

public static java.lang.String getText(Blob blob)
                                throws java.io.IOException
Getter for the Text of an Blob. This method respects the "charset" if present in the parameter of the Blob.

Parameters:
blob - the Blob. MUST NOT be null.
Returns:
the text
Throws:
java.io.IOException - on any exception while reading from the InputStream provided by the Blob.
java.lang.IllegalArgumentException - if the parsed Blob is null

getMimeTypeWithParameters

public static java.lang.String getMimeTypeWithParameters(Blob blob)
Creates the "{type}/{subtime}; [{param}={value}]+" mime type representation for the Blob.getMimeType() and Blob.getParameter() values

Parameters:
blob - the Blob
Returns:
the mime type with parameters (e.g. text/plain;charset=UTF-8)


Copyright © 2010-2012 The Apache Software Foundation. All Rights Reserved.