Source for org.jfree.data.xml.DatasetReader

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
   6:  *
   7:  * Project Info:  http://www.jfree.org/jfreechart/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  *
  27:  * ------------------
  28:  * DatasetReader.java
  29:  * ------------------
  30:  * (C) Copyright 2002-2005, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: DatasetReader.java,v 1.4.2.1 2005/10/25 21:36:10 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 20-Nov-2002 : Version 1 (DG);
  40:  *
  41:  */
  42: 
  43: package org.jfree.data.xml;
  44: 
  45: import java.io.File;
  46: import java.io.FileInputStream;
  47: import java.io.IOException;
  48: import java.io.InputStream;
  49: 
  50: import javax.xml.parsers.ParserConfigurationException;
  51: import javax.xml.parsers.SAXParser;
  52: import javax.xml.parsers.SAXParserFactory;
  53: 
  54: import org.jfree.data.category.CategoryDataset;
  55: import org.jfree.data.general.PieDataset;
  56: import org.xml.sax.SAXException;
  57: 
  58: /**
  59:  * A utility class for reading datasets from XML.
  60:  */
  61: public class DatasetReader {
  62: 
  63:     /**
  64:      * Reads a {@link PieDataset} from an XML file.
  65:      *
  66:      * @param file  the file.
  67:      *
  68:      * @return A dataset.
  69:      *
  70:      * @throws IOException if there is a problem reading the file.
  71:      */
  72:     public static PieDataset readPieDatasetFromXML(File file) 
  73:         throws IOException {
  74:         InputStream in = new FileInputStream(file);
  75:         return readPieDatasetFromXML(in);
  76:     }
  77: 
  78:     /**
  79:      * Reads a {@link PieDataset} from a stream.
  80:      *
  81:      * @param in  the input stream.
  82:      *
  83:      * @return A dataset.
  84:      *
  85:      * @throws IOException if there is an I/O error.
  86:      */
  87:     public static PieDataset readPieDatasetFromXML(InputStream in) 
  88:         throws IOException {
  89: 
  90:         PieDataset result = null;
  91:         SAXParserFactory factory = SAXParserFactory.newInstance();
  92:         try {
  93:             SAXParser parser = factory.newSAXParser();
  94:             PieDatasetHandler handler = new PieDatasetHandler();
  95:             parser.parse(in, handler);
  96:             result = handler.getDataset();
  97:         }
  98:         catch (SAXException e) {
  99:             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: }