view servlet/src/digilib/io/XMLListLoader.java @ 140:c878ea574c29

Servlet Version 1.12b2 - fixed small error in XML config parser - improved high quality scaling with minSubsample parameter
author robcast
date Fri, 15 Aug 2003 23:52:20 +0200
parents d18b0ff52b07
children 67ff8c7fecb9
line wrap: on
line source

/* XMLListLoader -- Load an XML list into a Hashtable

  Digital Image Library servlet components

  Copyright (C) 2001, 2002 Robert Casties (robcast@mail.berlios.de)

  This program is free software; you can redistribute  it and/or modify it
  under  the terms of  the GNU General  Public License as published by the
  Free Software Foundation;  either version 2 of the  License, or (at your
  option) any later version.
   
  Please read license.txt for the full details. A copy of the GPL
  may be found at http://www.gnu.org/copyleft/lgpl.html

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/

package digilib.io;

// JAXP packages
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

public class XMLListLoader {

	private String listTag = "list";
	private String entryTag = "entry";
	private String keyAtt = "key";
	private String valueAtt = "value";

	public XMLListLoader() {
	}

	public XMLListLoader(
		String list_tag,
		String entry_tag,
		String key_att,
		String value_att) {
		//System.out.println("xmlListLoader("+list_tag+","+entry_tag+","+key_att+","+value_att+")");
		listTag = list_tag;
		entryTag = entry_tag;
		keyAtt = key_att;
		valueAtt = value_att;
	}

	/**
	 *  inner class XMLListParser to be called by the parser
	 */
	private class XMLListParser extends DefaultHandler {

		private HashMap listData;
		private LinkedList tagSpace;

		public HashMap getData() {
			return listData;
		}

		// Parser calls this once at the beginning of a document
		public void startDocument() throws SAXException {
			listData = new HashMap();
			tagSpace = new LinkedList();
		}

		// Parser calls this for each element in a document
		public void startElement(
			String namespaceURI,
			String localName,
			String qName,
			Attributes atts)
			throws SAXException {
			//System.out.println("<"+qName);
			// open a new namespace
			tagSpace.addLast(qName);

			// ist it an entry tag?
			if (qName.equals(entryTag)) {
				// is it inside a list tag?
				if ((listTag.length() > 0) && (!tagSpace.contains(listTag))) {
					System.out.println(
						"BOO: Entry "
							+ entryTag
							+ " not inside list "
							+ listTag);
					throw new SAXParseException(
						"Entry " + entryTag + " not inside list " + listTag,
						null);
				}
				// get the attributes
				String key = atts.getValue(keyAtt);
				String val = atts.getValue(valueAtt);
				if ((key == null) || (val == null)) {
					System.out.println(
						"BOO: Entry "
							+ entryTag
							+ " does not have Attributes "
							+ keyAtt
							+ ", "
							+ valueAtt);
					throw new SAXParseException(
						"Entry "
							+ entryTag
							+ " does not have Attributes "
							+ keyAtt
							+ ", "
							+ valueAtt,
						null);
				}
				// add the values
				//System.out.println("DATA: "+key+" = "+val);
				listData.put(key, val);
			}
		}

		public void endElement(
			String namespaceURI,
			String localName,
			String qName)
			throws SAXException {
			// exit the namespace
			tagSpace.removeLast();
		}

	}

	/**
	 *  load and parse a file (as URL)
	 *    returns HashMap with list data
	 */
	public HashMap loadURL(String path) throws SAXException, IOException {
		//System.out.println("loadurl ("+path+")");
		// Create a JAXP SAXParserFactory and configure it
		SAXParserFactory spf = SAXParserFactory.newInstance();
		spf.setNamespaceAware(true);

		SAXParser parser = null;
		try {
			// Create a JAXP SAXParser
			parser = spf.newSAXParser();

		} catch (ParserConfigurationException e) {
			throw new SAXException(e);
		}

		// create a list parser (keeps the data!)
		XMLListParser listParser = new XMLListParser();

		// Tell the SAXParser to parse the XML document
		parser.parse(path, listParser);

		return listParser.getData();
	}

}