com.myjavatools.lib
Class Web

java.lang.Object
  extended by com.myjavatools.lib.Web

public class Web
extends java.lang.Object


Field Summary
protected static java.util.regex.Pattern HTML_CHARSET_PATTERN
           
protected static java.util.regex.Pattern XML_CHARSET_PATTERN
           
 
Constructor Summary
Web()
           
 
Method Summary
static java.lang.String downloadFile(java.lang.String url, java.lang.String filename)
          Downloads a file from a specified URL
static java.lang.String downloadFile(java.net.URL url, java.lang.String filename)
          Downloads a file from a specified URL
static java.lang.String getCharsetByEncoding(java.lang.String encoding)
          Deprecated. should use java.nio.Charset
static java.lang.String getEncodingByCharset(java.lang.String charset)
          gets Java encoding given an IANA charset
static java.lang.String getHtmlCharset(java.lang.CharSequence s)
          Detects charset from html contents
static java.lang.String getHtmlEncoding(java.lang.CharSequence s)
          gets Java encoding of HTML contents
static java.io.InputStream getUrlInputStream(java.net.URL url)
          gets an input stream for a given url
static java.lang.String getXmlCharset(java.lang.CharSequence s)
          Detects charset from xml contents
static java.lang.String getXmlEncoding(java.lang.CharSequence s)
          gets Java encoding of XML data
static java.lang.String quote(java.lang.String s)
          surrounds a string with single quotes (apostrophes), which is convenient for generating Javascript code, but just senseless otherwise.
static void sendMail(java.lang.String from, java.lang.String to, java.lang.String subj, java.lang.String message)
          sendMail sends emails.
static java.lang.String toWebReadable(java.lang.String s)
          converts a string into something that could be literally placed into a web page, that is, replaces CR with CRLF (to be polite with browser's 'view source'), replaces all '<' with "<", and html-encodes (&#dddd;) characters above low-ascii.
static java.lang.String url(java.lang.String href, java.util.Collection nvPairList)
          produces a GET request string for a uri and a collection of parameters
static java.lang.String url(java.lang.String uri, java.lang.String... paramvalue)
          produces a GET request string for a uri and request parameters
static java.lang.String urlEncode(java.lang.String name, java.lang.String value)
          converts a name-value pair into an element of url string
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

HTML_CHARSET_PATTERN

protected static java.util.regex.Pattern HTML_CHARSET_PATTERN

XML_CHARSET_PATTERN

protected static java.util.regex.Pattern XML_CHARSET_PATTERN
Constructor Detail

Web

public Web()
Method Detail

getHtmlCharset

public static java.lang.String getHtmlCharset(java.lang.CharSequence s)
Detects charset from html contents

Parameters:
s - the char sequence with html content
Returns:
charset name

Examples:
  • getHtmlCharset("") returns "shift-jis".

  • getXmlCharset

    public static java.lang.String getXmlCharset(java.lang.CharSequence s)
    Detects charset from xml contents

    Parameters:
    s - the char sequence with html content
    Returns:
    charset name

    Examples:
  • getXmlCharset("<?xml version=\"1.0\" encoding=\"UTF-8\"?>") returns "utf-8".

  • getCharsetByEncoding

    public static java.lang.String getCharsetByEncoding(java.lang.String encoding)
    Deprecated. should use java.nio.Charset

    gets IANA charset given Java encoding

    Parameters:
    encoding - the Java charset
    Returns:
    encoding name

    getEncodingByCharset

    public static java.lang.String getEncodingByCharset(java.lang.String charset)
    gets Java encoding given an IANA charset

    Parameters:
    charset - the IANA charset
    Returns:
    encoding name

    getHtmlEncoding

    public static java.lang.String getHtmlEncoding(java.lang.CharSequence s)
    gets Java encoding of HTML contents

    Parameters:
    s - HTML data
    Returns:
    its most probable Java encoding

    Examples:
  • getHtmlCharset("") returns "SJIS".

  • getXmlEncoding

    public static java.lang.String getXmlEncoding(java.lang.CharSequence s)
    gets Java encoding of XML data

    Parameters:
    s - XML data (first line is enough)
    Returns:
    Java encoding to decode the data

    Example:
  • getXmlEncoding("?xml version=\"1.0\" encoding=\"UTF-8"?>") will return "UTF8".

  • getUrlInputStream

    public static java.io.InputStream getUrlInputStream(java.net.URL url)
                                                 throws java.io.IOException,
                                                        java.lang.InstantiationException
    gets an input stream for a given url

    Parameters:
    url - the universal resource locator
    Returns:
    the input stream
    Throws:
    java.io.IOException
    java.lang.InstantiationException -

    Example:
  • getUrlInputStream(new URL("http://www.google.com/images/logo.gif")) will return an input stream that contains the Google gif.

  • downloadFile

    public static java.lang.String downloadFile(java.net.URL url,
                                                java.lang.String filename)
    Downloads a file from a specified URL

    Parameters:
    url - URL that points to a resource
    filename - the name of the file to store the contents
    Returns:
    error message; an empty string if none

    Example:
  • downloadFile(new URL("http://www.google.com/images/logo.gif"), "googlelogo.gif") will download the gif, store it into the file, and return an empty string.

  • downloadFile

    public static java.lang.String downloadFile(java.lang.String url,
                                                java.lang.String filename)
    Downloads a file from a specified URL

    Parameters:
    url - URL string that points to a resource
    filename - the name of the file to store the contents
    Returns:
    error message; an empty string if none

    Example:
  • downloadFile("http://www.google.com/images/logo.gif", "googlelogo.gif") will download the gif, store it into the file, and return an empty string.

  • sendMail

    public static void sendMail(java.lang.String from,
                                java.lang.String to,
                                java.lang.String subj,
                                java.lang.String message)
                         throws java.net.MalformedURLException,
                                java.io.IOException
    sendMail sends emails. Borrowed from Slavik Dimitrovich, Richmond, Virginia

    Parameters:
    from - sender address
    to - receiver address
    subj - message subject
    message - message body
    Throws:
    java.io.MalformedURLException
    java.io.IOException
    java.net.MalformedURLException
    See Also:
    The original article

    urlEncode

    public static final java.lang.String urlEncode(java.lang.String name,
                                                   java.lang.String value)
    converts a name-value pair into an element of url string

    Parameters:
    name - parameter name
    value - parameter value
    Returns:
    a string like "name=value", where value is url-encoded
    returns an empty string if value is empty

    Example:
  • urlEncode("dir", "C:\\Program Files"") will return "dir=C%3A%5CProgram+Files".

  • url

    public static final java.lang.String url(java.lang.String href,
                                             java.util.Collection nvPairList)
    produces a GET request string for a uri and a collection of parameters

    Parameters:
    href - url where to send the request
    nvPairList - a collection of (already url-encoded) name-value pair strings that look like "name=value"
    Returns:
    formatted url string, see example

    Example:
  • urlEncode("http://example.myjavatools.com/mycomputer", new ArrayList(new String[] {"dir=C%3A%5CProgram+Files", "cmd=dir+*"})) will return "http://example.myjavatools.com/mycomputer?dir=C%3A%5CProgram+Files&cmd=dir+*".

  • url

    public static final java.lang.String url(java.lang.String uri,
                                             java.lang.String... paramvalue)
    produces a GET request string for a uri and request parameters

    Parameters:
    uri - where to send the request
    paramvalue - String... (etc)
    Returns:
    formatted url string, see examples

    Examples:
  • urlEncode("http://example.myjavatools.com/mycomputer", "dir, "C:\Program Files")) will return "http://example.myjavatools.com/mycomputer?dir=C%3A%5CProgram+Files".
  • urlEncode("http://example.myjavatools.com/mycomputer", "dir, "C:\Program Files", "cmd", "dir *") will return "http://example.myjavatools.com/mycomputer?dir=C%3A%5CProgram+Files&cmd=dir+*".
  • urlEncode("http://mycomputer", "dir, "C:\Program Files", "cmd", "dir *", "login", "root", "password", "urowned") will return "http://mycomputer?dir=C%3A%5CProgram+Files&cmd=dir+*&login=root&password=urowned".

  • toWebReadable

    public static final java.lang.String toWebReadable(java.lang.String s)
    converts a string into something that could be literally placed into a web page, that is, replaces CR with CRLF (to be polite with browser's 'view source'), replaces all '<' with "<", and html-encodes (&#dddd;) characters above low-ascii.

    Parameters:
    s - string to convert
    Returns:
    converted string

    quote

    public static final java.lang.String quote(java.lang.String s)
    surrounds a string with single quotes (apostrophes), which is convenient for generating Javascript code, but just senseless otherwise.

    Parameters:
    s - string to quote
    Returns:
    "'" + s + "'"