comparison servlet/src/digilib/io/XMLListLoader.java @ 531:9cedd170b581 digilibPDF

* PDF generation works now even with subdirectories * genericsification and clean up
author robcast
date Fri, 05 Feb 2010 20:58:38 +0100
parents 0ff3ede32060
children e758a49258e8
comparison
equal deleted inserted replaced
530:bd6569a95a3c 531:9cedd170b581
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 import java.util.Map;
27 28
28 import java.util.*; 29 import javax.xml.parsers.ParserConfigurationException;
29 import java.io.*; 30 import javax.xml.parsers.SAXParser;
31 import javax.xml.parsers.SAXParserFactory;
30 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 */
31 public class XMLListLoader { 50 public class XMLListLoader {
32 51
33 private String listTag = "list"; 52 private Logger logger = Logger.getLogger(this.getClass());
34 private String entryTag = "entry"; 53 private String listTag = "list";
35 private String keyAtt = "key"; 54 private String entryTag = "entry";
36 private String valueAtt = "value"; 55 private String keyAtt = "key";
56 private String valueAtt = "value";
37 57
38 public XMLListLoader() { 58 public XMLListLoader() {
39 } 59 }
40 60
41 public XMLListLoader(String list_tag, String entry_tag, String key_att, String value_att) { 61 public XMLListLoader(
42 //System.out.println("xmlListLoader("+list_tag+","+entry_tag+","+key_att+","+value_att+")"); 62 String list_tag,
43 listTag = list_tag; 63 String entry_tag,
44 entryTag = entry_tag; 64 String key_att,
45 keyAtt = key_att; 65 String value_att) {
46 valueAtt = value_att; 66 logger.debug("xmlListLoader("+list_tag+","+entry_tag+","+key_att+","+value_att+")");
47 } 67 listTag = list_tag;
68 entryTag = entry_tag;
69 keyAtt = key_att;
70 valueAtt = value_att;
71 }
48 72
49 /** 73 /**
50 * inner class XMLListParser to be called by the parser 74 * inner class XMLListParser to be called by the parser
51 */ 75 */
52 private class XMLListParser extends DefaultHandler { 76 private class XMLListParser extends DefaultHandler {
53 77
54 private Hashtable listData; 78 private Map<String, String> listData;
55 private Stack nameSpace; 79 private LinkedList<String> tagSpace;
56 80
57 public Hashtable getData() { 81 public Map<String, String> getData() {
58 return listData; 82 return listData;
59 } 83 }
60 84
61 // Parser calls this once at the beginning of a document 85 // Parser calls this once at the beginning of a document
62 public void startDocument() throws SAXException { 86 public void startDocument() throws SAXException {
63 listData = new Hashtable(); 87 listData = new HashMap<String, String>();
64 nameSpace = new Stack(); 88 tagSpace = new LinkedList<String>();
65 } 89 }
66 90
67 // Parser calls this for each element in a document 91 // Parser calls this for each element in a document
68 public void startElement(String namespaceURI, String localName, 92 public void startElement(
69 String qName, Attributes atts) 93 String namespaceURI,
70 throws SAXException 94 String localName,
71 { 95 String qName,
72 //System.out.println("<"+qName); 96 Attributes atts)
73 // open a new namespace 97 throws SAXException {
74 nameSpace.push(qName); 98 //System.out.println("<"+qName);
99 // open a new namespace
100 tagSpace.addLast(qName);
75 101
76 // ist it an entry tag? 102 // ist it an entry tag?
77 if (qName.equals(entryTag)) { 103 if (qName.equals(entryTag)) {
78 // is it inside a list tag? 104 // is it inside a list tag?
79 if ((listTag.length() > 0)&&(nameSpace.search(listTag) < 0)) { 105 if ((listTag.length() > 0) && (!tagSpace.contains(listTag))) {
80 System.out.println("BOO: Entry "+entryTag+" not inside list "+listTag); 106 logger.error("BOO: Entry "
81 throw new SAXParseException("Entry "+entryTag+" not inside list "+listTag, null); 107 + entryTag
82 } 108 + " not inside list "
83 // get the attributes 109 + listTag);
84 String key = atts.getValue(keyAtt); 110 throw new SAXParseException(
85 String val = atts.getValue(valueAtt); 111 "Entry " + entryTag + " not inside list " + listTag,
86 if ((key == null)||(val == null)) { 112 null);
87 System.out.println("BOO: Entry "+entryTag+" does not have Attributes "+keyAtt+", "+valueAtt); 113 }
88 throw new SAXParseException("Entry "+entryTag+" does not have Attributes "+keyAtt+", "+valueAtt, null); 114 // get the attributes
89 } 115 String key = atts.getValue(keyAtt);
90 // add the values 116 String val = atts.getValue(valueAtt);
91 //System.out.println("DATA: "+key+" = "+val); 117 if ((key == null) || (val == null)) {
92 listData.put(key, val); 118 logger.error("BOO: Entry "
93 } 119 + entryTag
94 } 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 }
95 138
96 public void endElement(String namespaceURI, String localName, 139 public void endElement(
97 String qName) 140 String namespaceURI,
98 throws SAXException 141 String localName,
99 { 142 String qName)
100 // exit the namespace 143 throws SAXException {
101 nameSpace.pop(); 144 // exit the namespace
102 } 145 tagSpace.removeLast();
146 }
103 147
104 } 148 }
105 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);
106 159
107 /** 160 SAXParser parser = null;
108 * load and parse a file (as URL) 161 try {
109 * returns Hashtable with list data 162 // Create a JAXP SAXParser
110 */ 163 parser = spf.newSAXParser();
111 public Hashtable loadURL(String path) throws SAXException, IOException {
112 //System.out.println("loadurl ("+path+")");
113 // Create a JAXP SAXParserFactory and configure it
114 SAXParserFactory spf = SAXParserFactory.newInstance();
115 //spf.setNamespaceAware(true);
116 164
117 XMLReader xmlReader = null; 165 } catch (ParserConfigurationException e) {
118 try { 166 throw new SAXException(e);
119 // Create a JAXP SAXParser 167 }
120 SAXParser saxParser = spf.newSAXParser();
121 168
122 // Get the encapsulated SAX XMLReader 169 // create a list parser (keeps the data!)
123 xmlReader = saxParser.getXMLReader(); 170 XMLListParser listParser = new XMLListParser();
124 }
125 catch (ParserConfigurationException e) {
126 throw new SAXException(e);
127 }
128 171
129 // create a list parser (keeps the data!) 172 // Tell the SAXParser to parse the XML document
130 XMLListParser listParser = new XMLListParser(); 173 parser.parse(path, listParser);
131 174
132 // Set the ContentHandler of the XMLReader 175 return listParser.getData();
133 xmlReader.setContentHandler(listParser); 176 }
134
135 // Tell the XMLReader to parse the XML document
136 xmlReader.parse(path);
137
138 return listParser.getData();
139 }
140 177
141 } 178 }