messif.objects.extraction
Class Extractors

java.lang.Object
  extended by messif.objects.extraction.Extractors

public abstract class Extractors
extends java.lang.Object

Collection of utility methods for Extractors.


Constructor Summary
Extractors()
           
 
Method Summary
static
<T extends LocalAbstractObject>
Extractor<T>
cast(java.lang.Object extractorInstance, java.lang.Class<? extends T> extractedClass)
          Returns a type-safe cast of a given extractor instance.
static
<T extends LocalAbstractObject>
Extractor<T>
createExternalExtractor(java.lang.Class<? extends T> objectClass, java.lang.String command)
          Creates an extractor that creates objects from binary data by external command.
static
<T extends LocalAbstractObject>
Extractor<T>
createExternalExtractor(java.lang.Class<? extends T> objectClass, java.lang.String command, boolean fileAsArgument)
          Creates an extractor that creates objects from binary data by external command.
static Extractor<?> createExtractor(java.lang.Class<?> usingClass)
          Creates extractor for the provided class.
static
<T extends LocalAbstractObject>
Extractor<T>
createTextExtractor(java.lang.Class<? extends T> objectClass, java.lang.Object... additionalArguments)
          Creates an extractor that creates objects from text InputStream using the constructor that takes BufferedReader as argument.
static
<T extends LocalAbstractObject>
T
extract(Extractor<? extends T> extractor, java.io.File file)
          Extracts object from the file using the given extractor.
static
<T extends LocalAbstractObject>
T
extract(Extractor<? extends T> extractor, java.net.URL url, java.lang.String mimeTypeRegexp)
          Extracts object downloaded from the url using the given extractor.
static byte[] readStreamData(java.io.InputStream inputStream, int maxBytes)
          Read data from input stream into a byte buffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Extractors

public Extractors()
Method Detail

readStreamData

public static byte[] readStreamData(java.io.InputStream inputStream,
                                    int maxBytes)
                             throws java.io.IOException
Read data from input stream into a byte buffer. If the maxBytes parameter is greater than zero, then no more than maxBytes will be read from the input stream. Otherwise, the buffer will contain all the data from the input stream until the end-of-stream.

Note that the stream is not closed

.

Parameters:
inputStream - the stream from which to read the data
maxBytes - maximal number of bytes to read from the stream (unlimited if less or equal to zero)
Returns:
a buffer containing the data
Throws:
java.io.IOException - if there was a problem reading from the input stream

cast

public static <T extends LocalAbstractObject> Extractor<T> cast(java.lang.Object extractorInstance,
                                                                java.lang.Class<? extends T> extractedClass)
                                                     throws java.lang.ClassCastException
Returns a type-safe cast of a given extractor instance.

Type Parameters:
T - the class of objects the extractor creates
Parameters:
extractorInstance - the instance to cast
extractedClass - the class of objects the extractor creates
Returns:
a cast extractor
Throws:
java.lang.ClassCastException - if the specified extractorInstance is not an Extractor or it extracts an incompatible class

extract

public static <T extends LocalAbstractObject> T extract(Extractor<? extends T> extractor,
                                                        java.net.URL url,
                                                        java.lang.String mimeTypeRegexp)
                                             throws ExtractorException
Extracts object downloaded from the url using the given extractor.

Type Parameters:
T - the type of object returned by the extractor
Parameters:
extractor - the extractor to use on the data
url - the URL from which to download the data
mimeTypeRegexp - regular expression for the mimetype of the data on the given url
Returns:
the object extracted from the data downloaded from the given url
Throws:
ExtractorException - if there was an error reading or extracting the data

extract

public static <T extends LocalAbstractObject> T extract(Extractor<? extends T> extractor,
                                                        java.io.File file)
                                             throws ExtractorException
Extracts object from the file using the given extractor.

Type Parameters:
T - the type of object returned by the extractor
Parameters:
extractor - the extractor to use on the data
file - the file from which to load the data
Returns:
the object extracted from the file
Throws:
ExtractorException - if there was an error reading or extracting the data

createTextExtractor

public static <T extends LocalAbstractObject> Extractor<T> createTextExtractor(java.lang.Class<? extends T> objectClass,
                                                                               java.lang.Object... additionalArguments)
                                                                    throws java.lang.IllegalArgumentException
Creates an extractor that creates objects from text InputStream using the constructor that takes BufferedReader as argument.

Type Parameters:
T - the class of object that is created by the extractor
Parameters:
objectClass - the class of object that is created by the extractor
additionalArguments - additional arguments for the constructor
Returns:
object created by the extractor
Throws:
java.lang.IllegalArgumentException - if the objectClass has no valid constructor

createExternalExtractor

public static <T extends LocalAbstractObject> Extractor<T> createExternalExtractor(java.lang.Class<? extends T> objectClass,
                                                                                   java.lang.String command)
                                                                        throws java.lang.IllegalArgumentException
Creates an extractor that creates objects from binary data by external command. The command is executed using the specified cmdarray and is expected to receive the binary data on its standard input and return the text parsable by the constructor of objectClass on its standard output.

Type Parameters:
T - the class of object that is created by the extractor
Parameters:
objectClass - the class of object that is created by the extractor
command - the external command (including all necessary arguments)
Returns:
object created by the extractor
Throws:
java.lang.IllegalArgumentException - if the objectClass has no valid constructor

createExternalExtractor

public static <T extends LocalAbstractObject> Extractor<T> createExternalExtractor(java.lang.Class<? extends T> objectClass,
                                                                                   java.lang.String command,
                                                                                   boolean fileAsArgument)
                                                                        throws java.lang.IllegalArgumentException
Creates an extractor that creates objects from binary data by external command. The command is executed using the specified cmdarray and is expected to receive the binary data on its standard input if fileAsArgument is true or the data are read from file that is passed as "%s" argument to the external command if fileAsArgument is false. The extractor must return the text parsable by the constructor of objectClass on its standard output.

Type Parameters:
T - the class of object that is created by the extractor
Parameters:
objectClass - the class of object that is created by the extractor
command - the external command (including all necessary arguments)
fileAsArgument - if true, the "%s" argument of external command is replaced with the filename
Returns:
object created by the extractor
Throws:
java.lang.IllegalArgumentException - if the objectClass has no valid constructor

createExtractor

public static Extractor<?> createExtractor(java.lang.Class<?> usingClass)
Creates extractor for the provided class. If the class is a descendant of LocalAbstractObject, a text extractor is returned. If the class implements Extractor interface, a new instance of this class is returned (using nullary constructor).

Parameters:
usingClass - the class used to create the extractor
Returns:
extractor for the usingClass