|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| XMLTableModel.java | 75% | 55,6% | 53,3% | 56,5% |
|
||||||||||||||
| 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.util.List; | |
| 11 | ||
| 12 | import javax.swing.table.AbstractTableModel; | |
| 13 | ||
| 14 | import org.dom4j.Document; | |
| 15 | import org.dom4j.Element; | |
| 16 | import org.dom4j.XPath; | |
| 17 | ||
| 18 | /** | |
| 19 | * <p> | |
| 20 | * <code>XMLTableDefinition</code> repro. | |
| 21 | * </p> | |
| 22 | * | |
| 23 | * @author <a href="mailto:jstrachan@apache.org">James Strachan </a> | |
| 24 | * @version $Revision: 1.8 $ | |
| 25 | */ | |
| 26 | public class XMLTableModel extends AbstractTableModel { | |
| 27 | /** Holds value of property definition. */ | |
| 28 | private XMLTableDefinition definition; | |
| 29 | ||
| 30 | /** Holds value of property source. */ | |
| 31 | private Object source; | |
| 32 | ||
| 33 | /** The rows evaluated from the row XPath expression */ | |
| 34 | private List rows; | |
| 35 | ||
| 36 | /** | |
| 37 | * Creates a TableModel from an XML table definition document and an XML | |
| 38 | * source | |
| 39 | * | |
| 40 | * @param tableDefinition | |
| 41 | * DOCUMENT ME! | |
| 42 | * @param source | |
| 43 | * DOCUMENT ME! | |
| 44 | */ | |
| 45 | 0 | public XMLTableModel(Element tableDefinition, Object source) { |
| 46 | 0 | this(XMLTableDefinition.load(tableDefinition), source); |
| 47 | } | |
| 48 | ||
| 49 | /** | |
| 50 | * Creates a TableModel from an XML table definition document and an XML | |
| 51 | * source | |
| 52 | * | |
| 53 | * @param tableDefinition | |
| 54 | * DOCUMENT ME! | |
| 55 | * @param source | |
| 56 | * DOCUMENT ME! | |
| 57 | */ | |
| 58 | 1 | public XMLTableModel(Document tableDefinition, Object source) { |
| 59 | 1 | this(XMLTableDefinition.load(tableDefinition), source); |
| 60 | } | |
| 61 | ||
| 62 | 2 | public XMLTableModel(XMLTableDefinition definition, Object source) { |
| 63 | 2 | this.definition = definition; |
| 64 | 2 | this.source = source; |
| 65 | } | |
| 66 | ||
| 67 | 12 | public Object getRowValue(int rowIndex) { |
| 68 | 12 | return getRows().get(rowIndex); |
| 69 | } | |
| 70 | ||
| 71 | 14 | public List getRows() { |
| 72 | 14 | if (rows == null) { |
| 73 | 2 | rows = definition.getRowXPath().selectNodes(source); |
| 74 | } | |
| 75 | ||
| 76 | 14 | return rows; |
| 77 | } | |
| 78 | ||
| 79 | // TableModel interface | |
| 80 | // ------------------------------------------------------------------------- | |
| 81 | 0 | public Class getColumnClass(int columnIndex) { |
| 82 | 0 | return definition.getColumnClass(columnIndex); |
| 83 | } | |
| 84 | ||
| 85 | 2 | public int getColumnCount() { |
| 86 | 2 | return definition.getColumnCount(); |
| 87 | } | |
| 88 | ||
| 89 | 6 | public String getColumnName(int columnIndex) { |
| 90 | 6 | XPath xpath = definition.getColumnNameXPath(columnIndex); |
| 91 | ||
| 92 | 6 | if (xpath != null) { |
| 93 | 0 | System.out.println("Evaluating column xpath: " + xpath + " value: " |
| 94 | + xpath.valueOf(source)); | |
| 95 | ||
| 96 | 0 | return xpath.valueOf(source); |
| 97 | } | |
| 98 | ||
| 99 | 6 | return definition.getColumnName(columnIndex); |
| 100 | } | |
| 101 | ||
| 102 | 12 | public Object getValueAt(int rowIndex, int columnIndex) { |
| 103 | 12 | try { |
| 104 | 12 | Object row = getRowValue(rowIndex); |
| 105 | ||
| 106 | 12 | return definition.getValueAt(row, columnIndex); |
| 107 | } catch (Exception e) { | |
| 108 | 0 | handleException(e); |
| 109 | ||
| 110 | 0 | return null; |
| 111 | } | |
| 112 | } | |
| 113 | ||
| 114 | 2 | public int getRowCount() { |
| 115 | 2 | return getRows().size(); |
| 116 | } | |
| 117 | ||
| 118 | // Properties | |
| 119 | // ------------------------------------------------------------------------- | |
| 120 | ||
| 121 | /** | |
| 122 | * Getter for property definition. | |
| 123 | * | |
| 124 | * @return Value of property definition. | |
| 125 | */ | |
| 126 | 0 | public XMLTableDefinition getDefinition() { |
| 127 | 0 | return definition; |
| 128 | } | |
| 129 | ||
| 130 | /** | |
| 131 | * Setter for property definition. | |
| 132 | * | |
| 133 | * @param definition | |
| 134 | * New value of property definition. | |
| 135 | */ | |
| 136 | 0 | public void setDefinition(XMLTableDefinition definition) { |
| 137 | 0 | this.definition = definition; |
| 138 | } | |
| 139 | ||
| 140 | /** | |
| 141 | * Getter for the XML source, which is usually a Node or List of nodes. | |
| 142 | * | |
| 143 | * @return Value of property source. | |
| 144 | */ | |
| 145 | 0 | public Object getSource() { |
| 146 | 0 | return source; |
| 147 | } | |
| 148 | ||
| 149 | /** | |
| 150 | * Setter for the XML source, which is usually a Node or List of nodes. | |
| 151 | * | |
| 152 | * @param source | |
| 153 | * New value of property source. | |
| 154 | */ | |
| 155 | 0 | public void setSource(Object source) { |
| 156 | 0 | this.source = source; |
| 157 | 0 | this.rows = null; |
| 158 | } | |
| 159 | ||
| 160 | // Implementation methods | |
| 161 | // ------------------------------------------------------------------------- | |
| 162 | 0 | protected void handleException(Exception e) { |
| 163 | // #### should use jakarta commons-logging | |
| 164 | 0 | System.out.println("Caught: " + e); |
| 165 | } | |
| 166 | } | |
| 167 | ||
| 168 | /* | |
| 169 | * Redistribution and use of this software and associated documentation | |
| 170 | * ("Software"), with or without modification, are permitted provided that the | |
| 171 | * following conditions are met: | |
| 172 | * | |
| 173 | * 1. Redistributions of source code must retain copyright statements and | |
| 174 | * notices. Redistributions must also contain a copy of this document. | |
| 175 | * | |
| 176 | * 2. Redistributions in binary form must reproduce the above copyright notice, | |
| 177 | * this list of conditions and the following disclaimer in the documentation | |
| 178 | * and/or other materials provided with the distribution. | |
| 179 | * | |
| 180 | * 3. The name "DOM4J" must not be used to endorse or promote products derived | |
| 181 | * from this Software without prior written permission of MetaStuff, Ltd. For | |
| 182 | * written permission, please contact dom4j-info@metastuff.com. | |
| 183 | * | |
| 184 | * 4. Products derived from this Software may not be called "DOM4J" nor may | |
| 185 | * "DOM4J" appear in their names without prior written permission of MetaStuff, | |
| 186 | * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd. | |
| 187 | * | |
| 188 | * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org | |
| 189 | * | |
| 190 | * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND | |
| 191 | * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 192 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 193 | * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE | |
| 194 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 195 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 196 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 197 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 198 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 199 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 200 | * POSSIBILITY OF SUCH DAMAGE. | |
| 201 | * | |
| 202 | * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved. | |
| 203 | */ |
|
||||||||||