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