org.mycore.common
Class MCRUtils

java.lang.Object
  extended by org.mycore.common.MCRUtils

public class MCRUtils
extends Object

This class represent a general set of external methods to support the programming API.

Version:
$Revision: 14750 $ $Date: 2009-02-17 16:36:38 +0100 (Tue, 17 Feb 2009) $
Author:
Jens Kupferschmidt, Frank Lützenkirchen, Thomas Scheffler (yagee)

Field Summary
static char COMMAND_AND
           
static char COMMAND_OR
           
static char COMMAND_XOR
           
 
Constructor Summary
MCRUtils()
           
 
Method Summary
static String arrayToString(Object[] objArray, String seperator)
           
static boolean copyReader(Reader source, Writer target)
          Copies all content read from the given input stream to the given output stream.
static boolean copyStream(InputStream source, OutputStream target)
          Copies all content read from the given input stream to the given output stream.
static GregorianCalendar covertDateToGregorianCalendar(String indate)
          The methode convert the input date string to the GregorianCalendar.
static String covertDateToISO(String indate)
          The methode convert the input date string to the ISO output string.
static
<T> ArrayList<T>
cutArrayList(ArrayList<? extends T> arrayin, int maxitems)
          The method cut an ArrayList for a maximum of items.
static ArrayList<String> getAllDirectoryNames(File basedir)
          The method return a list of all directory names under the given directory and subdirectories of itself.
static ArrayList<String> getAllDirectoryNames(File basedir, String path)
          The method return a list of all directory names under the given directory and subdirectories of itself.
static ArrayList<String> getAllFileNames(File basedir)
          The method return a list of all file names under the given directory and subdirectories of itself.
static ArrayList<String> getAllFileNames(File basedir, String path)
          The method return a list of all file names under the given directory and subdirectories of itself.
static byte[] getByteArray(Document jdom)
          This method convert a JDOM tree to a byte array.
static String[] getStringArray(Object[] objects)
          Converts an Array of Objects to an Array of Strings using the toString() method.
static String[] getStringArray(Object[] objects, int maxitems)
          Converts an Array of Objects to an Array of Strings using the toString() method.
static boolean isSupportedLang(String lang)
          This method check the language string base on RFC 1766 to the supported languages in mycore.
static
<T> HashSet<T>
mergeHashSets(HashSet<? extends T> set1, HashSet<? extends T> set2, char operation)
          merges to HashSets of MyCoreIDs after specific rules
static String parseDocumentType(InputStream in)
           
static int readBlocking(InputStream in, byte[] b, int off, int len)
          Reads exactly len bytes from the input stream into the byte array.
static int readBlocking(Reader in, char[] c, int off, int len)
          Reads exactly len bytes from the input stream into the byte array.
static String replaceString(String in, String find, String newStr)
           Returns String in with newStr substituted for find String.
static String stringToXML(String in)
          This methode replace any characters to XML entity references.
static void writeElementToFile(Element elm, File xml)
          The method wrap the org.jdom.Element in a org.jdom.Document and write it to a file.
static void writeElementToSysout(Element elm)
          The method wrap the org.jdom.Element in a org.jdom.Document and write it to Sysout.
static void writeJDOMToFile(Document jdom, File xml)
          The method write a given JDOM Document to a file.
static void writeJDOMToSysout(Document jdom)
          The method write a given JDOM Document to the system output.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COMMAND_OR

public static final char COMMAND_OR
See Also:
Constant Field Values

COMMAND_AND

public static final char COMMAND_AND
See Also:
Constant Field Values

COMMAND_XOR

public static final char COMMAND_XOR
See Also:
Constant Field Values
Constructor Detail

MCRUtils

public MCRUtils()
Method Detail

isSupportedLang

public static final boolean isSupportedLang(String lang)
This method check the language string base on RFC 1766 to the supported languages in mycore.

Parameters:
lang - the language string
Returns:
true if the language was supported, otherwise false

covertDateToISO

public static final String covertDateToISO(String indate)
The methode convert the input date string to the ISO output string. If the input can't convert, the output is null.

Parameters:
indate - the date input
Returns:
the ISO output or null

covertDateToGregorianCalendar

public static final GregorianCalendar covertDateToGregorianCalendar(String indate)
The methode convert the input date string to the GregorianCalendar. If the input can't convert, the output is null.

Parameters:
indate - the date input
Returns:
the GregorianCalendar or null

stringToXML

public static final String stringToXML(String in)
This methode replace any characters to XML entity references.

Parameters:
in - a string
Returns:
the converted string.

getByteArray

public static final byte[] getByteArray(Document jdom)
                                 throws MCRPersistenceException
This method convert a JDOM tree to a byte array.

Parameters:
jdom - the JDOM tree
Returns:
a byte array of the JDOM tree
Throws:
MCRPersistenceException

getStringArray

public static final String[] getStringArray(Object[] objects)
Converts an Array of Objects to an Array of Strings using the toString() method.

Parameters:
objects - Array of Objects to be converted
Returns:
Array of Strings representing Objects

getStringArray

public static final String[] getStringArray(Object[] objects,
                                            int maxitems)
Converts an Array of Objects to an Array of Strings using the toString() method.

Parameters:
objects - Array of Objects to be converted
maxitems - The maximum of items to convert
Returns:
Array of Strings representing Objects

copyStream

public static boolean copyStream(InputStream source,
                                 OutputStream target)
Copies all content read from the given input stream to the given output stream. Note that this method will NOT close the streams when finished copying.

Parameters:
source - the InputStream to read the bytes from
target - out the OutputStream to write the bytes to, may be null
Returns:
true if Inputstream copied successfully to OutputStream

copyReader

public static boolean copyReader(Reader source,
                                 Writer target)
Copies all content read from the given input stream to the given output stream. Note that this method will NOT close the streams when finished copying.

Parameters:
source - the InputStream to read the bytes from
target - out the OutputStream to write the bytes to, may be null
Returns:
true if Inputstream copied successfully to OutputStream

mergeHashSets

public static final <T> HashSet<T> mergeHashSets(HashSet<? extends T> set1,
                                                 HashSet<? extends T> set2,
                                                 char operation)
merges to HashSets of MyCoreIDs after specific rules

Parameters:
set1 - 1st HashSet to be merged
set2 - 2nd HashSet to be merged
operation - available COMMAND_XYZ
Returns:
merged HashSet
See Also:
COMMAND_OR, COMMAND_AND, COMMAND_XOR

cutArrayList

public static final <T> ArrayList<T> cutArrayList(ArrayList<? extends T> arrayin,
                                                  int maxitems)
The method cut an ArrayList for a maximum of items.

Parameters:
arrayin - The incoming ArrayList
maxitem - The maximum number of items
Returns:
the cutted ArrayList

readBlocking

public static final int readBlocking(InputStream in,
                                     byte[] b,
                                     int off,
                                     int len)
                              throws IOException
Reads exactly len bytes from the input stream into the byte array. This method reads repeatedly from the underlying stream until all the bytes are read. InputStream.read is often documented to block like this, but in actuality it does not always do so, and returns early with just a few bytes. readBlockiyng blocks until all the bytes are read, the end of the stream is detected, or an exception is thrown. You will always get as many bytes as you asked for unless you get an eof or other exception. Unlike readFully, you find out how many bytes you did get.

Parameters:
b - the buffer into which the data is read.
off - the start offset of the data.
len - the number of bytes to read.
Returns:
number of bytes actually read.
Throws:
IOException - if an I/O error occurs.

readBlocking

public static final int readBlocking(Reader in,
                                     char[] c,
                                     int off,
                                     int len)
                              throws IOException
Reads exactly len bytes from the input stream into the byte array. This method reads repeatedly from the underlying stream until all the bytes are read. Reader.read is often documented to block like this, but in actuality it does not always do so, and returns early with just a few bytes. readBlockiyng blocks until all the bytes are read, the end of the stream is detected, or an exception is thrown. You will always get as many bytes as you asked for unless you get an eof or other exception. Unlike readFully, you find out how many bytes you did get.

Parameters:
c - the buffer into which the data is read.
off - the start offset of the data.
len - the number of bytes to read.
Returns:
number of bytes actually read.
Throws:
IOException - if an I/O error occurs.

replaceString

public static String replaceString(String in,
                                   String find,
                                   String newStr)

Returns String in with newStr substituted for find String.

Parameters:
in - String to edit
find - string to match
newStr - string to substitude for find

writeElementToFile

public static final void writeElementToFile(Element elm,
                                            File xml)
The method wrap the org.jdom.Element in a org.jdom.Document and write it to a file.

Parameters:
elm - the JDOM Document
xml - the File instance

writeJDOMToFile

public static final void writeJDOMToFile(Document jdom,
                                         File xml)
The method write a given JDOM Document to a file.

Parameters:
jdom - the JDOM Document
xml - the File instance

writeElementToSysout

public static final void writeElementToSysout(Element elm)
The method wrap the org.jdom.Element in a org.jdom.Document and write it to Sysout.

Parameters:
elm - the JDOM Document

writeJDOMToSysout

public static final void writeJDOMToSysout(Document jdom)
The method write a given JDOM Document to the system output.

Parameters:
jdom - the JDOM Document

getAllFileNames

public static ArrayList<String> getAllFileNames(File basedir)
The method return a list of all file names under the given directory and subdirectories of itself.

Parameters:
basedir - the File instance of the basic directory
Returns:
an ArrayList with file names as pathes

getAllFileNames

public static ArrayList<String> getAllFileNames(File basedir,
                                                String path)
The method return a list of all file names under the given directory and subdirectories of itself.

Parameters:
basedir - the File instance of the basic directory
path - the part of directory path
Returns:
an ArrayList with file names as pathes

getAllDirectoryNames

public static ArrayList<String> getAllDirectoryNames(File basedir)
The method return a list of all directory names under the given directory and subdirectories of itself.

Parameters:
basedir - the File instance of the basic directory
Returns:
an ArrayList with directory names as pathes

getAllDirectoryNames

public static ArrayList<String> getAllDirectoryNames(File basedir,
                                                     String path)
The method return a list of all directory names under the given directory and subdirectories of itself.

Parameters:
basedir - the File instance of the basic directory
path - the part of directory path
Returns:
an ArrayList with directory names as pathes

arrayToString

public static String arrayToString(Object[] objArray,
                                   String seperator)

parseDocumentType

public static String parseDocumentType(InputStream in)