comparison common/src/main/java/digilib/util/XMLListLoader.java @ 903:7779b37d1d05

refactored into maven modules per servlet type. can build servlet-api 2.3 and 3.0 via profile now!
author robcast
date Tue, 26 Apr 2011 20:24:31 +0200
parents servlet/src/main/java/digilib/util/XMLListLoader.java@ba1eb2d821a2
children
comparison
equal deleted inserted replaced
902:89ba3ffcf552 903:7779b37d1d05
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.util;
22
23 // JAXP packages
24 import java.io.IOException;
25 import java.util.HashMap;
26 import java.util.LinkedList;
27 import java.util.Map;
28
29 import javax.xml.parsers.ParserConfigurationException;
30 import javax.xml.parsers.SAXParser;
31 import javax.xml.parsers.SAXParserFactory;
32
33 import org.apache.log4j.Logger;
34 import org.xml.sax.Attributes;
35 import org.xml.sax.SAXException;
36 import org.xml.sax.SAXParseException;
37 import org.xml.sax.helpers.DefaultHandler;
38
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 */
50 public class XMLListLoader {
51
52 private Logger logger = Logger.getLogger(this.getClass());
53 private String listTag = "list";
54 private String entryTag = "entry";
55 private String keyAtt = "key";
56 private String valueAtt = "value";
57
58 public XMLListLoader() {
59 }
60
61 public XMLListLoader(
62 String list_tag,
63 String entry_tag,
64 String key_att,
65 String value_att) {
66 logger.debug("xmlListLoader("+list_tag+","+entry_tag+","+key_att+","+value_att+")");
67 listTag = list_tag;
68 entryTag = entry_tag;
69 keyAtt = key_att;
70 valueAtt = value_att;
71 }
72
73 /**
74 * inner class XMLListParser to be called by the parser
75 */
76 private class XMLListParser extends DefaultHandler {
77
78 private Map<String, String> listData;
79 private LinkedList<String> tagSpace;
80
81 public Map<String, String> getData() {
82 return listData;
83 }
84
85 // Parser calls this once at the beginning of a document
86 public void startDocument() throws SAXException {
87 listData = new HashMap<String, String>();
88 tagSpace = new LinkedList<String>();
89 }
90
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
100 tagSpace.addLast(qName);
101
102 // ist it an entry tag?
103 if (qName.equals(entryTag)) {
104 // is it inside a list tag?
105 if ((listTag.length() > 0) && (!tagSpace.contains(listTag))) {
106 logger.error("BOO: Entry "
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)) {
118 logger.error("BOO: Entry "
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 }
138
139 public void endElement(
140 String namespaceURI,
141 String localName,
142 String qName)
143 throws SAXException {
144 // exit the namespace
145 tagSpace.removeLast();
146 }
147
148 }
149
150 /**
151 * load and parse a file (as URL)
152 * returns HashMap with list data
153 */
154 public Map<String, String> loadURL(String path) throws SAXException, IOException {
155 //System.out.println("loadurl ("+path+")");
156 // Create a JAXP SAXParserFactory and configure it
157 SAXParserFactory spf = SAXParserFactory.newInstance();
158 spf.setNamespaceAware(true);
159
160 SAXParser parser = null;
161 try {
162 // Create a JAXP SAXParser
163 parser = spf.newSAXParser();
164
165 } catch (ParserConfigurationException e) {
166 throw new SAXException(e);
167 }
168
169 // create a list parser (keeps the data!)
170 XMLListParser listParser = new XMLListParser();
171
172 // Tell the SAXParser to parse the XML document
173 parser.parse(path, listParser);
174
175 return listParser.getData();
176 }
177
178 }