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