1 //==============================================================================
2 // file : PropertiesReader.java
3 // project: Java Common Utility
4 //
5 // last change: date: $Date: 2003/09/10 09:22:14 $
6 // by: $Author: bitiboy $
7 // revision: $Revision: 1.1 $
8 //------------------------------------------------------------------------------
9 // copyright: GNU GPL Software License (see class documentation)
10 //==============================================================================
11 package com.justhis.util;
12
13
14 /*
15 * $Id: PropertiesReader.java,v 1.1 2003/09/10 09:22:14 bitiboy Exp $
16 *
17 * Copyright 2003 Acai Software All Rights Reserved.
18 *
19 * This file is part of the Java Common Utility.
20
21 * The Java Common Utility is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation; either version 2 of the License, or
24 * (at your option) any later version.
25
26 * The Java Common Utility is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
30
31 * You should have received a copy of the GNU General Public License
32 * along with the Java Common Utility; if not, write to the Free Software
33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34
35 * http://www.justhis.com
36 * CONTACT: email = superaxis@sohu.com webmaster@justhis.com
37 */
38 import com.justhis.util.exception.PropertiesFileReadException;
39
40 import java.io.BufferedInputStream;
41 import java.io.FileInputStream;
42 import java.io.FileNotFoundException;
43 import java.io.IOException;
44 import java.io.InputStream;
45
46 import java.util.Enumeration;
47 import java.util.HashMap;
48 import java.util.Map;
49 import java.util.Properties;
50
51
52 /***
53 * ???????????? ????????????????????????????????????????????classpath??????????
54 *
55 * @author <a href="http://blog.ejb.cn">acai</a>
56 * @version $Revision: 1.1 $
57 */
58 public class PropertiesReader {
59 //~ Instance fields --------------------------------------------------------
60
61 /*** ?????? */
62 private Properties props = null;
63
64 //~ Constructors -----------------------------------------------------------
65
66 /***
67 * Creates a new PropertiesReader object. use properties file name or path
68 *
69 * @param propertiesFile file name or path
70 *
71 * @throws PropertiesFileReadException if any error occurs.
72 */
73 public PropertiesReader(String propertiesFile)
74 throws PropertiesFileReadException {
75 this.buildFromProperties(propertiesFile);
76 }
77
78 /***
79 * Creates a new PropertiesReader object. use stream.
80 *
81 * @param stream a stream of input
82 *
83 * @throws PropertiesFileReadException if any error occurs
84 */
85 public PropertiesReader(InputStream stream)
86 throws PropertiesFileReadException {
87 this.props = new Properties();
88
89 try {
90 this.props.load(new BufferedInputStream(stream));
91 } catch (IOException e) {
92 throw new PropertiesFileReadException(e);
93 }
94 }
95
96 //~ Methods ----------------------------------------------------------------
97
98 /***
99 * TODO
100 *
101 * @return TODO
102 */
103 public Enumeration getKeys() {
104 return this.props.propertyNames();
105 }
106
107 /***
108 * Get properties
109 *
110 * @return java.util.Properties
111 */
112 public Properties getProperties() {
113 return this.props;
114 }
115
116 /***
117 * get properties by construct a PropertiesReader
118 *
119 * @param fileName a properties name
120 *
121 * @return java.util.Properties
122 *
123 * @throws PropertiesFileReadException if any error occurs.
124 */
125 public static Properties getProperties(String fileName)
126 throws PropertiesFileReadException {
127 PropertiesReader pr = new PropertiesReader(fileName);
128
129 return pr.getProperties();
130 }
131
132 /***
133 * TODO
134 *
135 * @param propertyName TODO
136 *
137 * @return TODO
138 */
139 public String getProperty(String propertyName) {
140 return this.props.getProperty(propertyName);
141 }
142
143 /***
144 * TODO
145 *
146 * @return TODO
147 */
148 public Map getPropertyMap() {
149 Enumeration enum = this.getKeys();
150 Map hm = new HashMap();
151
152 while (enum.hasMoreElements()) {
153 String next = (String) enum.nextElement();
154 hm.put(next, this.getProperty(next));
155 }
156
157 return hm;
158 }
159
160 /***
161 * TODO
162 *
163 * @param file TODO
164 *
165 * @throws PropertiesFileReadException TODO
166 */
167 protected void buildFromFile(String file)
168 throws PropertiesFileReadException {
169 FileInputStream fStream;
170
171 try {
172 fStream = new FileInputStream(file);
173 } catch (FileNotFoundException e) {
174 throw new PropertiesFileReadException(e);
175 }
176
177 this.props = new Properties();
178
179 try {
180 this.props.load(new BufferedInputStream(fStream));
181 } catch (IOException e1) {
182 throw new PropertiesFileReadException(e1);
183 }
184 }
185
186 /***
187 * TODO
188 *
189 * @param file TODO
190 *
191 * @throws PropertiesFileReadException TODO
192 */
193 protected void buildFromProperties(String file)
194 throws PropertiesFileReadException {
195 String fName = null;
196
197 fName = file.replace('.', '/') + ".properties";
198
199 System.out.println(fName + "==========");
200
201 InputStream stream = getClass().getResourceAsStream(fName);
202 BufferedInputStream bStream = new BufferedInputStream(stream);
203 this.props = new Properties();
204
205 try {
206 this.props.load(bStream);
207 } catch (IOException e) {
208 throw new PropertiesFileReadException(e);
209 }
210 }
211 }
212
213
214 /*
215 * $Log: PropertiesReader.java,v $
216 * Revision 1.1 2003/09/10 09:22:14 bitiboy
217 * *** empty log message ***
218 *
219 *
220 */
This page was automatically generated by Maven