1
2
3
4
5
6
7 package genie.commons.chain;
8
9 import genie.commons.jxpath.JXPathContextUtils;
10
11 import java.util.Collection;
12 import java.util.Iterator;
13 import java.util.LinkedHashSet;
14 import java.util.Map;
15 import java.util.Set;
16 import java.util.Vector;
17
18 import org.apache.commons.chain.impl.ContextBase;
19 import org.apache.commons.jxpath.JXPathContext;
20 import org.apache.commons.jxpath.Pointer;
21
22 /***
23 *
24 *
25 * @author T. Kia Ntoni
26 *
27 * 9 janv. 2005
28 * NestingContext @version
29 */
30 public class NestingContext extends ContextBase {
31 protected JXPathContext parentContext = JXPathContext.newContext(null);
32 protected LinkedHashSet insertOrder = new LinkedHashSet();
33 /***
34 *
35 */
36 public NestingContext() {
37 super();
38
39 }
40
41 /***
42 * @param arg0
43 */
44 public NestingContext(Map map) {
45 super(map);
46
47 }
48
49
50
51 public boolean traversalContainsKey(String xpath) {
52 return JXPathContextUtils.getPointer(parentContext, this, xpath) == null ? false : true;
53 }
54
55
56
57
58 public boolean traversalContainsValue(Object value) {
59 for (Iterator<Pointer> iter = JXPathContextUtils.iteratePointers(parentContext, this, "//*"); iter.hasNext();) {
60 Pointer pointer = iter.next();
61 Object object = JXPathContextUtils.getValue(parentContext, this, pointer.asPath());
62 if (object == null && value == null) {
63 return true;
64 }
65 if (object != null && value != null && object.equals(value)) {
66 return true;
67 }
68 }
69 return false;
70 }
71
72
73
74 public Object traversalGet(String xpath) {
75 return JXPathContextUtils.getValue(parentContext, this, xpath);
76 }
77
78
79
80
81 public void traversalPut(String xpath, Object value) {
82 Pointer pointer = JXPathContextUtils.getPointer(parentContext, this, xpath);
83 if (pointer != null) {
84 JXPathContextUtils.setValue(parentContext, this, pointer.asPath(), value);
85 return;
86 }
87 JXPathContextUtils.createPathAndSetValue(parentContext, this, xpath, value);
88 }
89
90
91
92
93 public void traversalPutAll(Map map) {
94 for (Iterator iter = map.keySet().iterator(); iter.hasNext();) {
95 Object key = (Object) iter.next();
96 traversalPut(key != null ? key.toString() : null, map.get(key));
97 }
98 }
99
100
101
102
103 public void traversalRemove(String xpath) {
104 JXPathContextUtils.removePath(parentContext,this, xpath);
105 }
106
107 public void traversalRemoveAll(String xpath) {
108 JXPathContextUtils.removeAll(parentContext,this, xpath);
109 }
110
111 public Iterator iterateXpaths(String xpath) {
112 Set set = new LinkedHashSet();
113 for (Iterator<Pointer> iter = JXPathContextUtils.iteratePointers(parentContext, this, xpath); iter.hasNext();) {
114 set.add(iter.next().asPath()) ;
115 }
116 return set.iterator();
117 }
118
119 public Iterator iterateXpaths() {
120 return iterateXpaths("descendant::*");
121 }
122
123
124
125
126
127 public Collection traversalValues() {
128 return traversalValues("descendant::*");
129 }
130
131 public Collection traversalValues(String xpath) {
132 Vector collection = new Vector();
133 for (Iterator<String> iter = iterateXpaths(xpath != null ? xpath : "descendant::*"); iter.hasNext();) {
134 collection.add(traversalGet(iter.next()));
135 }
136 return collection;
137 }
138 }