Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

MusicObject complete.

  • Loading branch information...
commit 984f78bd0ad18742cd988e8cd5d81a5e39295b19 1 parent bd0734e
@gatesphere authored
Showing with 59 additions and 40 deletions.
  1. +59 −40 src/MusicObject.java
View
99 src/MusicObject.java
@@ -10,7 +10,13 @@
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
+enum Mode {
+ MODE_NONE, MODE_ARTIST, MODE_ALBUM_ARTIST, MODE_ALBUM, MODE_TRACK,
+ MODE_TITLE, MODE_YEAR, MODE_GENRE, MODE_TRACK_LENGTH
+ }
+
public class MusicObject implements Serializable {
+
private int id;
private String artist;
private String album_artist;
@@ -31,56 +37,40 @@
public String getGenre() {return genre;}
public int getTrackLength() {return track_length;}
- private MusicObject(int id, String artist, String album_artist, String album,
- int track, String title, int year, String genre, int track_length) {
- this.id = id;
- this.artist = artist;
- this.album_artist = album_artist;
- this.album = album;
- this.track = track;
- this.title = title;
- this.year = year;
- this.genre = genre;
- this.track_length = track_length;
- }
+ // setters... needed for factory method :(
+ public void setId(int id) {this.id = id;}
+ public void setArtist(String artist) {this.artist = artist;}
+ public void setAlbumArtist(String album_artist) {this.album_artist = album_artist;}
+ public void setAlbum(String album) {this.album = album;}
+ public void setTrack(int track) {this.track = track;}
+ public void setTitle(String title) {this.title = title;}
+ public void setYear(int year) {this.year = year;}
+ public void setGenre(String genre) {this.genre = genre;}
+ public void setTrackLength(int track_length) {this.track_length = track_length;}
+
+ private MusicObject(){}
public static MusicObject buildFromFile(File f, int id) {
+ final MusicObject mobj = new MusicObject();
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() {
+ Mode mode = Mode.MODE_NONE;
public void startElement(String uri, String localName,String qName,
Attributes attributes) throws SAXException {
//System.out.println("Start Element :" + qName);
- if (qName.equalsIgnoreCase("SONG")) {
-
- }
- if (qName.equalsIgnoreCase("ARTIST")) {
-
- }
- if (qName.equalsIgnoreCase("ALBUM_ARTIST")) {
-
- }
- if (qName.equalsIgnoreCase("ALBUM")) {
-
- }
- if (qName.equalsIgnoreCase("TRACK")) {
-
- }
- if (qName.equalsIgnoreCase("TITLE")) {
-
- }
- if (qNname.equalsIgnoreCase("YEAR")) {
-
- }
- if (qName.equalsIgnoreCase("GENRE")) {
-
- }
- if (qName.equalsIgnoreCase("TRACK_NAME")) {
-
- }
+ if (qName.equalsIgnoreCase("SONG")) {}
+ if (qName.equalsIgnoreCase("ARTIST")) {mode = Mode.MODE_ARTIST;}
+ if (qName.equalsIgnoreCase("ALBUM_ARTIST")) {mode = Mode.MODE_ALBUM_ARTIST;}
+ if (qName.equalsIgnoreCase("ALBUM")) {mode = Mode.MODE_ALBUM;}
+ if (qName.equalsIgnoreCase("TRACK")) {mode = Mode.MODE_TRACK;}
+ if (qName.equalsIgnoreCase("TITLE")) {mode = Mode.MODE_TITLE;}
+ if (qName.equalsIgnoreCase("YEAR")) {mode = Mode.MODE_YEAR;}
+ if (qName.equalsIgnoreCase("GENRE")) {mode = Mode.MODE_GENRE;}
+ if (qName.equalsIgnoreCase("TRACK_LENGTH")) {mode = Mode.MODE_TRACK_LENGTH;}
}
public void endElement(String uri, String localName,
@@ -90,6 +80,34 @@ public void endElement(String uri, String localName,
public void characters(char ch[], int start, int length) throws SAXException {
// read info
+ switch(mode) {
+ case MODE_ARTIST:
+ mobj.setArtist(new String(ch));
+ break;
+ case MODE_ALBUM_ARTIST:
+ mobj.setAlbumArtist(new String(ch));
+ break;
+ case MODE_ALBUM:
+ mobj.setAlbum(new String(ch));
+ break;
+ case MODE_TRACK:
+ mobj.setTrack(Integer.parseInt(new String(ch)));
+ break;
+ case MODE_TITLE:
+ mobj.setTitle(new String(ch));
+ break;
+ case MODE_YEAR:
+ mobj.setYear(Integer.parseInt(new String(ch)));
+ break;
+ case MODE_GENRE:
+ mobj.setGenre(new String(ch));
+ break;
+ case MODE_TRACK_LENGTH:
+ mobj.setTrackLength(Integer.parseInt(new String(ch)));
+ break;
+ default:
+ break; // unnecessary, but for symmetry
+ }
}
};
@@ -98,6 +116,7 @@ public void characters(char ch[], int start, int length) throws SAXException {
e.printStackTrace();
}
- return new MusicObject(id, artist, album_artist, album, track, title, year, genre, track_length);
+ mobj.setId(id);
+ return mobj;
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.