1
|
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
|
88
|
24 import java.io.IOException;
|
|
25 import java.util.HashMap;
|
|
26 import java.util.LinkedList;
|
1
|
27
|
88
|
28 import javax.xml.parsers.ParserConfigurationException;
|
|
29 import javax.xml.parsers.SAXParser;
|
|
30 import javax.xml.parsers.SAXParserFactory;
|
|
31
|
|
32 import org.xml.sax.Attributes;
|
|
33 import org.xml.sax.SAXException;
|
|
34 import org.xml.sax.SAXParseException;
|
140
|
35 import org.xml.sax.helpers.DefaultHandler;
|
1
|
36
|
176
|
37 /** Loads a simple XML list into a HashMap.
|
|
38 *
|
|
39 * The XML file has an outer <code>list_tag</code>. Every entry is an
|
|
40 * <code>entry_tag</code> with two attributes: the <code>key_att</code>
|
|
41 * key and the <code>value_att</code> value.
|
|
42 *
|
|
43 * The file is read by the <code>loadURL</code> method, that returns a
|
|
44 * HashMap with the key-value pairs.
|
|
45 *
|
|
46 * @author casties
|
|
47 */
|
1
|
48 public class XMLListLoader {
|
|
49
|
88
|
50 private String listTag = "list";
|
|
51 private String entryTag = "entry";
|
|
52 private String keyAtt = "key";
|
|
53 private String valueAtt = "value";
|
1
|
54
|
88
|
55 public XMLListLoader() {
|
|
56 }
|
1
|
57
|
88
|
58 public XMLListLoader(
|
|
59 String list_tag,
|
|
60 String entry_tag,
|
|
61 String key_att,
|
|
62 String value_att) {
|
|
63 //System.out.println("xmlListLoader("+list_tag+","+entry_tag+","+key_att+","+value_att+")");
|
|
64 listTag = list_tag;
|
|
65 entryTag = entry_tag;
|
|
66 keyAtt = key_att;
|
|
67 valueAtt = value_att;
|
|
68 }
|
1
|
69
|
88
|
70 /**
|
|
71 * inner class XMLListParser to be called by the parser
|
|
72 */
|
140
|
73 private class XMLListParser extends DefaultHandler {
|
88
|
74
|
|
75 private HashMap listData;
|
138
|
76 private LinkedList tagSpace;
|
88
|
77
|
|
78 public HashMap getData() {
|
|
79 return listData;
|
|
80 }
|
1
|
81
|
88
|
82 // Parser calls this once at the beginning of a document
|
|
83 public void startDocument() throws SAXException {
|
|
84 listData = new HashMap();
|
138
|
85 tagSpace = new LinkedList();
|
88
|
86 }
|
1
|
87
|
88
|
88 // Parser calls this for each element in a document
|
|
89 public void startElement(
|
|
90 String namespaceURI,
|
|
91 String localName,
|
|
92 String qName,
|
|
93 Attributes atts)
|
|
94 throws SAXException {
|
|
95 //System.out.println("<"+qName);
|
|
96 // open a new namespace
|
138
|
97 tagSpace.addLast(qName);
|
1
|
98
|
88
|
99 // ist it an entry tag?
|
|
100 if (qName.equals(entryTag)) {
|
|
101 // is it inside a list tag?
|
138
|
102 if ((listTag.length() > 0) && (!tagSpace.contains(listTag))) {
|
88
|
103 System.out.println(
|
|
104 "BOO: Entry "
|
|
105 + entryTag
|
|
106 + " not inside list "
|
|
107 + listTag);
|
|
108 throw new SAXParseException(
|
|
109 "Entry " + entryTag + " not inside list " + listTag,
|
|
110 null);
|
|
111 }
|
|
112 // get the attributes
|
|
113 String key = atts.getValue(keyAtt);
|
|
114 String val = atts.getValue(valueAtt);
|
|
115 if ((key == null) || (val == null)) {
|
|
116 System.out.println(
|
|
117 "BOO: Entry "
|
|
118 + entryTag
|
|
119 + " does not have Attributes "
|
|
120 + keyAtt
|
|
121 + ", "
|
|
122 + valueAtt);
|
|
123 throw new SAXParseException(
|
|
124 "Entry "
|
|
125 + entryTag
|
|
126 + " does not have Attributes "
|
|
127 + keyAtt
|
|
128 + ", "
|
|
129 + valueAtt,
|
|
130 null);
|
|
131 }
|
|
132 // add the values
|
|
133 //System.out.println("DATA: "+key+" = "+val);
|
|
134 listData.put(key, val);
|
|
135 }
|
|
136 }
|
1
|
137
|
88
|
138 public void endElement(
|
|
139 String namespaceURI,
|
|
140 String localName,
|
|
141 String qName)
|
|
142 throws SAXException {
|
|
143 // exit the namespace
|
138
|
144 tagSpace.removeLast();
|
88
|
145 }
|
1
|
146
|
88
|
147 }
|
|
148
|
|
149 /**
|
|
150 * load and parse a file (as URL)
|
|
151 * returns HashMap with list data
|
|
152 */
|
|
153 public HashMap loadURL(String path) throws SAXException, IOException {
|
|
154 //System.out.println("loadurl ("+path+")");
|
|
155 // Create a JAXP SAXParserFactory and configure it
|
|
156 SAXParserFactory spf = SAXParserFactory.newInstance();
|
138
|
157 spf.setNamespaceAware(true);
|
1
|
158
|
138
|
159 SAXParser parser = null;
|
88
|
160 try {
|
|
161 // Create a JAXP SAXParser
|
138
|
162 parser = spf.newSAXParser();
|
88
|
163
|
|
164 } catch (ParserConfigurationException e) {
|
|
165 throw new SAXException(e);
|
|
166 }
|
1
|
167
|
88
|
168 // create a list parser (keeps the data!)
|
|
169 XMLListParser listParser = new XMLListParser();
|
1
|
170
|
138
|
171 // Tell the SAXParser to parse the XML document
|
|
172 parser.parse(path, listParser);
|
1
|
173
|
88
|
174 return listParser.getData();
|
|
175 }
|
1
|
176
|
|
177 }
|