org.webharvest.utils
Class CommonUtil

java.lang.Object
  extended by org.webharvest.utils.CommonUtil

public class CommonUtil
extends java.lang.Object

Basic evaluation utilities


Nested Class Summary
static class CommonUtil.IntPair
          Contains pair of intger values
 
Constructor Summary
CommonUtil()
           
 
Method Summary
static java.lang.String adaptFilename(java.lang.String filePath)
           
static int countChars(java.lang.String text, char ch, int from, int to)
          Counts number of specified characters in give text.
static Variable createVariable(java.lang.Object value)
          Creates appropriate AbstractVariable instance for the specified object.
static java.lang.String encodeUrl(java.lang.String url, java.lang.String charset)
           
static java.lang.String escapeXml(java.lang.String s)
          Escapes XML string - special characters: &'"<> are replaced with XML escape sequences: & ' " < >
static boolean existsInStringArray(java.lang.String[] array, java.lang.String s, boolean caseSensitive)
          Checks if specified string exists in given array
static int findValueInCollection(java.util.Collection c, java.lang.Object value)
          Searches specified value in given collection
static java.lang.String fullUrl(java.lang.String pageUrl, java.lang.String link)
          Calculates full URL for specified page URL and link which could be full, absolute or relative like there can be found in A or IMG tags.
static java.lang.String getAbsoluteFilename(java.lang.String workingPath, java.lang.String filePath)
          For the goven working path and file path returns absolute file path.
static boolean getBooleanValue(java.lang.String value, boolean defaultValue)
          Reads boolean value from string
static java.lang.String getClassName(java.lang.Object o)
          Returns class name without packages for the specified object
static java.lang.String getDirectoryFromPath(java.lang.String path)
          Extracts a filename and directory from an absolute path.
static double getDoubleValue(java.lang.String value, double defaultValue)
          Reads double value from string
static java.lang.String getFileFromPath(java.lang.String path)
          Extracts a filename from an absolute path.
static int getIntValue(java.lang.String value, int defaultValue)
          Reads integer value from string
static java.lang.String getValidIdentifier(java.lang.String value)
          For the given string creates valid identifier name.
static boolean isBooleanTrue(java.lang.String value)
          Checks if specified string value represents boolean true value.
static boolean isEmpty(java.lang.String s)
           
static boolean isEmptyString(java.lang.Object o)
           
static boolean isFullUrl(java.lang.String link)
          Checks if specified link is full URL.
static boolean isPathAbsolute(java.lang.String path)
          Checks if specified file path is absolute.
static boolean isValidXmlIdentifier(java.lang.String name)
          Checks if given string is valid XML identifier, i.e.
static java.lang.String nvl(java.lang.Object value, java.lang.String defaultValue)
           
static byte[] readBytesFromFile(java.io.File file)
           
static java.lang.String readStringFromFile(java.io.File file, java.lang.String encoding)
           
static java.lang.String readStringFromUrl(java.net.URL url)
          Reads content from specified URL
static java.lang.String replicate(java.lang.String s, int count)
           
static void saveStringToFile(java.io.File file, java.lang.String content, java.lang.String charset)
          Saves specified content to the file with specified charset.
static java.lang.String serializeItem(net.sf.saxon.om.Item item)
          Serializes item after XPath or XQuery processor execution using Saxon.
static java.lang.String[] tokenize(java.lang.String s, java.lang.String delimiters)
          Tokenize given string for specified delimiter(s).
static java.lang.String[] tokenize(java.lang.String s, java.lang.String delimiters, boolean trimTokens, boolean allowEmptyTokens)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CommonUtil

public CommonUtil()
Method Detail

isEmptyString

public static boolean isEmptyString(java.lang.Object o)

nvl

public static java.lang.String nvl(java.lang.Object value,
                                   java.lang.String defaultValue)

adaptFilename

public static java.lang.String adaptFilename(java.lang.String filePath)

isEmpty

public static boolean isEmpty(java.lang.String s)

isValidXmlIdentifier

public static boolean isValidXmlIdentifier(java.lang.String name)
Checks if given string is valid XML identifier, i.e. it can be valid XML tag or attribute name.

Parameters:
name - String to be checked
Returns:
True if string is valid XML identifier, false otherwise.

isPathAbsolute

public static boolean isPathAbsolute(java.lang.String path)
Checks if specified file path is absolute. Criteria for recogning absolute file paths is that i starts with /, \, or X: where X is some letter.

Parameters:
path -
Returns:
True, if specified filepath is absolute, false otherwise.

getAbsoluteFilename

public static java.lang.String getAbsoluteFilename(java.lang.String workingPath,
                                                   java.lang.String filePath)
For the goven working path and file path returns absolute file path.

Parameters:
workingPath -
filePath -
Returns:
Absolute path of the second parameter according to absolute working path.

getDirectoryFromPath

public static java.lang.String getDirectoryFromPath(java.lang.String path)
Extracts a filename and directory from an absolute path.


getFileFromPath

public static java.lang.String getFileFromPath(java.lang.String path)
Extracts a filename from an absolute path.


getClassName

public static java.lang.String getClassName(java.lang.Object o)
Returns class name without packages for the specified object


replicate

public static java.lang.String replicate(java.lang.String s,
                                         int count)

encodeUrl

public static java.lang.String encodeUrl(java.lang.String url,
                                         java.lang.String charset)

isBooleanTrue

public static boolean isBooleanTrue(java.lang.String value)
Checks if specified string value represents boolean true value.

Returns:
If specified string equals (ignoring case) to 1, true or yes then true, otherwise false.

getBooleanValue

public static boolean getBooleanValue(java.lang.String value,
                                      boolean defaultValue)
Reads boolean value from string

Parameters:
value -
defaultValue - value to be returned if string value is not recognized

getIntValue

public static int getIntValue(java.lang.String value,
                              int defaultValue)
Reads integer value from string

Parameters:
value -
defaultValue - value to be returned if string value is not valid integer

getDoubleValue

public static double getDoubleValue(java.lang.String value,
                                    double defaultValue)
Reads double value from string

Parameters:
value -
defaultValue - value to be returned if string value is not valid double

escapeXml

public static java.lang.String escapeXml(java.lang.String s)
Escapes XML string - special characters: &'"<> are replaced with XML escape sequences: & ' " < >


serializeItem

public static java.lang.String serializeItem(net.sf.saxon.om.Item item)
                                      throws net.sf.saxon.trans.XPathException
Serializes item after XPath or XQuery processor execution using Saxon.

Throws:
net.sf.saxon.trans.XPathException

readStringFromFile

public static java.lang.String readStringFromFile(java.io.File file,
                                                  java.lang.String encoding)
                                           throws java.io.IOException
Throws:
java.io.IOException

saveStringToFile

public static void saveStringToFile(java.io.File file,
                                    java.lang.String content,
                                    java.lang.String charset)
                             throws java.io.IOException
Saves specified content to the file with specified charset.

Parameters:
file -
content -
charset -
Throws:
java.io.IOException
java.io.UnsupportedEncodingException

readBytesFromFile

public static byte[] readBytesFromFile(java.io.File file)
                                throws java.io.IOException
Throws:
java.io.IOException

isFullUrl

public static boolean isFullUrl(java.lang.String link)
Checks if specified link is full URL.

Parameters:
link -
Returns:
True, if full URl, false otherwise.

fullUrl

public static java.lang.String fullUrl(java.lang.String pageUrl,
                                       java.lang.String link)
Calculates full URL for specified page URL and link which could be full, absolute or relative like there can be found in A or IMG tags.


createVariable

public static Variable createVariable(java.lang.Object value)
Creates appropriate AbstractVariable instance for the specified object. For colleactions and arrays ListVariable instance is returned, for null it is an EmptyVariable, and for others it is NodeVariable that wraps specified object.

Parameters:
value -

readStringFromUrl

public static java.lang.String readStringFromUrl(java.net.URL url)
                                          throws java.io.IOException
Reads content from specified URL

Parameters:
url -
Returns:
Read content as string.
Throws:
java.io.IOException

countChars

public static int countChars(java.lang.String text,
                             char ch,
                             int from,
                             int to)
Counts number of specified characters in give text.

Parameters:
text - Text to be parsed
ch - Character to be counted
from - Text offset
to - Text end
Returns:
Number of character occurences in given text.

existsInStringArray

public static boolean existsInStringArray(java.lang.String[] array,
                                          java.lang.String s,
                                          boolean caseSensitive)
Checks if specified string exists in given array

Parameters:
array - Array of strings
s - String to be looked for in array
caseSensitive - Tells whether search is case sensitive
Returns:
True if string is found in array, false otherwise

tokenize

public static java.lang.String[] tokenize(java.lang.String s,
                                          java.lang.String delimiters)
Tokenize given string for specified delimiter(s).

Parameters:
s - String to be tokenized
delimiters - Delimiter character(s)
Returns:
Array of token strings

tokenize

public static java.lang.String[] tokenize(java.lang.String s,
                                          java.lang.String delimiters,
                                          boolean trimTokens,
                                          boolean allowEmptyTokens)

getValidIdentifier

public static java.lang.String getValidIdentifier(java.lang.String value)
For the given string creates valid identifier name. All invalid characters are transformed to underscores, and valid characters are preserved.

Parameters:
value - String to be transformed to valid identifier
Returns:
Valid identifier name made of specified string.

findValueInCollection

public static int findValueInCollection(java.util.Collection c,
                                        java.lang.Object value)
Searches specified value in given collection

Parameters:
c - Collection to be searched
value - Object searched for
Returns:
First index in collection of object found, or -1 if collection doesn't contain it