package com.bluedragontavern; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; import org.jdom.output.XMLOutputter; import org.jaxen.jdom.XPath; import java.util.List; import java.util.Iterator; import java.io.File; import java.io.FileWriter; import org.apache.regexp.RE; import com.bluedragontavern.Constants; /** * The purpose of this script is to search for artists/ablums/songs * which dont yet exist in the dat.xml file and add them and also * to remove artists/albums/songs from the dat.xml file which no * longer exist in the file structure. */ public class RebuildXML { //Variables for reporting changes static int artistsRemoved = 0; static int albumsRemoved = 0; static int songsRemoved = 0; static int artistsAdded = 0; static int albumsAdded = 0; static int songsAdded = 0; static Document doc = null; static Element rootElem = null; public static void main(String[] args) { try { long startTime = System.currentTimeMillis(); //Locate the dat.xml file File datFile = new File(Constants.CGIBIN + "/" + Constants.DATFILE); SAXBuilder builder = new SAXBuilder(); doc = builder.build(datFile); rootElem = doc.getRootElement(); //Check if any have been removed checkRemoved(); //Check if any have been added checkAdded(); System.out.println("Total time = " + ((System.currentTimeMillis() - startTime)* .001) + " Seconds"); System.out.println("Results\n--------------"); System.out.println("Num artists removed = " + artistsRemoved); System.out.println("Num albums removed = " + albumsRemoved); System.out.println("Num songs removed = " + songsRemoved); System.out.println("Num artists added = " + artistsAdded); System.out.println("Num albums added = " + albumsAdded); System.out.println("Num songs added = " + songsAdded); //Store the old file to a backup File datXML = new File(Constants.CGIBIN + File.separator + "dat.xml"); File bckDatXML = new File(Constants.CGIBIN + File.separator + "dat_bck.xml"); datXML.renameTo(bckDatXML); //Write out the new file File newDatXML = new File(Constants.CGIBIN + File.separator + "dat.xml"); FileWriter fw = new FileWriter(datXML); XMLOutputter xmlOutputter = new XMLOutputter(" ", true); xmlOutputter.setTextNormalize(true); xmlOutputter.output(doc, fw); fw.flush(); fw.close(); } catch (Exception e) { System.out.println(e.toString()); e.printStackTrace(System.out); System.exit(1); } } private static void checkRemoved() { try { List artists = rootElem.getChildren("Artist"); Iterator artistIter = artists.iterator(); while(artistIter.hasNext()) { Element artistElem = (Element)artistIter.next(); String artistName = artistElem.getAttributeValue("name"); //Substitute "&" with "&" RE re = new RE("&"); re.subst(artistName, "&"); File artistFile = new File(Constants.MP3ROOT + File.separator + artistName); if (!artistFile.exists()) { System.out.println("Artist REMOVED = " + artistName); ++artistsRemoved; rootElem.removeContent(artistElem); continue; } List albums = artistElem.getChildren("Album"); Iterator albumIter = albums.iterator(); while(albumIter.hasNext()) { Element albumElem = (Element)albumIter.next(); //Check if the album still exists String albumName = albumElem.getAttributeValue("name"); //Substitute "&" with "&" re.subst(albumName, "&"); File albumFile = new File(Constants.MP3ROOT + File.separator + artistName + File.separator + albumName); if (! albumFile.exists()) { System.out.println("Album REMOVED = " + albumName); ++albumsRemoved; artistElem.removeContent(albumElem); continue; } List songs = albumElem.getChildren("Song"); Iterator songIter = songs.iterator(); while(songIter.hasNext()) { Element songElem = (Element)songIter.next(); //Check if the album still exists String songName = songElem.getAttributeValue("name"); //Substitute "&" with "&" re.subst(songName, "&"); File songFile = new File(Constants.MP3ROOT + File.separator + artistName + File.separator + albumName + File.separator + songName); if (! songFile.exists()) { System.out.println("Song REMOVED = " + songName); ++songsRemoved; albumElem.removeContent(songElem); continue; } } } List songs = artistElem.getChildren("Song"); Iterator songIter = songs.iterator(); while(songIter.hasNext()) { Element songElem = (Element)songIter.next(); //Check if the album still exists String songName = songElem.getAttributeValue("name"); //Substitute "&" with "&" re.subst(songName, "&"); File songFile = new File(Constants.MP3ROOT + File.separator + artistName + File.separator + songName); if (! songFile.exists()) { System.out.println("Song REMOVED = " + songName); ++songsRemoved; artistElem.removeContent(songElem); continue; } } } } catch (Exception e) { System.out.println(e.toString()); e.printStackTrace(System.out); System.exit(1); } } /** * Check for added artists, albums, and songs. */ private static void checkAdded() { /************************************************** * Now, go through and add anything that needs it * **************************************************/ try { File mp3Root = new File(Constants.MP3ROOT); File[] artists = mp3Root.listFiles(); for(int artistIndex=0; artistIndex < artists.length; ++artistIndex) { if (artists[artistIndex].isDirectory()) { //Make sure the artist exists in the dat file String artistName = artists[artistIndex].getName(); //Substitute "&" with "&" RE re = new RE("&"); re.subst(artistName, "&"); XPath xpath = new XPath("/mp3s/Artist[@name=\"" + artistName + "\"]"); Element artistElem = (Element)xpath.selectSingleNode(doc); if (artistElem == null) { artistElem = new Element("Artist"); artistElem.setAttribute("name", artistName); rootElem.addContent(artistElem); System.out.println("Artist ADDED = " + artistName); ++artistsAdded; } //Check Artist checkArtist(artists[artistIndex], artistElem, artistName); } } } catch (Exception e) { System.out.println(e.toString()); e.printStackTrace(System.out); System.exit(1); } } /** * Check inside of artist directory for added albums and songs. */ private static void checkArtist(File artist, Element artistElem, String artistName) { try { File[] albumsAndSongs = artist.listFiles(); //Loop through and check albums for(int albumIndex=0; albumIndex < albumsAndSongs.length; ++albumIndex) { if (albumsAndSongs[albumIndex].isDirectory()) { //Make sure the album exists in the dat file String albumName = albumsAndSongs[albumIndex].getName(); //Substitute "&" with "&" RE re = new RE("&"); re.subst(albumName, "&"); XPath xpath = new XPath("/mp3s/Artist[@name=\"" + artistName + "\"]/Album[@name=\"" + albumName + "\"]"); Element albumElem = (Element)xpath.selectSingleNode(doc); if (albumElem == null) { albumElem = new Element("Album"); albumElem.setAttribute("name", albumName); artistElem.addContent(albumElem); System.out.println("Album ADDED = " + albumName); ++albumsAdded; } //Check Album checkAlbum(albumsAndSongs[albumIndex], albumElem, albumName, artistName); } } //Loop through and check songs for(int songIndex=0; songIndex < albumsAndSongs.length; ++songIndex) { if (albumsAndSongs[songIndex].isFile()) { //Make sure the song exists in the dat file String songName = albumsAndSongs[songIndex].getName(); //Make sure the song ends with ".mp3" if (!songName.endsWith(".mp3")) continue; //Substitute "&" with "&" RE re = new RE("&"); re.subst(songName, "&"); XPath xpath = new XPath("mp3s/Artist[@name=\"" + artistName + "\"]/Song[@name=\"" + songName + "\"]"); Element songElem = (Element)xpath.selectSingleNode(doc); if (songElem == null) { songElem = new Element("Song"); songElem.setAttribute("name", songName); artistElem.addContent(songElem); System.out.println("Song ADDED = " + songName); ++songsAdded; } } } } catch (Exception e) { System.out.println(e.toString()); e.printStackTrace(System.out); System.exit(1); } } /** * Check inside of album directory for added songs. */ private static void checkAlbum(File album, Element albumElem, String albumName, String artistName) { try { File[] songs = album.listFiles(); for(int songIndex=0; songIndex < songs.length; ++songIndex) { if (songs[songIndex].isFile()) { //Make sure the song exists in the dat file String songName = songs[songIndex].getName(); //Make sure the song ends with ".mp3" if (!songName.endsWith(".mp3")) continue; //Substitute "&" with "&" RE re = new RE("&"); re.subst(songName, "&"); XPath xpath = new XPath("/mp3s/Artist[@name=\"" + artistName + "\"]/Album[@name=\"" + albumName + "\"]/Song[@name=\"" + songName + "\"]"); Element songElem = (Element)xpath.selectSingleNode(doc); if (songElem == null) { songElem = new Element("Song"); songElem.setAttribute("name", songName); albumElem.addContent(songElem); System.out.println("Song ADDED = " + songName); ++songsAdded; } } } } catch (Exception e) { System.out.println(e.toString()); e.printStackTrace(System.out); System.exit(1); } } } //End of class RebuildXML