Mercurial > hg > digilib-old
annotate servlet/src/digilib/io/XMLMetaLoader.java @ 308:1324ebb50d2a
big autumn cleaning :-)
- finally digilib.jsp uses the new javascript code
- dito digicat.jsp
- all files corresponding to the old frontend moved into /oldskin
- all icons moved into /img
author | robcast |
---|---|
date | Thu, 28 Oct 2004 22:25:57 +0200 |
parents | d4c0c4d858a9 |
children | 6dde90968cf1 |
rev | line source |
---|---|
130 | 1 /* XMLMetaLoader -- Load an XML format metadata into a Hashtable |
2 | |
3 Digital Image Library servlet components | |
4 | |
5 Copyright (C) 2003 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 import java.io.IOException; | |
24 import java.util.HashMap; | |
25 import java.util.LinkedList; | |
273 | 26 import java.util.Map; |
130 | 27 |
28 import javax.xml.parsers.ParserConfigurationException; | |
29 import javax.xml.parsers.SAXParser; | |
30 import javax.xml.parsers.SAXParserFactory; | |
31 | |
181 | 32 import org.apache.log4j.Logger; |
130 | 33 import org.xml.sax.Attributes; |
34 import org.xml.sax.SAXException; | |
35 import org.xml.sax.helpers.DefaultHandler; | |
36 | |
37 public class XMLMetaLoader { | |
38 | |
181 | 39 private Logger logger = Logger.getLogger(this.getClass()); |
130 | 40 private String outerTag = "resource"; |
41 private String metaTag = "meta"; | |
42 private String fileTag = "file"; | |
43 private String fileNameTag = "name"; | |
44 private String filePathTag = "path"; | |
233 | 45 private String imgTag = "img"; |
46 private String collectTag = "context"; | |
130 | 47 |
48 public XMLMetaLoader() { | |
49 } | |
50 | |
51 /** | |
52 * inner class XMLMetaParser to be called by the parser | |
53 */ | |
54 private class XMLMetaParser extends DefaultHandler { | |
55 | |
56 private LinkedList tags; | |
273 | 57 private Map files; |
58 private Map meta; | |
130 | 59 private StringBuffer content; |
233 | 60 private boolean collecting; |
61 private StringBuffer collectedContent; | |
130 | 62 private String fileName; |
63 private String filePath; | |
64 | |
139
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
65 /** |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
66 * extracts the elements name from either localName ln or qName qn. |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
67 * |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
68 * @param ln localName |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
69 * @param qn qName |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
70 * @return element name |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
71 */ |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
72 private String getName(String ln, String qn) { |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
73 if (ln != null) { |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
74 if (ln.length() > 0) { |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
75 return ln; |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
76 } |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
77 } |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
78 // else it's qName (or nothing) |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
79 return qn; |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
80 } |
130 | 81 |
233 | 82 /** |
83 * returns all attributes as a String | |
84 * | |
85 * @param attrs | |
86 * @return | |
87 */ | |
88 private String getAttrString(Attributes attrs) { | |
89 StringBuffer s = new StringBuffer(); | |
90 for (int i = 0; i < attrs.getLength(); i++) { | |
91 String key = getName(attrs.getLocalName(i), attrs.getQName(i)); | |
246 | 92 s.append(" "+key+"=\""+attrs.getValue(i)+"\""); |
233 | 93 } |
94 return s.toString(); | |
95 } | |
96 | |
97 | |
130 | 98 // Parser calls this once at the beginning of a document |
99 public void startDocument() throws SAXException { | |
100 tags = new LinkedList(); | |
101 files = new HashMap(); | |
233 | 102 collecting = false; |
103 collectedContent = null; | |
130 | 104 } |
105 | |
106 // Parser calls this for each element in a document | |
107 public void startElement( | |
108 String namespaceURI, | |
109 String localName, | |
110 String qName, | |
111 Attributes atts) | |
112 throws SAXException { | |
113 | |
139
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
114 String name = getName(localName, qName); |
130 | 115 // open a new tag |
116 tags.addLast(name); | |
117 // start new content (no nesting of tags and content) | |
118 content = new StringBuffer(); | |
119 | |
120 if (name.equals(metaTag)) { | |
121 // new meta tag | |
122 meta = new HashMap(); | |
233 | 123 collectedContent = new StringBuffer(); |
130 | 124 } else if (name.equals(fileTag)) { |
125 // new file tag | |
126 fileName = null; | |
127 filePath = null; | |
139
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
128 meta = new HashMap(); |
233 | 129 collectedContent = new StringBuffer(); |
130 } else if (name.equals(collectTag)) { | |
131 // start collecting | |
132 collecting = true; | |
133 if (collectedContent == null) { | |
134 collectedContent = new StringBuffer(); | |
135 } | |
136 } | |
137 | |
138 // record mode | |
139 if (collecting) { | |
140 collectedContent.append("<"+name); | |
141 collectedContent.append(getAttrString(atts)); | |
142 collectedContent.append(">"); | |
130 | 143 } |
144 } | |
145 | |
146 // parser calls this for all tag content (possibly more than once) | |
147 public void characters(char[] ch, int start, int length) | |
148 throws SAXException { | |
149 // append data to current string buffer | |
233 | 150 if (content == null) { |
151 content = new StringBuffer(); | |
152 } | |
130 | 153 content.append(ch, start, length); |
154 } | |
155 | |
156 // parser calls this at the end of each element | |
157 public void endElement( | |
158 String namespaceURI, | |
159 String localName, | |
160 String qName) | |
161 throws SAXException { | |
151 | 162 |
139
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
163 String name = getName(localName, qName); |
130 | 164 // exit the tag |
165 tags.removeLast(); | |
233 | 166 String lastTag = (tags.isEmpty()) ? "" : (String) tags.getLast(); |
151 | 167 |
168 // was it a file/name tag? | |
233 | 169 if (name.equals(fileNameTag) && lastTag.equals(fileTag)) { |
130 | 170 // save name as filename |
151 | 171 if ((content != null) && (content.length() > 0)) { |
172 fileName = content.toString().trim(); | |
130 | 173 } |
233 | 174 content = null; |
130 | 175 return; |
176 } | |
177 | |
151 | 178 // was it a file/path tag? |
233 | 179 if (name.equals(filePathTag) && lastTag.equals(fileTag)) { |
130 | 180 // save path as filepath |
151 | 181 if ((content != null) && (content.length() > 0)) { |
182 filePath = content.toString().trim(); | |
130 | 183 } |
233 | 184 content = null; |
130 | 185 return; |
186 } | |
187 | |
188 // was it a file tag? | |
189 if (name.equals(fileTag)) { | |
190 // is there meta to save? | |
151 | 191 if ((meta != null) && (meta.size() > 0)) { |
192 // file name is (optional file/path) / file/name | |
130 | 193 String fn = null; |
151 | 194 |
195 if (fileName != null) { | |
196 if (filePath != null) { | |
197 fn = filePath + "/" + fileName; | |
198 } else { | |
199 fn = fileName; | |
200 } | |
130 | 201 } else { |
202 // no file name, no file | |
233 | 203 content = null; |
130 | 204 return; |
205 } | |
206 // save meta in file list | |
207 files.put(fn, meta); | |
208 } | |
233 | 209 content = null; |
130 | 210 return; |
211 } | |
212 | |
213 // was it a meta tag outside a file tag? | |
214 if (name.equals(metaTag) && !tags.contains(fileTag)) { | |
215 // save meta as dir meta | |
151 | 216 if ((meta != null) && (meta.size() > 0)) { |
130 | 217 files.put("", meta); |
218 } | |
233 | 219 content = null; |
130 | 220 return; |
221 } | |
222 | |
233 | 223 // is this inside an digilib info (=img) tag? |
224 if (lastTag.equals(imgTag)) { | |
130 | 225 // then add whatever this is |
151 | 226 if ((content != null) && (content.length() > 0)) { |
227 meta.put(name, content.toString().trim()); | |
130 | 228 } |
233 | 229 content = null; |
230 return; | |
130 | 231 } |
151 | 232 |
233 | 233 // is this the end of collectTag? |
234 if (name.equals(collectTag)) { | |
235 collecting = false; | |
236 collectedContent.append("</"+collectTag+">\n"); | |
237 // store collected stuff | |
238 meta.put(collectTag, collectedContent.toString()); | |
239 //logger.debug("collected: '"+collectedContent+"'"); | |
240 content = null; | |
241 return; | |
242 } | |
243 | |
244 // write collected content | |
245 if (collecting) { | |
246 String s = ""; | |
247 if ((content != null) && (content.length() > 0)) { | |
248 s = content.toString().trim(); | |
249 } | |
250 //logger.debug("collect:"+name+" = "+s); | |
251 collectedContent.append(s); | |
252 collectedContent.append("</"+name+">\n"); | |
253 content = null; | |
254 return; | |
255 } | |
130 | 256 } |
257 | |
258 } | |
259 | |
260 /** | |
261 * load and parse a file (as URL) | |
262 * returns HashMap with list data | |
263 */ | |
273 | 264 public Map loadURL(String path) throws SAXException, IOException { |
195 | 265 logger.debug("loading meta: "+path); |
130 | 266 // Create a JAXP SAXParserFactory and configure it |
267 SAXParserFactory spf = SAXParserFactory.newInstance(); | |
268 spf.setNamespaceAware(true); | |
269 | |
270 SAXParser parser = null; | |
271 try { | |
272 // Create a JAXP SAXParser | |
273 parser = spf.newSAXParser(); | |
274 | |
275 } catch (ParserConfigurationException e) { | |
276 throw new SAXException(e); | |
277 } | |
278 | |
279 // create a list parser (keeps the data!) | |
280 XMLMetaParser listParser = new XMLMetaParser(); | |
281 | |
282 // Tell the SAXParser to parse the XML document | |
283 parser.parse(path, listParser); | |
284 | |
285 return listParser.files; | |
286 } | |
287 | |
288 } |