View Javadoc

1   /*
2    * Copyright (c) 2003, Henri Yandell
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or 
6    * without modification, are permitted provided that the 
7    * following conditions are met:
8    * 
9    * + Redistributions of source code must retain the above copyright notice, 
10   *   this list of conditions and the following disclaimer.
11   * 
12   * + Redistributions in binary form must reproduce the above copyright notice, 
13   *   this list of conditions and the following disclaimer in the documentation 
14   *   and/or other materials provided with the distribution.
15   * 
16   * + Neither the name of OSJava nor the names of its contributors 
17   *   may be used to endorse or promote products derived from this software 
18   *   without specific prior written permission.
19   * 
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
23   * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
30   * POSSIBILITY OF SUCH DAMAGE.
31   */
32  package com.generationjava.config;
33  
34  import java.util.ArrayList;
35  import java.util.List;
36  import java.util.Date;
37  
38  public abstract class AbstractConfig implements Config {
39  
40      private String context = "";
41  
42      protected abstract Object getValue(String key);
43  
44      public Object get(String key) {
45          return getValue( getContext()+key );
46      }
47  
48      public boolean has(String key) {
49          return (get(key) != null);
50      }
51  
52      public Object getAbsolute(String key) {
53          return getValue(key);
54      }
55  
56      public String getString(String key) {
57          return (String)get(key);
58      }
59  
60      public Date getDate(String key) {
61          try {
62              return java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT).parse(key);
63          } catch(java.text.ParseException pe) {
64              return null;
65          }
66      }
67  
68      // rely on simple-jndi's type
69      public int getInt(String key) {
70          try {
71              return Integer.parseInt( getString(key) );
72          } catch(NumberFormatException nfe) {
73              return 0;
74          }
75      }
76  
77      public double getDouble(String key) {
78          try {
79              return Double.parseDouble( getString(key) );
80          } catch(NumberFormatException nfe) {
81              return 0.0;
82          }
83      }
84  
85      public List getList(String key) {
86          Object obj = get(key);
87          if(!(obj instanceof List)) {
88              List list = new ArrayList(1);
89              list.add(obj);
90              obj = list;
91          }
92          return (List)obj;
93      }
94  
95      public void setContext(String context) {
96          this.context = context;
97      }
98  
99      public void pushToContext(String contextElement) {
100         this.context = this.context + contextElement;
101     }
102 
103     public void popFromContext(String contextElement) {
104         if(this.context.endsWith(contextElement)) {
105             this.context = this.context.substring(0, this.context.length() - contextElement.length());
106         } else {
107             throw new IllegalArgumentException("Context does not end with: "+contextElement);
108         }
109     }
110 
111     public String getContext() {
112         return this.context;
113     }
114 
115     public Config cloneConfig() {
116         try {
117             return (Config)this.clone();
118         } catch(CloneNotSupportedException cnse) {
119             // ignore
120             throw new RuntimeException("Cloning of a Config failed. This should be impossible. ");
121         }
122     }
123 
124 }