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