1
2
3
4
5
6
7
8
9
10 package org.dom4j.swing;
11
12 import java.io.Serializable;
13
14 import org.dom4j.DocumentHelper;
15 import org.dom4j.Node;
16 import org.dom4j.XPath;
17
18 /*** <p><code>XMLTableColumnDefinition</code> a column
19 * within a table definition.</p>
20 *
21 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
22 * @version $Revision: 1.8 $
23 */
24 public class XMLTableColumnDefinition implements Serializable {
25
26 public static final int OBJECT_TYPE = 0;
27 public static final int STRING_TYPE = 1;
28 public static final int NUMBER_TYPE = 2;
29 public static final int NODE_TYPE = 3;
30
31 /*** Holds value of property type. */
32 private int type;
33
34 /*** Holds value of property name. */
35 private String name;
36
37 /*** Holds value of property xpath. */
38 private XPath xpath;
39
40 /*** Holds the XPath used for the column name */
41 private XPath columnNameXPath;
42
43 public static int parseType(String typeName) {
44 if ( typeName != null && typeName.length() > 0 ) {
45 if ( typeName.equals( "string" ) ) {
46 return STRING_TYPE;
47 }
48 else if ( typeName.equals( "number" ) ) {
49 return NUMBER_TYPE;
50 }
51 else if ( typeName.equals( "node" ) ) {
52 return NODE_TYPE;
53 }
54 }
55 return OBJECT_TYPE;
56 }
57
58 public XMLTableColumnDefinition() {
59 }
60
61 public XMLTableColumnDefinition(String name, String expression, int type) {
62 this.name = name;
63 this.type = type;
64 this.xpath = createXPath(expression);
65 }
66
67 public XMLTableColumnDefinition(String name, XPath xpath, int type) {
68 this.name = name;
69 this.xpath = xpath;
70 this.type = type;
71 }
72
73 public XMLTableColumnDefinition(XPath columnNameXPath, XPath xpath, int type) {
74 this.xpath = xpath;
75 this.columnNameXPath = columnNameXPath;
76 this.type = type;
77 }
78
79
80 public Class getColumnClass() {
81 switch (type) {
82 case STRING_TYPE:
83 return String.class;
84 case NUMBER_TYPE:
85 return Number.class;
86 case NODE_TYPE:
87 return Node.class;
88 default:
89 return Object.class;
90 }
91 }
92
93 public Object getValue(Object row) {
94 switch (type) {
95 case STRING_TYPE:
96 return xpath.valueOf( row );
97 case NUMBER_TYPE:
98 return xpath.numberValueOf( row );
99 case NODE_TYPE:
100 return xpath.selectSingleNode( row );
101 default:
102 return xpath.evaluate( row );
103 }
104 }
105
106
107
108
109 /*** Getter for property type.
110 * @return Value of property type.
111 */
112 public int getType() {
113 return type;
114 }
115
116 /*** Setter for property type.
117 * @param type New value of property type.
118 */
119 public void setType(int type) {
120 this.type = type;
121 }
122
123 /*** Getter for property name.
124 * @return Value of property name.
125 */
126 public String getName() {
127 return name;
128 }
129
130 /*** Setter for property name.
131 * @param name New value of property name.
132 */
133 public void setName(String name) {
134 this.name = name;
135 }
136
137 /*** Getter for property xpath.
138 * @return Value of property xpath.
139 */
140 public XPath getXPath() {
141 return xpath;
142 }
143
144 /*** Setter for property xpath.
145 * @param xpath New value of property xpath.
146 */
147 public void setXPath(XPath xpath) {
148 this.xpath = xpath;
149 }
150
151 /***
152 * @return the XPath used to create the column name
153 */
154 public XPath getColumnNameXPath() {
155 return columnNameXPath;
156 }
157
158 /*** Setter for property columnNameXPath.
159 * @param columnNameXPath New value of property xpath.
160 */
161 public void setColumnNameXPath(XPath columnNameXPath) {
162 this.columnNameXPath = columnNameXPath;
163 }
164
165
166
167 protected XPath createXPath(String expression) {
168 return DocumentHelper.createXPath(expression);
169 }
170
171 protected void handleException(Exception e) {
172
173 System.out.println( "Caught: " + e );
174 }
175 }
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223