KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > implementation > BasicFunctionValue


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10
11 package org.mmbase.bridge.implementation;
12 import org.mmbase.bridge.*;
13 import org.mmbase.util.Casting;
14 import org.mmbase.bridge.util.MapNode;
15 import org.mmbase.module.core.VirtualNode;
16 import org.mmbase.module.core.MMObjectNode;
17 import org.w3c.dom.Document JavaDoc;
18 import org.w3c.dom.Element JavaDoc;
19 import java.util.*;
20
21 /**
22  * This implementation of the Field Value interface is used by getFunctionValue of Node. This
23  * represents the result of a `function' on a node and it (therefore) is a unmodifiable.
24  *
25  * @author Michiel Meeuwissen
26  * @version $Id: BasicFunctionValue.java,v 1.19 2005/12/29 22:03:13 michiel Exp $
27  * @since MMBase-1.6
28  */

29 public class BasicFunctionValue extends org.mmbase.bridge.util.AbstractFieldValue {
30
31     private final Object JavaDoc value;
32
33     /**
34      * Constructor for a function value returned by a Node.
35      * @since MMBase-1.8
36      * @param node the node that called the function
37      * @param value the function value
38      */

39     BasicFunctionValue(Node node, Object JavaDoc value) {
40         super(node, null);
41         this.value = convert(value, null);
42     }
43
44     /**
45      * Constructor for a function value returned by a Module or NodeManager.
46      * @since MMBase-1.8
47      * @param cloud the cloud under which the call was run, used to instantiate NodeList values
48      * @param value the function value
49      */

50     BasicFunctionValue(Cloud cloud, Object JavaDoc value) {
51         super(null, cloud);
52         Object JavaDoc v = value;
53         if (v instanceof List) { // might be a collection of MMObjectNodes
54
List list = (List) v;
55             if (list.size() > 0) {
56                 Object JavaDoc first = list.get(0);
57                 if (first instanceof MMObjectNode || first instanceof Node) { // if List of MMObjectNodes, make NodeList
58
if (cloud == null) {
59                         if (first instanceof Node) {
60                             cloud = ((Node) first).getCloud();
61                         } else {
62                             cloud = ContextProvider.getDefaultCloudContext().getCloud("mmbase", "class", null);
63                         }
64                         // throw new IllegalStateException("Cloud is unknown, cannot convert MMObjectNode to Node");
65
}
66                     NodeList l = cloud.createNodeList();
67                     v = l;
68                     l.addAll(list);
69                 }
70             }
71         }
72         this.value = convert(v, cloud);
73     }
74
75     protected static Object JavaDoc convert(Object JavaDoc o, Cloud cloud) {
76         if (o instanceof VirtualNode) {
77             VirtualNode vn = (VirtualNode) o;
78             return new MapNode(vn.getValues(), cloud);
79         } else if (o instanceof MMObjectNode) {
80             MMObjectNode mn = (MMObjectNode) o;
81             return cloud.getNode(mn.getNumber());
82         }
83         return o;
84     }
85
86     public Object JavaDoc get() {
87         return value;
88     }
89
90 }
91
Popular Tags