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
|
181
|
32 import org.apache.log4j.Logger;
|
88
|
33 import org.xml.sax.Attributes;
|
|
34 import org.xml.sax.SAXException;
|
|
35 import org.xml.sax.SAXParseException;
|
140
|
36 import org.xml.sax.helpers.DefaultHandler;
|
1
|
37
|
176
|
38 /** Loads a simple XML list into a HashMap.
|
|
39 *
|
|
40 * The XML file has an outer <code>list_tag</code>. Every entry is an
|
|
41 * <code>entry_tag</code> with two attributes: the <code>key_att</code>
|
|
42 * key and the <code>value_att</code> value.
|
|
43 *
|
|
44 * The file is read by the <code>loadURL</code> method, that returns a
|
|
45 * HashMap with the key-value pairs.
|
|
46 *
|
|
47 * @author casties
|
|
48 */
|
1
|
49 public class XMLListLoader {
|
|
50
|
181
|
51 private Logger logger = Logger.getLogger(this.getClass());
|
88
|
52 private String listTag = "list";
|
|
53 private String entryTag = "entry";
|
|
54 private String keyAtt = "key";
|
|
55 private String valueAtt = "value";
|
1
|
56
|
88
|
57 public XMLListLoader() {
|
|
58 }
|
1
|
59
|
88
|
60 public XMLListLoader(
|
|
61 String list_tag,
|
|
62 String entry_tag,
|
|
63 String key_att,
|
|
64 String value_att) {
|
181
|
65 logger.debug("xmlListLoader("+list_tag+","+entry_tag+","+key_att+","+value_att+")");
|
88
|
66 listTag = list_tag;
|
|
67 entryTag = entry_tag;
|
|
68 keyAtt = key_att;
|
|
69 valueAtt = value_att;
|
|
70 }
|
1
|
71
|
88
|
72 /**
|
|
73 * inner class XMLListParser to be called by the parser
|
|
74 */
|
140
|
75 private class XMLListParser extends DefaultHandler {
|
88
|
76
|
|
77 private HashMap listData;
|
138
|
78 private LinkedList tagSpace;
|
88
|
79
|
|
80 public HashMap getData() {
|
|
81 return listData;
|
|
82 }
|
1
|
83
|
88
|
84 // Parser calls this once at the beginning of a document
|
|
85 public void startDocument() throws SAXException {
|
|
86 listData = new HashMap();
|
138
|
87 tagSpace = new LinkedList();
|
88
|
88 }
|
1
|
89
|
88
|
90 // Parser calls this for each element in a document
|
|
91 public void startElement(
|
|
92 String namespaceURI,
|
|
93 String localName,
|
|
94 String qName,
|
|
95 Attributes atts)
|
|
96 throws SAXException {
|
|
97 //System.out.println("<"+qName);
|
|
98 // open a new namespace
|
138
|
99 tagSpace.addLast(qName);
|
1
|
100
|
88
|
101 // ist it an entry tag?
|
|
102 if (qName.equals(entryTag)) {
|
|
103 // is it inside a list tag?
|
138
|
104 if ((listTag.length() > 0) && (!tagSpace.contains(listTag))) {
|
181
|
105 logger.error("BOO: Entry "
|
88
|
106 + entryTag
|
|
107 + " not inside list "
|
|
108 + listTag);
|
|
109 throw new SAXParseException(
|
|
110 "Entry " + entryTag + " not inside list " + listTag,
|
|
111 null);
|
|
112 }
|
|
113 // get the attributes
|
|
114 String key = atts.getValue(keyAtt);
|
|
115 String val = atts.getValue(valueAtt);
|
|
116 if ((key == null) || (val == null)) {
|
181
|
117 logger.error("BOO: Entry "
|
88
|
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 }
|