/edu/uncc/parsets/data/old/MetaDataParser.java

https://code.google.com/p/parsets/ · Java · 121 lines · 67 code · 21 blank · 33 comment · 27 complexity · c2a5e39a4c5650654c9021a30a094a7e MD5 · raw file

  1. package edu.uncc.parsets.data.old;
  2. import javax.xml.parsers.SAXParser;
  3. import javax.xml.parsers.SAXParserFactory;
  4. import org.xml.sax.Attributes;
  5. import org.xml.sax.helpers.DefaultHandler;
  6. import edu.uncc.parsets.data.DataType;
  7. import edu.uncc.parsets.util.PSLogging;
  8. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
  9. * Copyright (c) 2009, Robert Kosara, Caroline Ziemkiewicz,
  10. * and others (see Authors.txt for full list)
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are met:
  15. *
  16. * * Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * * Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. * * Neither the name of UNC Charlotte nor the names of its contributors
  22. * may be used to endorse or promote products derived from this software
  23. * without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY ITS AUTHORS ''AS IS'' AND ANY
  26. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  27. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28. * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  29. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  30. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  32. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  34. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  36. /**
  37. * Parse metadata in XML format. This is currently only for bulk conversion.
  38. */
  39. public class MetaDataParser {
  40. private CSVDataSet dataSet;
  41. private class MetaXMLHandler extends DefaultHandler {
  42. private String categoryKey;
  43. private StringBuilder characters = new StringBuilder();
  44. private DataDimension currentDimension;
  45. public void startElement(String uri, String localName, String qName, Attributes atts) {
  46. if (qName.equals("cat")) {
  47. categoryKey = atts.getValue("id").trim();
  48. characters.setLength(0);
  49. } else if (qName.equals("dim")) {
  50. String dimKey = atts.getValue("id").trim();
  51. DataType type = DataType.categorical;
  52. String typeString = atts.getValue("type");
  53. if (typeString != null)
  54. type = DataType.typeFromString(typeString);
  55. currentDimension = new DataDimension(dimKey, type);
  56. dataSet.addDimension(dimKey, currentDimension);
  57. } else if (qName.equals("name")) {
  58. characters.setLength(0);
  59. } else if (qName.equals("desc")) {
  60. characters.setLength(0);
  61. } else if (qName.equals("meta")) {
  62. if (atts.getValue("name") != null)
  63. dataSet.setName(atts.getValue("name"));
  64. if (atts.getValue("section") != null)
  65. dataSet.setSection(atts.getValue("section"));
  66. if (atts.getValue("source") != null)
  67. dataSet.setSource(atts.getValue("source"));
  68. if (atts.getValue("srcURL") != null)
  69. dataSet.setSourceURL(atts.getValue("srcURL"));
  70. } else if (qName.equals("user")) {
  71. // ignore for now
  72. } else {
  73. PSLogging.logger.warn("Unknown tag encountered: "+qName);
  74. }
  75. }
  76. public void endElement(String uri, String localName, String qName) {
  77. if (qName.equals("cat")) {
  78. currentDimension.addCategory(categoryKey, characters.toString().trim());
  79. } else if (qName.equals("name")) {
  80. currentDimension.setName(characters.toString().trim());
  81. } else if (qName.equals("desc")) {
  82. // ignore
  83. }
  84. }
  85. public void characters(char[] ch, int start, int length) {
  86. characters.append(ch, start, length);
  87. }
  88. }
  89. public boolean parse(CSVDataSet csvDataSet, String filename) {
  90. dataSet = csvDataSet;
  91. try {
  92. SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
  93. parser.parse(filename, new MetaXMLHandler());
  94. } catch (Exception e) {
  95. PSLogging.logger.error("Error parsing metadata file "+filename, e);
  96. return false;
  97. }
  98. return true;
  99. }
  100. }