cz.vity.freerapid.plugins.webclient.utils
Class PlugUtils

java.lang.Object
  extended by cz.vity.freerapid.plugins.webclient.utils.PlugUtils

public final class PlugUtils
extends Object

Author:
Ladislav Vitasek

Constructor Summary
PlugUtils()
           
 
Method Summary
static void addParameters(org.apache.commons.httpclient.methods.PostMethod postMethod, String content, String[] parameters)
          Parses content and search for parameter's value in tag form.
static boolean find(String regexp, String contentString)
          Help method to test whether given regular expression matches given content string.
static long getFileSizeFromString(String value)
          Parses input string and converts it into bytes.
static int getNumberBetween(String content, String stringBefore, String stringAfter)
          Returns number between 2 other strings.
static String getParameter(String name, String content)
          Find and return value of given parameter in html tag (eg. input).
static String getStringBetween(String content, String stringBefore, String stringAfter)
          Returns string between 2 other strings.
static int getWaitTimeBetween(String content, String stringBefore, String stringAfter, TimeUnit srcTimeUnit)
          Returns time value in seconds between 2 other strings.
static void checkFileSize(HttpFile file, String content, String fileSizeBefore, String fileSizeAfter)
          Extracts file name from the site
static void checkName(HttpFile file, String content, String fileNameBefore, String fileNameAfter)
          Extracts file name from the site.
static Matcher matcher(String regexp, String contentString)
          Help method to get Matcher for given regular expression and string.
static String recognize(BufferedImage image, String commandLineOptions)
          Method calls GOCR implementation to recognize text from image.
static String replaceEntities(String s)
          Replace entity \& with character &.
static String unescapeHtml(String str)
          Unescapes a string containing entity escapes to a string containing the actual Unicode characters corresponding to the escapes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PlugUtils

public PlugUtils()
Method Detail

getFileSizeFromString

public static long getFileSizeFromString(String value)
Parses input string and converts it into bytes.
Acceptable input:
1.35 Gb, 0.5 Mb 5 465kB, 45654 6544 bytes, 54654654, 280B, also buggy 280BB - default value is B
Function is not case sensitive. Spaces among numbers are not important (they are removed).
All ',' are converted to '.'

Parameters:
value - input string parsed from page
Returns:
filesize in bytes

matcher

public static Matcher matcher(String regexp,
                              String contentString)
Help method to get Matcher for given regular expression and string.
Pattern is case sensitive.

Parameters:
regexp - regular expression
contentString - string that is searched for regular expression pattern
Returns:
See Also:
Pattern

find

public static boolean find(String regexp,
                           String contentString)
Help method to test whether given regular expression matches given content string.
Pattern is case sensitive.

Parameters:
regexp - regular expression
contentString - string that is searched for regular expression pattern
Returns:
true if the given pattern was found in given string, false otherwise
See Also:
Pattern

unescapeHtml

public static String unescapeHtml(String str)

Unescapes a string containing entity escapes to a string containing the actual Unicode characters corresponding to the escapes. Supports HTML 4.0 entities.

For example, the string "&lt;Fran&ccedil;ais&gt;" will become "<Français>"

If an entity is unrecognized, it is left alone, and inserted verbatim into the result string. e.g. "&gt;&zzzz;x" will become ">&zzzz;x".

Parameters:
str - the String to unescape, may be null
Returns:
a new unescaped String, null if null string input

getParameter

public static String getParameter(String name,
                                  String content)
                           throws PluginImplementationException

Find and return value of given parameter in html tag (eg. input).

For example, the for content "input type="hidden" name="par" value="val>" and parameter "par" returns "val"

Parameters:
name - name of parameter
content - String to search in
Returns:
String value of parameter
Throws:
PluginImplementationException - given name not found in given content

addParameters

public static void addParameters(org.apache.commons.httpclient.methods.PostMethod postMethod,
                                 String content,
                                 String[] parameters)
                          throws PluginImplementationException
Parses content and search for parameter's value in tag form.
Html <form> should have structure name="paramName" .... value="paramValue"
This parameter and its value are added to POST method.

Parameters:
postMethod - method to add found parameters
content - String to search in
parameters - form parameter names
Throws:
PluginImplementationException - any of the parameter were not found in given content
See Also:
getParameter(String, String)

replaceEntities

public static String replaceEntities(String s)

Replace entity \& with character &.

Used in partly decode given URL, where unescapeHtml is not suitable

Parameters:
s - String where replace
Returns:
String with replacement

recognize

public static String recognize(BufferedImage image,
                               String commandLineOptions)
Method calls GOCR implementation to recognize text from image.

Parameters:
image - an image from which text should be recognized
commandLineOptions - additional command line options for GOCR application
Returns:
text that was recognized, or null if there was an error during calling of GOCR application

checkName

public static void checkName(HttpFile file,
                             String content,
                             String fileNameBefore,
                             String fileNameAfter)
                      throws PluginImplementationException
Extracts file name from the site. White space around file name is trimmed.

Parameters:
file - file to apply found file name
content - content to search
fileNameBefore - string before file name, character '\n' is replaced as \\s*
fileNameAfter - string after file name, character '\n' is replaced as \\s*
Throws:
PluginImplementationException - file name was not found
Since:
0.82

checkFileSize

public static void checkFileSize(HttpFile file,
                                 String content,
                                 String fileSizeBefore,
                                 String fileSizeAfter)
                          throws PluginImplementationException
Extracts file name from the site

Parameters:
file - file to apply found file name
content - content to search
fileSizeBefore - string before file name - without white space characters on the RIGHT side, character '\n' is replaced as \\s*
fileSizeAfter - string after file name - without white space characters on the LEFT side, character '\n' is replaced as \\s*
Throws:
PluginImplementationException - file size string was not found
Since:
0.82

getStringBetween

public static String getStringBetween(String content,
                                      String stringBefore,
                                      String stringAfter)
                               throws PluginImplementationException
Returns string between 2 other strings.

Parameters:
content - searched content
stringBefore - string before searched string - without white space characters on the RIGHT side
stringAfter - string after searched string - without white space characters on the LEFT side
Returns:
found string - result is trimmed
Throws:
PluginImplementationException - No string between stringBefore and stringAfter

getNumberBetween

public static int getNumberBetween(String content,
                                   String stringBefore,
                                   String stringAfter)
                            throws PluginImplementationException
Returns number between 2 other strings.

Parameters:
content - searched content
stringBefore - string before searched string - without white space characters on the RIGHT side
stringAfter - string after searched string - without white space characters on the LEFT side
Returns:
found number
Throws:
PluginImplementationException - No number between stringBefore and stringAfter

getWaitTimeBetween

public static int getWaitTimeBetween(String content,
                                     String stringBefore,
                                     String stringAfter,
                                     TimeUnit srcTimeUnit)
                              throws PluginImplementationException
Returns time value in seconds between 2 other strings.

Parameters:
content - searched content
stringBefore - string before searched string - without white space characters on the RIGHT side
stringAfter - string after searched string - without white space characters on the LEFT side
srcTimeUnit - source time unit - usually TimeUnit.SECONDS or TimeUnit.MILLISECONDS
Returns:
time value in seconds
Throws:
PluginImplementationException - No wait time value between stringBefore and stringAfter