SPT Core API

com.sptci.util
Class StringUtilities

java.lang.Object
  extended by com.sptci.util.StringUtilities

public final class StringUtilities
extends Object

Provides utility methods to perform common operations related to String objects.

Copyright 2005 Sans Pareil Technologies, Inc.

Version:
$Id: StringUtilities.java 4553 2008-12-24 10:34:16Z rakesh $
Author:
Rakesh Vidyadharan 2005-08-27

Field Summary
static String endOfLine
          The end of line characters to use in the returned String objects.
 
Method Summary
static String fromFile(String file)
          Return the contents of the specified File as a String using the system default encoding.
static String fromFile(String file, String encoding)
          Return the contents of the specified File as a String using the character encoding specified.
static String fromUrl(String url)
          Return the response body from the specified HTTP URL as a String using the encoding specified by the web server.
static String fromUrl(String url, Properties properties)
          Return the response body from the specified HTTP URL as a String using the encoding specified by the web server.
static String fromUrl(String url, Properties properties, String userName, String password)
          Return the response body from the specified HTTP URL as a String using the encoding specified by the web server.
static String fromUrl(String url, String userName, String password)
          Return the response body from the specified HTTP URL as a String using the encoding specified by the web server.
static String fromXML(String content)
          Replace escaped XML/HTML special characters (&, <, ...) in the String specified with their original equivalents.
static String parseEncoding(String type)
          Parse the charset from the content-type HTTP header if it was specified.
static String stackTrace(Throwable throwable)
          Returns the StackTrace for the specified instance of Throwable.
static String stripInvalidXMLCharacters(String content)
          Strip out invalid XML characters from the source and return the cleaned up string.
static void toFile(String content, String file)
          Write the contents of the specified String to the File specified using the system default encoding.
static void toFile(String content, String file, String encoding)
          Write the contents of the specified String to the File specified using the character encoding specified.
static String toXML(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 String endOfLine
The end of line characters to use in the returned String objects.

Method Detail

fromUrl

public static String fromUrl(String url)
                      throws MalformedURLException,
                             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 com.sptci.util.StringUtilities;

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

Parameters:
url - The URL from which the contents are to be retrieved.
Returns:
The contents of the URL specified.
Throws:
MalformedURLException - If the specified URL is improperly formatted.
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 String fromUrl(String url,
                             Properties properties)
                      throws MalformedURLException,
                             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 com.sptci.util.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.
Returns:
The contents of the URL specified;
Throws:
MalformedURLException - If the specified URL is improperly formatted.
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 String fromUrl(String url,
                             String userName,
                             String password)
                      throws MalformedURLException,
                             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 com.sptci.util.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.
Returns:
The contents of the URL specified.
Throws:
MalformedURLException - If the specified URL is improperly formatted.
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:
FileUtilities.setAuthenticator( String, String ), fromUrl( String )

fromUrl

public static String fromUrl(String url,
                             Properties properties,
                             String userName,
                             String password)
                      throws MalformedURLException,
                             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 com.sptci.util.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.
Returns:
The contents of the URL specified.
Throws:
MalformedURLException - If the specified URL is improperly formatted.
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:
FileUtilities.setAuthenticator( String, String ), fromUrl( String, Properties )

parseEncoding

public static String parseEncoding(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 String fromFile(String file)
                       throws FileNotFoundException,
                              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 com.sptci.util.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.
Returns:
The contents of the file specified.
Throws:
FileNotFoundException - If the specified File could not be found.
IOException - If errors are encountered while reading the data.

fromFile

public static String fromFile(String file,
                              String encoding)
                       throws FileNotFoundException,
                              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 com.sptci.util.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.
Returns:
The contents of the file specified.
Throws:
FileNotFoundException - If the specified File could not be found.
UnsupportedEncodingException - If the specified character encoding is invalid.
IOException - If errors are encountered while reading the data.

toFile

public static void toFile(String content,
                          String file)
                   throws IOException
Write the contents of the specified String to the File specified using the system default encoding. Note that any parent directories required by the File specified will be created.

The following code snippet shows how to use this method:

    import com.sptci.util.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:
IOException - If errors are encountered while writing the data.
Since:
version 1.16

toFile

public static void toFile(String content,
                          String file,
                          String encoding)
                   throws UnsupportedEncodingException,
                          IOException
Write the contents of the specified String to the File specified using the character encoding specified. Note that any parent directories required by the File specified will be created.

The following code snippet shows how to use this method:

    import com.sptci.util.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:
UnsupportedEncodingException - If the specified character encoding is invalid.
IOException - If errors are encountered while writing the data.
Since:
version 1.16

stackTrace

public static String stackTrace(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 String toXML(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:
The modified String object.

stripInvalidXMLCharacters

public static String stripInvalidXMLCharacters(String content)
Strip out invalid XML characters from the source and return the cleaned up string.

Parameters:
content - The String that is to be processed.
Returns:
The modified String object.
Since:
2007-10-29

fromXML

public static String fromXML(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:
The modified String object.
Since:
version 1.16

SPT Core API