001: package xdoclet.modules.ojb.model;
002:
003: /* Copyright 2004-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import java.util.*;
019:
020: /**
021: * Base type for ojb repository file defs.
022: *
023: * @author <a href="mailto:tomdz@users.sourceforge.net">Thomas Dudziak (tomdz@users.sourceforge.net)</a>
024: * @created April 13, 2003
025: */
026: public abstract class DefBase {
027: /** The owner (parent def) */
028: private DefBase _owner;
029: /** The name */
030: private String _name;
031: /** The properties of the class */
032: private Properties _properties = new Properties();
033:
034: /**
035: * Initializes the base def object.
036: *
037: * @param name The name
038: */
039: public DefBase(String name) {
040: _name = name;
041: }
042:
043: /**
044: * Initializes the base def object to be a copy of the given base def object (except for the owner).
045: *
046: * @param src The original base def object
047: * @param prefix A prefix for the name
048: */
049: public DefBase(DefBase src, String prefix) {
050: _name = (prefix != null ? prefix + src._name : src._name);
051:
052: String key;
053:
054: for (Iterator it = src._properties.keySet().iterator(); it
055: .hasNext();) {
056: key = (String) it.next();
057: setProperty(key, src._properties.getProperty(key));
058: }
059: }
060:
061: /**
062: * Returns the owner of this def.
063: *
064: * @return The owner
065: */
066: public DefBase getOwner() {
067: return _owner;
068: }
069:
070: /**
071: * Sets the owner of this def.
072: *
073: * @param owner The owner
074: */
075: public void setOwner(DefBase owner) {
076: _owner = owner;
077: }
078:
079: /**
080: * Returns the name of the def object.
081: *
082: * @return The name
083: */
084: public String getName() {
085: return _name;
086: }
087:
088: /**
089: * Returns the value of the specified property.
090: *
091: * @param name The name of the property
092: * @return The value
093: */
094: public String getProperty(String name) {
095: return _properties.getProperty(name);
096: }
097:
098: /**
099: * Returns the boolean value of the specified property.
100: *
101: * @param name The name of the property
102: * @param defaultValue The value to use if the property is not set or not a boolean
103: * @return The value
104: */
105: public boolean getBooleanProperty(String name, boolean defaultValue) {
106: return PropertyHelper.toBoolean(_properties.getProperty(name),
107: defaultValue);
108: }
109:
110: /**
111: * Returns the property names.
112: *
113: * @return The names
114: */
115: public Iterator getPropertyNames() {
116: return _properties.keySet().iterator();
117: }
118:
119: /**
120: * Sets a property.
121: *
122: * @param name The property name
123: * @param value The property value
124: */
125: public void setProperty(String name, String value) {
126: if ((value == null) || (value.length() == 0)) {
127: _properties.remove(name);
128: } else {
129: _properties.setProperty(name, value);
130: }
131: }
132:
133: /**
134: * Determines whether a properties exists.
135: *
136: * @param name The property name
137: * @return <code>true</code> if the property exists
138: */
139: public boolean hasProperty(String name) {
140: return _properties.containsKey(name);
141: }
142:
143: /**
144: * Applies the modifications contained in the given properties object.
145: * Properties are removed by having a <code>null</code> entry in the mods object.
146: * Properties that are new in mods object, are added to this def object.
147: *
148: * @param mods The modifications
149: */
150: public void applyModifications(Properties mods) {
151: String key;
152: String value;
153:
154: for (Iterator it = mods.keySet().iterator(); it.hasNext();) {
155: key = (String) it.next();
156: value = mods.getProperty(key);
157: setProperty(key, value);
158: }
159: }
160:
161: public String toString() {
162: return getName();
163: }
164: }
|