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.io.IOException;
023: import java.io.InputStream;
024: import java.net.MalformedURLException;
025: import java.net.URL;
026: import java.util.ArrayList;
027: import java.util.List;
028: import java.util.Vector;
029:
030: import javax.portlet.PortletException;
031: import javax.xml.parsers.DocumentBuilder;
032: import javax.xml.parsers.DocumentBuilderFactory;
033: import javax.xml.parsers.ParserConfigurationException;
034:
035: import org.w3c.dom.Document;
036: import org.w3c.dom.Element;
037: import org.xml.sax.InputSource;
038: import org.xml.sax.SAXException;
039:
040: import org.jfree.chart.tooltips.PieToolTipGenerator;
041: import org.jfree.chart.urls.PieURLGenerator;
042: import org.jfree.data.DefaultKeyedValues;
043: import org.jfree.data.PieDataset;
044:
045: import com.nabhinc.util.StringUtil;
046: import com.nabhinc.util.XMLUtil;
047:
048: /**
049: *
050: *
051: * @author Padmanabh Dabke
052: * (c) 2002, 2003 Nabh Information Systems, Inc. All Rights Reserved.
053: */
054: public class XMLPieDataset extends BaseXMLDataset implements
055: PieDataset, PieToolTipGenerator, PieURLGenerator {
056:
057: private static final long serialVersionUID = -3161347476078485479L;
058:
059: private DefaultKeyedValues xcdsPieData = null;
060:
061: /**
062: * Dummy data.
063: */
064: private String[] jpdsDummyData = { "mon", "tue", "wed", "thu",
065: "fri", "sat", "sun" };
066:
067: /**
068: * Categories specified in configuration.
069: * @see com.nabhinc.core.XMLInitable#init(Element)
070: */
071: private String[] xcdsConfiguredCategories = null;
072:
073: /**
074: * @see com.nabhinc.portal.charting.BaseJDBCDataSet#createDummyData()
075: */
076: public void createDummyData() {
077: if (xcdsPieData == null) {
078: xcdsPieData = new DefaultKeyedValues();
079: for (int i = 0; i < jpdsDummyData.length; i++) {
080: xcdsPieData.setValue(jpdsDummyData[i], new Integer(i
081: * i - i + 1));
082: }
083: }
084: }
085:
086: /**
087: * @see com.nabhinc.portal.charting.BaseXMLDataSet#populateDataSet()
088: */
089: public void executeQuery() throws PortletException {
090:
091: Object xObject = null;
092: int column = 0;
093: int currentColumn = 0;
094: int numberOfColumns = 0;
095: int numberOfValidColumns = 0;
096: int columnTypes[] = null;
097: List categoryNames = new ArrayList();
098: Vector rows = new Vector();
099: Number[] newRow;
100:
101: try {
102: xcdsPieData = new DefaultKeyedValues();
103: InputStream fis = new URL(bxdsXMLURL).openStream();
104: InputSource source = new InputSource(fis);
105: DocumentBuilder parser = DocumentBuilderFactory
106: .newInstance().newDocumentBuilder();
107:
108: Document d = parser.parse(source);
109: Element root = d.getDocumentElement();
110:
111: String cats = XMLUtil.getSubElementText(root, "categories");
112: if (cats != null) {
113: xcdsConfiguredCategories = StringUtil.split(cats, ",");
114: }
115:
116: Element[] seriesElems = XMLUtil.getSubElements(root,
117: "series");
118:
119: for (int i = 0; i < seriesElems.length; i++) {
120: String seriesName = seriesElems[i].getAttribute("name");
121: Element[] valueElems = XMLUtil.getSubElements(
122: seriesElems[i], "value");
123: for (int j = 0; j < valueElems.length; j++) {
124: xcdsPieData
125: .setValue(
126: (Comparable) seriesName,
127: Double
128: .valueOf(XMLUtil
129: .getChildCharacterData(valueElems[j])));
130: }
131:
132: }
133: fireDatasetChanged();
134: } catch (MalformedURLException mue) {
135: throw new PortletException("Invalid XML document URL: "
136: + bxdsXMLURL + ".", mue);
137: } catch (ParserConfigurationException pce) {
138: throw new PortletException("Bad XML parser configuration.",
139: pce);
140: } catch (IOException ioe) {
141: throw new PortletException("IO exception.", ioe);
142: } catch (SAXException se) {
143: throw new PortletException("SAX exception.", se);
144: }
145:
146: }
147:
148: /**
149: * @see de.laures.cewolf.PieSectionLinkGenerator#generateLink(java.lang.Object, java.lang.Object)
150: */
151: public String generateLink(Object dataset, Object category) {
152: return "";
153: }
154:
155: /**
156: * @see com.jrefinery.chart.tooltips.PieToolTipGenerator#generateToolTip(com.jrefinery.data.PieDataset, java.lang.Object)
157: */
158: public String generateToolTip(PieDataset data, Object category) {
159: return category.toString();
160: }
161:
162: /**
163: * @see org.jfree.chart.urls.PieURLGenerator#generateURL(com.jrefinery.data.PieDataset, java.lang.Object)
164: */
165: public String generateURL(PieDataset data, Object category) {
166: return "http://java.sun.com?cat=" + category.toString();
167: }
168:
169: /**
170: * @see org.jfree.data.KeyedValues#getKey(int)
171: */
172: public Comparable getKey(int arg0) {
173: return xcdsPieData.getKey(arg0);
174: }
175:
176: /**
177: * @see org.jfree.data.KeyedValues#getIndex(java.lang.Comparable)
178: */
179: public int getIndex(Comparable arg0) {
180: return xcdsPieData.getIndex(arg0);
181: }
182:
183: /**
184: * @see org.jfree.data.KeyedValues#getValue(java.lang.Comparable)
185: */
186: public Number getValue(Comparable arg0) {
187: return xcdsPieData.getValue(arg0);
188: }
189:
190: /**
191: * @see org.jfree.data.Values#getItemCount()
192: */
193: public int getItemCount() {
194: return xcdsPieData.getItemCount();
195:
196: }
197:
198: /**
199: * @see org.jfree.chart.tooltips.PieToolTipGenerator#generateToolTip(org.jfree.data.PieDataset, java.lang.Comparable, int)
200: */
201: public String generateToolTip(PieDataset arg0, Comparable arg1,
202: int arg2) {
203: return "";
204: }
205:
206: /**
207: * @see org.jfree.chart.urls.PieURLGenerator#generateURL(org.jfree.data.PieDataset, java.lang.Comparable, int)
208: */
209: public String generateURL(PieDataset arg0, Comparable arg1, int arg2) {
210: return "";
211: }
212:
213: /**
214: * @see org.jfree.data.KeyedValues#getKeys()
215: */
216: public List getKeys() {
217: return xcdsPieData.getKeys();
218:
219: }
220:
221: /**
222: * @see org.jfree.data.Values#getValue(int)
223: */
224: public Number getValue(int item) {
225: return xcdsPieData.getValue(item);
226: }
227: }
|