de.dfki.lt.hog.util
Class Utility

java.lang.Object
  extended by de.dfki.lt.hog.util.Utility

public class Utility
extends java.lang.Object

Utility class with static methods/constants. Created 2003-11-19


Field Summary
static java.lang.String ACID_ATTRIBUTE
           
protected static org.apache.log4j.Logger log
          A logger for Modules
static java.lang.String METADATA_ELEMENT
           
static java.lang.String SESSIONID_ATTRIBUTE
           
 
Constructor Summary
Utility()
           
 
Method Summary
static java.lang.String abstractList2String(java.util.AbstractList list, java.lang.String prefix, java.lang.String suffix)
           Converts objects of classes implementing Collection to a String.
static java.lang.String cleanXMLString(java.lang.String dirtyXMLString)
          Cleans an XML String by replacing '&', '<', '>' and '"' with '&amp;', '&lt;', '&gt;' and '&quot;', respectively, and by replacing all characters with ASCII code > 127 with the numeric representation.
static org.w3c.dom.Element conf2Element(org.w3c.dom.Document doc, java.util.Properties props)
          Creates a CONF Element from the contents of a given Properties instance.
static java.lang.String conf2Xml(java.util.Properties props)
          Creates a CONF XML String from the contents of a given Properties instance.
static byte[] convert(byte[] data, java.lang.String srcEncoding, java.lang.String targetEncoding)
           
static java.lang.String convert(java.lang.String str, java.lang.String fromEncoding, java.lang.String toEncoding)
           
static java.lang.String exception2String(java.lang.Exception e)
           
static java.lang.String extractHtmlElement(java.lang.String htmlDoc, java.lang.String tag)
          Returns the header part (<HEAD>...</HEAD>) of a given HTML document
static java.util.ArrayList findAllrx(java.lang.String regex, java.lang.String string)
          Uses the given regular expression to find all matching occurences and returns them as an ArrayList of Strings.
static java.lang.String formatDuration(java.util.Date duration)
          format duration encoded in Date object as String mm:ss,milliseconds
static java.lang.String formatDuration(long totalMillis)
          Format a time span given in milliseconds to a String.
static java.lang.String formatDurationShort(long totalMillis)
          Format a time span given in milliseconds to a String.
static java.lang.String formatTime()
          format date and time as ISO/RFC string
static java.lang.String getAttributeValue(java.lang.String inputString, java.lang.String attribute, int startpos)
           
static java.lang.String getComputedAnnotation(MoCoMan mocoman, java.lang.String inputAnnotation, java.lang.String seekAnnotationName)
          Get computed annotation from MoCoMan, taking sessionID and annotationCollection name from metadata of the input annotation The purpose of this method is to provide (from within a Module implementation) access to annotations other than the previous one.
static java.lang.String getNewLine()
           
static boolean isInterpunct(java.lang.String w)
          Checks for punctuation, returns true if w is a puncuation character.
static java.lang.String map2String(java.util.Map map)
          Converts a Map to a String.
static java.lang.String map2String(java.util.Map map, java.lang.String prefix)
          Converts a Map to a String.
static java.lang.String map2String(java.util.Map map, java.lang.String prefix, java.lang.String suffix)
          Converts a Map to a String.
static java.lang.String markSentenceBoundaries(java.lang.String jtokAnno, java.lang.String marker)
          Marks all sentence boundaries of a given text with a given marker.
static java.lang.String node2String(org.w3c.dom.Node node)
          Converts any given DOM Node instance to an XML String.
static java.lang.String readStringFromFile(java.lang.String sFilename, java.lang.String encoding)
          read string from file
static java.lang.String rmrsText(java.lang.String text)
          Write text to string containing <text><c>t</c><c>e</c><c>x</c><c>t</c></text> (for rmrs2html stylesheet).
static double roundTo(int decimals, double num)
          Rounds a number to the specified number of decimal places.
static java.util.ArrayList splitLines(java.lang.String str)
          Split a given string into an ArrayList of lines using "\n" as a separator
static java.util.ArrayList splitTabs(java.lang.String str)
          Splits a given string into an ArrayList of lines using "\t" as a separator
static org.w3c.dom.Element string2Element(java.lang.String xmlString)
          create DOM element from XML String
static java.lang.String unicodeTo7bit(java.lang.String str)
          Replaces all non-ascii-7 chars with char-entities.
static void writeStringToFile(java.lang.String sFilename, java.lang.String sContent)
          write string to file
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected static org.apache.log4j.Logger log
A logger for Modules


METADATA_ELEMENT

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

SESSIONID_ATTRIBUTE

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

ACID_ATTRIBUTE

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

Utility

public Utility()
Method Detail

getNewLine

public static java.lang.String getNewLine()

formatDuration

public static java.lang.String formatDuration(java.util.Date duration)
format duration encoded in Date object as String mm:ss,milliseconds

Parameters:
duration -
Returns:
String containing formatted duration

formatDuration

public static java.lang.String formatDuration(long totalMillis)
Format a time span given in milliseconds to a String. Example: 2 days 21 hours 11 minutes 32 seconds 382 milliseconds

Parameters:
totalMillis - The time span in milliseconds
Returns:
A formatted String

formatDurationShort

public static java.lang.String formatDurationShort(long totalMillis)
Format a time span given in milliseconds to a String. Short form. Example: 1T21:11:32

Parameters:
totalMillis - The time span in milliseconds
Returns:
A formatted String

formatTime

public static java.lang.String formatTime()
format date and time as ISO/RFC string

Returns:
String containing formatted current time and date

getComputedAnnotation

public static java.lang.String getComputedAnnotation(MoCoMan mocoman,
                                                     java.lang.String inputAnnotation,
                                                     java.lang.String seekAnnotationName)
Get computed annotation from MoCoMan, taking sessionID and annotationCollection name from metadata of the input annotation The purpose of this method is to provide (from within a Module implementation) access to annotations other than the previous one. The implementation via String parsing is an ugly hack.

Parameters:
mocoman - MoCoMan instance
inputAnnotation - annotation String that contains the previous annotation with metadata
seekAnnotationName - name of the annotation
Returns:
the computed annotation as String

getAttributeValue

public static java.lang.String getAttributeValue(java.lang.String inputString,
                                                 java.lang.String attribute,
                                                 int startpos)

readStringFromFile

public static java.lang.String readStringFromFile(java.lang.String sFilename,
                                                  java.lang.String encoding)
read string from file

Parameters:
sFilename -
encoding -
Returns:
text from file as String

writeStringToFile

public static void writeStringToFile(java.lang.String sFilename,
                                     java.lang.String sContent)
write string to file

Parameters:
sFilename -
sContent -

map2String

public static java.lang.String map2String(java.util.Map map)
Converts a Map to a String. Maps can be AbstractMap, Attributes, HashMap, Hashtable, IdentityHashMap, RenderingHints, TreeMap or WeakHashMap

Parameters:
map - The Map to be printed.
Returns:
The String representing the given Map.

map2String

public static java.lang.String map2String(java.util.Map map,
                                          java.lang.String prefix)
Converts a Map to a String. Maps can be AbstractMap, Attributes, HashMap, Hashtable, IdentityHashMap, RenderingHints, TreeMap or WeakHashMap. Each line will be followed by a line separator (e.g. "\n" for Linux).

Parameters:
map - The Map to be printed.
prefix - A String preceding each line.
Returns:
The String representing the given Map.

map2String

public static java.lang.String map2String(java.util.Map map,
                                          java.lang.String prefix,
                                          java.lang.String suffix)
Converts a Map to a String. Maps can be AbstractMap, Attributes, HashMap, Hashtable, IdentityHashMap, RenderingHints, TreeMap or WeakHashMap. Use prefix to add a String in front of each line.

Parameters:
map - The Map to be printed.
prefix - A String preceding each line.
suffix - A String following each line.
Returns:
The String representing the given Map.

abstractList2String

public static java.lang.String abstractList2String(java.util.AbstractList list,
                                                   java.lang.String prefix,
                                                   java.lang.String suffix)

Converts objects of classes implementing Collection to a String. Some known classes implementing interface Collection are LinkedList, ArrayList, Vector, Stack, HashSet, TreeSet, ...

Use prefix and suffix to add a String in front and at the end of each line respectively. Note that this method will not care about keys and values, but only about object entries in the collection and their String representaion directly.

Parameters:
coll - The Collection to be printed.
prefix - A String preceding each line.
suffix - A String following each line.
Returns:
The String representing the given Map.

cleanXMLString

public static final java.lang.String cleanXMLString(java.lang.String dirtyXMLString)
Cleans an XML String by replacing '&', '<', '>' and '"' with '&amp;', '&lt;', '&gt;' and '&quot;', respectively, and by replacing all characters with ASCII code > 127 with the numeric representation.

Parameters:
dirtyXMLString - The XML String to be cleaned
Returns:
The cleaned XML String

unicodeTo7bit

public static java.lang.String unicodeTo7bit(java.lang.String str)
Replaces all non-ascii-7 chars with char-entities.


node2String

public static java.lang.String node2String(org.w3c.dom.Node node)
Converts any given DOM Node instance to an XML String.

Parameters:
node - The DOM Node to be converted.
Returns:
The XML String representing the given Node

string2Element

public static org.w3c.dom.Element string2Element(java.lang.String xmlString)
create DOM element from XML String

Parameters:
xmlString - String to parse
Returns:
DOM Element

conf2Xml

public static java.lang.String conf2Xml(java.util.Properties props)
Creates a CONF XML String from the contents of a given Properties instance.

Parameters:
props - The Properties instance to be converted
Returns:
An XML String containing all information of props

conf2Element

public static org.w3c.dom.Element conf2Element(org.w3c.dom.Document doc,
                                               java.util.Properties props)
Creates a CONF Element from the contents of a given Properties instance.

Parameters:
doc - The Document the METADATE will be inserted in
props - The Properties instance to be converted
Returns:
A METADATA Element containing all information of props

splitLines

public static final java.util.ArrayList splitLines(java.lang.String str)
Split a given string into an ArrayList of lines using "\n" as a separator

Parameters:
str - input String containing newlines
Returns:
the split line Strings in an ArrayList

splitTabs

public static final java.util.ArrayList splitTabs(java.lang.String str)
Splits a given string into an ArrayList of lines using "\t" as a separator

Parameters:
str - input String containing tabs
Returns:
the split tab Strings in an ArrayList

isInterpunct

public static boolean isInterpunct(java.lang.String w)
Checks for punctuation, returns true if w is a puncuation character.

Parameters:
w - input String to tesdt for punctuation
Returns:
boolean true iff w is a punctuation character

rmrsText

public static java.lang.String rmrsText(java.lang.String text)
Write text to string containing <text><c>t</c><c>e</c><c>x</c><c>t</c></text> (for rmrs2html stylesheet).

Parameters:
s - String to convert
Returns:
the converted result as String

extractHtmlElement

public static java.lang.String extractHtmlElement(java.lang.String htmlDoc,
                                                  java.lang.String tag)
Returns the header part (<HEAD>...</HEAD>) of a given HTML document

Parameters:
HtmlDoc -
Returns:
The header part (<HEAD>...</HEAD>) of a given HTML document

exception2String

public static java.lang.String exception2String(java.lang.Exception e)

findAllrx

public static java.util.ArrayList findAllrx(java.lang.String regex,
                                            java.lang.String string)
Uses the given regular expression to find all matching occurences and returns them as an ArrayList of Strings.

Parameters:
regex - The regex to be matched.
string - The string to be scanned.
Returns:

convert

public static java.lang.String convert(java.lang.String str,
                                       java.lang.String fromEncoding,
                                       java.lang.String toEncoding)

convert

public static byte[] convert(byte[] data,
                             java.lang.String srcEncoding,
                             java.lang.String targetEncoding)

markSentenceBoundaries

public static java.lang.String markSentenceBoundaries(java.lang.String jtokAnno,
                                                      java.lang.String marker)
Marks all sentence boundaries of a given text with a given marker.


roundTo

public static double roundTo(int decimals,
                             double num)
Rounds a number to the specified number of decimal places.

Parameters:
decimals - the number of decimal places
num - the number to round
Returns:
the value rounded to the specified number of decimal places