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