001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-present, by David Gilbert 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 025 * Other names may be trademarks of their respective owners.] 026 * 027 * ------------------ 028 * DatasetReader.java 029 * ------------------ 030 * (C) Copyright 2002-present, by David Gilbert. 031 * 032 * Original Author: David Gilbert; 033 * Contributor(s): -; 034 * 035 */ 036 037package org.jfree.data.xml; 038 039import java.io.File; 040import java.io.FileInputStream; 041import java.io.IOException; 042import java.io.InputStream; 043 044import javax.xml.XMLConstants; 045import javax.xml.parsers.ParserConfigurationException; 046import javax.xml.parsers.SAXParser; 047import javax.xml.parsers.SAXParserFactory; 048 049import org.jfree.chart.util.Args; 050import org.jfree.data.category.CategoryDataset; 051import org.jfree.data.general.PieDataset; 052import org.xml.sax.SAXException; 053import org.xml.sax.SAXNotRecognizedException; 054import org.xml.sax.SAXNotSupportedException; 055 056/** 057 * A utility class for reading datasets from XML. 058 */ 059public class DatasetReader { 060 061 /** A factory for creating new parser instances. */ 062 static SAXParserFactory factory; 063 064 /** 065 * Returns the {@link SAXParserFactory} used to create {@link SAXParser} instances. 066 * 067 * @return The {@link SAXParserFactory} (never {@code null}). 068 */ 069 public static SAXParserFactory getSAXParserFactory() { 070 if (factory == null) { 071 SAXParserFactory f = SAXParserFactory.newInstance(); 072 try { 073 f.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); 074 f.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); 075 f.setFeature("http://xml.org/sax/features/external-general-entities", false); 076 factory = f; 077 } catch (SAXNotRecognizedException | SAXNotSupportedException | ParserConfigurationException e) { 078 throw new RuntimeException(e); 079 } 080 } 081 return factory; 082 } 083 084 /** 085 * Sets the SAXParserFactory that will be used to create SAXParser instances. 086 * You would only call this method if you wish to configure a new factory because 087 * the default does not meet requirements. 088 * 089 * @param f the new factory ({@code null} not permitted). 090 */ 091 public static void setSAXParserFactory(SAXParserFactory f) { 092 Args.nullNotPermitted(f, "f"); 093 factory = f; 094 } 095 096 /** 097 * Reads a {@link PieDataset} from an XML file. 098 * 099 * @param file the file ({@code null} not permitted). 100 * 101 * @return A dataset. 102 * 103 * @throws IOException if there is a problem reading the file. 104 */ 105 public static PieDataset readPieDatasetFromXML(File file) 106 throws IOException { 107 InputStream in = new FileInputStream(file); 108 return readPieDatasetFromXML(in); 109 } 110 111 /** 112 * Reads a {@link PieDataset} from a stream. 113 * 114 * @param in the input stream. 115 * 116 * @return A dataset. 117 * 118 * @throws IOException if there is an I/O error. 119 */ 120 public static PieDataset readPieDatasetFromXML(InputStream in) 121 throws IOException { 122 PieDataset result = null; 123 try { 124 SAXParser parser = getSAXParserFactory().newSAXParser(); 125 PieDatasetHandler handler = new PieDatasetHandler(); 126 parser.parse(in, handler); 127 result = handler.getDataset(); 128 } 129 catch (SAXException | ParserConfigurationException e) { 130 throw new RuntimeException(e); 131 } 132 return result; 133 } 134 135 /** 136 * Reads a {@link CategoryDataset} from a file. 137 * 138 * @param file the file. 139 * 140 * @return A dataset. 141 * 142 * @throws IOException if there is a problem reading the file. 143 */ 144 public static CategoryDataset readCategoryDatasetFromXML(File file) 145 throws IOException { 146 InputStream in = new FileInputStream(file); 147 return readCategoryDatasetFromXML(in); 148 } 149 150 /** 151 * Reads a {@link CategoryDataset} from a stream. 152 * 153 * @param in the stream. 154 * 155 * @return A dataset. 156 * 157 * @throws IOException if there is a problem reading the file. 158 */ 159 public static CategoryDataset readCategoryDatasetFromXML(InputStream in) 160 throws IOException { 161 CategoryDataset result = null; 162 try { 163 SAXParser parser = getSAXParserFactory().newSAXParser(); 164 CategoryDatasetHandler handler = new CategoryDatasetHandler(); 165 parser.parse(in, handler); 166 result = handler.getDataset(); 167 } 168 catch (SAXException | ParserConfigurationException e) { 169 throw new RuntimeException(e); 170 } 171 return result; 172 } 173 174}