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 }
098 catch (SAXException e) {
099 System.out.println(e.getMessage());
100 }
101 catch (ParserConfigurationException e2) {
102 System.out.println(e2.getMessage());
103 }
104 return result;
105
106 }
107
108 /**
109 * Reads a {@link CategoryDataset} from a file.
110 *
111 * @param file the file.
112 *
113 * @return A dataset.
114 *
115 * @throws IOException if there is a problem reading the file.
116 */
117 public static CategoryDataset readCategoryDatasetFromXML(File file)
118 throws IOException {
119 InputStream in = new FileInputStream(file);
120 return readCategoryDatasetFromXML(in);
121 }
122
123 /**
124 * Reads a {@link CategoryDataset} from a stream.
125 *
126 * @param in the stream.
127 *
128 * @return A dataset.
129 *
130 * @throws IOException if there is a problem reading the file.
131 */
132 public static CategoryDataset readCategoryDatasetFromXML(InputStream in)
133 throws IOException {
134
135 CategoryDataset result = null;
136
137 SAXParserFactory factory = SAXParserFactory.newInstance();
138 try {
139 SAXParser parser = factory.newSAXParser();
140 CategoryDatasetHandler handler = new CategoryDatasetHandler();
141 parser.parse(in, handler);
142 result = handler.getDataset();
143 }
144 catch (SAXException e) {
145 System.out.println(e.getMessage());
146 }
147 catch (ParserConfigurationException e2) {
148 System.out.println(e2.getMessage());
149 }
150 return result;
151
152 }
153
154 }