Clover coverage report - dom4j - 1.5
Coverage timestamp: vr sep 3 2004 20:47:03 GMT+01:00
file stats: LOC: 284   Methods: 27
NCLOC: 151   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
XMLTableDefinition.java 83,3% 78,1% 70,4% 76,7%
coverage coverage
 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: XMLTableDefinition.java,v 1.6 2004/06/25 08:03:40 maartenc Exp $
 8    */
 9   
 10    package org.dom4j.swing;
 11   
 12    import java.io.Serializable;
 13    import java.util.ArrayList;
 14    import java.util.HashMap;
 15    import java.util.Iterator;
 16    import java.util.List;
 17    import java.util.Map;
 18   
 19    import org.dom4j.Document;
 20    import org.dom4j.DocumentHelper;
 21    import org.dom4j.Element;
 22    import org.dom4j.XPath;
 23    import org.jaxen.VariableContext;
 24   
 25    /** <p><code>XMLTableDefinition</code> represents a
 26    * table definition based on XPath expression evaluated
 27    * on an XML document.</p>
 28    *
 29    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
 30    * @version $Revision: 1.6 $
 31    */
 32    public class XMLTableDefinition implements Serializable, VariableContext {
 33   
 34    /** Holds value of property rowXPath. */
 35    private XPath rowXPath;
 36   
 37    /** The columns to display in this table */
 38    private List columns = new ArrayList();
 39   
 40    /** integer index array cache */
 41    private XMLTableColumnDefinition[] columnArray;
 42    /** name index cache */
 43    private Map columnNameIndex;
 44   
 45    /** for cross-row variables */
 46    private VariableContext variableContext;
 47   
 48    /** stores the current row value for the variableContext */
 49    private Object rowValue;
 50   
 51  4 public XMLTableDefinition() {
 52    }
 53   
 54    /** Loads an XML table definition from an XML definition document */
 55  2 public static XMLTableDefinition load(Document definition) {
 56  2 return load( definition.getRootElement() );
 57    }
 58   
 59    /** Loads an XML table definition from an XML definition document */
 60  2 public static XMLTableDefinition load(Element definition) {
 61  2 XMLTableDefinition answer = new XMLTableDefinition();
 62  2 answer.setRowExpression( definition.attributeValue( "select" ) );
 63  2 for (Iterator iter = definition.elementIterator( "column" ); iter.hasNext(); ) {
 64  6 Element element = (Element) iter.next();
 65  6 String expression = element.attributeValue( "select" );
 66  6 String name = element.getText();
 67  6 String typeName = element.attributeValue( "type", "string" );
 68  6 String columnNameXPath = element.attributeValue( "columnNameXPath" );
 69  6 int type = XMLTableColumnDefinition.parseType( typeName );
 70  6 if ( columnNameXPath != null ) {
 71  0 answer.addColumnWithXPathName( columnNameXPath, expression, type );
 72    }
 73    else {
 74  6 answer.addColumn( name, expression, type );
 75    }
 76    }
 77  2 return answer;
 78    }
 79   
 80   
 81  0 public Class getColumnClass(int columnIndex) {
 82  0 return getColumn(columnIndex).getColumnClass();
 83    }
 84   
 85  4 public int getColumnCount() {
 86  4 return columns.size();
 87    }
 88   
 89    /**
 90    * @return the static column name. This is used if there is no columnNameXPath
 91    */
 92  12 public String getColumnName(int columnIndex) {
 93  12 return getColumn(columnIndex).getName();
 94    }
 95   
 96    /**
 97    * @return the XPath expression used to evaluate the value of cells in this column
 98    */
 99  0 public XPath getColumnXPath(int columnIndex) {
 100  0 return getColumn(columnIndex).getXPath();
 101    }
 102   
 103    /**
 104    * @return the XPath expresssion used to create the column name, if there is one
 105    * or null if there is no XPath expression to name the column.
 106    */
 107  12 public XPath getColumnNameXPath(int columnIndex) {
 108  12 return getColumn(columnIndex).getColumnNameXPath();
 109    }
 110   
 111  24 public synchronized Object getValueAt(Object row, int columnIndex) {
 112  24 XMLTableColumnDefinition column = getColumn(columnIndex);
 113  24 Object answer = null;
 114  24 synchronized (this) {
 115  24 this.rowValue = row;
 116  24 answer = column.getValue(row);
 117  24 this.rowValue = null;
 118    }
 119  24 return answer;
 120    }
 121   
 122   
 123  0 public void addColumn(String name, String expression) {
 124  0 addColumn( name, expression, XMLTableColumnDefinition.OBJECT_TYPE );
 125    }
 126   
 127  12 public void addColumn(String name, String expression, int type) {
 128  12 XPath xpath = createColumnXPath( expression );
 129  12 addColumn( new XMLTableColumnDefinition( name, xpath, type ) );
 130    }
 131   
 132  0 public void addColumnWithXPathName(String columnNameXPathExpression, String expression, int type) {
 133  0 XPath columnNameXPath = createColumnXPath( columnNameXPathExpression );
 134  0 XPath xpath = createColumnXPath( expression );
 135  0 addColumn( new XMLTableColumnDefinition( columnNameXPath, xpath, type ) );
 136    }
 137   
 138  6 public void addStringColumn(String name, String expression) {
 139  6 addColumn( name, expression, XMLTableColumnDefinition.STRING_TYPE );
 140    }
 141   
 142  0 public void addNumberColumn(String name, String expression) {
 143  0 addColumn( name, expression, XMLTableColumnDefinition.NUMBER_TYPE );
 144    }
 145   
 146  12 public void addColumn(XMLTableColumnDefinition column) {
 147  12 clearCaches();
 148  12 columns.add( column );
 149    }
 150   
 151  0 public void removeColumn(XMLTableColumnDefinition column) {
 152  0 clearCaches();
 153  0 columns.remove( column );
 154    }
 155   
 156  0 public void clear() {
 157  0 clearCaches();
 158  0 columns.clear();
 159    }
 160   
 161  48 public XMLTableColumnDefinition getColumn(int index) {
 162  48 if ( columnArray == null ) {
 163  4 columnArray = new XMLTableColumnDefinition[ columns.size() ];
 164  4 columns.toArray( columnArray );
 165    }
 166  48 return columnArray[index];
 167    }
 168   
 169  8 public XMLTableColumnDefinition getColumn(String columnName) {
 170  8 if ( columnNameIndex == null ) {
 171  4 columnNameIndex = new HashMap();
 172  4 for (Iterator iter = columns.iterator(); iter.hasNext(); ) {
 173  12 XMLTableColumnDefinition column = (XMLTableColumnDefinition) iter.next();
 174  12 columnNameIndex.put( column.getName(), column );
 175    }
 176    }
 177  8 return (XMLTableColumnDefinition) columnNameIndex.get(columnName);
 178    }
 179   
 180    /** Getter for property rowXPath.
 181    * @return Value of property rowXPath.
 182    */
 183  4 public XPath getRowXPath() {
 184  4 return rowXPath;
 185    }
 186   
 187    /** Setter for property rowXPath.
 188    * @param rowXPath New value of property rowXPath.
 189    */
 190  4 public void setRowXPath(XPath rowXPath) {
 191  4 this.rowXPath = rowXPath;
 192    }
 193   
 194  4 public void setRowExpression(String xpath) {
 195  4 setRowXPath( createXPath( xpath ) );
 196    }
 197   
 198   
 199    // VariableContext interface
 200    //-------------------------------------------------------------------------
 201  8 public Object getVariableValue(
 202    String namespaceURI,
 203    String prefix,
 204    String localName
 205    ) {
 206  8 XMLTableColumnDefinition column = getColumn(localName);
 207  8 if ( column != null ) {
 208  8 return column.getValue( rowValue );
 209    }
 210  0 return null;
 211    }
 212   
 213    // Implementation methods
 214    //-------------------------------------------------------------------------
 215  16 protected XPath createXPath(String expression) {
 216  16 return DocumentHelper.createXPath(expression);
 217    }
 218   
 219  12 protected XPath createColumnXPath(String expression) {
 220  12 XPath xpath = createXPath( expression );
 221    // associate my variable context
 222  12 xpath.setVariableContext( this );
 223  12 return xpath;
 224    }
 225   
 226   
 227  12 protected void clearCaches() {
 228  12 columnArray = null;
 229  12 columnNameIndex = null;
 230    }
 231   
 232  0 protected void handleException(Exception e) {
 233    // #### should use jakarta commons-logging
 234  0 System.out.println( "Caught: " + e );
 235    }
 236    }
 237   
 238   
 239   
 240   
 241    /*
 242    * Redistribution and use of this software and associated documentation
 243    * ("Software"), with or without modification, are permitted provided
 244    * that the following conditions are met:
 245    *
 246    * 1. Redistributions of source code must retain copyright
 247    * statements and notices. Redistributions must also contain a
 248    * copy of this document.
 249    *
 250    * 2. Redistributions in binary form must reproduce the
 251    * above copyright notice, this list of conditions and the
 252    * following disclaimer in the documentation and/or other
 253    * materials provided with the distribution.
 254    *
 255    * 3. The name "DOM4J" must not be used to endorse or promote
 256    * products derived from this Software without prior written
 257    * permission of MetaStuff, Ltd. For written permission,
 258    * please contact dom4j-info@metastuff.com.
 259    *
 260    * 4. Products derived from this Software may not be called "DOM4J"
 261    * nor may "DOM4J" appear in their names without prior written
 262    * permission of MetaStuff, Ltd. DOM4J is a registered
 263    * trademark of MetaStuff, Ltd.
 264    *
 265    * 5. Due credit should be given to the DOM4J Project -
 266    * http://www.dom4j.org
 267    *
 268    * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
 269    * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
 270    * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 271    * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 272    * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 273    * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 274    * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 275    * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 276    * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 277    * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 278    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 279    * OF THE POSSIBILITY OF SUCH DAMAGE.
 280    *
 281    * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
 282    *
 283    * $Id: XMLTableDefinition.java,v 1.6 2004/06/25 08:03:40 maartenc Exp $
 284    */