|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| package org.dom4j.swing; |
|
9 |
| |
|
10 |
| import java.io.Serializable; |
|
11 |
| import java.util.ArrayList; |
|
12 |
| import java.util.HashMap; |
|
13 |
| import java.util.Iterator; |
|
14 |
| import java.util.List; |
|
15 |
| import java.util.Map; |
|
16 |
| |
|
17 |
| import org.dom4j.Document; |
|
18 |
| import org.dom4j.DocumentHelper; |
|
19 |
| import org.dom4j.Element; |
|
20 |
| import org.dom4j.XPath; |
|
21 |
| |
|
22 |
| import org.jaxen.VariableContext; |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| public class XMLTableDefinition implements Serializable, VariableContext { |
|
34 |
| |
|
35 |
| private XPath rowXPath; |
|
36 |
| |
|
37 |
| |
|
38 |
| private List columns = new ArrayList(); |
|
39 |
| |
|
40 |
| |
|
41 |
| private XMLTableColumnDefinition[] columnArray; |
|
42 |
| |
|
43 |
| |
|
44 |
| private Map columnNameIndex; |
|
45 |
| |
|
46 |
| |
|
47 |
| private VariableContext variableContext; |
|
48 |
| |
|
49 |
| |
|
50 |
| private Object rowValue; |
|
51 |
| |
|
52 |
2
| public XMLTableDefinition() {
|
|
53 |
| } |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
1
| public static XMLTableDefinition load(Document definition) {
|
|
64 |
1
| return load(definition.getRootElement());
|
|
65 |
| } |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
1
| public static XMLTableDefinition load(Element definition) {
|
|
76 |
1
| XMLTableDefinition answer = new XMLTableDefinition();
|
|
77 |
1
| answer.setRowExpression(definition.attributeValue("select"));
|
|
78 |
| |
|
79 |
1
| for (Iterator iter = definition.elementIterator("column"); iter
|
|
80 |
| .hasNext();) { |
|
81 |
3
| Element element = (Element) iter.next();
|
|
82 |
3
| String expression = element.attributeValue("select");
|
|
83 |
3
| String name = element.getText();
|
|
84 |
3
| String typeName = element.attributeValue("type", "string");
|
|
85 |
3
| String columnXPath = element.attributeValue("columnNameXPath");
|
|
86 |
3
| int type = XMLTableColumnDefinition.parseType(typeName);
|
|
87 |
| |
|
88 |
3
| if (columnXPath != null) {
|
|
89 |
0
| answer.addColumnWithXPathName(columnXPath, expression, type);
|
|
90 |
| } else { |
|
91 |
3
| answer.addColumn(name, expression, type);
|
|
92 |
| } |
|
93 |
| } |
|
94 |
| |
|
95 |
1
| return answer;
|
|
96 |
| } |
|
97 |
| |
|
98 |
0
| public Class getColumnClass(int columnIndex) {
|
|
99 |
0
| return getColumn(columnIndex).getColumnClass();
|
|
100 |
| } |
|
101 |
| |
|
102 |
2
| public int getColumnCount() {
|
|
103 |
2
| return columns.size();
|
|
104 |
| } |
|
105 |
| |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
6
| public String getColumnName(int columnIndex) {
|
|
116 |
6
| return getColumn(columnIndex).getName();
|
|
117 |
| } |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
0
| public XPath getColumnXPath(int columnIndex) {
|
|
129 |
0
| return getColumn(columnIndex).getXPath();
|
|
130 |
| } |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
| |
|
139 |
| |
|
140 |
| |
|
141 |
6
| public XPath getColumnNameXPath(int columnIndex) {
|
|
142 |
6
| return getColumn(columnIndex).getColumnNameXPath();
|
|
143 |
| } |
|
144 |
| |
|
145 |
12
| public synchronized Object getValueAt(Object row, int columnIndex) {
|
|
146 |
12
| XMLTableColumnDefinition column = getColumn(columnIndex);
|
|
147 |
12
| Object answer = null;
|
|
148 |
| |
|
149 |
12
| synchronized (this) {
|
|
150 |
12
| this.rowValue = row;
|
|
151 |
12
| answer = column.getValue(row);
|
|
152 |
12
| this.rowValue = null;
|
|
153 |
| } |
|
154 |
| |
|
155 |
12
| return answer;
|
|
156 |
| } |
|
157 |
| |
|
158 |
0
| public void addColumn(String name, String expression) {
|
|
159 |
0
| addColumn(name, expression, XMLTableColumnDefinition.OBJECT_TYPE);
|
|
160 |
| } |
|
161 |
| |
|
162 |
6
| public void addColumn(String name, String expression, int type) {
|
|
163 |
6
| XPath xpath = createColumnXPath(expression);
|
|
164 |
6
| addColumn(new XMLTableColumnDefinition(name, xpath, type));
|
|
165 |
| } |
|
166 |
| |
|
167 |
0
| public void addColumnWithXPathName(String columnNameXPathExpression,
|
|
168 |
| String expression, int type) { |
|
169 |
0
| XPath columnNameXPath = createColumnXPath(columnNameXPathExpression);
|
|
170 |
0
| XPath xpath = createColumnXPath(expression);
|
|
171 |
0
| addColumn(new XMLTableColumnDefinition(columnNameXPath, xpath, type));
|
|
172 |
| } |
|
173 |
| |
|
174 |
3
| public void addStringColumn(String name, String expression) {
|
|
175 |
3
| addColumn(name, expression, XMLTableColumnDefinition.STRING_TYPE);
|
|
176 |
| } |
|
177 |
| |
|
178 |
0
| public void addNumberColumn(String name, String expression) {
|
|
179 |
0
| addColumn(name, expression, XMLTableColumnDefinition.NUMBER_TYPE);
|
|
180 |
| } |
|
181 |
| |
|
182 |
6
| public void addColumn(XMLTableColumnDefinition column) {
|
|
183 |
6
| clearCaches();
|
|
184 |
6
| columns.add(column);
|
|
185 |
| } |
|
186 |
| |
|
187 |
0
| public void removeColumn(XMLTableColumnDefinition column) {
|
|
188 |
0
| clearCaches();
|
|
189 |
0
| columns.remove(column);
|
|
190 |
| } |
|
191 |
| |
|
192 |
0
| public void clear() {
|
|
193 |
0
| clearCaches();
|
|
194 |
0
| columns.clear();
|
|
195 |
| } |
|
196 |
| |
|
197 |
24
| public XMLTableColumnDefinition getColumn(int index) {
|
|
198 |
24
| if (columnArray == null) {
|
|
199 |
2
| columnArray = new XMLTableColumnDefinition[columns.size()];
|
|
200 |
2
| columns.toArray(columnArray);
|
|
201 |
| } |
|
202 |
| |
|
203 |
24
| return columnArray[index];
|
|
204 |
| } |
|
205 |
| |
|
206 |
4
| public XMLTableColumnDefinition getColumn(String columnName) {
|
|
207 |
4
| if (columnNameIndex == null) {
|
|
208 |
2
| columnNameIndex = new HashMap();
|
|
209 |
| |
|
210 |
2
| for (Iterator it = columns.iterator(); it.hasNext();) {
|
|
211 |
6
| XMLTableColumnDefinition column = (XMLTableColumnDefinition) it
|
|
212 |
| .next(); |
|
213 |
6
| columnNameIndex.put(column.getName(), column);
|
|
214 |
| } |
|
215 |
| } |
|
216 |
| |
|
217 |
4
| return (XMLTableColumnDefinition) columnNameIndex.get(columnName);
|
|
218 |
| } |
|
219 |
| |
|
220 |
| |
|
221 |
| |
|
222 |
| |
|
223 |
| |
|
224 |
| |
|
225 |
2
| public XPath getRowXPath() {
|
|
226 |
2
| return rowXPath;
|
|
227 |
| } |
|
228 |
| |
|
229 |
| |
|
230 |
| |
|
231 |
| |
|
232 |
| |
|
233 |
| |
|
234 |
| |
|
235 |
2
| public void setRowXPath(XPath rowXPath) {
|
|
236 |
2
| this.rowXPath = rowXPath;
|
|
237 |
| } |
|
238 |
| |
|
239 |
2
| public void setRowExpression(String xpath) {
|
|
240 |
2
| setRowXPath(createXPath(xpath));
|
|
241 |
| } |
|
242 |
| |
|
243 |
| |
|
244 |
| |
|
245 |
4
| public Object getVariableValue(String namespaceURI, String prefix,
|
|
246 |
| String localName) { |
|
247 |
4
| XMLTableColumnDefinition column = getColumn(localName);
|
|
248 |
| |
|
249 |
4
| if (column != null) {
|
|
250 |
4
| return column.getValue(rowValue);
|
|
251 |
| } |
|
252 |
| |
|
253 |
0
| return null;
|
|
254 |
| } |
|
255 |
| |
|
256 |
| |
|
257 |
| |
|
258 |
8
| protected XPath createXPath(String expression) {
|
|
259 |
8
| return DocumentHelper.createXPath(expression);
|
|
260 |
| } |
|
261 |
| |
|
262 |
6
| protected XPath createColumnXPath(String expression) {
|
|
263 |
6
| XPath xpath = createXPath(expression);
|
|
264 |
| |
|
265 |
| |
|
266 |
6
| xpath.setVariableContext(this);
|
|
267 |
| |
|
268 |
6
| return xpath;
|
|
269 |
| } |
|
270 |
| |
|
271 |
6
| protected void clearCaches() {
|
|
272 |
6
| columnArray = null;
|
|
273 |
6
| columnNameIndex = null;
|
|
274 |
| } |
|
275 |
| |
|
276 |
0
| protected void handleException(Exception e) {
|
|
277 |
| |
|
278 |
0
| System.out.println("Caught: " + e);
|
|
279 |
| } |
|
280 |
| } |
|
281 |
| |
|
282 |
| |
|
283 |
| |
|
284 |
| |
|
285 |
| |
|
286 |
| |
|
287 |
| |
|
288 |
| |
|
289 |
| |
|
290 |
| |
|
291 |
| |
|
292 |
| |
|
293 |
| |
|
294 |
| |
|
295 |
| |
|
296 |
| |
|
297 |
| |
|
298 |
| |
|
299 |
| |
|
300 |
| |
|
301 |
| |
|
302 |
| |
|
303 |
| |
|
304 |
| |
|
305 |
| |
|
306 |
| |
|
307 |
| |
|
308 |
| |
|
309 |
| |
|
310 |
| |
|
311 |
| |
|
312 |
| |
|
313 |
| |
|
314 |
| |
|
315 |
| |
|
316 |
| |
|
317 |
| |