Mercurial > hg > digilib-old
annotate servlet/src/digilib/io/XMLMetaLoader.java @ 139:11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
- fixed lots of bugs in metadata handling.
author | robcast |
---|---|
date | Thu, 31 Jul 2003 20:56:51 +0200 |
parents | c36944be0b58 |
children | bc8df0133c04 |
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; | |
26 | |
27 import javax.xml.parsers.ParserConfigurationException; | |
28 import javax.xml.parsers.SAXParser; | |
29 import javax.xml.parsers.SAXParserFactory; | |
30 | |
31 import org.xml.sax.Attributes; | |
32 import org.xml.sax.SAXException; | |
33 import org.xml.sax.helpers.DefaultHandler; | |
34 | |
35 public class XMLMetaLoader { | |
36 | |
37 private String outerTag = "resource"; | |
38 private String metaTag = "meta"; | |
39 private String fileTag = "file"; | |
40 private String fileNameTag = "name"; | |
41 private String filePathTag = "path"; | |
42 private String infoTag = "img"; | |
43 | |
44 public XMLMetaLoader() { | |
45 } | |
46 | |
47 /** | |
48 * inner class XMLMetaParser to be called by the parser | |
49 */ | |
50 private class XMLMetaParser extends DefaultHandler { | |
51 | |
52 private LinkedList tags; | |
53 private HashMap files; | |
54 private HashMap meta; | |
55 private StringBuffer content; | |
56 private String fileName; | |
57 private String filePath; | |
58 | |
139
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
59 /** |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
60 * 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
|
61 * |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
62 * @param ln localName |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
63 * @param qn qName |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
64 * @return element name |
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 private String getName(String ln, String qn) { |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
67 if (ln != null) { |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
68 if (ln.length() > 0) { |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
69 return ln; |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
70 } |
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 // else it's qName (or nothing) |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
73 return qn; |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
74 } |
130 | 75 |
76 // Parser calls this once at the beginning of a document | |
77 public void startDocument() throws SAXException { | |
78 tags = new LinkedList(); | |
79 files = new HashMap(); | |
80 } | |
81 | |
82 // Parser calls this for each element in a document | |
83 public void startElement( | |
84 String namespaceURI, | |
85 String localName, | |
86 String qName, | |
87 Attributes atts) | |
88 throws SAXException { | |
89 | |
139
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
90 String name = getName(localName, qName); |
130 | 91 // open a new tag |
92 tags.addLast(name); | |
93 // start new content (no nesting of tags and content) | |
94 content = new StringBuffer(); | |
95 | |
96 if (name.equals(metaTag)) { | |
97 // new meta tag | |
98 meta = new HashMap(); | |
99 } else if (name.equals(fileTag)) { | |
100 // new file tag | |
101 fileName = null; | |
102 filePath = null; | |
139
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
103 meta = new HashMap(); |
130 | 104 } |
105 } | |
106 | |
107 // parser calls this for all tag content (possibly more than once) | |
108 public void characters(char[] ch, int start, int length) | |
109 throws SAXException { | |
110 // append data to current string buffer | |
111 content.append(ch, start, length); | |
112 } | |
113 | |
114 // parser calls this at the end of each element | |
115 public void endElement( | |
116 String namespaceURI, | |
117 String localName, | |
118 String qName) | |
119 throws SAXException { | |
120 | |
139
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
121 String name = getName(localName, qName); |
130 | 122 // exit the tag |
123 tags.removeLast(); | |
124 | |
125 // was it a file.name tag? | |
126 if (name.equals(fileNameTag) && tags.contains(fileTag)) { | |
127 // save name as filename | |
128 if ((content != null)&&(content.length() > 0)) { | |
129 fileName = content.toString(); | |
130 } | |
131 return; | |
132 } | |
133 | |
134 // was it a file.path tag? | |
135 if (name.equals(filePathTag) && tags.contains(fileTag)) { | |
136 // save path as filepath | |
137 if ((content != null)&&(content.length() > 0)) { | |
138 filePath = content.toString(); | |
139 } | |
140 return; | |
141 } | |
142 | |
143 // was it a file tag? | |
144 if (name.equals(fileTag)) { | |
145 // is there meta to save? | |
146 if ((meta != null)&&(meta.size() > 0)) { | |
147 // file name is either file.path or file.name | |
148 String fn = null; | |
149 if (filePath != null) { | |
150 fn = filePath; | |
151 } else if (fileName != null) { | |
152 fn = fileName; | |
153 } else { | |
154 // no file name, no file | |
155 return; | |
156 } | |
157 // save meta in file list | |
158 files.put(fn, meta); | |
159 } | |
160 return; | |
161 } | |
162 | |
163 // was it a meta tag outside a file tag? | |
164 if (name.equals(metaTag) && !tags.contains(fileTag)) { | |
165 // save meta as dir meta | |
166 if ((meta != null)&&(meta.size() > 0)) { | |
167 files.put("", meta); | |
168 } | |
169 return; | |
170 } | |
171 | |
172 // is this inside an info tag? | |
173 if (tags.contains(infoTag)) { | |
174 // then add whatever this is | |
175 if ((content != null)&&(content.length() > 0)) { | |
176 meta.put(name, content.toString()); | |
177 } | |
178 } | |
179 | |
180 } | |
181 | |
182 } | |
183 | |
184 /** | |
185 * load and parse a file (as URL) | |
186 * returns HashMap with list data | |
187 */ | |
188 public HashMap loadURL(String path) throws SAXException, IOException { | |
189 //System.out.println("loadurl ("+path+")"); | |
190 // Create a JAXP SAXParserFactory and configure it | |
191 SAXParserFactory spf = SAXParserFactory.newInstance(); | |
192 spf.setNamespaceAware(true); | |
193 | |
194 SAXParser parser = null; | |
195 try { | |
196 // Create a JAXP SAXParser | |
197 parser = spf.newSAXParser(); | |
198 | |
199 } catch (ParserConfigurationException e) { | |
200 throw new SAXException(e); | |
201 } | |
202 | |
203 // create a list parser (keeps the data!) | |
204 XMLMetaParser listParser = new XMLMetaParser(); | |
205 | |
206 // Tell the SAXParser to parse the XML document | |
207 parser.parse(path, listParser); | |
208 | |
209 return listParser.files; | |
210 } | |
211 | |
212 } |