001: /*
002: * (C) Copyright 2000 - 2003 Nabh Information Systems, Inc.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019:
020: package com.nabhinc.portlet.chart;
021:
022: import java.util.List;
023:
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.net.MalformedURLException;
027: import java.net.URL;
028: import java.util.ArrayList;
029: import java.util.Vector;
030:
031: import javax.portlet.PortletException;
032: import javax.xml.parsers.DocumentBuilder;
033: import javax.xml.parsers.DocumentBuilderFactory;
034: import javax.xml.parsers.ParserConfigurationException;
035:
036: import com.nabhinc.util.StringUtil;
037: import com.nabhinc.util.XMLUtil;
038:
039: import org.w3c.dom.Document;
040: import org.w3c.dom.Element;
041: import org.xml.sax.InputSource;
042: import org.xml.sax.SAXException;
043:
044: import org.jfree.data.CategoryDataset;
045: import org.jfree.data.DefaultKeyedValues2D;
046:
047: /**
048: *
049: *
050: * @author Padmanabh Dabke
051: * (c) 2002, 2003 Nabh Information Systems, Inc. All Rights Reserved.
052: */
053: public class XMLCategoryDataset extends BaseXMLDataset implements
054: CategoryDataset {
055:
056: private static final long serialVersionUID = 2285053009607685916L;
057:
058: private String[] xcdsCategoryArray = null;
059: private DefaultKeyedValues2D xcds2DData = new DefaultKeyedValues2D();
060:
061: /**
062: * @see com.nabhinc.portal.charting.BaseJDBCDataSet#executeQuery()
063: */
064: public void executeQuery() throws PortletException {
065: Object xObject = null;
066: int column = 0;
067: int currentColumn = 0;
068: int numberOfColumns = 0;
069: int numberOfValidColumns = 0;
070: int columnTypes[] = null;
071: List categoryNames = new ArrayList();
072: Vector rows = new Vector();
073: Number[] newRow;
074:
075: xcds2DData = new DefaultKeyedValues2D();
076:
077: try {
078: InputStream fis = new URL(bxdsXMLURL).openStream();
079: InputSource source = new InputSource(fis);
080: DocumentBuilder parser = DocumentBuilderFactory
081: .newInstance().newDocumentBuilder();
082:
083: Document d = parser.parse(source);
084: Element root = d.getDocumentElement();
085:
086: String cats = XMLUtil.getSubElementText(root, "categories");
087: if (cats != null) {
088: xcdsCategoryArray = StringUtil.split(cats, ",");
089: }
090:
091: Element[] seriesElems = XMLUtil.getSubElements(root,
092: "series");
093:
094: for (int i = 0; i < seriesElems.length; i++) {
095: String seriesName = seriesElems[i].getAttribute("name");
096: Element[] valueElems = XMLUtil.getSubElements(
097: seriesElems[i], "value");
098: for (int j = 0; j < valueElems.length; j++) {
099: xcds2DData.setValue(Double.valueOf(XMLUtil
100: .getChildCharacterData(valueElems[j])),
101: seriesName, xcdsCategoryArray[j]);
102: }
103: }
104: } catch (MalformedURLException mue) {
105: throw new PortletException("Invalid XML document URL: "
106: + bxdsXMLURL + ".", mue);
107: } catch (ParserConfigurationException pce) {
108: throw new PortletException("Bad XML parser configuration.",
109: pce);
110: } catch (IOException ioe) {
111: throw new PortletException("IO exception.", ioe);
112: } catch (SAXException se) {
113: throw new PortletException("SAX exception.", se);
114: }
115:
116: }
117:
118: /**
119: * @see com.nabhinc.portal.charting.BaseJDBCDataSet#createDummyData()
120: */
121: public void createDummyData() {
122:
123: if (xcds2DData == null) {
124: xcds2DData = new DefaultKeyedValues2D();
125:
126: xcdsCategoryArray = new String[] { "mon", "tue", "wed",
127: "thu", "fri", "sat", "sun" };
128: String[] xcdsSeriesArray = new String[] { "test1.jsp",
129: "test2.jsp", "test3.jsp", "test4.jsp" };
130:
131: for (int i = 0; i < xcdsCategoryArray.length; i++) {
132: int lastY = (int) (Math.random() * 1000 + 1000);
133: for (int j = 0; j < xcdsSeriesArray.length; j++) {
134: final int y = lastY
135: + (int) (Math.random() * 200 - 100);
136: lastY = y;
137: xcds2DData.setValue(new Integer(y),
138: xcdsSeriesArray[j], xcdsCategoryArray[i]);
139: }
140: }
141: }
142:
143: }
144:
145: /* (non-Javadoc)
146: * @see org.jfree.data.KeyedValues2D#getRowKey(int)
147: */
148: public Comparable getRowKey(int arg0) {
149: return xcds2DData.getRowKey(arg0);
150: }
151:
152: /* (non-Javadoc)
153: * @see org.jfree.data.KeyedValues2D#getRowIndex(java.lang.Comparable)
154: */
155: public int getRowIndex(Comparable arg0) {
156: return xcds2DData.getRowIndex(arg0);
157: }
158:
159: /* (non-Javadoc)
160: * @see org.jfree.data.KeyedValues2D#getRowKeys()
161: */
162: public List getRowKeys() {
163: return xcds2DData.getRowKeys();
164: }
165:
166: /* (non-Javadoc)
167: * @see org.jfree.data.KeyedValues2D#getColumnKey(int)
168: */
169: public Comparable getColumnKey(int arg0) {
170: return xcds2DData.getColumnKey(arg0);
171: }
172:
173: /* (non-Javadoc)
174: * @see org.jfree.data.KeyedValues2D#getColumnIndex(java.lang.Comparable)
175: */
176: public int getColumnIndex(Comparable arg0) {
177: return xcds2DData.getColumnIndex(arg0);
178: }
179:
180: /* (non-Javadoc)
181: * @see org.jfree.data.KeyedValues2D#getColumnKeys()
182: */
183: public List getColumnKeys() {
184: return xcds2DData.getColumnKeys();
185: }
186:
187: /* (non-Javadoc)
188: * @see org.jfree.data.KeyedValues2D#getValue(java.lang.Comparable, java.lang.Comparable)
189: */
190: public Number getValue(Comparable arg0, Comparable arg1) {
191: return xcds2DData.getValue(arg0, arg1);
192: }
193:
194: /* (non-Javadoc)
195: * @see org.jfree.data.Values2D#getRowCount()
196: */
197: public int getRowCount() {
198: return xcds2DData.getRowCount();
199: }
200:
201: /* (non-Javadoc)
202: * @see org.jfree.data.Values2D#getColumnCount()
203: */
204: public int getColumnCount() {
205: return xcds2DData.getColumnCount();
206: }
207:
208: /* (non-Javadoc)
209: * @see org.jfree.data.Values2D#getValue(int, int)
210: */
211: public Number getValue(int arg0, int arg1) {
212: return xcds2DData.getValue(arg0, arg1);
213: }
214: }
|