public final class StringUtilities extends HttpDownloader
String objects.
Copyright 2005 Sans Pareil Technologies, Inc.
| Modifier and Type | Field and Description |
|---|---|
static String |
endOfLine
The
end of line characters to use in the returned
String objects. |
logger| Modifier and Type | Method and Description |
|---|---|
String |
fromFile(String file)
|
String |
fromFile(String file,
String encoding)
|
String |
fromUrl(String url)
Return the response body from the specified
HTTP URL
as a String using the encoding specified by the
web server. |
String |
fromUrl(String url,
int timeout)
Return the response body from the specified
HTTP URL
as a String using the encoding specified by the
web server. |
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. |
String |
fromUrl(String url,
Properties headers,
byte[] body) |
String |
fromUrl(String url,
Properties headers,
byte[] body,
String userName,
String password) |
String |
fromUrl(String url,
Properties headers,
byte[] body,
String userName,
String password,
int timeout)
Return the response body from the specified
HTTP URL
as a String using the encoding specified by the
web server. |
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. |
String |
fromUrl(String url,
Properties properties,
String userName,
String password,
int timeout)
Return the response body from the specified
HTTP URL
as a String using the encoding specified by the
web server. |
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. |
String |
fromUrl(String url,
String userName,
String password,
int timeout)
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 StringUtilities |
getInstance() |
static boolean |
isEmpty(String string)
Check to see if the string is empty.
|
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 String |
toAscii(String latin)
Convert latin characters in input string to ascii equivalents where
possible.
|
static void |
toFile(String content,
String file)
|
static void |
toFile(String content,
String file,
String encoding)
|
static String |
toXML(char[] content,
int... indices)
Deprecated.
Use
toXMLAttribute(char[], int...) or
toXMLCharacters(char[], int...) instead. |
static String |
toXML(String content)
Deprecated.
Use
toXMLAttribute(String) or
toXMLCharacters(String) instead. |
static String |
toXMLAttribute(char[] content,
int... indices)
Replace XML/HTML special characters (&, <, ...) in the
String specified with their escaped equivalents.
|
static String |
toXMLAttribute(String content)
Replace XML/HTML special characters (&, <, ...) in the
String specified with their escaped equivalents.
|
static String |
toXMLCharacters(char[] content,
int... indices)
Replace XML/HTML special characters (&, <, ...) in the character
array specified with their escaped equivalents.
|
static String |
toXMLCharacters(String content)
Replace XML/HTML special characters (&, <, ...) in the
String specified with their escaped equivalents.
|
abortIfRequested, deregisterListener, doGET, doPOST, doPOST, getConditionVariable, getConnection, getInputStream, getListener, getReader, registerConditionVariable, registerListener, resetAcceptEncoding, retryCount, setAcceptEncoding, socketTimeoutpublic static final String endOfLine
end of line characters to use in the returned
String objects.public static StringUtilities getInstance()
public String fromUrl(String url)
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.getInstance().fromUrl( "http://www.sptci.com/" );
}
catch ( Throwable t )
{
// Exception handling
}
url - The URL from which the contents are to be
retrieved.RuntimeException - 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.fromUrl( String, String, String )public String fromUrl(String url, int timeout)
HTTP URL
as a String using the encoding specified by the
web server.url - The URL from which the contents are to be
retrieved.timeout - The socket timeout value in milliseconds to be used.RuntimeException - 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.fromUrl(String, String, String, int)public String fromUrl(String url, String userName, String password)
HTTP URL
as a String using the encoding specified by the
web server. Uses the specified credentials to
authenticate the request with the server.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.RuntimeException - 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.fromUrl( String, String, String, int )public String fromUrl(String url, Properties properties)
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. All post values are URL encoded as
UTF-8, so callers do not need to perform this step.
The following code snippet shows how to use this method:
import java.util.Properties;
import com.sptci.util.StringUtilities;
final Properties properties = new Properties();
properties.setProperty( "page", "2" );
properties.setProperty( "user", "scott" );
properties.setProperty( "password", "tiger" );
try
{
final String result = StringUtilities.getInstance().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.RuntimeException - 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.fromUrl( String, Properties, String, String )public String fromUrl(String url, Properties properties, String userName, String password)
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.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.RuntimeException - 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.fromUrl(String, java.util.Properties, String, String, int )public String fromUrl(String url, String userName, String password, int timeout)
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
{
final String url = "http://www.w3.org/";
final String userName = "scott";
final String password = "tiger";
final String result = StringUtilities.getInstance().fromUrl( url, userName, password );
}
catch ( Throwable t )
{
// Exception handling
}
The following system properties may be used to control socket timeout
and retry settings:
com.sptci.util.StringUtilities.socket.timeout - default value 5000 millisecondscom.sptci.util.StringUtilities.socket.retryCount - default value 0url - 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.timeout - The socket timeout interval in millisecondsRuntimeException - 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 ),
readResponseFromUrl( HttpURLConnection )public String fromUrl(String url, Properties properties, String userName, String password, int timeout)
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;
final String url = "http://www.w3.org/";
final String user = "scott";
final String password = "tiger";
final Properties properties = new Properties();
properties.setProperty( "page", "2" );
properties.setProperty( "content", "xml" );
properties.setProperty( "details", "heavy" );
try
{
final String result = StringUtilities.getInstance().fromUrl( url, properties, user, password, 2000 );
}
catch ( Throwable t )
{
// Exception handling
}
The following system properties may be used to control socket timeout
and retry settings:
com.sptci.util.StringUtilities.socket.timeout - default value 5000 millisecondscom.sptci.util.StringUtilities.socket.retryCount - default value 0url - 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.timeout - The socket timeout value to useRuntimeException - 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 ),
readResponseFromUrl( HttpURLConnection )public String fromUrl(String url, Properties headers, byte[] body)
public String fromUrl(String url, Properties headers, byte[] body, String userName, String password)
public String fromUrl(String url, Properties headers, byte[] body, String userName, String password, int timeout)
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 byte array as the
body. Request headers are added as specified in the
Properties header object. The userName and
password values specified are used to authenticate
the request with the server.
Note: It is strongly recommended that callers include
the Content-Type and Content-Encoding
header values in the headers passed in. This method does not
specify any content type header. This method does not post the data
as a multi-part form data with boundaries, so it is recommended to
use application/octet-stream or something similar as the
content type header.
The following code snippet shows how to use this method:
import java.util.Properties;
import static com.sptci.util.StringUtilities.fromUrl;
final String url = "http://www.w3.org/";
final String user = "scott";
final String password = "tiger";
final Properties headers = new Properties();
headers.setProperty( "page", "2" );
headers.setProperty( "content", "xml" );
headers.setProperty( "details", "heavy" );
final byte[] body = "... my long content...".getBytes( "UTF-8" );
try
{
final String result = fromUrl( url, headers, bytes, user, password, 5000 );
}
catch ( Throwable t )
{
// Exception handling
}
The following system properties may be used to control socket timeout
and retry settings:
com.sptci.util.StringUtilities.socket.timeout - default value 5000 millisecondscom.sptci.util.StringUtilities.socket.retryCount - default value 0url - The URL from which the contents are to be
retrieved.headers - A Properties object with the
name-value pairs that are to be set as requests headers.
Properties object is used to ensure that the keys and values
are String objects.body - The bytes that are to be sent as the body of the request.userName - The user name to use for authentication with the
server.password - The password to use for authentication with the
server.timeout - The socket timeout value to useFileUtilities.setAuthenticator( String, String ),
readResponseFromUrl( HttpURLConnection )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 String fromFile(String file)
File as a
String using the system default encoding.
The following code snippet shows how to use this method:
import com.sptci.HttpDownloadListener;
import com.sptci.util.StringUtilities;
final HttpDownloadListener listener = new HttpDownloadListener( { ... } );
StringUtilities.getInstance().registerListener( listener );
String result = StringUtilities.getInstance().fromFile( "/tmp/test.java" );
// Later in your code, usually in the HttpDownloadListener.notifyFinished implementation
StringUtilities.getInstance().deregisterListener( listener );
file - The File from which the contents are to
be read.RuntimeException - If errors are encountered while reading the
data.public String fromFile(String file, String encoding)
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.getInstance().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.RuntimeException - 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 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.@Deprecated public static String toXML(String content)
toXMLAttribute(String) or
toXMLCharacters(String) instead.content - The String that is to be processed.String object.toXML(char[], int...)@Deprecated public static String toXML(char[] content, int... indices)
toXMLAttribute(char[], int...) or
toXMLCharacters(char[], int...) instead.content - The char array that is to be processed.indices - The optional start and length parameters
that specify the offset into the content array to read from and the
total number of characters to read.String object.public static String toXMLAttribute(String content)
content - The String that is to be processed.String object.toXMLAttribute(char[], int...)public static String toXMLAttribute(char[] content, int... indices)
content - The char array that is to be processed.indices - The optional start and length parameters
that specify the offset into the content array to read from and the
total number of characters to read.String object.public static String toXMLCharacters(String content)
content - The String that is to be processed.String object.toXMLCharacters(char[], int...)public static String toXMLCharacters(char[] content, int... indices)
content - The char array that is to be processed.indices - The optional start and length parameters
that specify the offset into the content array to read from and the
total number of characters to read.String object.public static String stripInvalidXMLCharacters(String content)
public static String fromXML(String content)
public static boolean isEmpty(String string)
null or is of zero length.string - The input string to check.true if the string is null or
has zero length.public static String toAscii(String latin)
latin - The input string containing accented latin characters