View Javadoc

1   // Copyright (C) 2004, Brian Enigma <enigma at netninja.com>
2   // This file is part of iGallery.
3   //
4   // iGallery is free software; you can redistribute it and/or modify
5   // it under the terms of the GNU General Public License as published by
6   // the Free Software Foundation; either version 2 of the License, or
7   // (at your option) any later version.
8   //
9   // iGallery is distributed in the hope that it will be useful,
10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  // GNU General Public License for more details.
13  //
14  // You should have received a copy of the GNU General Public License
15  // along with Foobar; if not, write to the Free Software
16  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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                  // Check if it's a key
84                  if (currentTag.equals("key")) {
85                      value = value.trim();
86                      currentKey = value;
87                  }
88                  // Check if it's a value, then record it associated with the key, if 
89                  // it's a key we care about
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                 // If we're recording photograph key IDs, add them
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                 // If we're at the end of the photograph key IDs, stop recording
110                 if (recordPhotoIds && qName.equals("array"))
111                     recordPhotoIds = false;
112                 // If we're at the end of the album record, store it
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         // In capture-picture mode
122         if (mode == MODE_CAPTURE_PICTURES) {
123             // If we aren't currently capturing a picture record, start one if this is the start of one
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             // If this is the end of the picture record, save it
148             if ((currentPicture != null) && (qName.equals("dict"))) {
149                 //System.out.println(currentPicture);
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 }