Inspector | Message | Severity | Location |
---|---|---|---|
Java Inspector 048 | Copyrights information should be present in each file. | 1 | |
Java Inspector 070-A | Cyclomatic complexity is too high: 13, maximum allowed is 12 | 2 | 161:5 |
Java Inspector 089 | Constructor documentation is too short. It is only 1 words. Should be at least 3 words. | 2 | 72:5 |
Java Inspector 089 | Method is not properly documented | 2 | 94:5 |
Java Inspector 089 | Method is not properly documented | 2 | 129:5 |
Java Inspector 089 | Javadoc contains tag for exception which method doesn't throw FileNotFoundException | 2 | 129:5 |
Java Inspector 089 | Javadoc contains tag for exception which method doesn't throw IOException | 2 | 129:5 |
Java Inspector 089 | Javadoc contains tag for exception which method doesn't throw MalformedURLException | 2 | 129:5 |
Java Inspector 089 | Return value documentation is too short. It is only 1 words. Should be at least 3 words. | 2 | 129:5 |
Java Inspector 089 | Undocumented method | 2 | 161:5 |
Java Inspector 024 | Avoid hardwired character literals | 3 | 150:65 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 105:50 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 108:50 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 111:50 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 114:50 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 140:58 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 157:46 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 163:21 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 171:28 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 179:28 |
Java Inspector 051 | It is good practice to call in any case super() in a constructor. | 3 | 72:5 |
Java Inspector 090 | Unnecessary else part in if. The main part terminates control flow (return, break, throw, or continue). | 3 | 132:17 |
Java Inspector 090 | Unnecessary else part in if. The main part terminates control flow (return, break, throw, or continue). | 3 | 134:24 |
Java Inspector 090 | Unnecessary else part in if. The main part terminates control flow (return, break, throw, or continue). | 3 | 136:24 |
1/*
2 * hgcommons 9
3 * Hammurapi Group Common Library
4 * Copyright (C) 2003 Hammurapi Group
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21 * e-Mail: support@hammurapi.biz
22 */
23package biz.hammurapi.config;
24
25import java.io.FileInputStream;
26import java.io.FileNotFoundException;
27import java.io.IOException;
28import java.io.InputStream;
29import java.net.MalformedURLException;
30import java.net.URL;
31import java.text.MessageFormat;
32
33import javax.xml.parsers.DocumentBuilderFactory;
34import javax.xml.parsers.FactoryConfigurationError;
35import javax.xml.parsers.ParserConfigurationException;
36
37import org.w3c.dom.Document;
38import org.xml.sax.SAXException;
39
40/**
41 * Helper class to read XML configuration from file, url or classloader
42 * resource.
43 *
44 * @author Pavel Vlasov
45 * @version $Revision: 1.2 $
46 */
47public class XmlSource implements Parameterizable {
48
49 private String prefix;
50
51 private Class masterClass;
52
53 private String defaultResourceExtension;
54
55 /**
56 * Constructor
57 *
58 * @param prefix
59 * Prefix for configuration parameters. Parameter specifying
60 * configruration file would be <prefix>-file, url -
61 * <prefix>-url, resource - <prefix>-resource
62 * @param masterClass
63 * Master class' classloader will be used to load resources and
64 * default resource would be masterClass name with
65 * defaultResourceExtension. If master class is null then this
66 * class classloader will be used and no default resource will be
67 * loaded.
68 * @param defaultResourceExtension
69 * Extension for default resource. If it is null then default
70 * resource will not be loaded.
71 */
72 public XmlSource(String prefix, Class masterClass,
73 String defaultResourceExtension) {
74 this.prefix = prefix;
75 this.masterClass = masterClass;
76 this.defaultResourceExtension = defaultResourceExtension;
77 }
78
79 private static final String CONFIG_ERROR_MESSAGE = "'{0}-file', "
80 + "'{0}-resource', and '{0}-url' parameters are mutually exclusive";
81
82 private String resource;
83
84 private String url;
85
86 private String file;
87
88 private Document document;
89
90 /**
91 * @return parsed XML or null if config not found.
92 * @throws ConfigurationException
93 */
94 public Document getConfigDocument() throws ConfigurationException {
95 if (document == null) {
96 try {
97 InputStream configStream = getStream();
98
99 if (configStream!=null) {
100 document = DocumentBuilderFactory.newInstance()
101 .newDocumentBuilder().parse(configStream);
102 configStream.close();
103 }
104 } catch (IOException e) {
105 throw new ConfigurationException("Cannot load document, "
106 + e.getMessage(), e);
107 } catch (SAXException e) {
108 throw new ConfigurationException("Cannot load document, "
109 + e.getMessage(), e);
110 } catch (ParserConfigurationException e) {
111 throw new ConfigurationException("Cannot load document, "
112 + e.getMessage(), e);
113 } catch (FactoryConfigurationError e) {
114 throw new ConfigurationException("Cannot load document, "
115 + e.getMessage(), e);
116 }
117 }
118
119 return document;
120 }
121
122 /**
123 * @return Stream
124 * @throws FileNotFoundException
125 * @throws IOException
126 * @throws MalformedURLException
127 * @throws ConfigurationException
128 */
129 public InputStream getStream() throws ConfigurationException {
130 try {
131 Class clazz = masterClass==null ? getClass() : masterClass;
132 if (file != null) {
133 return new FileInputStream(file);
134 } else if (url != null) {
135 return new URL(url).openStream();
136 } else if (resource != null) {
137
138 InputStream configStream = clazz.getClassLoader().getResourceAsStream(resource);
139 if (configStream == null) {
140 throw new ConfigurationException("Resource not found: " + resource);
141 }
142
143 return configStream;
144 } else {
145 if (defaultResourceExtension==null) {
146 return null;
147 }
148
149 String className = clazz.getName();
150 int idx = className.lastIndexOf('.');
151 String rName = (idx == -1 ? className : className
152 .substring(idx + 1))
153 + defaultResourceExtension;
154 return clazz.getResourceAsStream(rName);
155 }
156 } catch (IOException e) {
157 throw new ConfigurationException("Cannot open stream, " + e.getMessage(), e);
158 }
159 }
160
161 public boolean setParameter(String name, Object value)
162 throws ConfigurationException {
163 if ((prefix+"-file").equals(name)) {
164 if (resource != null) {
165 throw new ConfigurationException(MessageFormat.format(CONFIG_ERROR_MESSAGE, new Object[] {prefix}));
166 }
167 if (url != null) {
168 throw new ConfigurationException(MessageFormat.format(CONFIG_ERROR_MESSAGE, new Object[] {prefix}));
169 }
170 file = value.toString();
171 } else if ((prefix+"-resource").equals(name)) {
172 if (file != null) {
173 throw new ConfigurationException(MessageFormat.format(CONFIG_ERROR_MESSAGE, new Object[] {prefix}));
174 }
175 if (url != null) {
176 throw new ConfigurationException(MessageFormat.format(CONFIG_ERROR_MESSAGE, new Object[] {prefix}));
177 }
178 resource = value.toString();
179 } else if ((prefix+"-url").equals(name)) {
180 if (file != null) {
181 throw new ConfigurationException(MessageFormat.format(CONFIG_ERROR_MESSAGE, new Object[] {prefix}));
182 }
183 if (resource != null) {
184 throw new ConfigurationException(MessageFormat.format(CONFIG_ERROR_MESSAGE, new Object[] {prefix}));
185 }
186 url = value.toString();
187 } else {
188 return false;
189 }
190 return true;
191 }
192}