comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/Builder.java @ 372:fc3cf0ef777e

Added meta data service. flys-artifacts/trunk@1781 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 29 Apr 2011 15:10:44 +0000
parents
children df02137b3b28
comparison
equal deleted inserted replaced
371:dfbb3d50b0bd 372:fc3cf0ef777e
1 package de.intevation.flys.artifacts.services.meta;
2
3 import java.util.regex.Matcher;
4
5 import java.util.ArrayList;
6 import java.util.List;
7 import java.util.HashMap;
8 import java.util.Map;
9
10 import org.w3c.dom.Document;
11 import org.w3c.dom.NodeList;
12 import org.w3c.dom.Node;
13 import org.w3c.dom.Element;
14
15 import java.sql.SQLException;
16 import java.sql.Connection;
17
18 import de.intevation.artifacts.common.utils.XMLUtils;
19
20 import org.apache.log4j.Logger;
21
22 public class Builder
23 {
24 private static Logger log = Logger.getLogger(Builder.class);
25
26 public static final String DC_NAMESPACE_URI =
27 "http://www.intevation.org/2011/Datacage";
28
29 protected Document template;
30
31 public class BuildHelper
32 {
33 protected Document output;
34 protected StackFrames frames;
35 protected Connection connection;
36 protected Map<String, CompiledStatement> statements;
37
38 public BuildHelper() {
39 frames = new StackFrames();
40 statements = new HashMap<String, CompiledStatement>();
41 }
42
43 public BuildHelper(Document output, Connection connection) {
44 this();
45 this.output = output;
46 this.connection = connection;
47 }
48
49 public void build(List<Node> elements) throws SQLException {
50 try {
51 for (Node current: elements) {
52 build(output, current);
53 }
54 }
55 finally {
56 for (CompiledStatement cs: statements.values()) {
57 cs.close();
58 }
59 statements.clear();
60 }
61 }
62
63 protected void context(Node parent, Element current)
64 throws SQLException
65 {
66 NodeList elements = current.getElementsByTagNameNS(
67 DC_NAMESPACE_URI, "elements");
68
69 if (elements.getLength() < 1) {
70 log.warn("no elements found -> ignore");
71 return;
72 }
73
74 NodeList subs = elements.item(0).getChildNodes();
75 int S = subs.getLength();
76
77 if (S < 1) {
78 log.warn("elements is empty -> ignore");
79 return;
80 }
81
82 NodeList stmntNode = current.getElementsByTagNameNS(
83 DC_NAMESPACE_URI, "statement");
84
85 if (stmntNode.getLength() < 1) {
86 log.warn("dc:context: too less statements");
87 return;
88 }
89
90 String stmntText = stmntNode.item(0).getTextContent();
91
92 if (stmntText == null) {
93 log.warn("dc:context: no sql statement found");
94 }
95
96 CompiledStatement cs = statements.get(stmntText);
97
98 if (cs == null) {
99 cs = new CompiledStatement(stmntText);
100 statements.put(stmntText, cs);
101 }
102
103 ResultData rd = cs.execute(connection, frames);
104
105 String [] columns = rd.getColumnLabels();
106
107
108 for (Object [] row: rd.getRows()) {
109 frames.enter();
110 try {
111 frames.put(columns, row);
112 for (int i = 0; i < S; ++i) {
113 build(parent, subs.item(i));
114 }
115 }
116 finally {
117 frames.leave();
118 }
119 }
120 }
121
122 protected void element(Node parent, Element current)
123 throws SQLException
124 {
125 String attr = expand(current.getAttribute("name"));
126
127 if (log.isDebugEnabled()) {
128 log.debug("dc:element -> '" + attr + "'");
129 }
130
131 if (attr.length() == 0) {
132 log.warn("no name attribute found");
133 return;
134 }
135
136 Element element = output.createElement(attr);
137
138 NodeList children = current.getChildNodes();
139 for (int i = 0, N = children.getLength(); i < N; ++i) {
140 build(element, children.item(i));
141 }
142
143 parent.appendChild(element);
144 }
145
146 protected void text(Node parent, Element current)
147 throws SQLException
148 {
149 log.debug("dc:text");
150 String value = expand(current.getTextContent());
151 parent.appendChild(output.createTextNode(value));
152 }
153
154
155 protected void attribute(Node parent, Element current) {
156
157 if (parent.getNodeType() != Node.ELEMENT_NODE) {
158 log.warn("need element here");
159 return;
160 }
161
162 String name = expand(current.getAttribute("name"));
163 String value = expand(current.getAttribute("value"));
164
165 Element element = (Element)parent;
166
167 element.setAttribute(name, value);
168 }
169
170 protected String expand(String s) {
171 Matcher m = CompiledStatement.VAR.matcher(s);
172
173 StringBuffer sb = new StringBuffer();
174 while (m.find()) {
175 String key = m.group(1);
176 Object value = frames.get(key);
177 m.appendReplacement(sb, value != null ? value.toString() : "");
178 }
179 m.appendTail(sb);
180 return sb.toString();
181 }
182
183 protected void build(Node parent, Node current)
184 throws SQLException
185 {
186 String ns = current.getNamespaceURI();
187 if (ns != null && ns.equals(DC_NAMESPACE_URI)) {
188 if (current.getNodeType() != Node.ELEMENT_NODE) {
189 log.warn("need elements here");
190 }
191 else {
192 String localName = current.getLocalName();
193 if ("context".equals(localName)) {
194 context(parent, (Element)current);
195 }
196 else if ("attribute".equals(localName)) {
197 attribute(parent, (Element)current);
198 }
199 else if ("element".equals(localName)) {
200 element(parent, (Element)current);
201 }
202 else if ("text".equals(localName)) {
203 text(parent, (Element)current);
204 }
205 else {
206 log.warn("unknown '" + localName + "' -> ignore");
207 }
208 }
209 return;
210 }
211
212 Node copy = output.importNode(current, false);
213
214 NodeList children = current.getChildNodes();
215 for (int i = 0, N = children.getLength(); i < N; ++i) {
216 build(copy, children.item(i));
217 }
218 parent.appendChild(copy);
219 }
220 } // class BuildHelper
221
222
223 public Builder() {
224 }
225
226 public Builder(Document template) {
227 this.template = template;
228 }
229
230 public Document build(Connection connection)
231 throws SQLException
232 {
233 return build(connection, XMLUtils.newDocument());
234 }
235
236 public Document build(Connection connection, Document output)
237 throws SQLException
238 {
239 NodeList roots = template.getElementsByTagNameNS(
240 DC_NAMESPACE_URI, "template");
241
242 BuildHelper helper = new BuildHelper(output, connection);
243
244 List<Node> elements = new ArrayList<Node>();
245
246 for (int i = 0, N = roots.getLength(); i < N; ++i) {
247 NodeList rootChildren = roots.item(i).getChildNodes();
248 for (int j = 0, M = rootChildren.getLength(); j < M; ++j) {
249 Node child = rootChildren.item(j);
250 if (child.getNodeType() == Node.ELEMENT_NODE) {
251 elements.add(child);
252 }
253 }
254 }
255 helper.build(elements);
256
257 return output;
258 }
259 }
260 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org