|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| package org.apache.xindice.core.xupdate; |
|
21 |
| |
|
22 |
| import org.apache.xindice.core.Collection; |
|
23 |
| import org.apache.xindice.core.data.NodeSet; |
|
24 |
| import org.apache.xindice.xml.NamespaceMap; |
|
25 |
| import org.apache.xindice.xml.NodeSource; |
|
26 |
| import org.apache.xindice.xml.dom.CompressedNode; |
|
27 |
| import org.apache.xindice.xml.dom.DBNode; |
|
28 |
| |
|
29 |
| import org.w3c.dom.Document; |
|
30 |
| import org.w3c.dom.Node; |
|
31 |
| import org.xml.sax.SAXException; |
|
32 |
| import org.xmldb.xupdate.lexus.XUpdateQueryImpl; |
|
33 |
| import org.xmldb.xupdate.lexus.commands.CommandConstants; |
|
34 |
| import org.xmldb.xupdate.lexus.commands.CommandObject; |
|
35 |
| import org.xmldb.xupdate.lexus.commands.DefaultCommand; |
|
36 |
| |
|
37 |
| import java.util.Enumeration; |
|
38 |
| import java.util.HashMap; |
|
39 |
| import java.util.Hashtable; |
|
40 |
| import java.util.Iterator; |
|
41 |
| import java.util.Map; |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| public class XUpdateImpl extends XUpdateQueryImpl { |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| private static final boolean API_NS_PRECEDENCE = true; |
|
58 |
| |
|
59 |
| protected int nodesModified; |
|
60 |
| protected NamespaceMap nsMap; |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
12
| public void setNamespaceMap(NamespaceMap nsMap) {
|
|
66 |
12
| if (nsMap == null) {
|
|
67 |
12
| return;
|
|
68 |
| } |
|
69 |
| |
|
70 |
0
| if (this.nsMap == null) {
|
|
71 |
0
| this.nsMap = nsMap;
|
|
72 |
| } else { |
|
73 |
0
| this.nsMap.includeNamespaces(nsMap, API_NS_PRECEDENCE);
|
|
74 |
| } |
|
75 |
| } |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
12
| public void setQString(String query) throws SAXException {
|
|
82 |
12
| super.setQString(query);
|
|
83 |
12
| if (nsMap == null) {
|
|
84 |
12
| nsMap = new NamespaceMap();
|
|
85 |
| } |
|
86 |
12
| nsMap.includeNamespaces(super.namespaces, !API_NS_PRECEDENCE);
|
|
87 |
| } |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
18
| public void execute(Node contextNode) throws Exception {
|
|
94 |
18
| CommandObject currentCommand = new DefaultCommand(contextNode);
|
|
95 |
18
| Enumeration commands = super.query[0].elements();
|
|
96 |
18
| Enumeration attributes = super.query[1].elements();
|
|
97 |
18
| Enumeration characters = super.query[2].elements();
|
|
98 |
18
| Node origNode = contextNode;
|
|
99 |
18
| CommandObject.getXPath().setNamespace(nsMap.getContextNode());
|
|
100 |
| |
|
101 |
18
| while (commands.hasMoreElements()) {
|
|
102 |
219
| int id = ((Integer) commands.nextElement()).intValue();
|
|
103 |
| |
|
104 |
219
| if (id == CommandConstants.ATTRIBUTES) {
|
|
105 |
57
| currentCommand.submitAttributes((Hashtable) attributes.nextElement());
|
|
106 |
162
| } else if (id == CommandConstants.CHARACTERS) {
|
|
107 |
48
| currentCommand.submitCharacters((String) characters.nextElement());
|
|
108 |
114
| } else if (id > 0) {
|
|
109 |
57
| if (!currentCommand.submitInstruction(id)) {
|
|
110 |
42
| super.commandConstants.setContextNode(contextNode);
|
|
111 |
42
| currentCommand = super.commandConstants.commandForID(id);
|
|
112 |
42
| if (currentCommand == null) {
|
|
113 |
0
| throw new Exception("Operation can not have any XUpdate-instruction!");
|
|
114 |
| } |
|
115 |
42
| currentCommand.reset();
|
|
116 |
| } |
|
117 |
| } else { |
|
118 |
57
| if (!currentCommand.executeInstruction()) {
|
|
119 |
42
| try {
|
|
120 |
42
| contextNode = currentCommand.execute();
|
|
121 |
| } catch (Exception e) { |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
0
| if (!"no nodes selected !".equals(e.getMessage())) {
|
|
128 |
0
| throw e;
|
|
129 |
| } |
|
130 |
| } |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
42
| currentCommand = new DefaultCommand(contextNode);
|
|
136 |
| } |
|
137 |
| } |
|
138 |
| } |
|
139 |
| |
|
140 |
18
| if (origNode instanceof CompressedNode) {
|
|
141 |
18
| CompressedNode cn = (CompressedNode) origNode;
|
|
142 |
18
| if (cn.isDirty()) {
|
|
143 |
18
| nodesModified++;
|
|
144 |
| } |
|
145 |
| } |
|
146 |
| } |
|
147 |
| |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
| |
|
154 |
6
| public void execute(Collection col) throws Exception {
|
|
155 |
6
| int attribIndex = 0;
|
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
| |
|
160 |
| |
|
161 |
| |
|
162 |
6
| HashMap docsUpdated = new HashMap();
|
|
163 |
6
| for (int i = 0; i < super.query[0].size(); i++) {
|
|
164 |
78
| int cmdID = ((Integer) super.query[0].elementAt(i)).intValue();
|
|
165 |
| |
|
166 |
78
| if (cmdID == CommandConstants.ATTRIBUTES) {
|
|
167 |
21
| Hashtable attribs = (Hashtable) super.query[1].elementAt(attribIndex);
|
|
168 |
21
| String selector = (String) attribs.get("select");
|
|
169 |
21
| attribIndex++;
|
|
170 |
| |
|
171 |
| |
|
172 |
| |
|
173 |
| |
|
174 |
21
| if (selector != null && !selector.startsWith("$")) {
|
|
175 |
15
| NodeSet ns = col.queryCollection("XPath", selector, nsMap);
|
|
176 |
15
| while (ns != null && ns.hasMoreNodes()) {
|
|
177 |
30
| DBNode node = (DBNode) ns.getNextNode();
|
|
178 |
30
| Document doc = node.getOwnerDocument();
|
|
179 |
30
| NodeSource source = node.getSource();
|
|
180 |
| |
|
181 |
30
| if (docsUpdated.containsKey(source.getKey())) {
|
|
182 |
18
| continue;
|
|
183 |
| } else { |
|
184 |
12
| docsUpdated.put(source.getKey(), doc);
|
|
185 |
| } |
|
186 |
| |
|
187 |
12
| execute(doc.getDocumentElement());
|
|
188 |
| } |
|
189 |
| } |
|
190 |
| } |
|
191 |
| } |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
6
| Iterator i = docsUpdated.entrySet().iterator();
|
|
196 |
6
| while (i.hasNext()) {
|
|
197 |
12
| Map.Entry set = (Map.Entry) i.next();
|
|
198 |
12
| col.setDocument(set.getKey(), (Document) set.getValue());
|
|
199 |
| } |
|
200 |
| } |
|
201 |
| |
|
202 |
12
| public int getModifiedCount() {
|
|
203 |
12
| return nodesModified;
|
|
204 |
| } |
|
205 |
| } |