1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.ninjasoft.igallery.logic;
18
19 import java.io.*;
20 import java.util.*;
21
22 import org.ninjasoft.igallery.data.*;
23 import org.xml.sax.Attributes;
24 import org.xml.sax.SAXException;
25 import org.xml.sax.helpers.DefaultHandler;
26 import javax.xml.parsers.*;
27
28 /***
29 * SAX handler to parse the AlbumData.xml file
30 * @author enigma
31 */
32 public class AlbumSaxHandler extends DefaultHandler {
33 private static final int MODE_NONE = 0;
34 private static final int MODE_CAPTURE_ALBUMS = 1;
35 private static final int MODE_CAPTURE_PICTURES = 2;
36 public int mode = MODE_NONE;
37 private StringBuffer characterData = new StringBuffer();
38 private boolean recordCharacterData = false;
39 private boolean recordPhotoIds = false;
40 private String currentTag = "";
41 private String currentKey = "";
42 private IPhotoAlbum currentAlbum = new IPhotoAlbum();
43 private IPhotoPicture currentPicture = null;
44 private int throttleCount = 0;
45 private Vector albums = new Vector();
46 private Hashtable pictureHash = new Hashtable();
47
48 private void throttle() {
49 if (throttleCount++ > 60)
50 System.exit(0);
51 }
52
53 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
54 super.startElement(uri, localName, qName, attributes);
55 currentTag = qName;
56 if (currentTag.equals("key"))
57 recordCharacterData = true;
58 if ((currentKey.length() > 0) && (qName.equals("string")))
59 recordCharacterData = true;
60 if (recordPhotoIds)
61 recordCharacterData = true;
62 if (mode == MODE_CAPTURE_ALBUMS) {
63 if ((currentAlbum == null) && qName.equals("dict"))
64 currentAlbum = new IPhotoAlbum();
65 }
66 }
67 public void endElement(String uri, String localName, String qName) throws SAXException {
68 super.endElement(uri, localName, qName);
69 recordCharacterData = false;
70 String value = characterData.toString();
71 characterData.delete(0, characterData.length());
72 if (currentTag.equals("key")) {
73 if ((mode == MODE_NONE) && value.equals("List of Albums")) {
74 System.out.println("Album mode");
75 mode = MODE_CAPTURE_ALBUMS;
76 } if ((mode == MODE_CAPTURE_ALBUMS) && value.equals("Master Image List")) {
77 System.out.println("Picture mode");
78 mode = MODE_CAPTURE_PICTURES;
79 }
80 }
81 if (mode == MODE_CAPTURE_ALBUMS) {
82 if (currentAlbum != null) {
83
84 if (currentTag.equals("key")) {
85 value = value.trim();
86 currentKey = value;
87 }
88
89
90 if (currentTag.equals("string") && (currentKey.length() > 0)) {
91 value = value.trim();
92 if (currentKey.equals("AlbumName"))
93 currentAlbum.setName(value);
94 else if (currentKey.equals("Album Type"))
95 currentAlbum.setType(value);
96 else if (currentKey.equals("KeyList"))
97 recordPhotoIds = true;
98 currentKey = "";
99 }
100
101 if (recordPhotoIds && (currentTag.equals("string"))) {
102 int id = 0;
103 try{
104 id = Integer.parseInt(value);
105 }catch(Throwable t){}
106 if (id > 0)
107 currentAlbum.addPicture(id);
108 }
109
110 if (recordPhotoIds && qName.equals("array"))
111 recordPhotoIds = false;
112
113 if ((currentAlbum != null) && (qName.equals("dict"))) {
114 System.out.println(currentAlbum);
115 albums.add(currentAlbum);
116 currentAlbum = null;
117 currentKey = "";
118 }
119 }
120 }
121
122 if (mode == MODE_CAPTURE_PICTURES) {
123
124 if ((currentPicture == null) && currentTag.equals("key")) {
125 int id = 0;
126 try{
127 id = Integer.parseInt(value);
128 }catch(Throwable t){}
129 if (id > 0) {
130 currentPicture = new IPhotoPicture();
131 currentPicture.setId(id);
132 currentKey = "";
133 }
134 }
135 if ((currentPicture != null) && (currentTag.equals("key"))) {
136 value = value.trim();
137 currentKey = value;
138 }
139 if ((currentPicture != null) && currentTag.equals("string") && (currentKey.length() > 0)) {
140 value = value.trim();
141 if (currentKey.equals("Caption"))
142 currentPicture.setCaption(value);
143 else if (currentKey.equals("ImagePath"))
144 currentPicture.setFilename(value);
145 currentKey = "";
146 }
147
148 if ((currentPicture != null) && (qName.equals("dict"))) {
149
150 pictureHash.put(currentPicture.getId(), currentPicture);
151 currentPicture = null;
152 currentKey = "";
153 }
154 }
155 currentTag = "";
156 }
157 public void characters(char[] data, int start, int length) throws SAXException {
158 super.characters(data, start, length);
159 if (recordCharacterData)
160 characterData.append(data, start, length);
161 }
162
163 public Vector getAlbums() {
164 return this.albums;
165 }
166
167 public Hashtable getPictureHash() {
168 return this.pictureHash;
169 }
170
171 public static void main(String[] argv) throws Exception {
172 AlbumSaxHandler handler = new AlbumSaxHandler();
173 System.out.println("Scanning...");
174 SAXParserFactory.newInstance().newSAXParser().parse(
175 new FileInputStream("/Users/enigma/Desktop/AlbumDataCopy.xml"),
176 handler);
177 System.out.println("Obtained data about " + handler.getAlbums().size() + " album(s), " + handler.getPictureHash().size() + " picture(s)");
178 }
179 }