KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > meshcms > util > CustomProperties


1 /*
2  * MeshCMS - A simple CMS based on SiteMesh
3  * Copyright (C) 2004-2007 Luciano Vernaschi
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * You can contact the author at http://www.cromoteca.com
20  * and at info@cromoteca.com
21  */

22
23 package org.meshcms.util;
24
25 import java.util.*;
26
27 /**
28  * Convenience class to use java.util.Properties to store values that are not
29  * strings. All additional getters require a default value, but you can safely
30  * use null for non-primitive types.
31  */

32 public class CustomProperties extends Properties {
33   public int getProperty(String JavaDoc key, int defaultValue) {
34     try {
35       defaultValue = Integer.parseInt(getProperty(key));
36     } catch (Exception JavaDoc ex) {
37       //
38
}
39     
40     return defaultValue;
41   }
42   
43   public void setProperty(String JavaDoc key, int value) {
44     setProperty(key, Integer.toString(value));
45   }
46
47   public boolean getProperty(String JavaDoc key, boolean defaultValue) {
48     try {
49       defaultValue = Utils.isTrue(key, true);
50     } catch (Exception JavaDoc ex) {
51       //
52
}
53     
54     return defaultValue;
55   }
56   
57   public void setProperty(String JavaDoc key, boolean value) {
58     setProperty(key, Boolean.toString(value));
59   }
60
61   public String JavaDoc[] getProperty(String JavaDoc key, String JavaDoc[] defaultValue, char separator) {
62     String JavaDoc value = getProperty(key);
63     
64     if (value != null) {
65       defaultValue = Utils.tokenize(value, Character.toString(separator));
66     }
67     
68     return defaultValue;
69   }
70   
71   public void setProperty(String JavaDoc key, String JavaDoc[] value, char separator) {
72     setProperty(key, Utils.generateList(value, Character.toString(separator)));
73   }
74 }
75
Popular Tags