Mercurial > hg > digilib-old
comparison 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 |
comparison
equal
deleted
inserted
replaced
137:4f1752d80560 | 138:d18b0ff52b07 |
---|---|
61 * inner class XMLListParser to be called by the parser | 61 * inner class XMLListParser to be called by the parser |
62 */ | 62 */ |
63 private class XMLListParser extends XMLFilterImpl { | 63 private class XMLListParser extends XMLFilterImpl { |
64 | 64 |
65 private HashMap listData; | 65 private HashMap listData; |
66 private LinkedList nameSpace; | 66 private LinkedList tagSpace; |
67 | 67 |
68 public HashMap getData() { | 68 public HashMap getData() { |
69 return listData; | 69 return listData; |
70 } | 70 } |
71 | 71 |
72 // Parser calls this once at the beginning of a document | 72 // Parser calls this once at the beginning of a document |
73 public void startDocument() throws SAXException { | 73 public void startDocument() throws SAXException { |
74 listData = new HashMap(); | 74 listData = new HashMap(); |
75 nameSpace = new LinkedList(); | 75 tagSpace = new LinkedList(); |
76 } | 76 } |
77 | 77 |
78 // Parser calls this for each element in a document | 78 // Parser calls this for each element in a document |
79 public void startElement( | 79 public void startElement( |
80 String namespaceURI, | 80 String namespaceURI, |
82 String qName, | 82 String qName, |
83 Attributes atts) | 83 Attributes atts) |
84 throws SAXException { | 84 throws SAXException { |
85 //System.out.println("<"+qName); | 85 //System.out.println("<"+qName); |
86 // open a new namespace | 86 // open a new namespace |
87 nameSpace.addLast(qName); | 87 tagSpace.addLast(qName); |
88 | 88 |
89 // ist it an entry tag? | 89 // ist it an entry tag? |
90 if (qName.equals(entryTag)) { | 90 if (qName.equals(entryTag)) { |
91 // is it inside a list tag? | 91 // is it inside a list tag? |
92 if ((listTag.length() > 0) && (!nameSpace.contains(listTag))) { | 92 if ((listTag.length() > 0) && (!tagSpace.contains(listTag))) { |
93 System.out.println( | 93 System.out.println( |
94 "BOO: Entry " | 94 "BOO: Entry " |
95 + entryTag | 95 + entryTag |
96 + " not inside list " | 96 + " not inside list " |
97 + listTag); | 97 + listTag); |
129 String namespaceURI, | 129 String namespaceURI, |
130 String localName, | 130 String localName, |
131 String qName) | 131 String qName) |
132 throws SAXException { | 132 throws SAXException { |
133 // exit the namespace | 133 // exit the namespace |
134 nameSpace.removeLast(); | 134 tagSpace.removeLast(); |
135 } | 135 } |
136 | 136 |
137 } | 137 } |
138 | 138 |
139 /** | 139 /** |
142 */ | 142 */ |
143 public HashMap loadURL(String path) throws SAXException, IOException { | 143 public HashMap loadURL(String path) throws SAXException, IOException { |
144 //System.out.println("loadurl ("+path+")"); | 144 //System.out.println("loadurl ("+path+")"); |
145 // Create a JAXP SAXParserFactory and configure it | 145 // Create a JAXP SAXParserFactory and configure it |
146 SAXParserFactory spf = SAXParserFactory.newInstance(); | 146 SAXParserFactory spf = SAXParserFactory.newInstance(); |
147 //spf.setNamespaceAware(true); | 147 spf.setNamespaceAware(true); |
148 | 148 |
149 XMLReader xmlReader = null; | 149 SAXParser parser = null; |
150 try { | 150 try { |
151 // Create a JAXP SAXParser | 151 // Create a JAXP SAXParser |
152 SAXParser saxParser = spf.newSAXParser(); | 152 parser = spf.newSAXParser(); |
153 | 153 |
154 // Get the encapsulated SAX XMLReader | |
155 xmlReader = saxParser.getXMLReader(); | |
156 } catch (ParserConfigurationException e) { | 154 } catch (ParserConfigurationException e) { |
157 throw new SAXException(e); | 155 throw new SAXException(e); |
158 } | 156 } |
159 | 157 |
160 // create a list parser (keeps the data!) | 158 // create a list parser (keeps the data!) |
161 XMLListParser listParser = new XMLListParser(); | 159 XMLListParser listParser = new XMLListParser(); |
162 | 160 |
163 // Set the ContentHandler of the XMLReader | 161 // Tell the SAXParser to parse the XML document |
164 xmlReader.setContentHandler(listParser); | 162 parser.parse(path, listParser); |
165 | |
166 // Tell the XMLReader to parse the XML document | |
167 xmlReader.parse(path); | |
168 | 163 |
169 return listParser.getData(); | 164 return listParser.getData(); |
170 } | 165 } |
171 | 166 |
172 } | 167 } |