KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > 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 License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * ------------------
27  * DatasetReader.java
28  * ------------------
29  * (C) Copyright 2002-2005, by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: DatasetReader.java,v 1.4 2005/03/15 17:23:43 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 20-Nov-2002 : Version 1 (DG);
39  *
40  */

41
42 package org.jfree.data.xml;
43
44 import java.io.File JavaDoc;
45 import java.io.FileInputStream JavaDoc;
46 import java.io.IOException JavaDoc;
47 import java.io.InputStream JavaDoc;
48
49 import javax.xml.parsers.ParserConfigurationException JavaDoc;
50 import javax.xml.parsers.SAXParser JavaDoc;
51 import javax.xml.parsers.SAXParserFactory JavaDoc;
52
53 import org.jfree.data.category.CategoryDataset;
54 import org.jfree.data.general.PieDataset;
55 import org.xml.sax.SAXException JavaDoc;
56
57 /**
58  * A utility class for reading datasets from XML.
59  */

60 public class DatasetReader {
61
62     /**
63      * Reads a {@link PieDataset} from an XML file.
64      *
65      * @param file the file.
66      *
67      * @return A dataset.
68      *
69      * @throws IOException if there is a problem reading the file.
70      */

71     public static PieDataset readPieDatasetFromXML(File JavaDoc file)
72         throws IOException JavaDoc {
73         InputStream JavaDoc in = new FileInputStream JavaDoc(file);
74         return readPieDatasetFromXML(in);
75     }
76
77     /**
78      * Reads a {@link PieDataset} from a stream.
79      *
80      * @param in the input stream.
81      *
82      * @return A dataset.
83      *
84      * @throws IOException if there is an I/O error.
85      */

86     public static PieDataset readPieDatasetFromXML(InputStream JavaDoc in)
87         throws IOException JavaDoc {
88
89         PieDataset result = null;
90         SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
91         try {
92             SAXParser JavaDoc parser = factory.newSAXParser();
93             PieDatasetHandler handler = new PieDatasetHandler();
94             parser.parse(in, handler);
95             result = handler.getDataset();
96         }
97         catch (SAXException JavaDoc e) {
98             System.out.println(e.getMessage());
99         }
100         catch (ParserConfigurationException JavaDoc e2) {
101             System.out.println(e2.getMessage());
102         }
103         return result;
104
105     }
106
107     /**
108      * Reads a {@link CategoryDataset} from a file.
109      *
110      * @param file the file.
111      *
112      * @return A dataset.
113      *
114      * @throws IOException if there is a problem reading the file.
115      */

116     public static CategoryDataset readCategoryDatasetFromXML(File JavaDoc file)
117         throws IOException JavaDoc {
118         InputStream JavaDoc in = new FileInputStream JavaDoc(file);
119         return readCategoryDatasetFromXML(in);
120     }
121
122     /**
123      * Reads a {@link CategoryDataset} from a stream.
124      *
125      * @param in the stream.
126      *
127      * @return A dataset.
128      *
129      * @throws IOException if there is a problem reading the file.
130      */

131     public static CategoryDataset readCategoryDatasetFromXML(InputStream JavaDoc in)
132         throws IOException JavaDoc {
133
134         CategoryDataset result = null;
135
136         SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
137         try {
138             SAXParser JavaDoc parser = factory.newSAXParser();
139             CategoryDatasetHandler handler = new CategoryDatasetHandler();
140             parser.parse(in, handler);
141             result = handler.getDataset();
142         }
143         catch (SAXException JavaDoc e) {
144             System.out.println(e.getMessage());
145         }
146         catch (ParserConfigurationException JavaDoc e2) {
147             System.out.println(e2.getMessage());
148         }
149         return result;
150
151     }
152
153 }
154
Popular Tags