PageRenderTime 32ms CodeModel.GetById 9ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

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