KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > ojb > model > DefBase


1 package xdoclet.modules.ojb.model;
2
3 /* Copyright 2004-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import java.util.*;
19
20 /**
21  * Base type for ojb repository file defs.
22  *
23  * @author <a HREF="mailto:tomdz@users.sourceforge.net">Thomas Dudziak (tomdz@users.sourceforge.net)</a>
24  * @created April 13, 2003
25  */

26 public abstract class DefBase
27 {
28     /** The owner (parent def) */
29     private DefBase _owner;
30     /** The name */
31     private String JavaDoc _name;
32     /** The properties of the class */
33     private Properties _properties = new Properties();
34
35     /**
36      * Initializes the base def object.
37      *
38      * @param name The name
39      */

40     public DefBase(String JavaDoc name)
41     {
42         _name = name;
43     }
44
45     /**
46      * Initializes the base def object to be a copy of the given base def object (except for the owner).
47      *
48      * @param src The original base def object
49      * @param prefix A prefix for the name
50      */

51     public DefBase(DefBase src, String JavaDoc prefix)
52     {
53         _name = (prefix != null ? prefix + src._name : src._name);
54
55         String JavaDoc key;
56
57         for (Iterator it = src._properties.keySet().iterator(); it.hasNext();)
58         {
59             key = (String JavaDoc)it.next();
60             setProperty(key, src._properties.getProperty(key));
61         }
62     }
63
64     /**
65      * Returns the owner of this def.
66      *
67      * @return The owner
68      */

69     public DefBase getOwner()
70     {
71         return _owner;
72     }
73
74     /**
75      * Sets the owner of this def.
76      *
77      * @param owner The owner
78      */

79     public void setOwner(DefBase owner)
80     {
81         _owner = owner;
82     }
83
84     /**
85      * Returns the name of the def object.
86      *
87      * @return The name
88      */

89     public String JavaDoc getName()
90     {
91         return _name;
92     }
93
94     /**
95      * Returns the value of the specified property.
96      *
97      * @param name The name of the property
98      * @return The value
99      */

100     public String JavaDoc getProperty(String JavaDoc name)
101     {
102         return _properties.getProperty(name);
103     }
104
105     /**
106      * Returns the boolean value of the specified property.
107      *
108      * @param name The name of the property
109      * @param defaultValue The value to use if the property is not set or not a boolean
110      * @return The value
111      */

112     public boolean getBooleanProperty(String JavaDoc name, boolean defaultValue)
113     {
114         return PropertyHelper.toBoolean(_properties.getProperty(name), defaultValue);
115     }
116
117     /**
118      * Returns the property names.
119      *
120      * @return The names
121      */

122     public Iterator getPropertyNames()
123     {
124         return _properties.keySet().iterator();
125     }
126
127     /**
128      * Sets a property.
129      *
130      * @param name The property name
131      * @param value The property value
132      */

133     public void setProperty(String JavaDoc name, String JavaDoc value)
134     {
135         if ((value == null) || (value.length() == 0))
136         {
137             _properties.remove(name);
138         }
139         else
140         {
141             _properties.setProperty(name, value);
142         }
143     }
144
145     /**
146      * Determines whether a properties exists.
147      *
148      * @param name The property name
149      * @return <code>true</code> if the property exists
150      */

151     public boolean hasProperty(String JavaDoc name)
152     {
153         return _properties.containsKey(name);
154     }
155
156     /**
157      * Applies the modifications contained in the given properties object.
158      * Properties are removed by having a <code>null</code> entry in the mods object.
159      * Properties that are new in mods object, are added to this def object.
160      *
161      * @param mods The modifications
162      */

163     public void applyModifications(Properties mods)
164     {
165         String JavaDoc key;
166         String JavaDoc value;
167
168         for (Iterator it = mods.keySet().iterator(); it.hasNext();)
169         {
170             key = (String JavaDoc)it.next();
171             value = mods.getProperty(key);
172             setProperty(key, value);
173         }
174     }
175
176     public String JavaDoc toString()
177     {
178         return getName();
179     }
180 }
181
Popular Tags