View Javadoc

1   /*
2    * Copyright 2001-2004 (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    * $Id: XMLTableColumnDefinition.java,v 1.8 2004/06/25 12:34:49 maartenc Exp $
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     // Properties
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     // Implementation methods
166     //-------------------------------------------------------------------------                
167     protected XPath createXPath(String expression) {
168         return DocumentHelper.createXPath(expression);
169     }
170         
171     protected void handleException(Exception e) {
172         // #### should use jakarta commons-logging
173         System.out.println( "Caught: " + e );
174     }
175 }
176 
177 
178 
179 
180 /*
181  * Redistribution and use of this software and associated documentation
182  * ("Software"), with or without modification, are permitted provided
183  * that the following conditions are met:
184  *
185  * 1. Redistributions of source code must retain copyright
186  *    statements and notices.  Redistributions must also contain a
187  *    copy of this document.
188  *
189  * 2. Redistributions in binary form must reproduce the
190  *    above copyright notice, this list of conditions and the
191  *    following disclaimer in the documentation and/or other
192  *    materials provided with the distribution.
193  *
194  * 3. The name "DOM4J" must not be used to endorse or promote
195  *    products derived from this Software without prior written
196  *    permission of MetaStuff, Ltd.  For written permission,
197  *    please contact dom4j-info@metastuff.com.
198  *
199  * 4. Products derived from this Software may not be called "DOM4J"
200  *    nor may "DOM4J" appear in their names without prior written
201  *    permission of MetaStuff, Ltd. DOM4J is a registered
202  *    trademark of MetaStuff, Ltd.
203  *
204  * 5. Due credit should be given to the DOM4J Project - 
205  *    http://www.dom4j.org
206  *
207  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
208  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
209  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
210  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
211  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
212  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
213  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
214  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
215  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
216  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
217  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
218  * OF THE POSSIBILITY OF SUCH DAMAGE.
219  *
220  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
221  *
222  * $Id: XMLTableColumnDefinition.java,v 1.8 2004/06/25 12:34:49 maartenc Exp $
223  */