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 under the terms
  10. * of the GNU Lesser General Public License as published by the Free Software Foundation;
  11. * either version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License along with this
  18. * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. *
  21. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  22. * in the United States and other countries.]
  23. *
  24. * ------------------
  25. * DatasetReader.java
  26. * ------------------
  27. * (C) Copyright 2002-2005, by Object Refinery Limited.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: DatasetReader.java,v 1.3 2005/01/14 17:28:40 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 20-Nov-2002 : Version 1 (DG);
  37. *
  38. */
  39. package org.jfree.data.xml;
  40. import java.io.File;
  41. import java.io.FileInputStream;
  42. import java.io.IOException;
  43. import java.io.InputStream;
  44. import javax.xml.parsers.ParserConfigurationException;
  45. import javax.xml.parsers.SAXParser;
  46. import javax.xml.parsers.SAXParserFactory;
  47. import org.jfree.data.category.CategoryDataset;
  48. import org.jfree.data.general.PieDataset;
  49. import org.xml.sax.SAXException;
  50. /**
  51. * A utility class for reading datasets from XML.
  52. */
  53. public class DatasetReader {
  54. /**
  55. * Reads a {@link PieDataset} from an XML file.
  56. *
  57. * @param file the file.
  58. *
  59. * @return A dataset.
  60. *
  61. * @throws IOException if there is a problem reading the file.
  62. */
  63. public static PieDataset readPieDatasetFromXML(File file) throws IOException {
  64. InputStream in = new FileInputStream(file);
  65. return readPieDatasetFromXML(in);
  66. }
  67. /**
  68. * Reads a {@link PieDataset} from a stream.
  69. *
  70. * @param in the input stream.
  71. *
  72. * @return A dataset.
  73. *
  74. * @throws IOException if there is an I/O error.
  75. */
  76. public static PieDataset readPieDatasetFromXML(InputStream in) throws IOException {
  77. PieDataset result = null;
  78. SAXParserFactory factory = SAXParserFactory.newInstance();
  79. try {
  80. SAXParser parser = factory.newSAXParser();
  81. PieDatasetHandler handler = new PieDatasetHandler();
  82. parser.parse(in, handler);
  83. result = handler.getDataset();
  84. }
  85. catch (SAXException e) {
  86. System.out.println(e.getMessage());
  87. }
  88. catch (ParserConfigurationException e2) {
  89. System.out.println(e2.getMessage());
  90. }
  91. return result;
  92. }
  93. /**
  94. * Reads a {@link CategoryDataset} from a file.
  95. *
  96. * @param file the file.
  97. *
  98. * @return A dataset.
  99. *
  100. * @throws IOException if there is a problem reading the file.
  101. */
  102. public static CategoryDataset readCategoryDatasetFromXML(File file) throws IOException {
  103. InputStream in = new FileInputStream(file);
  104. return readCategoryDatasetFromXML(in);
  105. }
  106. /**
  107. * Reads a {@link CategoryDataset} from a stream.
  108. *
  109. * @param in the stream.
  110. *
  111. * @return A dataset.
  112. *
  113. * @throws IOException if there is a problem reading the file.
  114. */
  115. public static CategoryDataset readCategoryDatasetFromXML(InputStream in) throws IOException {
  116. CategoryDataset result = null;
  117. SAXParserFactory factory = SAXParserFactory.newInstance();
  118. try {
  119. SAXParser parser = factory.newSAXParser();
  120. CategoryDatasetHandler handler = new CategoryDatasetHandler();
  121. parser.parse(in, handler);
  122. result = handler.getDataset();
  123. }
  124. catch (SAXException e) {
  125. System.out.println(e.getMessage());
  126. }
  127. catch (ParserConfigurationException e2) {
  128. System.out.println(e2.getMessage());
  129. }
  130. return result;
  131. }
  132. }