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-2007, 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-2007, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * Changes
  36:  * -------
  37:  * 20-Nov-2002 : Version 1 (DG);
  38:  *
  39:  */
  40: 
  41: package org.jfree.data.xml;
  42: 
  43: import java.io.File;
  44: import java.io.FileInputStream;
  45: import java.io.IOException;
  46: import java.io.InputStream;
  47: 
  48: import javax.xml.parsers.ParserConfigurationException;
  49: import javax.xml.parsers.SAXParser;
  50: import javax.xml.parsers.SAXParserFactory;
  51: 
  52: import org.jfree.data.category.CategoryDataset;
  53: import org.jfree.data.general.PieDataset;
  54: import org.xml.sax.SAXException;
  55: 
  56: /**
  57:  * A utility class for reading datasets from XML.
  58:  */
  59: public class DatasetReader {
  60: 
  61:     /**
  62:      * Reads a {@link PieDataset} from an XML file.
  63:      *
  64:      * @param file  the file.
  65:      *
  66:      * @return A dataset.
  67:      *
  68:      * @throws IOException if there is a problem reading the file.
  69:      */
  70:     public static PieDataset readPieDatasetFromXML(File file) 
  71:         throws IOException {
  72:         InputStream in = new FileInputStream(file);
  73:         return readPieDatasetFromXML(in);
  74:     }
  75: 
  76:     /**
  77:      * Reads a {@link PieDataset} from a stream.
  78:      *
  79:      * @param in  the input stream.
  80:      *
  81:      * @return A dataset.
  82:      *
  83:      * @throws IOException if there is an I/O error.
  84:      */
  85:     public static PieDataset readPieDatasetFromXML(InputStream in) 
  86:         throws IOException {
  87: 
  88:         PieDataset result = null;
  89:         SAXParserFactory factory = SAXParserFactory.newInstance();
  90:         try {
  91:             SAXParser parser = factory.newSAXParser();
  92:             PieDatasetHandler handler = new PieDatasetHandler();
  93:             parser.parse(in, handler);
  94:             result = handler.getDataset();
  95:         }
  96:         catch (SAXException e) {
  97:             System.out.println(e.getMessage());
  98:         }
  99:         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: }