001/*
002 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
003 * 
004 * This software is open source. 
005 * See the bottom of this file for the licence.
006 * 
007 * $Id: XMLTableColumnDefinition.java,v 1.1 2001/12/14 11:32:09 jstrachan Exp $
008 */
009
010package org.dom4j.swing;
011
012import java.io.Serializable;
013   
014import org.dom4j.DocumentHelper;
015import org.dom4j.Node;
016import org.dom4j.XPath;
017
018/** <p><code>XMLTableColumnDefinition</code> a column
019  * within a table definition.</p>
020  *
021  * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
022  * @version $Revision: 1.1 $ 
023  */
024public class XMLTableColumnDefinition implements Serializable {
025
026    public static final int OBJECT_TYPE = 0;
027    public static final int STRING_TYPE = 1;
028    public static final int NUMBER_TYPE = 2;
029    public static final int NODE_TYPE = 3;
030    
031    /** Holds value of property type. */
032    private int type;
033    
034    /** Holds value of property name. */
035    private String name;
036    
037    /** Holds value of property xpath. */
038    private XPath xpath;
039
040    public static int parseType(String typeName) {
041        if ( typeName != null && typeName.length() > 0 ) {
042            if ( typeName.equals( "string" ) ) {
043                return STRING_TYPE;
044            }
045            else if ( typeName.equals( "number" ) ) {
046                return NUMBER_TYPE;
047            }
048            else if ( typeName.equals( "node" ) ) {
049                return NODE_TYPE;
050            }
051        }
052        return OBJECT_TYPE;
053    }
054    
055    public XMLTableColumnDefinition() {
056    }
057    
058    public XMLTableColumnDefinition(String name, String expression, int type) {
059        this.name = name;
060        this.type = type;
061        this.xpath = createXPath(expression);
062    }
063    
064    public XMLTableColumnDefinition(String name, XPath xpath, int type) {
065        this.name = name;
066        this.xpath = xpath;
067        this.type = type;
068    }
069    
070    
071    public Class getColumnClass() {
072        switch (type) {
073            case STRING_TYPE:
074                return String.class;
075            case NUMBER_TYPE:
076                return Number.class;
077            case NODE_TYPE:
078                return Node.class;
079            default:
080                return Object.class;
081        }
082    }
083    
084    public Object getValue(Object row) {
085        switch (type) {
086            case STRING_TYPE:
087                return xpath.valueOf( row );
088            case NUMBER_TYPE:
089                return xpath.numberValueOf( row );
090            case NODE_TYPE:
091                return xpath.selectSingleNode( row );
092            default:
093                return xpath.evaluate( row );
094        }
095    }
096    
097    // Properties
098    //-------------------------------------------------------------------------                
099    
100    /** Getter for property type.
101     * @return Value of property type.
102     */
103    public int getType() {
104        return type;
105    }
106    
107    /** Setter for property type.
108     * @param type New value of property type.
109     */
110    public void setType(int type) {
111        this.type = type;
112    }
113    
114    /** Getter for property name.
115     * @return Value of property name.
116     */
117    public String getName() {
118        return name;
119    }
120    
121    /** Setter for property name.
122     * @param name New value of property name.
123     */
124    public void setName(String name) {
125        this.name = name;
126    }
127    
128    /** Getter for property xpath.
129     * @return Value of property xpath.
130     */
131    public XPath getXPath() {
132        return xpath;
133    }
134    
135    /** Setter for property xpath.
136     * @param xpath New value of property xpath.
137     */
138    public void setXPath(XPath xpath) {
139        this.xpath = xpath;
140    }
141    
142    // Implementation methods
143    //-------------------------------------------------------------------------                
144    protected XPath createXPath(String expression) {
145        return DocumentHelper.createXPath(expression);
146    }
147        
148    protected void handleException(Exception e) {
149        // #### should use jakarta commons-logging
150        System.out.println( "Caught: " + e );
151    }
152}
153
154
155
156
157/*
158 * Redistribution and use of this software and associated documentation
159 * ("Software"), with or without modification, are permitted provided
160 * that the following conditions are met:
161 *
162 * 1. Redistributions of source code must retain copyright
163 *    statements and notices.  Redistributions must also contain a
164 *    copy of this document.
165 *
166 * 2. Redistributions in binary form must reproduce the
167 *    above copyright notice, this list of conditions and the
168 *    following disclaimer in the documentation and/or other
169 *    materials provided with the distribution.
170 *
171 * 3. The name "DOM4J" must not be used to endorse or promote
172 *    products derived from this Software without prior written
173 *    permission of MetaStuff, Ltd.  For written permission,
174 *    please contact dom4j-info@metastuff.com.
175 *
176 * 4. Products derived from this Software may not be called "DOM4J"
177 *    nor may "DOM4J" appear in their names without prior written
178 *    permission of MetaStuff, Ltd. DOM4J is a registered
179 *    trademark of MetaStuff, Ltd.
180 *
181 * 5. Due credit should be given to the DOM4J Project
182 *    (http://dom4j.org/).
183 *
184 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
185 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
186 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
187 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
188 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
189 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
190 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
191 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
192 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
193 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
194 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
195 * OF THE POSSIBILITY OF SUCH DAMAGE.
196 *
197 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
198 *
199 * $Id: XMLTableColumnDefinition.java,v 1.1 2001/12/14 11:32:09 jstrachan Exp $
200 */