1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.sf.jour.util;
22
23 import java.io.InputStream;
24 import java.net.URL;
25
26 import javax.xml.bind.JAXBContext;
27 import javax.xml.bind.JAXBException;
28
29 /***
30 * TODO Add docs
31 * Need this Class so runtime would not depend on JAXB.
32 *
33 * Created on 04.12.2004
34 *
35 * Contributing Author(s):
36 *
37 * Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
38 * Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
39 *
40 * @author vlads
41 * @version $Revision: 1.3 $ ($Author: vlads $) $Date: 2004/12/09 21:35:18 $
42 */
43
44 public class ConfigFileUtil extends FileUtil {
45
46 public static Object unmarshalConfigFile(String fileName, String packageName) {/package-summary.html">ong> static Object unmarshalConfigFile(String fileName, String packageName) {
47 try {
48 InputStream is = ConfigFileUtil.loadConfig(fileName);
49 if (is == null) {
50 return null;
51 }
52 JAXBContext jc = JAXBContext.newInstance(packageName);
53 javax.xml.bind.Unmarshaller unmarshaller;
54 unmarshaller = jc.createUnmarshaller();
55 unmarshaller.setValidating(false);
56
57 return unmarshaller.unmarshal(is);
58
59 } catch (JAXBException jaxbe) {
60 FileUtil.log.error("Configuration error", jaxbe);
61 return null;
62 }
63 }
64
65 public static InputStream loadConfig(String fileName) {
66 URL location = FileUtil.getFile(fileName);
67 if (location != null) {
68 try {
69 FileUtil.log.info("Using config file " + location);
70 return location.openStream();
71 } catch (Exception e) {
72 FileUtil.log.error("Error reading " + fileName, e);
73 return null;
74 }
75 } else {
76 FileUtil.log.error("Config file not found: " + fileName);
77 return null;
78 }
79 }
80 }