|
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.commons.logging.Log; |
|
23 |
| import org.apache.commons.logging.LogFactory; |
|
24 |
| import org.apache.xindice.core.Collection; |
|
25 |
| import org.apache.xindice.core.data.Key; |
|
26 |
| import org.apache.xindice.core.data.NodeSet; |
|
27 |
| import org.apache.xindice.core.query.CompilationException; |
|
28 |
| import org.apache.xindice.core.query.NodeListSet; |
|
29 |
| import org.apache.xindice.core.query.ProcessingException; |
|
30 |
| import org.apache.xindice.core.query.Query; |
|
31 |
| import org.apache.xindice.core.query.QueryEngine; |
|
32 |
| import org.apache.xindice.core.query.QueryException; |
|
33 |
| import org.apache.xindice.core.query.QueryResolver; |
|
34 |
| import org.apache.xindice.util.Configuration; |
|
35 |
| import org.apache.xindice.util.SimpleConfigurable; |
|
36 |
| import org.apache.xindice.util.XindiceException; |
|
37 |
| import org.apache.xindice.xml.NamespaceMap; |
|
38 |
| import org.apache.xindice.xml.NodeSource; |
|
39 |
| import org.apache.xindice.xml.dom.DocumentImpl; |
|
40 |
| import org.apache.xindice.xml.dom.NodeImpl; |
|
41 |
| |
|
42 |
| import org.w3c.dom.Document; |
|
43 |
| import org.w3c.dom.Element; |
|
44 |
| import org.w3c.dom.Text; |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| public class XUpdateQueryResolver extends SimpleConfigurable implements QueryResolver { |
|
52 |
| |
|
53 |
| private static final Log log = LogFactory.getLog(XUpdateQueryResolver.class); |
|
54 |
| |
|
55 |
| private static final String XUPDATE_XPATH_PROP = "org.xmldb.common.xml.queries.XPathQueryFactory"; |
|
56 |
| private static final String XUPDATE_XPATH_IMPL = "org.apache.xindice.core.xupdate.XPathQueryFactoryImpl"; |
|
57 |
| |
|
58 |
| static { |
|
59 |
19
| System.setProperty(XUPDATE_XPATH_PROP, XUPDATE_XPATH_IMPL);
|
|
60 |
| } |
|
61 |
| |
|
62 |
| public static final String STYLE_XUPDATE = "XUpdate"; |
|
63 |
| |
|
64 |
| |
|
65 |
123
| public void setConfig(Configuration config) throws XindiceException {
|
|
66 |
123
| super.setConfig(config);
|
|
67 |
| } |
|
68 |
| |
|
69 |
123
| public String getQueryStyle() {
|
|
70 |
123
| return STYLE_XUPDATE;
|
|
71 |
| } |
|
72 |
| |
|
73 |
123
| public void setQueryEngine(QueryEngine engine) {
|
|
74 |
| |
|
75 |
| } |
|
76 |
| |
|
77 |
0
| public Query compileQuery(Collection context, String query, NamespaceMap nsMap, Key[] keys) throws QueryException {
|
|
78 |
0
| return new XUpdateQuery(context, query, nsMap, keys);
|
|
79 |
| } |
|
80 |
| |
|
81 |
12
| public NodeSet query(Collection context, String query, NamespaceMap nsMap, Key[] keys) throws QueryException {
|
|
82 |
12
| XUpdateQuery xq = new XUpdateQuery(context, query, nsMap, keys);
|
|
83 |
12
| return xq.execute();
|
|
84 |
| } |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| private class XUpdateQuery implements Query { |
|
91 |
| public Collection context; |
|
92 |
| public String query; |
|
93 |
| public NamespaceMap nsMap; |
|
94 |
| public XUpdateImpl xu; |
|
95 |
| public Key[] keys; |
|
96 |
| |
|
97 |
12
| public XUpdateQuery(Collection context, String query, NamespaceMap nsMap, Key[] keys) throws QueryException {
|
|
98 |
12
| this.context = context;
|
|
99 |
12
| this.query = query;
|
|
100 |
12
| this.nsMap = nsMap;
|
|
101 |
12
| this.keys = keys;
|
|
102 |
| |
|
103 |
12
| try {
|
|
104 |
12
| xu = new XUpdateImpl();
|
|
105 |
12
| xu.setQString(query);
|
|
106 |
12
| xu.setNamespaceMap(nsMap);
|
|
107 |
| } catch (Exception e) { |
|
108 |
0
| if (e instanceof QueryException) {
|
|
109 |
0
| throw (QueryException) e.fillInStackTrace();
|
|
110 |
| } |
|
111 |
0
| throw new CompilationException("Error Compiling XUpdate Query", e);
|
|
112 |
| } |
|
113 |
| } |
|
114 |
| |
|
115 |
0
| public String getQueryStyle() {
|
|
116 |
0
| return STYLE_XUPDATE;
|
|
117 |
| } |
|
118 |
| |
|
119 |
0
| public Collection getQueryContext() {
|
|
120 |
0
| return context;
|
|
121 |
| } |
|
122 |
| |
|
123 |
0
| public String getQueryString() {
|
|
124 |
0
| return query;
|
|
125 |
| } |
|
126 |
| |
|
127 |
0
| public NamespaceMap getNamespaceMap() {
|
|
128 |
0
| return nsMap;
|
|
129 |
| } |
|
130 |
| |
|
131 |
0
| public Key[] getKeySet() {
|
|
132 |
0
| return keys;
|
|
133 |
| } |
|
134 |
| |
|
135 |
12
| public NodeSet execute() throws QueryException {
|
|
136 |
12
| try {
|
|
137 |
12
| if (keys != null) {
|
|
138 |
6
| try {
|
|
139 |
6
| for (int i = 0; i < keys.length; i++) {
|
|
140 |
6
| Document doc = context.getDocument(keys[i]);
|
|
141 |
6
| xu.execute(doc.getDocumentElement());
|
|
142 |
6
| context.setDocument(keys[i], doc);
|
|
143 |
| } |
|
144 |
| } catch (Exception e) { |
|
145 |
0
| if (log.isWarnEnabled()) {
|
|
146 |
0
| log.warn("ignored exception", e);
|
|
147 |
| } |
|
148 |
| } |
|
149 |
| } else { |
|
150 |
6
| xu.execute(context);
|
|
151 |
| } |
|
152 |
| |
|
153 |
12
| DocumentImpl doc = new DocumentImpl();
|
|
154 |
12
| Element elem = doc.createElementNS(NodeSource.SOURCE_NS, "src:" + NodeSource.SOURCE_MODIFIED);
|
|
155 |
12
| elem.setAttribute(NodeImpl.XMLNS_PREFIX + ":src", NodeSource.SOURCE_NS);
|
|
156 |
12
| doc.appendChild(elem);
|
|
157 |
12
| Text count = doc.createTextNode(Integer.toString(xu.getModifiedCount()));
|
|
158 |
12
| elem.appendChild(count);
|
|
159 |
| |
|
160 |
12
| return new NodeListSet(doc.getChildNodes());
|
|
161 |
| } catch (Exception e) { |
|
162 |
0
| if (e instanceof QueryException) {
|
|
163 |
0
| throw (QueryException) e.fillInStackTrace();
|
|
164 |
| } |
|
165 |
0
| throw new ProcessingException("Error executing XUpdate query", e);
|
|
166 |
| } |
|
167 |
| } |
|
168 |
| } |
|
169 |
| } |