|
From: <mic...@us...> - 2003-11-28 09:16:32
|
Update of /cvsroot/babeldoc/babeldoc/modules/jfreereports/src/com/babeldoc/utils
In directory sc8-pr-cvs1:/tmp/cvs-serv31272/modules/jfreereports/src/com/babeldoc/utils
Modified Files:
Tag: TEMP_MIKEA
XMLTableModel.java
Log Message:
Added some implementations of AbstractTableModel default methods
Index: XMLTableModel.java
===================================================================
RCS file: /cvsroot/babeldoc/babeldoc/modules/jfreereports/src/com/babeldoc/utils/Attic/XMLTableModel.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
*** XMLTableModel.java 25 Nov 2003 08:01:31 -0000 1.1.2.2
--- XMLTableModel.java 28 Nov 2003 09:16:29 -0000 1.1.2.3
***************
*** 8,14 ****
import com.babeldoc.core.LogService;
-
import com.babeldoc.core.I18n;
import java.lang.reflect.Constructor;
import javax.swing.table.AbstractTableModel;
--- 8,16 ----
import com.babeldoc.core.LogService;
import com.babeldoc.core.I18n;
+
import java.lang.reflect.Constructor;
+ import java.util.HashMap;
+ import java.util.Map;
import javax.swing.table.AbstractTableModel;
***************
*** 16,19 ****
--- 18,23 ----
import javax.xml.transform.TransformerException;
+ import org.apache.xpath.objects.XNumber;
+ import org.apache.xpath.objects.XObject;
import org.apache.xpath.XPathAPI;
***************
*** 46,54 ****
public static final String COLUMN_NUMBER = "column-number";
public static final String COLUMN_CLASS = "column-class";
! public static final String DEFAULT_CLASS = "java.lang.String";
private Document xmlData;
private int columnCount = -1;
private int rowCount = -1;
public XMLTableModel() {
--- 50,62 ----
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() {
***************
*** 62,65 ****
--- 70,74 ----
public void setDocument(org.dom4j.Document doc) throws org.dom4j.DocumentException {
xmlData = transformToDOM(doc);
+ columnClasses = new Class[getColumnCount()];
}
***************
*** 80,86 ****
")";
! Node n;
try {
! n = XPathAPI.selectSingleNode(xmlData, xpath);
} catch (TransformerException e) {
log.logError(e);
--- 89,95 ----
")";
! XNumber n;
try {
! n = (XNumber) XPathAPI.eval(xmlData, xpath);
} catch (TransformerException e) {
log.logError(e);
***************
*** 89,93 ****
// Get the result value from the node and cache
! columnCount = new Integer(n.getNodeValue()).intValue();
return (columnCount);
--- 98,102 ----
// Get the result value from the node and cache
! columnCount = new Double(n.num()).intValue();
return (columnCount);
***************
*** 109,115 ****
")";
! Node n;
try {
! n = XPathAPI.selectSingleNode(xmlData, xpath);
} catch (TransformerException e) {
log.logError(e);
--- 118,124 ----
")";
! XNumber n;
try {
! n = (XNumber) XPathAPI.eval(xmlData, xpath);
} catch (TransformerException e) {
log.logError(e);
***************
*** 118,122 ****
// Get the result value from the node and cache
! rowCount = new Integer(n.getNodeValue()).intValue();
return (rowCount);
--- 127,131 ----
// Get the result value from the node and cache
! rowCount = new Double(n.num()).intValue();
return (rowCount);
***************
*** 147,151 ****
String className = attrs.getNamedItem(COLUMN_CLASS).getNodeValue();
if (className == null || className.equals("")) {
! className = DEFAULT_CLASS;
}
--- 156,160 ----
String className = attrs.getNamedItem(COLUMN_CLASS).getNodeValue();
if (className == null || className.equals("")) {
! className = DEFAULT_CLASS_NAME;
}
***************
*** 167,169 ****
--- 176,293 ----
return writer.write(doc);
}
+
+ /**
+ * Returns the class of the column specified
+ *
+ * @param columnIndex the column being queried
+ * @return the Object.class
+ */
+ public Class getColumnClass(int columnIndex) {
+ Class c = columnClasses[columnIndex];
+ if (c == null) {
+
+ if (xmlData == null) return (null);
+
+ // This is the xpath for a cell in an XML data document from the SqlQuery
+ // pipeline stage
+ String xpath = QUERY_RESULTS + "/" +
+ ROW + "[1]/" +
+ COLUMN + "[@" + COLUMN_NUMBER + "=" + columnIndex + "]";
+
+ Node n;
+ try {
+ n = XPathAPI.selectSingleNode(xmlData, xpath);
+ } catch (TransformerException e) {
+ log.logError(e);
+ return null;
+ }
+
+ NamedNodeMap attrs = n.getAttributes();
+ String className = attrs.getNamedItem(COLUMN_CLASS).getNodeValue();
+ if (className == null || className.equals("")) {
+ className = DEFAULT_CLASS_NAME;
+ }
+
+ try {
+ c = Class.forName(className).getClass();
+ } catch (ClassNotFoundException e) {
+ log.logWarn(e.getMessage());
+ c = DEFAULT_CLASS;
+ }
+ columnClasses[columnIndex] = c;
+ }
+ return c;
+ }
+
+ /**
+ * Returns the column name for the column index specified
+ *
+ * @param column the column being queried
+ * @return a string containing the default name of <code>column</code>
+ */
+ public String getColumnName(int column) {
+ String name = columnNames[column];
+ if (name == null) {
+
+ if (xmlData == null) return (null);
+
+ // This is the xpath for a cell in an XML data document from the SqlQuery
+ // pipeline stage
+ String xpath = QUERY_RESULTS + "/" +
+ ROW + "[1]/" +
+ COLUMN + "[@" + COLUMN_NUMBER + "=" + column + "]";
+
+ Node n;
+ try {
+ n = XPathAPI.selectSingleNode(xmlData, xpath);
+ } catch (TransformerException e) {
+ log.logError(e);
+ return null;
+ }
+
+ NamedNodeMap attrs = n.getAttributes();
+ name = attrs.getNamedItem(COLUMN_NAME).getNodeValue();
+
+ columnNames[column] = name;
+ }
+ return (name);
+ }
+
+ /**
+ * Returns a column index given its name.
+ *
+ * @param columnName string containing name of column to be located
+ * @return the column with <code>columnName</code>, or -1 if not found
+ */
+ public int findColumn(String columnName) {
+
+ if (xmlData == null) return (-1);
+
+ Integer columnNumber = (Integer) columnNumbers.get(columnName);
+
+ if (columnNumber == null) {
+
+ // This is the xpath for the count of columns in the first row
+ String xpath = QUERY_RESULTS + "/" +
+ ROW + "[1]/" +
+ COLUMN + "[@" + COLUMN_NAME + "=" + columnName + "]";
+
+ Node n;
+ try {
+ n = XPathAPI.selectSingleNode(xmlData, xpath);
+ } catch (TransformerException e) {
+ log.logError(e);
+ return -1;
+ }
+
+ NamedNodeMap attrs = n.getAttributes();
+ columnNumber = new Integer(attrs.getNamedItem(COLUMN_NAME).getNodeValue());
+
+ columnNumbers.put(columnName, columnNumber);
+
+ }
+
+ return (columnNumber.intValue());
+ }
+
}
|