KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > jwi > jgallery > Configuration


1
2 package de.jwi.jgallery;
3
4 /*
5  * jGallery - Java web application to display image galleries
6  *
7  * Copyright (C) 2004 Juergen Weber
8  *
9  * This file is part of jGallery.
10  *
11  * jGallery is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * jGallery is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with jGallery; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston
24  */

25
26
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.Serializable JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Properties JavaDoc;
33
34
35 /**
36  * @author Jürgen Weber Source file created on 29.02.2004
37  *
38  */

39 public class Configuration implements Serializable JavaDoc
40 {
41
42     private Properties JavaDoc properties;
43
44     private Configuration parent = null;
45     
46     private IThumbnailWriter thumbnailWriter;
47
48     public Configuration(Configuration parent)
49     {
50         this.parent = parent;
51         properties = new Properties JavaDoc();
52     }
53
54     public void addProperty(String JavaDoc key, String JavaDoc val)
55     {
56         properties.put(key, val);
57     }
58
59     public Configuration(InputStream JavaDoc is, Configuration parent)
60             throws IOException JavaDoc
61     {
62         this.parent = parent;
63
64         properties = new Properties JavaDoc();
65
66         if (is != null)
67         {
68             properties.load(is);
69         }
70     }
71
72     public String JavaDoc toString()
73     {
74         return properties.toString();
75     }
76
77     public Configuration(InputStream JavaDoc is) throws IOException JavaDoc
78     {
79         this(is, null);
80     }
81
82     public int getInt(String JavaDoc key, int defaultValue)
83     {
84         String JavaDoc s = getString(key);
85         if (s==null)
86         {
87             return defaultValue;
88         }
89         return Integer.parseInt(s);
90     }
91
92
93     public float getFloat(String JavaDoc key, float defaultValue)
94     {
95         String JavaDoc s = getString(key);
96         if (s==null)
97         {
98             return defaultValue;
99         }
100         
101         return Float.parseFloat(s);
102     }
103
104
105     public boolean getBoolean(String JavaDoc key, boolean defaultValue)
106     {
107         String JavaDoc s = getString(key);
108         if (s==null)
109         {
110             return defaultValue;
111         }
112         
113         return Boolean.valueOf(s).booleanValue();
114     }
115
116     public String JavaDoc getString(String JavaDoc key)
117     {
118         return getString(key,null);
119     }
120     
121     public String JavaDoc getString(String JavaDoc key, String JavaDoc defaultValue)
122     {
123         String JavaDoc val = properties.getProperty(key);
124         if (null == val)
125         {
126             if (null != parent)
127             {
128                 val = parent.getString(key);
129             }
130         }
131         if (val == null)
132         {
133             val = defaultValue;
134         }
135         return val;
136     }
137
138     /**
139      * Add all entries of this Configuration starting with variable (e.g.
140      * variable.copyright) to a map, but only if they do not exist, yet. This
141      * has the effect of variables of parent Configurations overwriting this
142      * Configuration's variables.
143      *
144      * @param map
145      * the map
146      */

147     void getUserVariables(Map JavaDoc map) throws GalleryException
148     {
149         for (Enumeration JavaDoc e = properties.propertyNames(); e.hasMoreElements();)
150         {
151             String JavaDoc key = (String JavaDoc) e.nextElement();
152             if (key.startsWith("variable."))
153             {
154                 String JavaDoc name = key.substring(key.indexOf('.') + 1);
155                 if (name.length() < 1)
156                 {
157                     throw new GalleryException("invalid variable entry: " + key);
158                 }
159                 if (!map.containsKey(name))
160                 {
161                     map.put(name, properties.getProperty(key));
162                 }
163             }
164
165         }
166         if (null != parent)
167         {
168             parent.getUserVariables(map);
169         }
170     }
171
172     public IThumbnailWriter getThumbnailWriter()
173     {
174         IThumbnailWriter writer = this.thumbnailWriter;
175         
176         if (null == writer)
177         {
178             if (null != parent)
179             {
180                 writer = parent.getThumbnailWriter();
181             }
182         }
183         return writer;
184     }
185     
186     public void setThumbnailWriter(IThumbnailWriter thumbnailWriter)
187     {
188         this.thumbnailWriter = thumbnailWriter;
189     }
190 }
Popular Tags