Class JRLoader
Many JasperReports processes, like report compilation, report filling and exporting, often work with serialized objects. Sometimes it is useful to manually load those serialized objects before submitting them to the desired JasperReport process.
The most interesting method exposed by this class is
getLocationInputStream(String location)
.
When calling this method to load an InputStream object from
the supplied location, the program first tries to interpret the location as a valid resource name. If
this fails, then the program assumes that the supplied location is the name of a file on
disk and tries to read from it. If no file is found at that location, it will assume that the location
represents a valid URL. Only after this third
try fails an exception is thrown.
- Author:
- Teodor Danciu (teodord@users.sourceforge.net)
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionprotected static void
collectResources
(String resourceName, ClassLoader classLoader, Map<URL, ClassLoaderResource> resources) protected static void
collectResources
(String resourceName, ClassLoader classLoader, Set<URL> resources) static List
<ClassLoaderResource> getClassLoaderResources
(String resource) Scans the context classloader and the classloader of this class for all resources that have a specified name, and returns a list ofClassLoaderResource
objects for the found resources.static InputStream
getFileInputStream
(String filename) Tries to open a file for reading.static InputStream
getInputStream
(File file) static InputStream
getInputStream
(URL url) static InputStream
getLocationInputStream
(String location) Tries to open an input stream for a location.static URL
getResource
(String resource) Returns the resource URL for a specified resource name.static InputStream
getResourceInputStream
(String resource) Tries to open an input stream for a resource.getResources
(String resource) Scans the context classloader and the classloader of this class for all resources that have a specified name, and returns a list ofURL
s for the found resources.static InputStream
getURLInputStream
(String spec) Tries to open an input stream for an URL.static byte[]
static byte[]
loadBytes
(InputStream is) static byte[]
static byte[]
loadBytesFromResource
(String resourceName) static byte[]
loadBytesFromResource
(String resourceName, ClassLoader classLoader) static JasperPrint
loadJasperPrint
(File file, JRVirtualizer virtualizer) Loads a JasperPrint object from a file, optionally using a virtualizer for the object.static JasperPrint
loadJasperPrint
(InputStream is, JRVirtualizer virtualizer) Loads a JasperPrint object from a stream, optionally using a virtualizer for the object.static JasperPrint
loadJasperPrint
(URL url, JRVirtualizer virtualizer) Loads a JasperPrint object from a URL, optionally using a virtualizer for the object.static JasperPrint
loadJasperPrintFromFile
(String fileName, JRVirtualizer virtualizer) Loads a JasperPrint object from a file, optionally using a virtualizer for the object.static Object
loadObject
(File file) static Object
static Object
loadObject
(URL url) static Object
loadObject
(JasperReportsContext jasperReportsContext, File file) static Object
loadObject
(JasperReportsContext jasperReportsContext, InputStream is) static Object
loadObject
(JasperReportsContext jasperReportsContext, URL url) static Object
loadObjectFromFile
(String fileName) static byte[]
readBytes
(InputStream is)
-
Field Details
-
EXCEPTION_MESSAGE_KEY_BYTE_DATA_FROM_INPUT_STREAM_ERROR
- See Also:
-
EXCEPTION_MESSAGE_KEY_BYTE_DATA_LOADING_ERROR
- See Also:
-
EXCEPTION_MESSAGE_KEY_CLASS_NOT_FOUND_FROM_FILE
- See Also:
-
EXCEPTION_MESSAGE_KEY_CLASS_NOT_FOUND_FROM_INPUT_STREAM
- See Also:
-
EXCEPTION_MESSAGE_KEY_CLASS_NOT_FOUND_FROM_URL
- See Also:
-
EXCEPTION_MESSAGE_KEY_FILE_OPEN_ERROR
- See Also:
-
EXCEPTION_MESSAGE_KEY_INPUT_STREAM_FROM_FILE_OPEN_ERROR
- See Also:
-
EXCEPTION_MESSAGE_KEY_INPUT_STREAM_FROM_URL_OPEN_ERROR
- See Also:
-
EXCEPTION_MESSAGE_KEY_OBJECT_FROM_FILE_LOADING_ERROR
- See Also:
-
EXCEPTION_MESSAGE_KEY_OBJECT_FROM_INPUT_STREAM_LOADING_ERROR
- See Also:
-
EXCEPTION_MESSAGE_KEY_OBJECT_FROM_URL_LOADING_ERROR
- See Also:
-
EXCEPTION_MESSAGE_KEY_RESOURCE_NOT_FOUND
- See Also:
-
EXCEPTION_MESSAGE_KEY_URL_OPEN_ERROR
- See Also:
-
-
Method Details
-
loadObjectFromFile
- Throws:
JRException
-
loadObject
- Throws:
JRException
-
loadObject
public static Object loadObject(JasperReportsContext jasperReportsContext, File file) throws JRException - Throws:
JRException
-
loadObject
- Throws:
JRException
-
loadObject
public static Object loadObject(JasperReportsContext jasperReportsContext, URL url) throws JRException - Throws:
JRException
-
loadObject
- Throws:
JRException
-
loadObject
public static Object loadObject(JasperReportsContext jasperReportsContext, InputStream is) throws JRException - Throws:
JRException
-
getInputStream
- Throws:
JRException
-
getInputStream
- Throws:
JRException
-
loadBytes
- Throws:
JRException
-
loadBytes
- Throws:
JRException
-
loadBytes
- Throws:
JRException
-
readBytes
- Throws:
IOException
-
loadBytesFromResource
- Throws:
JRException
-
loadBytesFromResource
public static byte[] loadBytesFromResource(String resourceName, ClassLoader classLoader) throws JRException - Throws:
JRException
-
getLocationInputStream
Tries to open an input stream for a location.The method tries to interpret the location as a file name, a resource name or an URL. If any of these succeed, an input stream is created and returned.
- Parameters:
location
- the location- Returns:
- an input stream if the location is an existing file name, a resource name on
the classpath or an URL or
null
otherwise. - Throws:
JRException
-
getFileInputStream
Tries to open a file for reading.- Parameters:
filename
- the file name- Returns:
- an input stream for the file or
null
if the file was not found - Throws:
JRException
-
getResourceInputStream
Tries to open an input stream for a resource.- Parameters:
resource
- the resource name- Returns:
- an input stream for the resource or
null
if the resource was not found
-
getResources
Scans the context classloader and the classloader of this class for all resources that have a specified name, and returns a list ofURL
s for the found resources.- Parameters:
resource
- the resource names- Returns:
- a list of
URL
s of resources with the specified name; the list is empty if no resources have been found for the name - See Also:
-
collectResources
protected static void collectResources(String resourceName, ClassLoader classLoader, Set<URL> resources) -
getClassLoaderResources
Scans the context classloader and the classloader of this class for all resources that have a specified name, and returns a list ofClassLoaderResource
objects for the found resources.The returned list contains the URLs of the resources, and for each resource the highest classloader in the classloader hierarchy on which the resource was found.
- Parameters:
resource
- the resource names- Returns:
- a list of resources with the specified name; the list is empty if no resources have been found for the name
- See Also:
-
collectResources
protected static void collectResources(String resourceName, ClassLoader classLoader, Map<URL, ClassLoaderResource> resources) -
getResource
Returns the resource URL for a specified resource name.- Parameters:
resource
- the resource name- Returns:
- the URL of the resource having the specified name, or
null
if none found - See Also:
-
getURLInputStream
Tries to open an input stream for an URL.- Parameters:
spec
- the string to parse as an URL- Returns:
- an input stream for the URL or null if
spec
is not a valid URL - Throws:
JRException
-
loadJasperPrintFromFile
public static JasperPrint loadJasperPrintFromFile(String fileName, JRVirtualizer virtualizer) throws JRException Loads a JasperPrint object from a file, optionally using a virtualizer for the object.- Parameters:
fileName
- the file namevirtualizer
- the virtualizer- Returns:
- a JasperPrint object
- Throws:
JRException
- See Also:
-
loadJasperPrint
Loads a JasperPrint object from a file, optionally using a virtualizer for the object.- Parameters:
file
- the filevirtualizer
- the virtualizer- Returns:
- a JasperPrint object
- Throws:
JRException
- See Also:
-
loadJasperPrint
Loads a JasperPrint object from a URL, optionally using a virtualizer for the object.- Parameters:
url
- the URLvirtualizer
- the virtualizer- Returns:
- a JasperPrint object
- Throws:
JRException
- See Also:
-
loadJasperPrint
public static JasperPrint loadJasperPrint(InputStream is, JRVirtualizer virtualizer) throws JRException Loads a JasperPrint object from a stream, optionally using a virtualizer for the object.- Parameters:
is
- the streamvirtualizer
- the virtualizer- Returns:
- a JasperPrint object
- Throws:
JRException
- See Also:
-