001: /*
002: * argun 1.0
003: * Web 2.0 delivery framework
004: * Copyright (C) 2007 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.web.properties;
024:
025: import java.util.HashMap;
026: import java.util.HashSet;
027: import java.util.Iterator;
028: import java.util.Map;
029: import java.util.Set;
030:
031: import org.w3c.dom.Element;
032:
033: import biz.hammurapi.xml.dom.AbstractDomObject;
034:
035: public class PropertySubSet implements PropertySet {
036:
037: private PropertySet master;
038: private String prefix;
039:
040: public PropertySubSet(PropertySet master, String prefix) {
041: this .master = master;
042: this .prefix = prefix;
043: }
044:
045: public Set getPropertyNames() {
046: Set ret = new HashSet();
047: Iterator it = master.getPropertyNames().iterator();
048: while (it.hasNext()) {
049: String name = (String) it.next();
050: if (name.startsWith(prefix)) {
051: ret.add(name.substring(prefix.length()));
052: }
053: }
054: return ret;
055: }
056:
057: public PropertySet getSubset(String prefix) {
058: return new PropertySubSet(this , prefix);
059: }
060:
061: public void remove(String name) {
062: master.remove(prefix + name);
063: }
064:
065: public void set(String name, Object value) {
066: master.set(prefix + name, value);
067: }
068:
069: public Object get(String name) {
070: Object ret = master.get(prefix + name);
071: if (ret == null) {
072: Iterator mit = mounts.entrySet().iterator();
073: while (mit.hasNext()) {
074: Map.Entry entry = (Map.Entry) mit.next();
075: String prefix = (String) entry.getKey();
076: if (name.startsWith(prefix)) {
077: ret = ((PropertySet) entry.getValue()).get(name
078: .substring(prefix.length()));
079: if (ret != null) {
080: return ret;
081: }
082: }
083: }
084: }
085: return ret;
086: }
087:
088: public void clear() {
089: Iterator it = master.getPropertyNames().iterator();
090: while (it.hasNext()) {
091: String name = (String) it.next();
092: if (name.startsWith(prefix)) {
093: master.remove(name);
094: }
095: }
096: }
097:
098: public void load(PropertySet source) {
099: Iterator it = source.getPropertyNames().iterator();
100: while (it.hasNext()) {
101: String key = (String) it.next();
102: master.set(prefix + key, source.get(key));
103: }
104: }
105:
106: private Map mounts = new HashMap();
107:
108: public void mount(String prefix, PropertySet source) {
109: mounts.put(prefix, source);
110: }
111:
112: public void toDom(Element holder) {
113: Iterator pit = getPropertyNames().iterator();
114: while (pit.hasNext()) {
115: String pName = (String) pit.next();
116: Object pValue = get(pName);
117: Element pe = AbstractDomObject.addTextElement(holder,
118: "property", pValue.toString());
119: pe.setAttribute("name", pName);
120: pe.setAttribute("type", pValue.getClass().getName());
121: }
122: }
123:
124: public void setAll(PropertySet source) {
125: Iterator nit = source.getPropertyNames().iterator();
126: while (nit.hasNext()) {
127: String pName = (String) nit.next();
128: set(pName, source.get(pName));
129: }
130: }
131:
132: /**
133: * @return true if other property set entries are equal to this property set entries.
134: * Other Property set attribute
135: */
136: public boolean compareProperties(PropertySet otherSet) {
137: Set myNames = new HashSet(getPropertyNames());
138: Set otherNames = new HashSet(otherSet.getPropertyNames());
139: if (!myNames.equals(otherNames)) {
140: return false;
141: }
142:
143: Iterator nit = myNames.iterator();
144: while (nit.hasNext()) {
145: String pName = (String) nit.next();
146: Object myValue = get(pName);
147: Object otherValue = otherSet.get(pName);
148: if (myValue == null) {
149: if (otherValue != null) {
150: return false;
151: }
152: } else if (!myValue.equals(otherValue)) {
153: return false;
154: }
155: }
156:
157: return true;
158: }
159:
160: public boolean containsAll(PropertySet subSet) {
161: Set myNames = new HashSet(getPropertyNames());
162: Set subsetNames = new HashSet(subSet.getPropertyNames());
163: if (!myNames.containsAll(subsetNames)) {
164: return false;
165: }
166:
167: Iterator nit = subsetNames.iterator();
168: while (nit.hasNext()) {
169: String pName = (String) nit.next();
170: Object myValue = get(pName);
171: Object subsetValue = subSet.get(pName);
172: if (myValue == null) {
173: if (subsetValue != null) {
174: return false;
175: }
176: } else if (!myValue.equals(subsetValue)) {
177: return false;
178: }
179: }
180:
181: return true;
182: }
183: }
|