|
SPT Core API | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.sptci.util.StringUtilities
public final class StringUtilities
Provides utility methods to perform common operations related to String objects.
Copyright 2005 Sans Pareil Technologies, Inc.
| 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 |
|---|
public static String endOfLine
end of line characters to use in the returned
String objects.
| Method Detail |
|---|
public static String fromUrl(String url)
throws MalformedURLException,
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 com.sptci.util.StringUtilities;
try
{
String result = StringUtilities.fromUrl( "http://www.sptci.com/" );
}
catch ( Throwable t )
{
// Exception handling
}
url - The URL from which the contents are to be
retrieved.
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.readResponseFromUrl( HttpURLConnection )
public static String fromUrl(String url,
Properties properties)
throws MalformedURLException,
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 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
}
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.
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.readResponseFromUrl( HttpURLConnection )
public static String fromUrl(String url,
String userName,
String password)
throws MalformedURLException,
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 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
}
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.
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.FileUtilities.setAuthenticator( String, String ),
fromUrl( String )
public static String fromUrl(String url,
Properties properties,
String userName,
String password)
throws MalformedURLException,
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 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
}
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.
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.FileUtilities.setAuthenticator( String, String ),
fromUrl( String, Properties )public static String parseEncoding(String type)
charset from the content-type
HTTP header if it was specified.
type - The content-type header value.
null.
public static String fromFile(String file)
throws FileNotFoundException,
IOException
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
}
file - The File from which the contents are to
be read.
FileNotFoundException - If the specified File
could not be found.
IOException - If errors are encountered while reading the
data.
public static String fromFile(String file,
String encoding)
throws FileNotFoundException,
IOException
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
}
file - The File from which the contents are to
be read.encoding - The character encoding scheme to use
to read the file contents.
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.
public static void toFile(String content,
String file)
throws IOException
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
}
content - The content that is to be written to file.file - The fully qualified file name to which
the contents are to be written.
IOException - If errors are encountered while writing the
data.
public static void toFile(String content,
String file,
String encoding)
throws UnsupportedEncodingException,
IOException
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
}
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.
UnsupportedEncodingException - If the specified character
encoding is invalid.
IOException - If errors are encountered while writing the
data.public static String stackTrace(Throwable throwable)
StackTrace for the specified instance
of Throwable.
throwable - The instance whose stack trace is desired.
public static String toXML(String content)
content - The String that is to be processed.
String object.public static String stripInvalidXMLCharacters(String content)
content - The String that is to be processed.
String object.public static String fromXML(String content)
content - The String that is to be processed.
String object.
|
SPT Core API | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||