Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* {{{ header & license
* GeneralUtil.java
* Copyright (c) 2004, 2005 Patrick Wright
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* }}}
*/
package com.openhtmltopdf.util;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Description of the Class
*
* @author Patrick Wright
*/
public class GeneralUtil {
/**
* Used to format an Object's hashcode into a 0-padded 10 char String, e.g.
* for 24993066 returns "0024993066"
*/
public final static java.text.DecimalFormat PADDED_HASH_FORMAT = new java.text.DecimalFormat("0000000000");
/**
* Description of the Method
*
* @param obj PARAM
* @param resource PARAM
* @return Returns
*/
public static InputStream openStreamFromClasspath(Object obj, String resource) {
InputStream readStream = null;
try {
ClassLoader loader = obj.getClass().getClassLoader();
if (loader == null) {
readStream = ClassLoader.getSystemResourceAsStream(resource);
} else {
readStream = loader.getResourceAsStream(resource);
}
if (readStream == null) {
URL stream = resource.getClass().getResource(resource);
if (stream != null) readStream = stream.openStream();
}
} catch (Exception ex) {
XRLog.exception("Could not open stream from CLASSPATH: " + resource, ex);
}
return readStream;
}
public static URL getURLFromClasspath(Object obj, String resource) {
URL url = null;
try {
ClassLoader loader = obj.getClass().getClassLoader();
if (loader == null) {
url = ClassLoader.getSystemResource(resource);
} else {
url = loader.getResource(resource);
}
if (url == null) {
url = resource.getClass().getResource(resource);
}
} catch (Exception ex) {
XRLog.exception("Could not get URL from CLASSPATH: " + resource, ex);
}
return url;
}
/**
* Dumps an exception to the console, only the last 5 lines of the stack
* trace.
*
* @param ex PARAM
*/
public static void dumpShortException(Exception ex) {
String s = ex.getMessage();
if (s == null || s.trim().equals("null")) {
s = "{no ex. message}";
}
System.out.println(s + ", " + ex.getClass());
StackTraceElement[] stes = ex.getStackTrace();
for (int i = 0; i < stes.length && i < 5; i++) {
StackTraceElement ste = stes[i];
System.out.println(" " + ste.getClassName() + "." + ste.getMethodName() + "(ln " + ste.getLineNumber() + ")");
}
}
/**
* 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 the trackBack() call
* from. Example:
*
* // called from Box.calcBorders(), line 639
* String tback = GeneralUtil.trackBack(6);
* System.out.println(tback);
*