Mercurial > hg > digilib-old
comparison servlet/src/digilib/io/XMLMetaLoader.java @ 233:e40d8b2e3978
Servlet version 1.19b1 -- with context metadata
- reads and caches context tags from index meta file
- for use in dlContext-xml.jsp
| author | robcast |
|---|---|
| date | Sat, 17 Jul 2004 19:25:26 +0200 |
| parents | f921735dc578 |
| children | aaf6eace011d |
comparison
equal
deleted
inserted
replaced
| 232:69b616b50431 | 233:e40d8b2e3978 |
|---|---|
| 39 private String outerTag = "resource"; | 39 private String outerTag = "resource"; |
| 40 private String metaTag = "meta"; | 40 private String metaTag = "meta"; |
| 41 private String fileTag = "file"; | 41 private String fileTag = "file"; |
| 42 private String fileNameTag = "name"; | 42 private String fileNameTag = "name"; |
| 43 private String filePathTag = "path"; | 43 private String filePathTag = "path"; |
| 44 private String infoTag = "img"; | 44 private String imgTag = "img"; |
| 45 private String collectTag = "context"; | |
| 45 | 46 |
| 46 public XMLMetaLoader() { | 47 public XMLMetaLoader() { |
| 47 } | 48 } |
| 48 | 49 |
| 49 /** | 50 /** |
| 53 | 54 |
| 54 private LinkedList tags; | 55 private LinkedList tags; |
| 55 private HashMap files; | 56 private HashMap files; |
| 56 private HashMap meta; | 57 private HashMap meta; |
| 57 private StringBuffer content; | 58 private StringBuffer content; |
| 59 private boolean collecting; | |
| 60 private StringBuffer collectedContent; | |
| 58 private String fileName; | 61 private String fileName; |
| 59 private String filePath; | 62 private String filePath; |
| 60 | 63 |
| 61 /** | 64 /** |
| 62 * extracts the elements name from either localName ln or qName qn. | 65 * extracts the elements name from either localName ln or qName qn. |
| 73 } | 76 } |
| 74 // else it's qName (or nothing) | 77 // else it's qName (or nothing) |
| 75 return qn; | 78 return qn; |
| 76 } | 79 } |
| 77 | 80 |
| 81 /** | |
| 82 * returns all attributes as a String | |
| 83 * | |
| 84 * @param attrs | |
| 85 * @return | |
| 86 */ | |
| 87 private String getAttrString(Attributes attrs) { | |
| 88 StringBuffer s = new StringBuffer(); | |
| 89 for (int i = 0; i < attrs.getLength(); i++) { | |
| 90 String key = getName(attrs.getLocalName(i), attrs.getQName(i)); | |
| 91 s.append(" "+key+"=\""+attrs.getValue(i)); | |
| 92 } | |
| 93 return s.toString(); | |
| 94 } | |
| 95 | |
| 96 | |
| 78 // Parser calls this once at the beginning of a document | 97 // Parser calls this once at the beginning of a document |
| 79 public void startDocument() throws SAXException { | 98 public void startDocument() throws SAXException { |
| 80 tags = new LinkedList(); | 99 tags = new LinkedList(); |
| 81 files = new HashMap(); | 100 files = new HashMap(); |
| 101 collecting = false; | |
| 102 collectedContent = null; | |
| 82 } | 103 } |
| 83 | 104 |
| 84 // Parser calls this for each element in a document | 105 // Parser calls this for each element in a document |
| 85 public void startElement( | 106 public void startElement( |
| 86 String namespaceURI, | 107 String namespaceURI, |
| 96 content = new StringBuffer(); | 117 content = new StringBuffer(); |
| 97 | 118 |
| 98 if (name.equals(metaTag)) { | 119 if (name.equals(metaTag)) { |
| 99 // new meta tag | 120 // new meta tag |
| 100 meta = new HashMap(); | 121 meta = new HashMap(); |
| 122 collectedContent = new StringBuffer(); | |
| 101 } else if (name.equals(fileTag)) { | 123 } else if (name.equals(fileTag)) { |
| 102 // new file tag | 124 // new file tag |
| 103 fileName = null; | 125 fileName = null; |
| 104 filePath = null; | 126 filePath = null; |
| 105 meta = new HashMap(); | 127 meta = new HashMap(); |
| 128 collectedContent = new StringBuffer(); | |
| 129 } else if (name.equals(collectTag)) { | |
| 130 // start collecting | |
| 131 collecting = true; | |
| 132 if (collectedContent == null) { | |
| 133 collectedContent = new StringBuffer(); | |
| 134 } | |
| 135 } | |
| 136 | |
| 137 // record mode | |
| 138 if (collecting) { | |
| 139 collectedContent.append("<"+name); | |
| 140 collectedContent.append(getAttrString(atts)); | |
| 141 collectedContent.append(">"); | |
| 106 } | 142 } |
| 107 } | 143 } |
| 108 | 144 |
| 109 // parser calls this for all tag content (possibly more than once) | 145 // parser calls this for all tag content (possibly more than once) |
| 110 public void characters(char[] ch, int start, int length) | 146 public void characters(char[] ch, int start, int length) |
| 111 throws SAXException { | 147 throws SAXException { |
| 112 // append data to current string buffer | 148 // append data to current string buffer |
| 149 if (content == null) { | |
| 150 content = new StringBuffer(); | |
| 151 } | |
| 113 content.append(ch, start, length); | 152 content.append(ch, start, length); |
| 114 } | 153 } |
| 115 | 154 |
| 116 // parser calls this at the end of each element | 155 // parser calls this at the end of each element |
| 117 public void endElement( | 156 public void endElement( |
| 121 throws SAXException { | 160 throws SAXException { |
| 122 | 161 |
| 123 String name = getName(localName, qName); | 162 String name = getName(localName, qName); |
| 124 // exit the tag | 163 // exit the tag |
| 125 tags.removeLast(); | 164 tags.removeLast(); |
| 165 String lastTag = (tags.isEmpty()) ? "" : (String) tags.getLast(); | |
| 126 | 166 |
| 127 // was it a file/name tag? | 167 // was it a file/name tag? |
| 128 if (name.equals(fileNameTag) && tags.contains(fileTag)) { | 168 if (name.equals(fileNameTag) && lastTag.equals(fileTag)) { |
| 129 // save name as filename | 169 // save name as filename |
| 130 if ((content != null) && (content.length() > 0)) { | 170 if ((content != null) && (content.length() > 0)) { |
| 131 fileName = content.toString().trim(); | 171 fileName = content.toString().trim(); |
| 132 } | 172 } |
| 173 content = null; | |
| 133 return; | 174 return; |
| 134 } | 175 } |
| 135 | 176 |
| 136 // was it a file/path tag? | 177 // was it a file/path tag? |
| 137 if (name.equals(filePathTag) && tags.contains(fileTag)) { | 178 if (name.equals(filePathTag) && lastTag.equals(fileTag)) { |
| 138 // save path as filepath | 179 // save path as filepath |
| 139 if ((content != null) && (content.length() > 0)) { | 180 if ((content != null) && (content.length() > 0)) { |
| 140 filePath = content.toString().trim(); | 181 filePath = content.toString().trim(); |
| 141 } | 182 } |
| 183 content = null; | |
| 142 return; | 184 return; |
| 143 } | 185 } |
| 144 | 186 |
| 145 // was it a file tag? | 187 // was it a file tag? |
| 146 if (name.equals(fileTag)) { | 188 if (name.equals(fileTag)) { |
| 155 } else { | 197 } else { |
| 156 fn = fileName; | 198 fn = fileName; |
| 157 } | 199 } |
| 158 } else { | 200 } else { |
| 159 // no file name, no file | 201 // no file name, no file |
| 202 content = null; | |
| 160 return; | 203 return; |
| 161 } | 204 } |
| 162 // save meta in file list | 205 // save meta in file list |
| 163 files.put(fn, meta); | 206 files.put(fn, meta); |
| 164 } | 207 } |
| 208 content = null; | |
| 165 return; | 209 return; |
| 166 } | 210 } |
| 167 | 211 |
| 168 // was it a meta tag outside a file tag? | 212 // was it a meta tag outside a file tag? |
| 169 if (name.equals(metaTag) && !tags.contains(fileTag)) { | 213 if (name.equals(metaTag) && !tags.contains(fileTag)) { |
| 170 // save meta as dir meta | 214 // save meta as dir meta |
| 171 if ((meta != null) && (meta.size() > 0)) { | 215 if ((meta != null) && (meta.size() > 0)) { |
| 172 files.put("", meta); | 216 files.put("", meta); |
| 173 } | 217 } |
| 174 return; | 218 content = null; |
| 175 } | 219 return; |
| 176 | 220 } |
| 177 // is this inside an info (=img) tag? | 221 |
| 178 if (tags.contains(infoTag)) { | 222 // is this inside an digilib info (=img) tag? |
| 223 if (lastTag.equals(imgTag)) { | |
| 179 // then add whatever this is | 224 // then add whatever this is |
| 180 if ((content != null) && (content.length() > 0)) { | 225 if ((content != null) && (content.length() > 0)) { |
| 181 meta.put(name, content.toString().trim()); | 226 meta.put(name, content.toString().trim()); |
| 182 } | 227 } |
| 183 } | 228 content = null; |
| 184 | 229 return; |
| 230 } | |
| 231 | |
| 232 // is this the end of collectTag? | |
| 233 if (name.equals(collectTag)) { | |
| 234 collecting = false; | |
| 235 collectedContent.append("</"+collectTag+">\n"); | |
| 236 // store collected stuff | |
| 237 meta.put(collectTag, collectedContent.toString()); | |
| 238 //logger.debug("collected: '"+collectedContent+"'"); | |
| 239 content = null; | |
| 240 return; | |
| 241 } | |
| 242 | |
| 243 // write collected content | |
| 244 if (collecting) { | |
| 245 String s = ""; | |
| 246 if ((content != null) && (content.length() > 0)) { | |
| 247 s = content.toString().trim(); | |
| 248 } | |
| 249 //logger.debug("collect:"+name+" = "+s); | |
| 250 collectedContent.append(s); | |
| 251 collectedContent.append("</"+name+">\n"); | |
| 252 content = null; | |
| 253 return; | |
| 254 } | |
| 185 } | 255 } |
| 186 | 256 |
| 187 } | 257 } |
| 188 | 258 |
| 189 /** | 259 /** |
