Package com.openhtmltopdf.util
Class GeneralUtil
java.lang.Object
com.openhtmltopdf.util.GeneralUtil
public class GeneralUtil extends Object
Description of the Class
- Author:
- Patrick Wright
-
Field Summary
Fields Modifier and Type Field Description static DecimalFormatPADDED_HASH_FORMATUsed to format an Object's hashcode into a 0-padded 10 char String, e.g. -
Constructor Summary
Constructors Constructor Description GeneralUtil() -
Method Summary
Modifier and Type Method Description static StringclassNameOnly(Object o)Given an Object instance, returns just the classname with no packagestatic StringclassNameOnly(String cname)Given a String classname, returns just the classname with no packagestatic voiddumpShortException(Exception ex)Dumps an exception to the console, only the last 5 lines of the stack trace.static StringescapeHTML(String s)Converts any special characters into their corresponding HTML entities, for example < to <.static StringBuilderhtmlEscapeSpace(String uri)static StringinputStreamToString(InputStream is)Reads all content from a given InputStream into a String using the default platform encoding.static booleanisMacOSX()static StringpaddedHashCode(Object o)Description of the Methodstatic intparseIntRelaxed(String s)Parses an integer from a string using less restrictive rules about which characters we won't accept.static StringtrackBack(int cnt)Returns a String tracking the last n method calls, from oldest to most recent.static voidwriteStringToFile(String content, String encoding, String fileName)
-
Field Details
-
PADDED_HASH_FORMAT
Used to format an Object's hashcode into a 0-padded 10 char String, e.g. for 24993066 returns "0024993066"
-
-
Constructor Details
-
GeneralUtil
public GeneralUtil()
-
-
Method Details
-
dumpShortException
Dumps an exception to the console, only the last 5 lines of the stack trace.- Parameters:
ex- PARAM
-
trackBack
Returns a String tracking the last n method calls, from oldest to most recent. You can use this as a simple tracing mechanism to find out the calls that got to where you execute thetrackBack()call from. Example:
// called from Box.calcBorders(), line 639 String tback = GeneralUtil.trackBack(6); System.out.println(tback);
producesBoxing.layoutChildren(ln 204) BlockBoxing.layoutContent(ln 81) Boxing.layout(ln 72) Boxing.layout(ln 133) Box.totalLeftPadding(ln 306) Box.calcBorders(ln 639)
ThetrackBack()method itself is always excluded from the dump. Note the output may not be useful if HotSpot has been optimizing the code.- Parameters:
cnt- How far back in the call tree to go; if call tree is smaller, will be limited to call tree.- Returns:
- see desc
-
classNameOnly
Given an Object instance, returns just the classname with no package- Parameters:
o- PARAM- Returns:
- Returns
-
classNameOnly
Given a String classname, returns just the classname with no package- Parameters:
cname- PARAM- Returns:
- Returns
-
paddedHashCode
Description of the Method- Parameters:
o- PARAM- Returns:
- Returns
-
isMacOSX
public static boolean isMacOSX() -
htmlEscapeSpace
-
inputStreamToString
Reads all content from a given InputStream into a String using the default platform encoding.- Parameters:
is- the InputStream to read from. Must already be open, and will NOT be closed by this function. Failing to close this stream after the call will result in a resource leak.- Returns:
- String containing contents read from the stream
- Throws:
IOException- if the stream could not be read
-
writeStringToFile
public static void writeStringToFile(String content, String encoding, String fileName) throws IOException- Throws:
IOException
-
parseIntRelaxed
Parses an integer from a string using less restrictive rules about which characters we won't accept. This scavenges the supplied string for any numeric character, while dropping all others.- Parameters:
s- The string to parse- Returns:
- The number represented by the passed string, or 0 if the string is null, empty, white-space only, contains only non-numeric characters, or simply evaluates to 0 after parsing (e.g. "0")
-
escapeHTML
Converts any special characters into their corresponding HTML entities, for example < to <. This is done using a character by character test, so you may consider other approaches for large documents. Make sure you declare the entities that might appear in this replacement, e.g. the latin-1 entities This method was taken from a code-samples website, written and hosted by Real Gagnon, at http://www.rgagnon.com/javadetails/java-0306.html.- Parameters:
s- The String which may contain characters to escape.- Returns:
- The string with the characters as HTML entities.
-