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 TransientPropertySet implements PropertySet {
036: private Map map = new HashMap();
037:
038: public TransientPropertySet() {
039: // Default constructor
040: }
041:
042: public TransientPropertySet(Map source) {
043: if (source != null) {
044: map.putAll(source);
045: }
046: }
047:
048: public Set getPropertyNames() {
049: return new HashSet(map.entrySet());
050: }
051:
052: public PropertySet getSubset(String prefix) {
053: return new PropertySubSet(this , prefix);
054: }
055:
056: public void remove(String name) {
057: map.remove(name);
058: }
059:
060: public void set(String name, Object value) {
061: map.put(name, value);
062: }
063:
064: public Object get(String name) {
065: Object ret = map.get(name);
066: if (ret == null) {
067: Iterator mit = mounts.entrySet().iterator();
068: while (mit.hasNext()) {
069: Map.Entry entry = (Map.Entry) mit.next();
070: String prefix = (String) entry.getKey();
071: if (name.startsWith(prefix)) {
072: ret = ((PropertySet) entry.getValue()).get(name
073: .substring(prefix.length()));
074: if (ret != null) {
075: return ret;
076: }
077: }
078: }
079: }
080: return ret;
081: }
082:
083: public void clear() {
084: map.clear();
085: }
086:
087: public void load(PropertySet source) {
088: Iterator it = source.getPropertyNames().iterator();
089: while (it.hasNext()) {
090: String key = (String) it.next();
091: set(key, source.get(key));
092: }
093: }
094:
095: private Map mounts = new HashMap();
096:
097: public void mount(String prefix, PropertySet source) {
098: mounts.put(prefix, source);
099: }
100:
101: public void toDom(Element holder) {
102: Iterator pit = getPropertyNames().iterator();
103: while (pit.hasNext()) {
104: String pName = (String) pit.next();
105: Object pValue = get(pName);
106: Element pe = AbstractDomObject.addTextElement(holder,
107: "property", pValue.toString());
108: pe.setAttribute("name", pName);
109: pe.setAttribute("type", pValue.getClass().getName());
110: }
111: }
112:
113: public void setAll(PropertySet source) {
114: Iterator nit = source.getPropertyNames().iterator();
115: while (nit.hasNext()) {
116: String pName = (String) nit.next();
117: set(pName, source.get(pName));
118: }
119: }
120:
121: /**
122: * @return true if other property set entries are equal to this property set entries.
123: * Other Property set attribute
124: */
125: public boolean compareProperties(PropertySet otherSet) {
126: Set myNames = new HashSet(getPropertyNames());
127: Set otherNames = new HashSet(otherSet.getPropertyNames());
128: if (!myNames.equals(otherNames)) {
129: return false;
130: }
131:
132: Iterator nit = myNames.iterator();
133: while (nit.hasNext()) {
134: String pName = (String) nit.next();
135: Object myValue = get(pName);
136: Object otherValue = otherSet.get(pName);
137: if (myValue == null) {
138: if (otherValue != null) {
139: return false;
140: }
141: } else if (!myValue.equals(otherValue)) {
142: return false;
143: }
144: }
145:
146: return true;
147: }
148:
149: public boolean containsAll(PropertySet subSet) {
150: Set myNames = new HashSet(getPropertyNames());
151: Set subsetNames = new HashSet(subSet.getPropertyNames());
152: if (!myNames.containsAll(subsetNames)) {
153: return false;
154: }
155:
156: Iterator nit = subsetNames.iterator();
157: while (nit.hasNext()) {
158: String pName = (String) nit.next();
159: Object myValue = get(pName);
160: Object subsetValue = subSet.get(pName);
161: if (myValue == null) {
162: if (subsetValue != null) {
163: return false;
164: }
165: } else if (!myValue.equals(subsetValue)) {
166: return false;
167: }
168: }
169:
170: return true;
171: }
172: }
|