comparison servlet/src/digilib/io/XMLListLoader.java @ 1:0ff3ede32060

Initial revision
author robcast
date Thu, 17 Jan 2002 15:25:46 +0100
parents
children 2ea78a56ecf8 9cedd170b581
comparison
equal deleted inserted replaced
0:ffd2df307e81 1:0ff3ede32060
1 /* XMLListLoader -- Load an XML list into a Hashtable
2
3 Digital Image Library servlet components
4
5 Copyright (C) 2001, 2002 Robert Casties (robcast@mail.berlios.de)
6
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2 of the License, or (at your
10 option) any later version.
11
12 Please read license.txt for the full details. A copy of the GPL
13 may be found at http://www.gnu.org/copyleft/lgpl.html
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 */
20
21 package digilib.io;
22
23 // JAXP packages
24 import javax.xml.parsers.*;
25 import org.xml.sax.*;
26 import org.xml.sax.helpers.*;
27
28 import java.util.*;
29 import java.io.*;
30
31 public class XMLListLoader {
32
33 private String listTag = "list";
34 private String entryTag = "entry";
35 private String keyAtt = "key";
36 private String valueAtt = "value";
37
38 public XMLListLoader() {
39 }
40
41 public XMLListLoader(String list_tag, String entry_tag, String key_att, String value_att) {
42 //System.out.println("xmlListLoader("+list_tag+","+entry_tag+","+key_att+","+value_att+")");
43 listTag = list_tag;
44 entryTag = entry_tag;
45 keyAtt = key_att;
46 valueAtt = value_att;
47 }
48
49 /**
50 * inner class XMLListParser to be called by the parser
51 */
52 private class XMLListParser extends DefaultHandler {
53
54 private Hashtable listData;
55 private Stack nameSpace;
56
57 public Hashtable getData() {
58 return listData;
59 }
60
61 // Parser calls this once at the beginning of a document
62 public void startDocument() throws SAXException {
63 listData = new Hashtable();
64 nameSpace = new Stack();
65 }
66
67 // Parser calls this for each element in a document
68 public void startElement(String namespaceURI, String localName,
69 String qName, Attributes atts)
70 throws SAXException
71 {
72 //System.out.println("<"+qName);
73 // open a new namespace
74 nameSpace.push(qName);
75
76 // ist it an entry tag?
77 if (qName.equals(entryTag)) {
78 // is it inside a list tag?
79 if ((listTag.length() > 0)&&(nameSpace.search(listTag) < 0)) {
80 System.out.println("BOO: Entry "+entryTag+" not inside list "+listTag);
81 throw new SAXParseException("Entry "+entryTag+" not inside list "+listTag, null);
82 }
83 // get the attributes
84 String key = atts.getValue(keyAtt);
85 String val = atts.getValue(valueAtt);
86 if ((key == null)||(val == null)) {
87 System.out.println("BOO: Entry "+entryTag+" does not have Attributes "+keyAtt+", "+valueAtt);
88 throw new SAXParseException("Entry "+entryTag+" does not have Attributes "+keyAtt+", "+valueAtt, null);
89 }
90 // add the values
91 //System.out.println("DATA: "+key+" = "+val);
92 listData.put(key, val);
93 }
94 }
95
96 public void endElement(String namespaceURI, String localName,
97 String qName)
98 throws SAXException
99 {
100 // exit the namespace
101 nameSpace.pop();
102 }
103
104 }
105
106
107 /**
108 * load and parse a file (as URL)
109 * returns Hashtable with list data
110 */
111 public Hashtable loadURL(String path) throws SAXException, IOException {
112 //System.out.println("loadurl ("+path+")");
113 // Create a JAXP SAXParserFactory and configure it
114 SAXParserFactory spf = SAXParserFactory.newInstance();
115 //spf.setNamespaceAware(true);
116
117 XMLReader xmlReader = null;
118 try {
119 // Create a JAXP SAXParser
120 SAXParser saxParser = spf.newSAXParser();
121
122 // Get the encapsulated SAX XMLReader
123 xmlReader = saxParser.getXMLReader();
124 }
125 catch (ParserConfigurationException e) {
126 throw new SAXException(e);
127 }
128
129 // create a list parser (keeps the data!)
130 XMLListParser listParser = new XMLListParser();
131
132 // Set the ContentHandler of the XMLReader
133 xmlReader.setContentHandler(listParser);
134
135 // Tell the XMLReader to parse the XML document
136 xmlReader.parse(path);
137
138 return listParser.getData();
139 }
140
141 }