    001    /* ===========================================================
    002     * JFreeChart : a free chart library for the Java(tm) platform
    003     * ===========================================================
    004     *
    005     * (C) Copyright 2000-2007, 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-2007, by Object Refinery Limited.
    031     *
    032     * Original Author:  David Gilbert (for Object Refinery Limited);
    033     * Contributor(s):   -;
    034     *
    035     * Changes
    036     * -------
    037     * 20-Nov-2002 : Version 1 (DG);
    038     *
    039     */
    040
    041    package org.jfree.data.xml;
    042
    043    import java.io.File;
    044    import java.io.FileInputStream;
    045    import java.io.IOException;
    046    import java.io.InputStream;
    047
    048    import javax.xml.parsers.ParserConfigurationException;
    049    import javax.xml.parsers.SAXParser;
    050    import javax.xml.parsers.SAXParserFactory;
    051
    052    import org.jfree.data.category.CategoryDataset;
    053    import org.jfree.data.general.PieDataset;
    054    import org.xml.sax.SAXException;
    055
    056    /**
    057     * A utility class for reading datasets from XML.
    058     */
    059    public class DatasetReader {
    060
    061        /**
    062         * Reads a {@link PieDataset} from an XML file.
    063         *
    064         * @param file  the file.
    065         *
    066         * @return A dataset.
    067         *
    068         * @throws IOException if there is a problem reading the file.
    069         */
    070        public static PieDataset readPieDatasetFromXML(File file)
    071            throws IOException {
    072            InputStream in = new FileInputStream(file);
    073            return readPieDatasetFromXML(in);
    074        }
    075
    076        /**
    077         * Reads a {@link PieDataset} from a stream.
    078         *
    079         * @param in  the input stream.
    080         *
    081         * @return A dataset.
    082         *
    083         * @throws IOException if there is an I/O error.
    084         */
    085        public static PieDataset readPieDatasetFromXML(InputStream in)
    086            throws IOException {
    087
    088            PieDataset result = null;
    089            SAXParserFactory factory = SAXParserFactory.newInstance();
    090            try {
    091                SAXParser parser = factory.newSAXParser();
    092                PieDatasetHandler handler = new PieDatasetHandler();
    093                parser.parse(in, handler);
    094                result = handler.getDataset();
    095            }
    096            catch (SAXException e) {
    097                System.out.println(e.getMessage());
    098            }
    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(InputStream in)
    131            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            }
    142            catch (SAXException e) {
    143                System.out.println(e.getMessage());
    144            }
    145            catch (ParserConfigurationException e2) {
    146                System.out.println(e2.getMessage());
    147            }
    148            return result;
    149
    150        }
    151
    152    }