Mercurial > hg > digilib-old
comparison servlet/src/digilib/io/XMLMetaLoader.java @ 130:c36944be0b58
Servlet Version 1.11a1with original size(!)
- new meta data file loader
- new parameter ddpi, ddpix, ddpiy (client->servlet)
- new parameter mo=osize
- osize scales based on ddpi and original dpi (currently only equally for x and y)
| author | robcast |
|---|---|
| date | Wed, 02 Jul 2003 00:02:18 +0200 |
| parents | |
| children | 11cfe4c89fdc |
comparison
equal
deleted
inserted
replaced
| 129:ed7c1e4dd177 | 130:c36944be0b58 |
|---|---|
| 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 | |
| 59 // public HashMap getData() { | |
| 60 // return meta; | |
| 61 // } | |
| 62 | |
| 63 // Parser calls this once at the beginning of a document | |
| 64 public void startDocument() throws SAXException { | |
| 65 tags = new LinkedList(); | |
| 66 files = new HashMap(); | |
| 67 } | |
| 68 | |
| 69 // Parser calls this for each element in a document | |
| 70 public void startElement( | |
| 71 String namespaceURI, | |
| 72 String localName, | |
| 73 String qName, | |
| 74 Attributes atts) | |
| 75 throws SAXException { | |
| 76 | |
| 77 String name = (localName != null) ? localName : qName; | |
| 78 // open a new tag | |
| 79 tags.addLast(name); | |
| 80 // start new content (no nesting of tags and content) | |
| 81 content = new StringBuffer(); | |
| 82 | |
| 83 if (name.equals(metaTag)) { | |
| 84 // new meta tag | |
| 85 meta = new HashMap(); | |
| 86 } else if (name.equals(fileTag)) { | |
| 87 // new file tag | |
| 88 fileName = null; | |
| 89 filePath = null; | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 // parser calls this for all tag content (possibly more than once) | |
| 94 public void characters(char[] ch, int start, int length) | |
| 95 throws SAXException { | |
| 96 // append data to current string buffer | |
| 97 content.append(ch, start, length); | |
| 98 } | |
| 99 | |
| 100 // parser calls this at the end of each element | |
| 101 public void endElement( | |
| 102 String namespaceURI, | |
| 103 String localName, | |
| 104 String qName) | |
| 105 throws SAXException { | |
| 106 | |
| 107 String name = (localName != null) ? localName : qName; | |
| 108 // exit the tag | |
| 109 tags.removeLast(); | |
| 110 | |
| 111 // was it a file.name tag? | |
| 112 if (name.equals(fileNameTag) && tags.contains(fileTag)) { | |
| 113 // save name as filename | |
| 114 if ((content != null)&&(content.length() > 0)) { | |
| 115 fileName = content.toString(); | |
| 116 } | |
| 117 return; | |
| 118 } | |
| 119 | |
| 120 // was it a file.path tag? | |
| 121 if (name.equals(filePathTag) && tags.contains(fileTag)) { | |
| 122 // save path as filepath | |
| 123 if ((content != null)&&(content.length() > 0)) { | |
| 124 filePath = content.toString(); | |
| 125 } | |
| 126 return; | |
| 127 } | |
| 128 | |
| 129 // was it a file tag? | |
| 130 if (name.equals(fileTag)) { | |
| 131 // is there meta to save? | |
| 132 if ((meta != null)&&(meta.size() > 0)) { | |
| 133 // file name is either file.path or file.name | |
| 134 String fn = null; | |
| 135 if (filePath != null) { | |
| 136 fn = filePath; | |
| 137 } else if (fileName != null) { | |
| 138 fn = fileName; | |
| 139 } else { | |
| 140 // no file name, no file | |
| 141 return; | |
| 142 } | |
| 143 // save meta in file list | |
| 144 files.put(fn, meta); | |
| 145 } | |
| 146 return; | |
| 147 } | |
| 148 | |
| 149 // was it a meta tag outside a file tag? | |
| 150 if (name.equals(metaTag) && !tags.contains(fileTag)) { | |
| 151 // save meta as dir meta | |
| 152 if ((meta != null)&&(meta.size() > 0)) { | |
| 153 files.put("", meta); | |
| 154 } | |
| 155 return; | |
| 156 } | |
| 157 | |
| 158 // is this inside an info tag? | |
| 159 if (tags.contains(infoTag)) { | |
| 160 // then add whatever this is | |
| 161 if ((content != null)&&(content.length() > 0)) { | |
| 162 meta.put(name, content.toString()); | |
| 163 } | |
| 164 } | |
| 165 | |
| 166 } | |
| 167 | |
| 168 } | |
| 169 | |
| 170 /** | |
| 171 * load and parse a file (as URL) | |
| 172 * returns HashMap with list data | |
| 173 */ | |
| 174 public HashMap loadURL(String path) throws SAXException, IOException { | |
| 175 //System.out.println("loadurl ("+path+")"); | |
| 176 // Create a JAXP SAXParserFactory and configure it | |
| 177 SAXParserFactory spf = SAXParserFactory.newInstance(); | |
| 178 spf.setNamespaceAware(true); | |
| 179 | |
| 180 SAXParser parser = null; | |
| 181 try { | |
| 182 // Create a JAXP SAXParser | |
| 183 parser = spf.newSAXParser(); | |
| 184 | |
| 185 } catch (ParserConfigurationException e) { | |
| 186 throw new SAXException(e); | |
| 187 } | |
| 188 | |
| 189 // create a list parser (keeps the data!) | |
| 190 XMLMetaParser listParser = new XMLMetaParser(); | |
| 191 | |
| 192 // Tell the SAXParser to parse the XML document | |
| 193 parser.parse(path, listParser); | |
| 194 | |
| 195 return listParser.files; | |
| 196 } | |
| 197 | |
| 198 } |
