org.mycore.datamodel.ifs2
Class MCRContent

java.lang.Object
  extended by org.mycore.datamodel.ifs2.MCRContent

public class MCRContent
extends Object

Used to read/write content from any source to any target. Sources and targets can be strings, local files, Apache VFS file objects, XML documents, byte[] arrays and streams. MCRContent can only be consumed once, otherwise the getters throw MCRUsageException. Use makeCopies() to avoid this. The underlying input stream is closed after consumption, excepted you use getInputStream(), of course.

Author:
Frank Lützenkirchen

Field Summary
protected  boolean consumed
          If true, this content already was used and cannot be used again
protected  InputStream in
          The content itself
protected  boolean isXML
          If true, we are absolutely sure that source is XML
 
Method Summary
 byte[] asByteArray()
          Returns the raw content
 String asString()
          Returns content as String, assuming UTF-8 encoding
 String asString(String encoding)
          Returns the content as String, assuming the provided encoding
 Document asXML()
          Parses content, assuming it is XML, and returns the parsed document.
protected  void checkConsumed()
          Ensures that this content is not already consumed, because it can only be used once.
 MCRContent ensureXML()
          Ensures that content is XML
 MCRContentInputStream getContentInputStream()
          Returns content as content input stream, which provides MD5 functionality.
 InputStream getInputStream()
          Returns content as input stream.
 MCRContent[] makeCopies(int numCopies)
          Makes copies of the content, consuming this content
static MCRContent readFrom(byte[] bytes)
          Creates content from byte[] arrray
static MCRContent readFrom(Document xml)
          Creates content from XML document.
static MCRContent readFrom(File file)
          Creates content from a local file
static MCRContent readFrom(org.apache.commons.vfs.FileObject fo)
          Creates content from Apache VFS file object
static MCRContent readFrom(InputStream in)
          Creates new content from input stream
static MCRContent readFrom(String text)
          Creates content from a String, using UTF-8 encoding
static MCRContent readFrom(String text, String encoding)
          Creates content from a String, using the given encoding
static MCRContent readFrom(URL url)
          Creates new content reading from the given URL.
 void sendTo(File target)
          Sends content to the given local file
 void sendTo(org.apache.commons.vfs.FileObject target)
          Sends the content to the given file object
 void sendTo(OutputStream out)
          Sends content to the given OutputStream
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

in

protected InputStream in
The content itself


consumed

protected boolean consumed
If true, this content already was used and cannot be used again


isXML

protected boolean isXML
If true, we are absolutely sure that source is XML

Method Detail

readFrom

public static MCRContent readFrom(String text)
                           throws IOException,
                                  UnsupportedEncodingException
Creates content from a String, using UTF-8 encoding

Parameters:
text - the content
Throws:
IOException
UnsupportedEncodingException

readFrom

public static MCRContent readFrom(String text,
                                  String encoding)
                           throws IOException,
                                  UnsupportedEncodingException
Creates content from a String, using the given encoding

Parameters:
text - the content
encoding - the encoding to be used to write bytes
Throws:
IOException
UnsupportedEncodingException

readFrom

public static MCRContent readFrom(File file)
                           throws IOException
Creates content from a local file

Parameters:
file - the local file to read
Throws:
IOException

readFrom

public static MCRContent readFrom(org.apache.commons.vfs.FileObject fo)
                           throws org.apache.commons.vfs.FileSystemException
Creates content from Apache VFS file object

Parameters:
fo - the file object to read content from
Throws:
org.apache.commons.vfs.FileSystemException

readFrom

public static MCRContent readFrom(byte[] bytes)
                           throws IOException
Creates content from byte[] arrray

Parameters:
bytes - the content's bytes
Throws:
IOException

readFrom

public static MCRContent readFrom(Document xml)
                           throws Exception
Creates content from XML document. Content will be written pretty-formatted, using UTF-8 encoding and line indentation.

Parameters:
xml - the XML document to read in as content
Throws:
Exception

readFrom

public static MCRContent readFrom(InputStream in)
Creates new content from input stream

Parameters:
in - the input stream to read content from

readFrom

public static MCRContent readFrom(URL url)
                           throws org.apache.commons.vfs.FileSystemException
Creates new content reading from the given URL.

Parameters:
url - the url to read content from
Throws:
org.apache.commons.vfs.FileSystemException

ensureXML

public MCRContent ensureXML()
                     throws Exception
Ensures that content is XML

Throws:
Exception

getInputStream

public InputStream getInputStream()
Returns content as input stream. Be sure to close this stream properly!

Returns:
input stream to read content from

getContentInputStream

public MCRContentInputStream getContentInputStream()
Returns content as content input stream, which provides MD5 functionality. Be sure to close this stream properly!

Returns:
the content input stream

sendTo

public void sendTo(OutputStream out)
            throws IOException
Sends content to the given OutputStream

Parameters:
out - the OutputStream to write the content to
Throws:
IOException

sendTo

public void sendTo(File target)
            throws IOException
Sends content to the given local file

Parameters:
target - the file to write the content to
Throws:
IOException

sendTo

public void sendTo(org.apache.commons.vfs.FileObject target)
            throws IOException,
                   org.apache.commons.vfs.FileSystemException
Sends the content to the given file object

Parameters:
target - the file to write the content to
Throws:
IOException
org.apache.commons.vfs.FileSystemException

asXML

public Document asXML()
               throws JDOMException,
                      IOException
Parses content, assuming it is XML, and returns the parsed document.

Returns:
the XML document parsed from content
Throws:
JDOMException
IOException

asByteArray

public byte[] asByteArray()
                   throws IOException
Returns the raw content

Returns:
the content
Throws:
IOException

asString

public String asString(String encoding)
                throws IOException,
                       UnsupportedEncodingException
Returns the content as String, assuming the provided encoding

Parameters:
encoding - the encoding to use to build the characters
Returns:
content as String
Throws:
IOException
UnsupportedEncodingException

asString

public String asString()
                throws IOException,
                       UnsupportedEncodingException
Returns content as String, assuming UTF-8 encoding

Returns:
content as String
Throws:
IOException
UnsupportedEncodingException

checkConsumed

protected void checkConsumed()
Ensures that this content is not already consumed, because it can only be used once.


makeCopies

public MCRContent[] makeCopies(int numCopies)
                        throws IOException
Makes copies of the content, consuming this content

Parameters:
numCopies - the number of copies to make
Returns:
copies of the content
Throws:
IOException