1 /*
2 * db2sa: DB2 Syntax Assistant
3 * Copyright (C) Andres Gomez Casanova
4 *
5 * This file is part of db2sa.
6 *
7 * db2sa is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * db2sa is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, see <http://www.gnu.org/licenses/>.
19 *
20 * Contact:
21 * a n g o c a at y a h o o dot c o m
22 * Cra. 45 No 61 - 31, Bogota, Colombia.
23 *
24 * Author: $LastChangedBy: angoca $:
25 * Date: $LastChangedDate: 2009-06-12 00:06:18 +0200 (Fri, 12 Jun 2009) $:
26 * Revision: $LastChangedRevision: 227 $:
27 * URL: $HeadURL: https://db2sa.svn.sourceforge.net/svnroot/db2sa/branches/db2sa_beta/source-code/src/main/java/name/angoca/db2sa/readers/ConfigurationReader.java $:
28 */
29 package name.angoca.db2sa.readers;
30
31 import java.io.FileInputStream;
32 import java.io.FileNotFoundException;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.util.InvalidPropertiesFormatException;
36 import java.util.Properties;
37
38 import name.angoca.db2sa.readers.exceptions.AbstractReaderException;
39 import name.angoca.db2sa.readers.exceptions.ConfigurationFileCorruptException;
40 import name.angoca.db2sa.readers.exceptions.ConfigurationFileNotFoundException;
41 import name.angoca.db2sa.readers.exceptions.ConfigurationFileProblemException;
42
43 /**
44 * This class read the configuration file and then it can return the set of
45 * parameters to the controller.<br/>
46 * <b>Control Version</b><br />
47 * <ul>
48 * <li>0.0.1 Class creation.</li>
49 * <li>0.1.0 Recommendations from PMD.</li>
50 * <li>0.1.1 Organized.</li>
51 * <li>0.1.2 Close streams.</li>
52 * <li>1.0.0 Moved to version 1.</li>
53 * </ul>
54 *
55 * @author Andres Gomez Casanova <a
56 * href="mailto:a n g o c a at y a h o o dot c o m">(AngocA)</a>
57 * @version 1.0.0 2009-07-19
58 */
59 public final class ConfigurationReader {
60
61 /**
62 * Default constructor private.
63 */
64 private ConfigurationReader() {
65 // Nothing to do in the constructor. No tests.
66 }
67
68 /**
69 * Reads a file given its filename, and the retrieves the properties stored
70 * in the file.
71 *
72 * @param filename
73 * name of the file that contains the configuration of the
74 * application.
75 * @return The set of properties read from the file.
76 * @throws AbstractReaderException
77 * If an error reading the file occurs, error such as file not
78 * found, or invalid structure.
79 */
80 public static Properties/* ! */readFile(final String/* ! */filename)
81 throws AbstractReaderException {
82 InputStream inputStream = null;
83 final Properties properties = new Properties();
84 try {
85 // This file has the properties.
86 inputStream = new FileInputStream(filename);
87 // The file is XML and put the values in the properties.
88 properties.loadFromXML(inputStream);
89 } catch (FileNotFoundException e) {
90 throw new ConfigurationFileNotFoundException(filename, e);
91 } catch (InvalidPropertiesFormatException e) {
92 throw new ConfigurationFileCorruptException(filename, e);
93 } catch (IOException e) {
94 // I don't know how to test this part. It's outside my scope.
95 // TODO revisar la excepción, ya que puede confundir un poco.
96 throw new ConfigurationFileProblemException(e);
97 } finally {
98 try {
99 if (inputStream != null) {
100 inputStream.close();
101 }
102 } catch (IOException e) {
103 // I don't know how to test this part. It's outside my scope.
104 // TODO Auto-generated catch block
105 e.printStackTrace();
106 }
107 }
108 return properties;
109 }
110
111 }