Rakesh API

org.rakeshv.utils
Class StringUtilities

java.lang.Object
  extended by org.rakeshv.utils.StringUtilities

public final class StringUtilities
extends java.lang.Object

Provides utility methods to return String objects from commonly used resources.

Copyright 2004-2006 Rakesh Vidyadharan

Version:
$Id: StringUtilities.java,v 1.15 2006/03/14 22:55:36 rakesh Exp $
Author:
Rakesh Vidyadharan 2004 August 27

Field Summary
static java.lang.String endOfLine
          The end of line characters to use in the returned String objects.
 
Constructor Summary
private StringUtilities()
          Default constructor.
 
Method Summary
static java.lang.String fromFile(java.lang.String file)
          Return the contents of the specified file as a String using the system default encoding.
static java.lang.String fromFile(java.lang.String file, java.lang.String encoding)
          Return the contents of the specified file as a String using the character encoding specified.
static java.lang.String fromUrl(java.lang.String url)
          Return the response body from the specified HTTP URL as a String using the encoding specified by the web server.
static java.lang.String fromUrl(java.lang.String url, java.util.Properties properties)
          Return the response body from the specified HTTP URL as a String using the encoding specified by the web server.
static java.lang.String fromUrl(java.lang.String url, java.util.Properties properties, java.lang.String userName, java.lang.String password)
          Return the response body from the specified HTTP URL as a String using the encoding specified by the web server.
static java.lang.String fromUrl(java.lang.String url, java.lang.String userName, java.lang.String password)
          Return the response body from the specified HTTP URL as a String using the encoding specified by the web server.
static java.lang.String fromXML(java.lang.String content)
          Replace escaped XML/HTML special characters (&, <, ...) in the String specified with their original equivalents.
static java.lang.String parseEncoding(java.lang.String type)
          Parse the charset from the content-type HTTP header if it was specified.
private static java.lang.StringBuilder printTrace(java.lang.Throwable throwable, java.lang.StringBuilder builder)
          Return the stack trace for the specified instance of Throwable.
private static java.lang.String readData(java.io.BufferedReader reader)
          Returns all the data from the specified BufferedReader.
private static java.lang.String readResponseFromUrl(java.net.HttpURLConnection httpConnection)
          Read the response from the HttpURLConnection.
private static void setAuthenticator(java.lang.String userName, java.lang.String password)
          Create a new instance of Authenticator using the specified credentials.
static java.lang.String stackTrace(java.lang.Throwable throwable)
          Returns the StackTrace for the specified instance of Throwable.
static void toFile(java.lang.String content, java.lang.String file)
          Write the contents of the specified String to the file specified using the system default encoding.
static void toFile(java.lang.String content, java.lang.String file, java.lang.String encoding)
          Write the contents of the specified String to the file specified using the character encoding specified.
static java.lang.String toXML(java.lang.String content)
          Replace XML/HTML special characters (&, <, ...) in the String specified with their escaped equivalents.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

endOfLine

public static final java.lang.String endOfLine
The end of line characters to use in the returned String objects.

Constructor Detail

StringUtilities

private StringUtilities()
Default constructor. Cannot be instantiated.

Method Detail

fromUrl

public static final java.lang.String fromUrl(java.lang.String url)
                                      throws java.net.MalformedURLException,
                                             java.io.IOException
Return the response body from the specified HTTP URL as a String using the encoding specified by the web server.

The following code snippet shows how to use this method:

    import org.rakeshv.utils.StringUtilities;

        try
        {
          String result = StringUtilities.fromUrl( "http://www.w3.org/" );
        }
        catch ( Throwable t )
        {
          // Exception handling
        }
 

Parameters:
url - The URL from which the contents are to be retrieved.
Throws:
java.net.MalformedURLException - If the specified url is improperly formatted.
java.io.IOException - If errors are encountered while reading the data. An IOException is thrown with the error response from the server if a HTTP error is encountered.
See Also:
readResponseFromUrl( HttpURLConnection )

fromUrl

public static final java.lang.String fromUrl(java.lang.String url,
                                             java.util.Properties properties)
                                      throws java.net.MalformedURLException,
                                             java.io.IOException
Return the response body from the specified HTTP URL as a String using the encoding specified by the web server. The response is obtained by POST'ing the data specified in the Properties object.

The following code snippet shows how to use this method:

    import java.util.Properties;
    import org.rakeshv.utils.StringUtilities;

        Properties properties = new Properties();
        properties.setProperty( "page", "2" );
        properties.setProperty( "user", "scott" );
        properties.setProperty( "password", "tiger" );
        try
        {
          String result = StringUtilities.fromUrl( 
            "http://www.w3.org/", properties );
        }
        catch ( Throwable t )
        {
          // Exception handling
        }
 

Parameters:
url - The URL from which the contents are to be retrieved.
properties - A Properties object with the name-value pairs that are to be submitted to the server. A Properties object is used to ensure that the keys and values are String objects.
Throws:
java.net.MalformedURLException - If the specified url is improperly formatted.
java.io.IOException - If errors are encountered while reading the data. An IOException is thrown with the error response from the server if a HTTP error is encountered.
See Also:
readResponseFromUrl( HttpURLConnection )

fromUrl

public static final java.lang.String fromUrl(java.lang.String url,
                                             java.lang.String userName,
                                             java.lang.String password)
                                      throws java.net.MalformedURLException,
                                             java.io.IOException
Return the response body from the specified HTTP URL as a String using the encoding specified by the web server. Uses the specified credentials to authenticate the request with the server.

The following code snippet shows how to use this method:

    import org.rakeshv.utils.StringUtilities;

        try
        {
          String url = "http://www.w3.org/";
          String userName = "scott";
          String password = "tiger";
          String result = StringUtilities.fromUrl( url, userName, password );
        }
        catch ( Throwable t )
        {
          // Exception handling
        }
 

Parameters:
url - The URL from which the contents are to be retrieved.
userName - The user name to use for authentication with the server.
password - The password to use for authentication with the server.
Throws:
java.net.MalformedURLException - If the specified url is improperly formatted.
java.io.IOException - If errors are encountered while reading the data. An IOException is thrown with the error response from the server if a HTTP error is encountered.
See Also:
setAuthenticator( String, String ), fromUrl( String )

fromUrl

public static final java.lang.String fromUrl(java.lang.String url,
                                             java.util.Properties properties,
                                             java.lang.String userName,
                                             java.lang.String password)
                                      throws java.net.MalformedURLException,
                                             java.io.IOException
Return the response body from the specified HTTP URL as a String using the encoding specified by the web server. The response is obtained by POST'ing the data specified in the Properties object. The userName and password values specified are used to authenticate the request with the server.

The following code snippet shows how to use this method:

    import java.util.Properties;
    import org.rakeshv.utils.StringUtilities;

        String url = "http://www.w3.org/";
        String user = "scott";
        String password = "tiger";
        Properties properties = new Properties();
        properties.setProperty( "page", "2" );
        properties.setProperty( "content", "xml" );
        properties.setProperty( "details", "heavy" );
        try
        {
          String result = StringUtilities.fromUrl( 
            url, properties, user, password );
        }
        catch ( Throwable t )
        {
          // Exception handling
        }
 

Parameters:
url - The URL from which the contents are to be retrieved.
properties - A Properties object with the name-value pairs that are to be submitted to the server. A Properties object is used to ensure that the keys and values are String objects.
userName - The user name to use for authentication with the server.
password - The password to use for authentication with the server.
Throws:
java.net.MalformedURLException - If the specified url is improperly formatted.
java.io.IOException - If errors are encountered while reading the data. An IOException is thrown with the error response from the server if a HTTP error is encountered.
See Also:
setAuthenticator( String, String ), fromUrl( String, Properties )

parseEncoding

public static final java.lang.String parseEncoding(java.lang.String type)
Parse the charset from the content-type HTTP header if it was specified.

Parameters:
type - The content-type header value.
Returns:
String - The encoding value if specified. If it was not specified, this method returns null.

fromFile

public static final java.lang.String fromFile(java.lang.String file)
                                       throws java.io.FileNotFoundException,
                                              java.io.IOException
Return the contents of the specified file as a String using the system default encoding.

The following code snippet shows how to use this method:

    import org.rakeshv.utils.StringUtilities;

        try
        {
          String result = StringUtilities.fromFile( "/tmp/test.java" );
        }
        catch ( Throwable t )
        {
          // Exception handling
        }
 

Parameters:
file - The file from which the contents are to be read.
Throws:
java.io.FileNotFoundException - If the specified file could not be found.
java.io.IOException - If errors are encountered while reading the data.

fromFile

public static final java.lang.String fromFile(java.lang.String file,
                                              java.lang.String encoding)
                                       throws java.io.FileNotFoundException,
                                              java.io.IOException
Return the contents of the specified file as a String using the character encoding specified.

The following code snippet shows how to use this method:

    import org.rakeshv.utils.StringUtilities;

        try
        {
          String result = StringUtilities.fromFile( "/tmp/test.java", "UTF-8" );
        }
        catch ( Throwable t )
        {
          // Exception handling
        }
 

Parameters:
file - The file from which the contents are to be read.
encoding - The character encoding scheme to use to read the file contents.
Throws:
java.io.FileNotFoundException - If the specified file could not be found.
java.io.UnsupportedEncodingException - If the specified character encoding is invalid.
java.io.IOException - If errors are encountered while reading the data.

toFile

public static final void toFile(java.lang.String content,
                                java.lang.String file)
                         throws java.io.IOException
Write the contents of the specified String to the file specified using the system default encoding.

The following code snippet shows how to use this method:

    import org.rakeshv.utils.StringUtilities;

        try
        {
          String content = "blah .... blah";
          StringUtilities.toFile( content, "/tmp/test.txt" );
        }
        catch ( Throwable t )
        {
          // Exception handling
        }
 

Parameters:
content - The content that is to be written to file.
file - The fully qualified file name to which the contents are to be written.
Throws:
java.io.IOException - If errors are encountered while writing the data.
Since:
version 1.16

toFile

public static final void toFile(java.lang.String content,
                                java.lang.String file,
                                java.lang.String encoding)
                         throws java.io.UnsupportedEncodingException,
                                java.io.IOException
Write the contents of the specified String to the file specified using the character encoding specified.

The following code snippet shows how to use this method:

    import org.rakeshv.utils.StringUtilities;

        try
        {
          String content = "blah ... blah";
          StringUtilities.toFile( content, "/tmp/test.txt", "UTF-8" );
        }
        catch ( Throwable t )
        {
          // Exception handling
        }
 

Parameters:
content - The content that is to be written to file.
file - The file from which the contents are to be read.
encoding - The character encoding scheme to use to read the file contents.
Throws:
java.io.UnsupportedEncodingException - If the specified character encoding is invalid.
java.io.IOException - If errors are encountered while reading the data.
Since:
version 1.16

stackTrace

public static final java.lang.String stackTrace(java.lang.Throwable throwable)
Returns the StackTrace for the specified instance of Throwable.

Parameters:
throwable - The instance whose stack trace is desired.
Returns:
String - The stack trace for the throwable instance.

toXML

public static final java.lang.String toXML(java.lang.String content)
Replace XML/HTML special characters (&, <, ...) in the String specified with their escaped equivalents.

Parameters:
content - - The String that is to be processed.
Returns:
String - The modified String object.

fromXML

public static final java.lang.String fromXML(java.lang.String content)
Replace escaped XML/HTML special characters (&, <, ...) in the String specified with their original equivalents.

Parameters:
content - - The String that is to be processed.
Returns:
String - The modified String object.
Since:
version 1.16

readResponseFromUrl

private static final java.lang.String readResponseFromUrl(java.net.HttpURLConnection httpConnection)
                                                   throws java.io.IOException
Read the response from the HttpURLConnection. Check the Content-encoding response header to see if the response is gzip encoded. If it is, use a GZIPInputStream to read the response.

Parameters:
httpConnection - The HttpURLConnection from which the response is to be read.
Throws:
java.io.IOException - If errors are encountered while reading the response from the server.

setAuthenticator

private static final void setAuthenticator(java.lang.String userName,
                                           java.lang.String password)
Create a new instance of Authenticator using the specified credentials. Create an anonymous sub-class of Authenticator that implements the getPasswordAuthentication method by returning a new instance of PasswordAuthentication using the userName and password specified.

Parameters:
userName - The user name to use for authentication.
password - The password to use for authentication.

readData

private static java.lang.String readData(java.io.BufferedReader reader)
                                  throws java.io.IOException
Returns all the data from the specified BufferedReader.

Parameters:
reader - The BufferedReader from which the data is to be read.
Returns:
String - The data read from the specified BufferedReader.
Throws:
java.io.IOException - If errors are encountered while reading the data.

printTrace

private static final java.lang.StringBuilder printTrace(java.lang.Throwable throwable,
                                                        java.lang.StringBuilder builder)
Return the stack trace for the specified instance of Throwable.

Parameters:
throwable - The instance of Throwable whose stack trace is to be retrieved.
builder - The StringBuilder to which the stack trace is to be appended.
Returns:
StringBuilder - The reference to the StringBuilder that was passed in for convenience.

Rakesh API

Copyright © 2002-2005 - Rakesh Vidyadharan