001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028: package net.sf.jasperreports.engine.util;
029:
030: import java.io.BufferedInputStream;
031: import java.io.ByteArrayOutputStream;
032: import java.io.File;
033: import java.io.FileInputStream;
034: import java.io.FileNotFoundException;
035: import java.io.IOException;
036: import java.io.InputStream;
037: import java.io.ObjectInputStream;
038: import java.net.MalformedURLException;
039: import java.net.URL;
040: import java.net.URLStreamHandlerFactory;
041:
042: import net.sf.jasperreports.engine.JRException;
043:
044: /**
045: * @author Teodor Danciu (teodord@users.sourceforge.net)
046: * @version $Id: JRLoader.java 1507 2006-11-27 15:12:17Z teodord $
047: */
048: public class JRLoader {
049:
050: /**
051: *
052: */
053: //private static boolean wasWarning = false;
054:
055: /**
056: *
057: */
058: public static Object loadObject(String fileName) throws JRException {
059: return loadObject(new File(fileName));
060: }
061:
062: /**
063: *
064: */
065: public static Object loadObject(File file) throws JRException {
066: if (!file.exists() || !file.isFile()) {
067: throw new JRException(new FileNotFoundException(String
068: .valueOf(file)));
069: }
070:
071: Object obj = null;
072:
073: FileInputStream fis = null;
074: ObjectInputStream ois = null;
075:
076: try {
077: fis = new FileInputStream(file);
078: BufferedInputStream bufferedIn = new BufferedInputStream(
079: fis);
080: ois = new ObjectInputStream(bufferedIn);
081: obj = ois.readObject();
082: } catch (IOException e) {
083: throw new JRException("Error loading object from file : "
084: + file, e);
085: } catch (ClassNotFoundException e) {
086: throw new JRException(
087: "Class not found when loading object from file : "
088: + file, e);
089: } finally {
090: if (ois != null) {
091: try {
092: ois.close();
093: } catch (IOException e) {
094: }
095: }
096:
097: if (fis != null) {
098: try {
099: fis.close();
100: } catch (IOException e) {
101: }
102: }
103: }
104:
105: return obj;
106: }
107:
108: /**
109: *
110: */
111: public static Object loadObject(URL url) throws JRException {
112: Object obj = null;
113:
114: InputStream is = null;
115: ObjectInputStream ois = null;
116:
117: try {
118: is = url.openStream();
119: ois = new ObjectInputStream(is);
120: obj = ois.readObject();
121: } catch (IOException e) {
122: throw new JRException("Error loading object from URL : "
123: + url, e);
124: } catch (ClassNotFoundException e) {
125: throw new JRException(
126: "Class not found when loading object from URL : "
127: + url, e);
128: } finally {
129: if (ois != null) {
130: try {
131: ois.close();
132: } catch (IOException e) {
133: }
134: }
135:
136: if (is != null) {
137: try {
138: is.close();
139: } catch (IOException e) {
140: }
141: }
142: }
143:
144: return obj;
145: }
146:
147: /**
148: *
149: */
150: public static Object loadObject(InputStream is) throws JRException {
151: Object obj = null;
152:
153: ObjectInputStream ois = null;
154:
155: try {
156: ois = new ObjectInputStream(is);
157: obj = ois.readObject();
158: } catch (IOException e) {
159: throw new JRException(
160: "Error loading object from InputStream", e);
161: } catch (ClassNotFoundException e) {
162: throw new JRException(
163: "Class not found when loading object from InputStream",
164: e);
165: } finally {
166: //FIXMENOW should not close the stream
167: if (ois != null) {
168: try {
169: ois.close();
170: } catch (IOException e) {
171: }
172: }
173: }
174:
175: return obj;
176: }
177:
178: /**
179: *
180: */
181: public static Object loadObjectFromLocation(String location)
182: throws JRException {
183: return loadObjectFromLocation(location, null, null);
184: }
185:
186: /**
187: *
188: */
189: public static Object loadObjectFromLocation(String location,
190: ClassLoader classLoader) throws JRException {
191: return loadObjectFromLocation(location, classLoader, null);
192: }
193:
194: /**
195: *
196: */
197: public static Object loadObjectFromLocation(String location,
198: ClassLoader classLoader,
199: URLStreamHandlerFactory urlHandlerFactory)
200: throws JRException {
201: URL url = JRResourcesUtil
202: .createURL(location, urlHandlerFactory);
203: if (url != null) {
204: return loadObject(url);
205: }
206:
207: File file = new File(location);
208: if (file.exists() && file.isFile()) {
209: return loadObject(file);
210: }
211:
212: url = JRResourcesUtil.findClassLoaderResource(location,
213: classLoader, JRLoader.class);
214: if (url != null) {
215: return loadObject(url);
216: }
217:
218: throw new JRException("Could not load object from location : "
219: + location);
220: }
221:
222: /**
223: *
224: */
225: public static byte[] loadBytes(File file) throws JRException {
226: ByteArrayOutputStream baos = null;
227: FileInputStream fis = null;
228:
229: try {
230: fis = new FileInputStream(file);
231: baos = new ByteArrayOutputStream();
232:
233: byte[] bytes = new byte[10000];
234: int ln = 0;
235: while ((ln = fis.read(bytes)) > 0) {
236: baos.write(bytes, 0, ln);
237: }
238:
239: baos.flush();
240: } catch (IOException e) {
241: throw new JRException("Error loading byte data : " + file,
242: e);
243: } finally {
244: if (baos != null) {
245: try {
246: baos.close();
247: } catch (IOException e) {
248: }
249: }
250:
251: if (fis != null) {
252: try {
253: fis.close();
254: } catch (IOException e) {
255: }
256: }
257: }
258:
259: return baos.toByteArray();
260: }
261:
262: /**
263: *
264: */
265: public static byte[] loadBytes(URL url) throws JRException {
266: ByteArrayOutputStream baos = null;
267: InputStream is = null;
268:
269: try {
270: is = url.openStream();
271: baos = new ByteArrayOutputStream();
272:
273: byte[] bytes = new byte[10000];
274: int ln = 0;
275: while ((ln = is.read(bytes)) > 0) {
276: baos.write(bytes, 0, ln);
277: }
278:
279: baos.flush();
280: } catch (IOException e) {
281: throw new JRException("Error loading byte data : " + url, e);
282: } finally {
283: if (baos != null) {
284: try {
285: baos.close();
286: } catch (IOException e) {
287: }
288: }
289:
290: if (is != null) {
291: try {
292: is.close();
293: } catch (IOException e) {
294: }
295: }
296: }
297:
298: return baos.toByteArray();
299: }
300:
301: /**
302: *
303: */
304: public static byte[] loadBytes(InputStream is) throws JRException {
305: ByteArrayOutputStream baos = null;
306:
307: try {
308: baos = new ByteArrayOutputStream();
309:
310: byte[] bytes = new byte[10000];
311: int ln = 0;
312: while ((ln = is.read(bytes)) > 0) {
313: baos.write(bytes, 0, ln);
314: }
315:
316: baos.flush();
317: } catch (IOException e) {
318: throw new JRException(
319: "Error loading byte data from input stream.", e);
320: } finally {
321: if (baos != null) {
322: try {
323: baos.close();
324: } catch (IOException e) {
325: }
326: }
327: }
328:
329: return baos.toByteArray();
330: }
331:
332: /**
333: *
334: */
335: public static byte[] loadBytesFromLocation(String location)
336: throws JRException {
337: return loadBytesFromLocation(location, null, null);
338: }
339:
340: /**
341: *
342: */
343: public static byte[] loadBytesFromLocation(String location,
344: ClassLoader classLoader) throws JRException {
345: return loadBytesFromLocation(location, classLoader, null);
346: }
347:
348: /**
349: *
350: */
351: public static byte[] loadBytesFromLocation(String location,
352: ClassLoader classLoader,
353: URLStreamHandlerFactory urlHandlerFactory)
354: throws JRException {
355: URL url = JRResourcesUtil
356: .createURL(location, urlHandlerFactory);
357: if (url != null) {
358: return loadBytes(url);
359: }
360:
361: File file = new File(location);
362: if (file.exists() && file.isFile()) {
363: return loadBytes(file);
364: }
365:
366: url = JRResourcesUtil.findClassLoaderResource(location,
367: classLoader, JRLoader.class);
368: if (url != null) {
369: return loadBytes(url);
370: }
371:
372: throw new JRException("Byte data not found at location : "
373: + location);
374: }
375:
376: /**
377: * Tries to open an input stream for a location.
378: * <p>
379: * The method tries to interpret the location as a file name, a resource name or
380: * an URL. If any of these succeed, an input stream is created and returned.
381: *
382: * @param location the location
383: * @return an input stream if the location is an existing file name, a resource name on
384: * the classpath or an URL or <code>null</code> otherwise.
385: *
386: * @throws JRException
387: */
388: public static InputStream getLocationInputStream(String location)
389: throws JRException {
390: InputStream is = null;
391:
392: is = getResourceInputStream(location);
393:
394: if (is == null) {
395: is = getFileInputStream(location);
396: }
397:
398: if (is == null) {
399: is = getURLInputStream(location);
400: }
401:
402: return is;
403: }
404:
405: /**
406: * Tries to open a file for reading.
407: *
408: * @param filename the file name
409: * @return an input stream for the file or <code>null</code> if the file was not found
410: * @throws JRException
411: */
412: public static InputStream getFileInputStream(String filename)
413: throws JRException {
414: InputStream is = null;
415:
416: File file = new File(filename);
417: if (file.exists() && file.isFile()) {
418: try {
419: is = new FileInputStream(file);
420: } catch (FileNotFoundException e) {
421: throw new JRException("Error opening file " + filename);
422: }
423: }
424:
425: return is;
426: }
427:
428: /**
429: * Tries to open an input stream for a resource.
430: *
431: * @param resource the resource name
432: * @return an input stream for the resource or <code>null</code> if the resource was not found
433: */
434: public static InputStream getResourceInputStream(String resource) {
435: InputStream is = null;
436:
437: ClassLoader classLoader = Thread.currentThread()
438: .getContextClassLoader();
439: if (classLoader != null) {
440: is = classLoader.getResourceAsStream(resource);
441: }
442:
443: if (is == null) {
444: classLoader = JRLoader.class.getClassLoader();
445: if (classLoader != null) {
446: is = classLoader.getResourceAsStream(resource);
447: }
448:
449: if (is == null) {
450: is = JRProperties.class.getResourceAsStream("/"
451: + resource);
452: }
453: }
454:
455: return is;
456: }
457:
458: /**
459: * Tries to open an input stream for an URL.
460: *
461: * @param spec the string to parse as an URL
462: * @return an input stream for the URL or null if <code>spec</code> is not a valid URL
463: * @throws JRException
464: */
465: public static InputStream getURLInputStream(String spec)
466: throws JRException {
467: InputStream is = null;
468:
469: try {
470: URL url = new URL(spec);
471: is = url.openStream();
472: } catch (MalformedURLException e) {
473: } catch (IOException e) {
474: throw new JRException("Error opening URL " + spec);
475: }
476:
477: return is;
478: }
479: }
|