001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. 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: package org.apache.cocoon.portal.pluto.om.common;
018:
019: import java.util.ArrayList;
020: import java.util.Collection;
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import org.apache.commons.lang.BooleanUtils;
025: import org.apache.pluto.om.common.Preference;
026: import org.apache.pluto.om.common.PreferenceCtrl;
027: import org.apache.pluto.util.StringUtils;
028:
029: /**
030: *
031: *
032: * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
033: *
034: * @version CVS $Id: PreferenceImpl.java 433543 2006-08-22 06:22:54Z crossley $
035: */
036: public class PreferenceImpl implements Preference, PreferenceCtrl,
037: java.io.Serializable {
038:
039: private final static String NULL_VALUE = "#*!0_NULL_0!*#";
040: private final static String NULL_ARRAYENTRY = "#*!1_NULL_1!*#";
041:
042: private String name;
043: private ArrayList value;
044: private Boolean readOnly;
045:
046: public PreferenceImpl() {
047: // nothing to do
048: }
049:
050: /**
051: * @see org.apache.pluto.om.common.Preference#getName()
052: */
053: public String getName() {
054: return name;
055: }
056:
057: /**
058: * @see org.apache.pluto.om.common.Preference#getValues()
059: */
060: public Iterator getValues() {
061: // replace the NULL_VALUE String by NULL
062: if (value.contains(NULL_VALUE)) {
063: return null;
064: }
065:
066: ArrayList returnValue = new ArrayList(value.size());
067: returnValue.addAll(value);
068:
069: // replace all NULL_ARRAYENTRY Strings by NULL
070: for (int i = 0; i < returnValue.size(); i++) {
071: if (NULL_ARRAYENTRY.equals(returnValue.get(i))) {
072: returnValue.set(i, null);
073: }
074: }
075:
076: return returnValue.iterator();
077: }
078:
079: /**
080: * @see org.apache.pluto.om.common.Preference#isReadOnly()
081: */
082: public boolean isReadOnly() {
083: if (readOnly == null) {
084: return false;
085: }
086: return readOnly.booleanValue();
087: }
088:
089: /**
090: * @see org.apache.pluto.om.common.Preference#isValueSet()
091: */
092: public boolean isValueSet() {
093: return value != null;
094: }
095:
096: // PreferenceCtrl implementation.
097:
098: /**
099: * @see org.apache.pluto.om.common.PreferenceCtrl#setName(java.lang.String)
100: */
101: public void setName(String name) {
102: this .name = name;
103: }
104:
105: /**
106: * @see org.apache.pluto.om.common.PreferenceCtrl#setValues(java.util.List)
107: */
108: public void setValues(java.util.List _value) {
109: if (this .value == null) {
110: this .value = new ArrayList();
111: } else {
112: this .value.clear();
113: }
114:
115: List addValue = null;
116:
117: // replace NULL by the NULL_VALUE String
118: if (_value == null) {
119: addValue = new ArrayList(1);
120: addValue.add(NULL_VALUE);
121: } else {
122: // replace all NULL by the NULL_ARRAYENTRY String
123: addValue = new ArrayList(_value.size());
124: addValue.addAll(_value);
125: for (int i = 0; i < addValue.size(); i++) {
126: if (addValue.get(i) == null) {
127: addValue.set(i, NULL_ARRAYENTRY);
128: }
129: }
130: }
131:
132: this .value.addAll(addValue);
133: }
134:
135: /**
136: * @see org.apache.pluto.om.common.PreferenceCtrl#setReadOnly(java.lang.String)
137: */
138: public void setReadOnly(String readOnly) {
139: this .readOnly = BooleanUtils.toBooleanObject(readOnly);
140: }
141:
142: // additional methods.
143:
144: // internal methods only used by castor
145:
146: public String getReadOnly() {
147: return BooleanUtils.toStringTrueFalse(BooleanUtils
148: .toBoolean(readOnly));
149: }
150:
151: public Collection getCastorValues() {
152: return value;
153: }
154:
155: public void setCastorValues(Collection _value) {
156: if (value == null) {
157: value = new ArrayList();
158: } else {
159: value.clear();
160: }
161: value.addAll(_value);
162: }
163:
164: protected List getClonedCastorValuesAsList() {
165: List returnValue = new ArrayList(value.size());
166:
167: Iterator iter = value.iterator();
168: while (iter.hasNext()) {
169: String value = (String) iter.next();
170: returnValue.add(value);
171: }
172: return returnValue;
173: }
174:
175: public String toString() {
176: return toString(0);
177: }
178:
179: public String toString(int indent) {
180: StringBuffer buffer = new StringBuffer(50);
181: StringUtils.newLine(buffer, indent);
182: buffer.append(getClass().toString());
183: buffer.append(": name='");
184: buffer.append(name);
185: buffer.append("', value='");
186:
187: if (value == null) {
188: buffer.append("null");
189: } else {
190: StringUtils.newLine(buffer, indent);
191: buffer.append("{");
192: Iterator iterator = value.iterator();
193: if (iterator.hasNext()) {
194: StringUtils.newLine(buffer, indent);
195: buffer.append((String) iterator.next());
196: }
197: while (iterator.hasNext()) {
198: StringUtils.indent(buffer, indent + 2);
199: buffer.append((String) iterator.next());
200: }
201: StringUtils.newLine(buffer, indent);
202: buffer.append("}");
203: }
204:
205: buffer.append("', isReadOnly='");
206: buffer.append(isReadOnly());
207: buffer.append("'");
208: return buffer.toString();
209: }
210:
211: }
|