001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010:
011: package org.mmbase.bridge.util;
012:
013: import org.mmbase.bridge.*;
014: import org.mmbase.util.Casting;
015: import org.w3c.dom.Document;
016: import org.w3c.dom.Element;
017: import java.util.*;
018:
019: /**
020: * This implementation of the Field Value interface is used by getFunctionValue of Node. This
021: * represents the result of a `function' on a node and it (therefore) is an unmodifiable.
022: *
023: * @author Michiel Meeuwissen
024: * @version $Id: AbstractFieldValue.java,v 1.4 2007/02/16 20:06:13 michiel Exp $
025: * @since MMBase-1.8
026: */
027: public abstract class AbstractFieldValue implements FieldValue {
028:
029: static protected BridgeException CANNOTCHANGE = new BridgeException(
030: "Cannot change function value");
031:
032: protected final Node node;
033: protected final Cloud cloud;
034:
035: protected AbstractFieldValue(Node n, Cloud c) {
036: node = n;
037: cloud = c != null ? c : (n != null ? n.getCloud() : null);
038: }
039:
040: /**
041: * Function values cannot be changed
042: * @return false
043: */
044: public boolean canModify() {
045: return false;
046: }
047:
048: public boolean isNull() {
049: return get() == null;
050: }
051:
052: public abstract Object get();
053:
054: public Field getField() {
055: return null;
056: }
057:
058: public Node getNode() {
059: return node;
060: }
061:
062: public boolean toBoolean() {
063: return Casting.toBoolean(get());
064: }
065:
066: public byte[] toByte() {
067: return Casting.toByte(get());
068: }
069:
070: public float toFloat() {
071: return Casting.toFloat(get());
072: }
073:
074: public double toDouble() {
075: return Casting.toDouble(get());
076: }
077:
078: public long toLong() {
079: return Casting.toLong(get());
080: }
081:
082: public int toInt() {
083: return Casting.toInt(get());
084: }
085:
086: public Node toNode() {
087: return Casting.toNode(get(), cloud);
088: }
089:
090: @Override
091: public String toString() {
092: return Casting.toString(get());
093: }
094:
095: public Document toXML() throws IllegalArgumentException {
096: return Casting.toXML(get());
097: }
098:
099: public final Element toXML(Document tree)
100: throws IllegalArgumentException {
101: Document doc = toXML();
102: if (doc == null)
103: return null;
104: return (Element) tree
105: .importNode(doc.getDocumentElement(), true);
106: }
107:
108: /**
109: * @since MMBase-1.8
110: */
111: public Date toDate() {
112: return Casting.toDate(get());
113: }
114:
115: /**
116: * Function values cannot be changed, and all set-functions throw an exception.
117: * @param value set value
118: * @throws BridgeException
119: */
120: public void set(Object value) {
121: throw CANNOTCHANGE;
122: }
123:
124: public void setObject(Object value) {
125: throw CANNOTCHANGE;
126: }
127:
128: /**
129: * Function values cannot be changed, and all set-functions throw an exception.
130: * @param value set value
131: * @throws BridgeException
132: */
133: public void setBoolean(boolean value) {
134: throw CANNOTCHANGE;
135: }
136:
137: /**
138: * Function values cannot be changed, and all set-functions throw an exception.
139: * @param value set value
140: * @throws BridgeException
141: */
142: public void setFLoat(float value) {
143: throw CANNOTCHANGE;
144: }
145:
146: /**
147: * Function values cannot be changed, and all set-functions throw an exception.
148: * @param value set value
149: * @throws BridgeException
150: */
151: public void setDouble(double value) {
152: throw CANNOTCHANGE;
153: }
154:
155: /**
156: * Function values cannot be changed, and all set-functions throw an exception.
157: * @param value set value
158: * @throws BridgeException
159: */
160: public void setLong(long value) {
161: throw CANNOTCHANGE;
162: }
163:
164: /**
165: * Function values cannot be changed, and all set-functions throw an exception.
166: * @param value set value
167: * @throws BridgeException
168: */
169: public void setInt(int value) {
170: throw CANNOTCHANGE;
171: }
172:
173: /**
174: * Function values cannot be changed, and all set-functions throw an exception.
175: * @param value set value
176: * @throws BridgeException
177: */
178: public void setByte(byte[] value) {
179: throw CANNOTCHANGE;
180: }
181:
182: /**
183: * Function values cannot be changed, and all set-functions throw an exception.
184: * @param value set value
185: * @throws BridgeException
186: */
187: public void setString(String value) {
188: throw CANNOTCHANGE;
189: }
190:
191: /**
192: * Function values cannot be changed, and all set-functions throw an exception.
193: * @param value set value
194: * @throws BridgeException
195: */
196: public void setNode(Node value) {
197: throw CANNOTCHANGE;
198: }
199:
200: /**
201: * Function values cannot be changed, and all set-functions throw an exception.
202: * @param value set value
203: * @throws BridgeException
204: */
205: public void setXML(Document value) {
206: throw CANNOTCHANGE;
207: }
208:
209: /**
210: * Function values cannot be changed, and all set-functions throw an exception.
211: * @throws BridgeException
212: * @since MMBase-1.8
213: */
214: public void setDate(Date value) {
215: throw CANNOTCHANGE;
216: }
217:
218: }
|