Mercurial > hg > digilib-old
annotate servlet/src/digilib/io/XMLMetaLoader.java @ 152:f4a5cfe37469
Servlet version 1.16a1
- cleanup of DigilibConfig class
- now uses new Parameter and ParameterMap classes
- new parameter default-quality
author | robcast |
---|---|
date | Wed, 03 Sep 2003 00:54:38 +0200 |
parents | bc8df0133c04 |
children | afe7ff98bb71 |
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 { | |
151 | 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(); | |
151 | 124 |
125 // was it a file/name tag? | |
130 | 126 if (name.equals(fileNameTag) && tags.contains(fileTag)) { |
127 // save name as filename | |
151 | 128 if ((content != null) && (content.length() > 0)) { |
129 fileName = content.toString().trim(); | |
130 | 130 } |
131 return; | |
132 } | |
133 | |
151 | 134 // was it a file/path tag? |
130 | 135 if (name.equals(filePathTag) && tags.contains(fileTag)) { |
136 // save path as filepath | |
151 | 137 if ((content != null) && (content.length() > 0)) { |
138 filePath = content.toString().trim(); | |
130 | 139 } |
140 return; | |
141 } | |
142 | |
143 // was it a file tag? | |
144 if (name.equals(fileTag)) { | |
145 // is there meta to save? | |
151 | 146 if ((meta != null) && (meta.size() > 0)) { |
147 // file name is (optional file/path) / file/name | |
130 | 148 String fn = null; |
151 | 149 |
150 if (fileName != null) { | |
151 if (filePath != null) { | |
152 fn = filePath + "/" + fileName; | |
153 } else { | |
154 fn = fileName; | |
155 } | |
130 | 156 } else { |
157 // no file name, no file | |
158 return; | |
159 } | |
160 // save meta in file list | |
161 files.put(fn, meta); | |
162 } | |
163 return; | |
164 } | |
165 | |
166 // was it a meta tag outside a file tag? | |
167 if (name.equals(metaTag) && !tags.contains(fileTag)) { | |
168 // save meta as dir meta | |
151 | 169 if ((meta != null) && (meta.size() > 0)) { |
130 | 170 files.put("", meta); |
171 } | |
172 return; | |
173 } | |
174 | |
151 | 175 // is this inside an info (=img) tag? |
130 | 176 if (tags.contains(infoTag)) { |
177 // then add whatever this is | |
151 | 178 if ((content != null) && (content.length() > 0)) { |
179 meta.put(name, content.toString().trim()); | |
130 | 180 } |
181 } | |
151 | 182 |
130 | 183 } |
184 | |
185 } | |
186 | |
187 /** | |
188 * load and parse a file (as URL) | |
189 * returns HashMap with list data | |
190 */ | |
191 public HashMap loadURL(String path) throws SAXException, IOException { | |
192 //System.out.println("loadurl ("+path+")"); | |
193 // Create a JAXP SAXParserFactory and configure it | |
194 SAXParserFactory spf = SAXParserFactory.newInstance(); | |
195 spf.setNamespaceAware(true); | |
196 | |
197 SAXParser parser = null; | |
198 try { | |
199 // Create a JAXP SAXParser | |
200 parser = spf.newSAXParser(); | |
201 | |
202 } catch (ParserConfigurationException e) { | |
203 throw new SAXException(e); | |
204 } | |
205 | |
206 // create a list parser (keeps the data!) | |
207 XMLMetaParser listParser = new XMLMetaParser(); | |
208 | |
209 // Tell the SAXParser to parse the XML document | |
210 parser.parse(path, listParser); | |
211 | |
212 return listParser.files; | |
213 } | |
214 | |
215 } |