com.myjavatools.lib
Class Web

java.lang.Object
  extended bycom.myjavatools.lib.Objects
      extended bycom.myjavatools.lib.Strings
          extended bycom.myjavatools.lib.Files
              extended bycom.myjavatools.lib.Web
Direct Known Subclasses:
Tools

public class Web
extends Files


Nested Class Summary
 
Nested classes inherited from class com.myjavatools.lib.Files
Files.BufferingFilter, Files.ByteFilter
 
Field Summary
 
Fields inherited from class com.myjavatools.lib.Files
directoryFilter, FIND_ALL, FIND_DIRECTORY, FIND_FILE
 
Fields inherited from class com.myjavatools.lib.Strings
_FALSE_, _TRUE_, _UNDEF_, ESCAPED, ESCAPEE
 
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)
          Deprecated. should use java.nio.Charset
static java.lang.String getHtmlCharset(java.lang.CharSequence s)
          Detects charset from html contents
static java.lang.String getHtmlEncoding(java.lang.CharSequence s)
          Deprecated. should use java.nio.Charset
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)
          Deprecated. should use java.nio.Charset
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 param, java.lang.String value)
          produces a GET request string for a uri and a request parameter
static java.lang.String url(java.lang.String uri, java.lang.String param1, java.lang.String value1, java.lang.String param2, java.lang.String value2)
          produces a GET request string for a uri and two request parameters
static java.lang.String url(java.lang.String uri, java.lang.String param1, java.lang.String value1, java.lang.String param2, java.lang.String value2, java.lang.String param3, java.lang.String value3)
          produces a GET request string for a uri and three request parameters
static java.lang.String url(java.lang.String uri, java.lang.String param1, java.lang.String value1, java.lang.String param2, java.lang.String value2, java.lang.String param3, java.lang.String value3, java.lang.String param4, java.lang.String value4)
          produces a GET request string for a uri and four request parameters
static java.lang.String url(java.lang.String uri, java.lang.String param1, java.lang.String value1, java.lang.String param2, java.lang.String value2, java.lang.String param3, java.lang.String value3, java.lang.String param4, java.lang.String value4, java.lang.String param5, java.lang.String value5)
          produces a GET request string for a uri and five request parameters
static java.lang.String url(java.lang.String uri, java.lang.String param1, java.lang.String value1, java.lang.String param2, java.lang.String value2, java.lang.String param3, java.lang.String value3, java.lang.String param4, java.lang.String value4, java.lang.String param5, java.lang.String value5, java.lang.String param6, java.lang.String value6)
          produces a GET request string for a uri and six 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 com.myjavatools.lib.Files
adjustSizeByMooreLaw, appendBytesToFile, appendBytesToFile, appendToFile, appendToFile, appendToFile, chdir, compare, copy, copy, copy, copy, deleteFile, deleteFile, dirname, dirname, filename, find, find, find, findLatest, findLatest, findLatestDirectory, findLatestFile, getcwd, getFullPath, getFullPath, getPackageName, install, install, lastModified, listSubdirectories, makeFile, makeFile, makeFile, makeFile, makeFile, makeFile, makeFile, makeFile, makeFileWriter, path, pipe, pipe, pipe, readBytesFromFile, readBytesFromStream, readBytesFromStream, readString, readStringFromFile, readStringFromFile, readStringFromFile, relPath, splitPath, synchronize, synchronize, unzip, writeBytesToFile, writeToFile, writeToFile, writeToFile, writeToFile
 
Methods inherited from class com.myjavatools.lib.Strings
asProperties, countChar, countLeadingSpaces, countTrailingSpaces, crc32, crcreport, crcreport, decode, decode, decodeJavaString, encode, extractValue, fill, findDiff, format, format, format, grep, grep, hasAlpha, hexDump, hexDump, hexDump, htmlEncode, indexOf, indexOf, indexOf, indexOf, isAlmostEmpty, isAlpha, isTrue, isVowel, join, join, lastIndexOf, needsEncoding, needsEncoding, oneOf, oneOf, oneOf, pack, replace, sgmlEntity, split, startsWith, textHeight, textWidth, toBoolean, toCEncoding, toCEncoding, toHex, toHex, toHex, toHex, toHex, toHexReadable, toHexReadable, toHexReadable, toHexReadable, toJavaEncoding, toJavaEncoding, toJavaEncoding, toJavaEncoding, toJavaEncoding, toJavaEncoding, toJavaHexEncoding, toJavaHexEncoding, toJavaOctalEncoding, toPropertiesEncoding, toPropertiesEncoding, toReadable, toReadable, toSgmlEncoding, toSgmlEncoding, toString, toStrings, unpack, unzip, unzip, unzip, wordCount, write, zip, zip2bytes, zip8bit
 
Methods inherited from class com.myjavatools.lib.Objects
asMap, cat, compose, crc32, crc32, getList, getMap, getMap, getSet, indexOf, indexOf, indexOf, indexOf, inverse, isEmpty, map, map, map, map, map, reallocate, toBytes, toBytes, toChars, toMap, toMap, toMap, toMap, toSet, union
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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)
    Deprecated. should use java.nio.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)
    Deprecated. should use java.nio.Charset

    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)
    Deprecated. should use java.nio.Charset

    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 param,
                                             java.lang.String value)
    produces a GET request string for a uri and a request parameter

    Parameters:
    uri - where to send the request
    param - parameter name
    value - parameter value
    Returns:
    formatted url string, see example

    Example:
  • urlEncode("http://example.myjavatools.com/mycomputer", "dir, "C:\Program Files")) will return "http://example.myjavatools.com/mycomputer?dir=C%3A%5CProgram+Files".

  • url

    public static final java.lang.String url(java.lang.String uri,
                                             java.lang.String param1,
                                             java.lang.String value1,
                                             java.lang.String param2,
                                             java.lang.String value2)
    produces a GET request string for a uri and two request parameters

    Parameters:
    uri - where to send the request
    param1 - parameter1 name
    value1 - parameter1 value
    param2 - parameter2 name
    value2 - parameter2 value
    Returns:
    formatted url string, see example

    Example:
  • 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+*".

  • url

    public static final java.lang.String url(java.lang.String uri,
                                             java.lang.String param1,
                                             java.lang.String value1,
                                             java.lang.String param2,
                                             java.lang.String value2,
                                             java.lang.String param3,
                                             java.lang.String value3)
    produces a GET request string for a uri and three request parameters

    Parameters:
    uri - where to send the request
    param1 - parameter1 name
    value1 - parameter1 value
    param2 - parameter2 name
    value2 - parameter2 value
    param3 - parameter3 name
    value3 - parameter3 value
    Returns:
    formatted url string, see example

    Example:
  • urlEncode("http://mycomputer", "dir, "C:\Program Files", "cmd", "dir *", "login", "") will return "http://mycomputer?dir=C%3A%5CProgram+Files&cmd=dir+*".

  • url

    public static final java.lang.String url(java.lang.String uri,
                                             java.lang.String param1,
                                             java.lang.String value1,
                                             java.lang.String param2,
                                             java.lang.String value2,
                                             java.lang.String param3,
                                             java.lang.String value3,
                                             java.lang.String param4,
                                             java.lang.String value4)
    produces a GET request string for a uri and four request parameters

    Parameters:
    uri - where to send the request
    param1 - parameter1 name
    value1 - parameter1 value
    param2 - parameter2 name
    value2 - parameter2 value
    param3 - parameter3 name
    value3 - parameter3 value
    param4 - parameter4 name
    value4 - parameter4 value
    Returns:
    formatted url string, see example

    Example:
  • 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".

  • url

    public static final java.lang.String url(java.lang.String uri,
                                             java.lang.String param1,
                                             java.lang.String value1,
                                             java.lang.String param2,
                                             java.lang.String value2,
                                             java.lang.String param3,
                                             java.lang.String value3,
                                             java.lang.String param4,
                                             java.lang.String value4,
                                             java.lang.String param5,
                                             java.lang.String value5)
    produces a GET request string for a uri and five request parameters

    Parameters:
    uri - where to send the request
    param1 - parameter1 name
    value1 - parameter1 value
    param2 - parameter2 name
    value2 - parameter2 value
    param3 - parameter3 name
    value3 - parameter3 value
    param4 - parameter4 name
    value4 - parameter4 value
    param5 - parameter5 name
    value5 - parameter5 value
    Returns:
    formatted url string, see example

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

  • url

    public static final java.lang.String url(java.lang.String uri,
                                             java.lang.String param1,
                                             java.lang.String value1,
                                             java.lang.String param2,
                                             java.lang.String value2,
                                             java.lang.String param3,
                                             java.lang.String value3,
                                             java.lang.String param4,
                                             java.lang.String value4,
                                             java.lang.String param5,
                                             java.lang.String value5,
                                             java.lang.String param6,
                                             java.lang.String value6)
    produces a GET request string for a uri and six request parameters

    Parameters:
    uri - where to send the request
    param1 - parameter1 name
    value1 - parameter1 value
    param2 - parameter2 name
    value2 - parameter2 value
    param3 - parameter3 name
    value3 - parameter3 value
    param4 - parameter4 name
    value4 - parameter4 value
    param5 - parameter5 name
    value5 - parameter5 value
    param6 - parameter6 name
    value6 - parameter6 value
    Returns:
    formatted url string, see other examples

    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 + "'"