|
Rakesh API | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.rakeshv.utils.StringUtilities
public final class StringUtilities
Provides utility methods to return String objects
from commonly used resources.
Copyright 2004-2006 Rakesh Vidyadharan
| 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 |
|---|
public static final java.lang.String endOfLine
end of line characters to use in the returned
String objects.
| Constructor Detail |
|---|
private StringUtilities()
| Method Detail |
|---|
public static final java.lang.String fromUrl(java.lang.String url)
throws java.net.MalformedURLException,
java.io.IOException
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
}
url - The URL from which the contents are to be
retrieved.
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.readResponseFromUrl( HttpURLConnection )
public static final java.lang.String fromUrl(java.lang.String url,
java.util.Properties properties)
throws java.net.MalformedURLException,
java.io.IOException
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
}
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.
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.readResponseFromUrl( HttpURLConnection )
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
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
}
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.
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.setAuthenticator( String, String ),
fromUrl( String )
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
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
}
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.
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.setAuthenticator( String, String ),
fromUrl( String, Properties )public static final java.lang.String parseEncoding(java.lang.String type)
charset from the content-type
HTTP header if it was specified.
type - The content-type header value.
null.
public static final java.lang.String fromFile(java.lang.String file)
throws java.io.FileNotFoundException,
java.io.IOException
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
}
file - The file from which the contents are to
be read.
java.io.FileNotFoundException - If the specified file
could not be found.
java.io.IOException - If errors are encountered while reading the
data.
public static final java.lang.String fromFile(java.lang.String file,
java.lang.String encoding)
throws java.io.FileNotFoundException,
java.io.IOException
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
}
file - The file from which the contents are to
be read.encoding - The character encoding scheme to use
to read the file contents.
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.
public static final void toFile(java.lang.String content,
java.lang.String file)
throws java.io.IOException
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
}
content - The content that is to be written to file.file - The fully qualified file name to which
the contents are to be written.
java.io.IOException - If errors are encountered while writing the
data.
public static final void toFile(java.lang.String content,
java.lang.String file,
java.lang.String encoding)
throws java.io.UnsupportedEncodingException,
java.io.IOException
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
}
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.
java.io.UnsupportedEncodingException - If the specified character
encoding is invalid.
java.io.IOException - If errors are encountered while reading the
data.public static final java.lang.String stackTrace(java.lang.Throwable throwable)
StackTrace for the specified instance
of Throwable.
throwable - The instance whose stack trace is desired.
public static final java.lang.String toXML(java.lang.String content)
content - - The String that
is to be processed.
String object.public static final java.lang.String fromXML(java.lang.String content)
content - - The String that
is to be processed.
String object.
private static final java.lang.String readResponseFromUrl(java.net.HttpURLConnection httpConnection)
throws java.io.IOException
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.
httpConnection - The HttpURLConnection from
which the response is to be read.
java.io.IOException - If errors are encountered while reading the
response from the server.
private static final void setAuthenticator(java.lang.String userName,
java.lang.String password)
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.
userName - The user name to use for authentication.password - The password to use for authentication.
private static java.lang.String readData(java.io.BufferedReader reader)
throws java.io.IOException
BufferedReader.
reader - The BufferedReader from which the
data is to be read.
java.io.IOException - If errors are encountered while reading the
data.
private static final java.lang.StringBuilder printTrace(java.lang.Throwable throwable,
java.lang.StringBuilder builder)
stack trace for the specified instance
of Throwable.
throwable - The instance of Throwable
whose stack trace is to be retrieved.builder - The StringBuilder to which the stack trace is to
be appended.
|
Rakesh API | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||