comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java @ 1023:5cb84ceee2a0

Datacage: Templates now can nest <dc:elements> into other elements in the body of <dc:context> flys-artifacts/trunk@2481 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 10 Aug 2011 10:08:01 +0000
parents 8637756275e5
children 3f3988bb6284
comparison
equal deleted inserted replaced
1022:930a9ce24294 1023:5cb84ceee2a0
1 package de.intevation.flys.artifacts.datacage.templating; 1 package de.intevation.flys.artifacts.datacage.templating;
2 2
3 import java.util.regex.Pattern; 3 import java.util.regex.Pattern;
4 import java.util.regex.Matcher; 4 import java.util.regex.Matcher;
5 5
6 import java.util.Arrays;
6 import java.util.ArrayList; 7 import java.util.ArrayList;
7 import java.util.List; 8 import java.util.List;
8 import java.util.HashMap; 9 import java.util.HashMap;
9 import java.util.Map; 10 import java.util.Map;
10 import java.util.Deque; 11 import java.util.Deque;
85 protected Document owner; 86 protected Document owner;
86 protected StackFrames frames; 87 protected StackFrames frames;
87 protected List<NamedConnection> connections; 88 protected List<NamedConnection> connections;
88 protected Map<String, CompiledStatement.Instance> statements; 89 protected Map<String, CompiledStatement.Instance> statements;
89 protected Deque<NamedConnection> connectionsStack; 90 protected Deque<NamedConnection> connectionsStack;
91 protected Deque<ResultData> resultsStack;
90 92
91 public BuildHelper( 93 public BuildHelper(
92 Node output, 94 Node output,
93 List<NamedConnection> connections, 95 List<NamedConnection> connections,
94 Map<String, Object> parameters 96 Map<String, Object> parameters
97 throw new IllegalArgumentException("no connections given."); 99 throw new IllegalArgumentException("no connections given.");
98 } 100 }
99 101
100 this.connections = connections; 102 this.connections = connections;
101 connectionsStack = new ArrayDeque<NamedConnection>(); 103 connectionsStack = new ArrayDeque<NamedConnection>();
104 resultsStack = new ArrayDeque<ResultData>();
102 this.output = output; 105 this.output = output;
103 frames = new StackFrames(parameters); 106 frames = new StackFrames(parameters);
104 owner = getOwnerDocument(output); 107 owner = getOwnerDocument(output);
105 statements = 108 statements =
106 new HashMap<String, CompiledStatement.Instance>(); 109 new HashMap<String, CompiledStatement.Instance>();
127 } 130 }
128 131
129 protected void context(Node parent, Element current) 132 protected void context(Node parent, Element current)
130 throws SQLException 133 throws SQLException
131 { 134 {
132 NodeList elements = current.getElementsByTagNameNS( 135 log.debug("dc:context");
133 DC_NAMESPACE_URI, "elements"); 136
134 137 NodeList subs = current.getChildNodes();
135 if (elements.getLength() < 1) {
136 log.warn("no elements found -> ignore");
137 return;
138 }
139
140 NodeList subs = elements.item(0).getChildNodes();
141 int S = subs.getLength(); 138 int S = subs.getLength();
142 139
143 if (S < 1) { 140 // check only direct children
144 log.warn("elements is empty -> ignore"); 141 Node stmntNode = null;
145 return; 142 for (int i = 0; i < S; ++i) {
146 } 143 Node node = subs.item(i);
147 144 String ns;
148 NodeList stmntNode = current.getElementsByTagNameNS( 145 if (node.getNodeType() == Node.ELEMENT_NODE
149 DC_NAMESPACE_URI, "statement"); 146 && node.getLocalName().equals("statement")
150 147 && (ns = node.getNamespaceURI()) != null
151 if (stmntNode.getLength() < 1) { 148 && ns.equals(DC_NAMESPACE_URI)) {
152 log.warn("dc:context: too less statements"); 149 stmntNode = node;
150 break;
151 }
152 }
153
154 if (stmntNode == null) {
155 log.warn("dc:context: cannot find statement");
153 return; 156 return;
154 } 157 }
155 158
156 String con = current.getAttribute("connection"); 159 String con = current.getAttribute("connection");
157 160
158 String stmntText = stmntNode.item(0).getTextContent(); 161 String stmntText = stmntNode.getTextContent();
159 162
160 String key = con + "-" + stmntText; 163 String key = con + "-" + stmntText;
161 164
162 CompiledStatement.Instance csi = statements.get(key); 165 CompiledStatement.Instance csi = statements.get(key);
163 166
178 break; 181 break;
179 } 182 }
180 } 183 }
181 } 184 }
182 185
183 connectionsStack.push(connection); 186 ResultData rd = csi.execute(
184 try { 187 connection.connection,
185 ResultData rd = csi.execute( 188 frames,
186 connection.connection, 189 connection.cached);
187 frames, 190
188 connection.cached); 191 // only descent if there are results
189 192 if (!rd.isEmpty()) {
190 String [] columns = rd.getColumnLabels(); 193 resultsStack.push(rd);
191 194 connectionsStack.push(connection);
192 for (Object [] row: rd.getRows()) { 195 try {
193 frames.enter(); 196 for (int i = 0; i < S; ++i) {
194 try { 197 build(parent, subs.item(i));
195 frames.put(columns, row); 198 }
196 for (int i = 0; i < S; ++i) { 199 }
197 build(parent, subs.item(i)); 200 finally {
198 } 201 connectionsStack.pop();
199 } 202 resultsStack.pop();
200 finally { 203 }
201 frames.leave(); 204 }
202 } 205 }
203 } 206
204 } 207 protected void elements(Node parent, Element current)
205 finally { 208 throws SQLException
206 connectionsStack.pop(); 209 {
210 log.debug("dc:elements");
211
212 if (resultsStack.isEmpty()) {
213 log.warn("dc:elements without having results");
214 return;
215 }
216
217 NodeList subs = current.getChildNodes();
218 int S = subs.getLength();
219
220 if (S == 0) {
221 log.debug("dc:elements has no children");
222 return;
223 }
224
225 ResultData rd = resultsStack.peek();
226
227 String [] columns = rd.getColumnLabels();
228
229 //if (log.isDebugEnabled()) {
230 // log.debug("pushing vars: "
231 // + java.util.Arrays.toString(columns));
232 //}
233
234 for (Object [] row: rd.getRows()) {
235 frames.enter();
236 try {
237 frames.put(columns, row);
238 //if (log.isDebugEnabled()) {
239 // log.debug("current vars: " + frames.dump());
240 //}
241 for (int i = 0; i < S; ++i) {
242 build(parent, subs.item(i));
243 }
244 }
245 finally {
246 frames.leave();
247 }
207 } 248 }
208 } 249 }
209 250
210 protected void element(Node parent, Element current) 251 protected void element(Node parent, Element current)
211 throws SQLException 252 throws SQLException
426 // simply ignore the definition. 467 // simply ignore the definition.
427 } 468 }
428 else if ("element".equals(localName)) { 469 else if ("element".equals(localName)) {
429 element(parent, (Element)current); 470 element(parent, (Element)current);
430 } 471 }
472 else if ("elements".equals(localName)) {
473 elements(parent, (Element)current);
474 }
431 else if ("text".equals(localName)) { 475 else if ("text".equals(localName)) {
432 text(parent, (Element)current); 476 text(parent, (Element)current);
433 } 477 }
434 else if ("comment".equals(localName)) { 478 else if ("comment".equals(localName)
435 // ignore comments in output 479 || "statement".equals(localName)) {
480 // ignore comments and statements in output
436 } 481 }
437 else if ("convert".equals(localName)) { 482 else if ("convert".equals(localName)) {
438 convert(parent, (Element)current); 483 convert(parent, (Element)current);
439 } 484 }
440 else { 485 else {

http://dive4elements.wald.intevation.org