001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * ------------------
028: * DatasetReader.java
029: * ------------------
030: * (C) Copyright 2002-2005, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: DatasetReader.java,v 1.4.2.1 2005/10/25 21:36:10 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 20-Nov-2002 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.data.xml;
044:
045: import java.io.File;
046: import java.io.FileInputStream;
047: import java.io.IOException;
048: import java.io.InputStream;
049:
050: import javax.xml.parsers.ParserConfigurationException;
051: import javax.xml.parsers.SAXParser;
052: import javax.xml.parsers.SAXParserFactory;
053:
054: import org.jfree.data.category.CategoryDataset;
055: import org.jfree.data.general.PieDataset;
056: import org.xml.sax.SAXException;
057:
058: /**
059: * A utility class for reading datasets from XML.
060: */
061: public class DatasetReader {
062:
063: /**
064: * Reads a {@link PieDataset} from an XML file.
065: *
066: * @param file the file.
067: *
068: * @return A dataset.
069: *
070: * @throws IOException if there is a problem reading the file.
071: */
072: public static PieDataset readPieDatasetFromXML(File file)
073: throws IOException {
074: InputStream in = new FileInputStream(file);
075: return readPieDatasetFromXML(in);
076: }
077:
078: /**
079: * Reads a {@link PieDataset} from a stream.
080: *
081: * @param in the input stream.
082: *
083: * @return A dataset.
084: *
085: * @throws IOException if there is an I/O error.
086: */
087: public static PieDataset readPieDatasetFromXML(InputStream in)
088: throws IOException {
089:
090: PieDataset result = null;
091: SAXParserFactory factory = SAXParserFactory.newInstance();
092: try {
093: SAXParser parser = factory.newSAXParser();
094: PieDatasetHandler handler = new PieDatasetHandler();
095: parser.parse(in, handler);
096: result = handler.getDataset();
097: } catch (SAXException e) {
098: System.out.println(e.getMessage());
099: } catch (ParserConfigurationException e2) {
100: System.out.println(e2.getMessage());
101: }
102: return result;
103:
104: }
105:
106: /**
107: * Reads a {@link CategoryDataset} from a file.
108: *
109: * @param file the file.
110: *
111: * @return A dataset.
112: *
113: * @throws IOException if there is a problem reading the file.
114: */
115: public static CategoryDataset readCategoryDatasetFromXML(File file)
116: throws IOException {
117: InputStream in = new FileInputStream(file);
118: return readCategoryDatasetFromXML(in);
119: }
120:
121: /**
122: * Reads a {@link CategoryDataset} from a stream.
123: *
124: * @param in the stream.
125: *
126: * @return A dataset.
127: *
128: * @throws IOException if there is a problem reading the file.
129: */
130: public static CategoryDataset readCategoryDatasetFromXML(
131: InputStream in) throws IOException {
132:
133: CategoryDataset result = null;
134:
135: SAXParserFactory factory = SAXParserFactory.newInstance();
136: try {
137: SAXParser parser = factory.newSAXParser();
138: CategoryDatasetHandler handler = new CategoryDatasetHandler();
139: parser.parse(in, handler);
140: result = handler.getDataset();
141: } catch (SAXException e) {
142: System.out.println(e.getMessage());
143: } catch (ParserConfigurationException e2) {
144: System.out.println(e2.getMessage());
145: }
146: return result;
147:
148: }
149:
150: }
|