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