|
From: <mic...@us...> - 2003-11-21 09:17:08
|
Update of /cvsroot/babeldoc/babeldoc/modules/jfreereports/src/com/babeldoc/utils
In directory sc8-pr-cvs1:/tmp/cvs-serv16985/modules/jfreereports/src/com/babeldoc/utils
Added Files:
Tag: TEMP_MIKEA
XMLTableModel.java
Log Message:
New table model for XML files using XPath
--- NEW FILE: XMLTableModel.java ---
/*
* 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;
import com.babeldoc.core.LogService;
import com.babeldoc.core.I18n;
import java.lang.reflect.Constructor;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import javax.xml.transform.TransformerException;
import org.apache.xpath.XPathAPI;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.traversal.NodeIterator;
import org.dom4j.io.DOMWriter;
import org.dom4j.DocumentException;
/**
* @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 = "java.lang.String";
private Document xmlData;
private boolean gotColumnCount = false;
private int columnCount;
private boolean gotRowCount = false;
private int rowCount;
public XMLTableModel() {
xmlData = null;
}
public XMLTableModel(org.dom4j.Document data) throws org.dom4j.DocumentException {
xmlData = transformToDOM(data);
}
public void setDocument(org.dom4j.Document doc) throws org.dom4j.DocumentException {
xmlData = transformToDOM(doc);
}
/* (non-Javadoc)
* @see javax.swing.table.TableModel#getColumnCount()
*/
public int getColumnCount() {
if (xmlData == null) return (0);
if (gotColumnCount) return (columnCount);
// This is the xpath for the count of columns in the first row
String xpath = "count(" +
QUERY_RESULTS + "/" +
ROW + "[1]/" +
COLUMN +
")";
Node n;
try {
n = XPathAPI.selectSingleNode(xmlData, xpath);
} catch (TransformerException e) {
log.logError(e);
return (-1);
}
// Get the result value from the node and cache
columnCount = new Integer(n.getNodeValue()).intValue();
gotColumnCount = true;
return (columnCount);
}
/* (non-Javadoc)
* @see javax.swing.table.TableModel#getRowCount()
*/
public int getRowCount() {
if (xmlData == null) return (0);
if (gotRowCount) return (rowCount);
// This is the xpath for the count of columns in the first row
String xpath = "count(" +
QUERY_RESULTS + "/" +
ROW +
")";
Node n;
try {
n = XPathAPI.selectSingleNode(xmlData, xpath);
} catch (TransformerException e) {
log.logError(e);
return (-1);
}
// Get the result value from the node and cache
rowCount = new Integer(n.getNodeValue()).intValue();
gotRowCount = true;
return (rowCount);
}
/* (non-Javadoc)
* @see javax.swing.table.TableModel#getValueAt(int, int)
*/
public Object getValueAt(int rowIndex, int columnIndex) {
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 + "[@" + ROW_NUMBER + "=" + rowIndex + "]/" +
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;
}
Object v;
try {
Class c = Class.forName(className).getClass();
Constructor cons = c.getConstructor(new Class[] {String.class});
v = cons.newInstance(new Object[] {n.getNodeValue()});
} catch (Exception e) {
log.logWarn(e.getMessage());
v = new String("[" + I18n.get("jfr.104", className) + "]");
}
return v;
}
private org.w3c.dom.Document transformToDOM(org.dom4j.Document doc) throws DocumentException {
DOMWriter writer = new DOMWriter();
return writer.write(doc);
}
}
|