annotate servlet/src/digilib/io/XMLListLoader.java @ 138:d18b0ff52b07

*** empty log message ***
author robcast
date Sat, 12 Jul 2003 01:26:56 +0200
parents 0530f8295c4b
children c878ea574c29
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
1 /* XMLListLoader -- Load an XML list into a Hashtable
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
2
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
3 Digital Image Library servlet components
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
4
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
5 Copyright (C) 2001, 2002 Robert Casties (robcast@mail.berlios.de)
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
6
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
7 This program is free software; you can redistribute it and/or modify it
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
8 under the terms of the GNU General Public License as published by the
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
9 Free Software Foundation; either version 2 of the License, or (at your
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
10 option) any later version.
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
11
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
12 Please read license.txt for the full details. A copy of the GPL
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
13 may be found at http://www.gnu.org/copyleft/lgpl.html
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
14
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
16 along with this program; if not, write to the Free Software
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
18
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
19 */
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
20
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
21 package digilib.io;
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
22
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
23 // JAXP packages
88
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
24 import java.io.IOException;
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
25 import java.util.HashMap;
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
26 import java.util.LinkedList;
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
27
88
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
28 import javax.xml.parsers.ParserConfigurationException;
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
29 import javax.xml.parsers.SAXParser;
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
30 import javax.xml.parsers.SAXParserFactory;
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
31
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
32 import org.xml.sax.Attributes;
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
33 import org.xml.sax.SAXException;
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
34 import org.xml.sax.SAXParseException;
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
35 import org.xml.sax.XMLReader;
128
0530f8295c4b Small fix using newer API.
robcast
parents: 88
diff changeset
36 import org.xml.sax.helpers.XMLFilterImpl;
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
37
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
38 public class XMLListLoader {
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
39
88
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
40 private String listTag = "list";
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
41 private String entryTag = "entry";
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
42 private String keyAtt = "key";
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
43 private String valueAtt = "value";
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
44
88
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
45 public XMLListLoader() {
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
46 }
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
47
88
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
48 public XMLListLoader(
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
49 String list_tag,
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
50 String entry_tag,
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
51 String key_att,
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
52 String value_att) {
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
53 //System.out.println("xmlListLoader("+list_tag+","+entry_tag+","+key_att+","+value_att+")");
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
54 listTag = list_tag;
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
55 entryTag = entry_tag;
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
56 keyAtt = key_att;
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
57 valueAtt = value_att;
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
58 }
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
59
88
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
60 /**
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
61 * inner class XMLListParser to be called by the parser
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
62 */
128
0530f8295c4b Small fix using newer API.
robcast
parents: 88
diff changeset
63 private class XMLListParser extends XMLFilterImpl {
88
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
64
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
65 private HashMap listData;
138
d18b0ff52b07 *** empty log message ***
robcast
parents: 128
diff changeset
66 private LinkedList tagSpace;
88
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
67
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
68 public HashMap getData() {
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
69 return listData;
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
70 }
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
71
88
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
72 // Parser calls this once at the beginning of a document
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
73 public void startDocument() throws SAXException {
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
74 listData = new HashMap();
138
d18b0ff52b07 *** empty log message ***
robcast
parents: 128
diff changeset
75 tagSpace = new LinkedList();
88
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
76 }
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
77
88
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
78 // Parser calls this for each element in a document
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
79 public void startElement(
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
80 String namespaceURI,
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
81 String localName,
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
82 String qName,
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
83 Attributes atts)
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
84 throws SAXException {
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
85 //System.out.println("<"+qName);
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
86 // open a new namespace
138
d18b0ff52b07 *** empty log message ***
robcast
parents: 128
diff changeset
87 tagSpace.addLast(qName);
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
88
88
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
89 // ist it an entry tag?
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
90 if (qName.equals(entryTag)) {
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
91 // is it inside a list tag?
138
d18b0ff52b07 *** empty log message ***
robcast
parents: 128
diff changeset
92 if ((listTag.length() > 0) && (!tagSpace.contains(listTag))) {
88
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
93 System.out.println(
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
94 "BOO: Entry "
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
95 + entryTag
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
96 + " not inside list "
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
97 + listTag);
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
98 throw new SAXParseException(
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
99 "Entry " + entryTag + " not inside list " + listTag,
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
100 null);
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
101 }
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
102 // get the attributes
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
103 String key = atts.getValue(keyAtt);
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
104 String val = atts.getValue(valueAtt);
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
105 if ((key == null) || (val == null)) {
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
106 System.out.println(
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
107 "BOO: Entry "
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
108 + entryTag
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
109 + " does not have Attributes "
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
110 + keyAtt
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
111 + ", "
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
112 + valueAtt);
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
113 throw new SAXParseException(
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
114 "Entry "
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
115 + entryTag
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
116 + " does not have Attributes "
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
117 + keyAtt
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
118 + ", "
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
119 + valueAtt,
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
120 null);
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
121 }
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
122 // add the values
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
123 //System.out.println("DATA: "+key+" = "+val);
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
124 listData.put(key, val);
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
125 }
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
126 }
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
127
88
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
128 public void endElement(
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
129 String namespaceURI,
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
130 String localName,
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
131 String qName)
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
132 throws SAXException {
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
133 // exit the namespace
138
d18b0ff52b07 *** empty log message ***
robcast
parents: 128
diff changeset
134 tagSpace.removeLast();
88
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
135 }
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
136
88
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
137 }
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
138
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
139 /**
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
140 * load and parse a file (as URL)
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
141 * returns HashMap with list data
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
142 */
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
143 public HashMap loadURL(String path) throws SAXException, IOException {
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
144 //System.out.println("loadurl ("+path+")");
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
145 // Create a JAXP SAXParserFactory and configure it
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
146 SAXParserFactory spf = SAXParserFactory.newInstance();
138
d18b0ff52b07 *** empty log message ***
robcast
parents: 128
diff changeset
147 spf.setNamespaceAware(true);
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
148
138
d18b0ff52b07 *** empty log message ***
robcast
parents: 128
diff changeset
149 SAXParser parser = null;
88
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
150 try {
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
151 // Create a JAXP SAXParser
138
d18b0ff52b07 *** empty log message ***
robcast
parents: 128
diff changeset
152 parser = spf.newSAXParser();
88
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
153
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
154 } catch (ParserConfigurationException e) {
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
155 throw new SAXException(e);
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
156 }
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
157
88
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
158 // create a list parser (keeps the data!)
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
159 XMLListParser listParser = new XMLListParser();
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
160
138
d18b0ff52b07 *** empty log message ***
robcast
parents: 128
diff changeset
161 // Tell the SAXParser to parse the XML document
d18b0ff52b07 *** empty log message ***
robcast
parents: 128
diff changeset
162 parser.parse(path, listParser);
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
163
88
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
164 return listParser.getData();
398d39ee1014 New version 1.8b4.
robcast
parents: 56
diff changeset
165 }
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
166
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
167 }