|
From: <mic...@us...> - 2003-12-01 00:24:51
|
Update of /cvsroot/babeldoc/babeldoc/modules/jfreereports/src/com/babeldoc/utils
In directory sc8-pr-cvs1:/tmp/cvs-serv6835/modules/jfreereports/src/com/babeldoc/utils
Modified Files:
Tag: TEMP_MIKEA
XMLTableModel.java XMLTableModelTest.java
Log Message:
Completed initial implementation
Index: XMLTableModel.java
===================================================================
RCS file: /cvsroot/babeldoc/babeldoc/modules/jfreereports/src/com/babeldoc/utils/Attic/XMLTableModel.java,v
retrieving revision 1.1.2.4
retrieving revision 1.1.2.5
diff -C2 -d -r1.1.2.4 -r1.1.2.5
*** XMLTableModel.java 29 Nov 2003 02:32:14 -0000 1.1.2.4
--- XMLTableModel.java 1 Dec 2003 00:24:48 -0000 1.1.2.5
***************
*** 1,8 ****
! /*
! * Created on 19-Nov-2003
*
- * To change the template for this generated file go to
- * Window>Preferences>Java>Code Generation>Code and Comments
*/
package com.babeldoc.utils;
--- 1,68 ----
! /* ====================================================================
! * The Apache Software License, Version 1.1
! *
! * Copyright (c) 2000 The Apache Software Foundation. All rights
! * reserved.
! *
! * Redistribution and use in source and binary forms, with or without
! * modification, are permitted provided that the following conditions
! * are met:
! *
! * 1. Redistributions of source code must retain the above copyright
! * notice, this list of conditions and the following disclaimer.
! *
! * 2. Redistributions in binary form must reproduce the above copyright
! * notice, this list of conditions and the following disclaimer in
! * the documentation and/or other materials provided with the
! * distribution.
! *
! * 3. The end-user documentation included with the redistribution,
! * if any, must include the following acknowledgment:
! * "This product includes software developed by the
! * Apache Software Foundation (http://www.apache.org/)."
! * Alternately, this acknowledgment may appear in the software itself,
! * if and wherever such third-party acknowledgments normally appear.
! *
! * 4. The names "Apache" and "Apache Software Foundation" must
! * not be used to endorse or promote products derived from this
! * software without prior written permission. For written
! * permission, please contact ap...@ap....
! *
! * 5. Products derived from this software may not be called "Apache",
! * nor may "Apache" appear in their name, without prior written
! * permission of the Apache Software Foundation.
! *
! * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
! * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
! * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
! * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
! * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
! * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
! * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
! * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
! * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
! * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
! * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
! * SUCH DAMAGE.
! * ====================================================================
! *
! * This software consists of voluntary contributions made by many
! * individuals on behalf of the Apache Software Foundation. For more
! * information on the Apache Software Foundation, please see
! * <http://www.apache.org/>.
! *
! * Portions of this software are based upon public domain software
! * originally written at the National Center for Supercomputing Applications,
! * University of Illinois, Urbana-Champaign.
! * ====================================================================
! *
! * Babeldoc: The Universal Document Processor
! *
! * $Header$
! * $DateTime$
! * $Author$
*
*/
+
package com.babeldoc.utils;
***************
*** 16,24 ****
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
- import javax.swing.table.DefaultTableModel;
import javax.xml.transform.TransformerException;
import org.apache.xpath.objects.XNumber;
- import org.apache.xpath.objects.XObject;
import org.apache.xpath.XPathAPI;
--- 76,82 ----
***************
*** 31,69 ****
/**
* @author mikea
*
- * To change the template for this generated type comment go to
- * Window>Preferences>Java>Code Generation>Code and Comments
*/
public class XMLTableModel extends AbstractTableModel implements TableModel {
private static LogService log =
LogService.getInstance(XMLTableModel.class.getName());
public static final String XML_MIME_TYPE = "text/xml";
! public static final String QUERY_RESULTS = "queryresults";
! public static final String QUERY = "query";
! public static final String QUERY_NAME = "query-name";
! public static final String QUERY_NUMBER = "query-number";
! public static final String ROW = "row";
public static final String ROW_NUMBER = "row-number";
! public static final String COLUMN = "column";
public static final String COLUMN_NAME = "column-name";
public static final String COLUMN_NUMBER = "column-number";
public static final String COLUMN_CLASS = "column-class";
public static final String DEFAULT_CLASS_NAME = "java.lang.String";
public static final Class DEFAULT_CLASS = java.lang.String.class;
private Document xmlData;
private int columnCount = -1;
private int rowCount = -1;
private Class[] columnClasses;
private String[] columnNames;
private Map columnNumbers = new HashMap();
public XMLTableModel() {
xmlData = null;
}
! public XMLTableModel(org.dom4j.Document data) throws org.dom4j.DocumentException {
xmlData = transformToDOM(data);
columnClasses = new Class[getColumnCount()];
--- 89,159 ----
/**
+ * A class to provide an AbstractTableModel implementation for data held in
+ * XML format.
+ *
* @author mikea
*
*/
public class XMLTableModel extends AbstractTableModel implements TableModel {
+ /** static: log service */
private static LogService log =
LogService.getInstance(XMLTableModel.class.getName());
+ /** constant: document mime type */
public static final String XML_MIME_TYPE = "text/xml";
! /** instance: root path for data */
! public String QUERY_RESULTS = "";
! /** instance: element identifier for rows */
! public String ROW = "row";
! /** constant: element identifier for row numbers,
! * TODO: should be configurable by the user */
public static final String ROW_NUMBER = "row-number";
! /** instance: element identifier for columns */
! public String COLUMN = "column";
! /** constant: element identifier for column names,
! * TODO: should be configurable by the user */
public static final String COLUMN_NAME = "column-name";
+ /** constant: element identifier for column numbers,
+ * TODO: should be configurable by the user */
public static final String COLUMN_NUMBER = "column-number";
+ /** constant: element identifier for column class identifiers,
+ * TODO: should be configurable by the user */
public static final String COLUMN_CLASS = "column-class";
+ /** constant: the default column class identifier */
public static final String DEFAULT_CLASS_NAME = "java.lang.String";
+ /** constant: the default column class */
public static final Class DEFAULT_CLASS = java.lang.String.class;
+ /** instance: the data */
private Document xmlData;
+ /** instance: cache of the column count */
private int columnCount = -1;
+ /** instance: cache of the row count */
private int rowCount = -1;
+ /** instance: cache of the column classes */
private Class[] columnClasses;
+ /** instance: cache of the column names */
private String[] columnNames;
+ /** instance: cache of the column ordinal positions keyed on name */
private Map columnNumbers = new HashMap();
+ /**
+ * The default constructor
+ *
+ */
public XMLTableModel() {
xmlData = null;
}
! /**
! * Constructor with data document and root path to data
! *
! * @param data the data document
! * @param rootPath the string representation of the path to the
! * data within the document
! *
! */
! public XMLTableModel(org.dom4j.Document data, String rootPath) throws org.dom4j.DocumentException {
! QUERY_RESULTS = rootPath;
xmlData = transformToDOM(data);
columnClasses = new Class[getColumnCount()];
***************
*** 71,78 ****
}
! public void setDocument(org.dom4j.Document doc) throws org.dom4j.DocumentException {
xmlData = transformToDOM(doc);
columnClasses = new Class[getColumnCount()];
columnNames = new String[getColumnCount()];
}
--- 161,177 ----
}
! /**
! * Set the data document and clear the cached information
! *
! * @param info
! */
! public void setDocument(org.dom4j.Document doc, String rootPath) throws org.dom4j.DocumentException {
! QUERY_RESULTS = rootPath;
xmlData = transformToDOM(doc);
+ columnCount = -1;
+ rowCount = -1;
columnClasses = new Class[getColumnCount()];
columnNames = new String[getColumnCount()];
+ columnNumbers = new HashMap();
}
***************
*** 89,93 ****
String xpath = "count(" +
QUERY_RESULTS + "/" +
- QUERY + "/" +
ROW + "[@row-number=\"0\"]/" +
COLUMN +
--- 188,191 ----
***************
*** 102,106 ****
}
- // Get the result value from the node and cache
columnCount = new Double(n.num()).intValue();
--- 200,203 ----
***************
*** 120,124 ****
String xpath = "count(" +
QUERY_RESULTS + "/" +
- QUERY + "/" +
ROW +
")";
--- 217,220 ----
***************
*** 147,151 ****
// This is the xpath for a cell in an XML data document from the SqlQuery
// pipeline stage
! String xpath = QUERY_RESULTS + "/" + QUERY + "/" +
ROW + "[@" + ROW_NUMBER + "=\"" + rowIndex + "\"]/" +
COLUMN + "[@" + COLUMN_NUMBER + "=\"" + (columnIndex + 1) + "\"]";
--- 243,247 ----
// This is the xpath for a cell in an XML data document from the SqlQuery
// pipeline stage
! String xpath = QUERY_RESULTS + "/" +
ROW + "[@" + ROW_NUMBER + "=\"" + rowIndex + "\"]/" +
COLUMN + "[@" + COLUMN_NUMBER + "=\"" + (columnIndex + 1) + "\"]";
***************
*** 175,182 ****
}
} catch (Exception e) {
! e.printStackTrace();
! //log.logWarn(e.getMessage());
! v = "";
! //v = new String("[" + I18n.get("jfr.104", className) + "]");
}
--- 271,276 ----
}
} catch (Exception e) {
! log.logWarn(e.getMessage());
! v = new String("[" + I18n.get("jfr.104", className) + "]");
}
***************
*** 184,187 ****
--- 278,286 ----
}
+ /**
+ * Convert the document to w3c format
+ *
+ * @param info
+ */
private org.w3c.dom.Document transformToDOM(org.dom4j.Document doc) throws DocumentException {
DOMWriter writer = new DOMWriter();
***************
*** 193,199 ****
*
* @param columnIndex the column being queried
! * @return the Object.class
*/
public Class getColumnClass(int columnIndex) {
Class c = columnClasses[columnIndex];
if (c == null) {
--- 292,299 ----
*
* @param columnIndex the column being queried
! * @return the Object.class, or null if the position is not valid
*/
public Class getColumnClass(int columnIndex) {
+ if ((columnIndex < 0) || (columnIndex >= columnClasses.length)) return (null); // Invalid index
Class c = columnClasses[columnIndex];
if (c == null) {
***************
*** 203,207 ****
// This is the xpath for a cell in an XML data document from the SqlQuery
// pipeline stage
! String xpath = QUERY_RESULTS + "/" + QUERY + "/" +
ROW + "[@row-number=\"0\"]/" +
COLUMN + "[@" + COLUMN_NUMBER + "=\"" + (columnIndex + 1) + "\"]";
--- 303,307 ----
// This is the xpath for a cell in an XML data document from the SqlQuery
// pipeline stage
! String xpath = QUERY_RESULTS + "/" +
ROW + "[@row-number=\"0\"]/" +
COLUMN + "[@" + COLUMN_NUMBER + "=\"" + (columnIndex + 1) + "\"]";
***************
*** 214,217 ****
--- 314,318 ----
}
+ if (n == null) return (null); // Column position not found
NamedNodeMap attrs = n.getAttributes();
String className = attrs.getNamedItem(COLUMN_CLASS).getNodeValue();
***************
*** 235,242 ****
*
* @param column the column being queried
! * @return a string containing the default name of <code>column</code>
*/
public String getColumnName(int column) {
String name;
name = columnNames[column];
if (name == null) {
--- 336,345 ----
*
* @param column the column being queried
! * @return a string containing the name of <code>column</code> or
! * an empty string if not found
*/
public String getColumnName(int column) {
String name;
+ if ((column < 0) || (column >= columnNames.length)) return (""); // Invalid index
name = columnNames[column];
if (name == null) {
***************
*** 245,249 ****
// This is the xpath for a cell in an XML data document from the SqlQuery
// pipeline stage
! String xpath = QUERY_RESULTS + "/" + QUERY + "/" +
ROW + "[@row-number=\"0\"]/" +
COLUMN + "[@" + COLUMN_NUMBER + "=\"" + (column + 1) + "\"]";
--- 348,352 ----
// This is the xpath for a cell in an XML data document from the SqlQuery
// pipeline stage
! String xpath = QUERY_RESULTS + "/" +
ROW + "[@row-number=\"0\"]/" +
COLUMN + "[@" + COLUMN_NUMBER + "=\"" + (column + 1) + "\"]";
***************
*** 256,259 ****
--- 359,363 ----
}
+ if (n == null) return (""); // Column position not found
NamedNodeMap attrs = n.getAttributes();
name = attrs.getNamedItem(COLUMN_NAME).getNodeValue();
***************
*** 279,283 ****
// This is the xpath for the count of columns in the first row
! String xpath = QUERY_RESULTS + "/" + QUERY + "/" +
ROW + "[@row-number=\"0\"]/" +
COLUMN + "[@" + COLUMN_NAME + "=\"" + columnName + "\"]";
--- 383,387 ----
// This is the xpath for the count of columns in the first row
! String xpath = QUERY_RESULTS + "/" +
ROW + "[@row-number=\"0\"]/" +
COLUMN + "[@" + COLUMN_NAME + "=\"" + columnName + "\"]";
***************
*** 290,293 ****
--- 394,398 ----
}
+ if (n == null) return (-1); // Column name not found
NamedNodeMap attrs = n.getAttributes();
columnNumber = new Integer(attrs.getNamedItem(COLUMN_NUMBER).getNodeValue());
***************
*** 298,301 ****
--- 403,422 ----
return (columnNumber.intValue() - 1);
+ }
+
+ public String getColumnID() {
+ return COLUMN;
+ }
+
+ public void setColumnID(String column) {
+ COLUMN = column;
+ }
+
+ public String getRowID() {
+ return ROW;
+ }
+
+ public void setRowID(String row) {
+ ROW = row;
}
Index: XMLTableModelTest.java
===================================================================
RCS file: /cvsroot/babeldoc/babeldoc/modules/jfreereports/src/com/babeldoc/utils/Attic/XMLTableModelTest.java,v
retrieving revision 1.1.2.2
retrieving revision 1.1.2.3
diff -C2 -d -r1.1.2.2 -r1.1.2.3
*** XMLTableModelTest.java 29 Nov 2003 02:34:40 -0000 1.1.2.2
--- XMLTableModelTest.java 1 Dec 2003 00:24:48 -0000 1.1.2.3
***************
*** 29,33 ****
" <column column-name=\"last_name\" column-number=\"3\" column-class=\"java.lang.String\">Abel</column>" +
" <column column-name=\"first_name\" column-number=\"4\" column-class=\"java.lang.String\">Peter</column>" +
! " <column column-name=\"main_phone\" column-number=\"5\" column-class=\"java.lang.String\">(510)555-8188</column>" +
" <column column-name=\"fk_seminar_code\" column-number=\"6\" column-class=\"java.lang.String\">001</column>" +
" <column column-name=\"seminar_tmpl_name\" column-number=\"7\" column-class=\"java.lang.String\">Annual Update</column>" +
--- 29,33 ----
" <column column-name=\"last_name\" column-number=\"3\" column-class=\"java.lang.String\">Abel</column>" +
" <column column-name=\"first_name\" column-number=\"4\" column-class=\"java.lang.String\">Peter</column>" +
! " <column column-name=\"main_phone\" column-number=\"5\" column-class=\"java.lang.String\">(888)555-8188</column>" +
" <column column-name=\"fk_seminar_code\" column-number=\"6\" column-class=\"java.lang.String\">001</column>" +
" <column column-name=\"seminar_tmpl_name\" column-number=\"7\" column-class=\"java.lang.String\">Annual Update</column>" +
***************
*** 45,49 ****
" <column column-name=\"last_name\" column-number=\"3\" column-class=\"java.lang.String\">Bartley</column>" +
" <column column-name=\"first_name\" column-number=\"4\" column-class=\"java.lang.String\">David</column>" +
! " <column column-name=\"main_phone\" column-number=\"5\" column-class=\"java.lang.String\">(415)555-7703</column>" +
" <column column-name=\"fk_seminar_code\" column-number=\"6\" column-class=\"java.lang.String\">001</column>" +
" <column column-name=\"seminar_tmpl_name\" column-number=\"7\" column-class=\"java.lang.String\">Annual Update</column>" +
--- 45,49 ----
" <column column-name=\"last_name\" column-number=\"3\" column-class=\"java.lang.String\">Bartley</column>" +
" <column column-name=\"first_name\" column-number=\"4\" column-class=\"java.lang.String\">David</column>" +
! " <column column-name=\"main_phone\" column-number=\"5\" column-class=\"java.lang.String\">(888)555-7703</column>" +
" <column column-name=\"fk_seminar_code\" column-number=\"6\" column-class=\"java.lang.String\">001</column>" +
" <column column-name=\"seminar_tmpl_name\" column-number=\"7\" column-class=\"java.lang.String\">Annual Update</column>" +
***************
*** 65,69 ****
try {
doc = DocumentHelper.parseText(strDoc);
! tm = new XMLTableModel(doc);
// Column and row counts
--- 65,69 ----
try {
doc = DocumentHelper.parseText(strDoc);
! tm = new XMLTableModel(doc, "/queryresults/query");
// Column and row counts
***************
*** 76,79 ****
--- 76,85 ----
System.out.println("Position: " + i + ", Name: " + name + ", Ordinal: " + tm.findColumn(name) + ", Class: " + tm.getColumnClass(i).getName());
}
+ // Find a non-existent column name
+ System.out.println(tm.findColumn("XXX"));
+ // Find a non-existent column position
+ System.out.println(tm.getColumnName(15));
+ // Find a non-existent column class
+ System.out.println(tm.getColumnClass(15));
// getValueAt
|