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