View Javadoc

1   /*
2    * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
3    *
4    * This software is open source.
5    * See the bottom of this file for the licence.
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     // Properties
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     // Implementation methods
192     // -------------------------------------------------------------------------
193     protected XPath createXPath(String expression) {
194         return DocumentHelper.createXPath(expression);
195     }
196 
197     protected void handleException(Exception e) {
198         // #### should use jakarta commons-logging
199         System.out.println("Caught: " + e);
200     }
201 }
202 
203 /*
204  * Redistribution and use of this software and associated documentation
205  * ("Software"), with or without modification, are permitted provided that the
206  * following conditions are met:
207  * 
208  * 1. Redistributions of source code must retain copyright statements and
209  * notices. Redistributions must also contain a copy of this document.
210  * 
211  * 2. Redistributions in binary form must reproduce the above copyright notice,
212  * this list of conditions and the following disclaimer in the documentation
213  * and/or other materials provided with the distribution.
214  * 
215  * 3. The name "DOM4J" must not be used to endorse or promote products derived
216  * from this Software without prior written permission of MetaStuff, Ltd. For
217  * written permission, please contact dom4j-info@metastuff.com.
218  * 
219  * 4. Products derived from this Software may not be called "DOM4J" nor may
220  * "DOM4J" appear in their names without prior written permission of MetaStuff,
221  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
222  * 
223  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
224  * 
225  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
226  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
227  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
228  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
229  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
230  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
231  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
232  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
233  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
234  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
235  * POSSIBILITY OF SUCH DAMAGE.
236  * 
237  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
238  */