00001
00002
00003
00004
00005
00006 package i2bpro.playlist;
00007
00008 import i2bpro.Exceptions.PlayListException;
00009 import i2bpro.layer.Dialogs.Utils;
00010 import java.io.BufferedReader;
00011 import java.io.BufferedWriter;
00012 import java.io.File;
00013 import java.io.FileNotFoundException;
00014 import java.io.FileReader;
00015 import java.io.FileWriter;
00016 import java.io.IOException;
00017 import java.util.HashMap;
00018 import java.util.regex.Matcher;
00019 import java.util.regex.Pattern;
00020 import org.jaudiotagger.audio.exceptions.CannotReadException;
00021 import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
00022 import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
00023 import org.jaudiotagger.tag.TagException;
00024
00029 public class CUE extends basePlayList
00030 {
00031 HashMap <String, String> albumDaten = new HashMap<String, String>();
00032 CUE(String filename) throws FileNotFoundException, IOException, PlayListException, CannotReadException, TagException, ReadOnlyFileException, InvalidAudioFrameException
00033 {
00034
00035 BufferedReader f;
00036 String line;
00037 String lines = "";
00038 f = new BufferedReader( new FileReader(filename));
00039
00040 playlist.clear();
00041 playlisttype = "cue";
00042
00043 while((line = f.readLine()) != null)
00044 {
00045 lines += line;
00046 }
00047 f.close();
00048 Pattern pattern = Pattern.compile("FILE");
00049 Matcher matcher = pattern.matcher(lines);
00050 if (matcher.find())
00051 {
00052 readAlbum(lines.substring(0,matcher.start()));
00053 readFiles(lines.substring(matcher.start(), lines.length()));
00054 } else {
00055 throw new PlayListException(203);
00056 }
00057 }
00058
00059 private void readAlbum(String substring)
00060 {
00061 albumDaten.clear();
00062
00063
00064 Pattern pattern = Pattern.compile("TITLE\\s+\"");
00065 Matcher matcher = pattern.matcher(substring);
00066 if (matcher.find())
00067 {
00068
00069 albumDaten.put("title", substring.substring(
00070 matcher.end(), substring.indexOf('"', matcher.end())));
00071 } else {
00072 albumDaten.put("title", "");
00073 }
00074
00075
00076 pattern = Pattern.compile("PERFORMER\\s+\"");
00077 matcher = pattern.matcher(substring);
00078 if (matcher.find())
00079 {
00080 albumDaten.put("album_artist", substring.substring(
00081 matcher.end(), substring.indexOf('"', matcher.end())));
00082 } else {
00083 albumDaten.put("album_artist", "");
00084 }
00085 }
00086
00087 private void readFiles(String substring) throws CannotReadException, IOException, TagException, ReadOnlyFileException, InvalidAudioFrameException
00088 {
00089
00090 String[] files = substring.split("FILE");
00091
00092
00093 String file;
00094 Pattern p1 = Pattern.compile("WAVE");
00095 Pattern p2 = Pattern.compile("MP3");
00096 Matcher m1,m2;
00097
00098
00099 int j = 0;
00100 for(int i=0; i<files.length; i++)
00101 {
00102
00103 file = files[i];
00104
00105
00106
00107 m1 = p1.matcher(file);
00108 m2 = p2.matcher(file);
00109 if (m1.find() || m2.find())
00110 {
00111
00112 String filename = file.substring(file.indexOf('"')+1,
00113 file.indexOf('"', file.indexOf('"')+1));
00114
00115
00116 readTracks(file, filename);
00117 }
00118 }
00119 }
00120
00121 private void readTracks(String substring, String filename) throws CannotReadException, IOException, TagException, ReadOnlyFileException, InvalidAudioFrameException
00122 {
00123 Pattern pattern = Pattern.compile("TRACK\\s+\\d+");
00124 Matcher matcher = pattern.matcher(substring);
00125 while(matcher.find())
00126 {
00127 String trackTitle = matcher.group();
00128 int ende = substring.indexOf("TRACK", matcher.end());
00129 if (ende == -1) ende = substring.length();
00130 String track = substring.substring(matcher.end(), ende);
00131 Pattern p = Pattern.compile("\"[\\w\\s]+\"");
00132 Matcher m = p.matcher(track);
00133 String title = null;
00134 if (m.find())
00135 {
00136 title = track.substring(m.start()+1,m.end()-1);
00137 } else {
00138 title = trackTitle;
00139 }
00140 p = Pattern.compile("INDEX\\s+\\d\\d\\s+");
00141 m = p.matcher(track);
00142 String index = "";
00143 if (m.find())
00144 {
00145 int start = m.end();
00146 index = track.substring(start);
00147 p = Pattern.compile("\\d{2}:\\d{2}:\\d{2}");
00148 m = p.matcher(index);
00149 if (m.find()) index = m.group();
00150 }
00151 HashMap<String, String> metadaten = new HashMap<String, String>();
00152 if (Utils.getExtension(new File(filename)).equals(Utils.mp3))
00153 {
00154 metadaten = new ID3v2(filename).getMetaDaten();
00155 } else if (Utils.getExtension(new File(filename)).equals(Utils.wav))
00156 {
00157 metadaten = new WavTag(filename).getMetaDaten();
00158 }
00159 int id = playlist.size();
00160 playlist.put(id, new HashMap<String, String>());
00161 playlist.get(id).putAll(metadaten);
00162 playlist.get(id).put("media", filename);
00163 playlist.get(id).put("title", title);
00164 playlist.get(id).put("album_artist", albumDaten.get("album_artist"));
00165 playlist.get(id).put("album", albumDaten.get("title"));
00166 playlist.get(id).put("index", index);
00167 }
00168 }
00169
00170 public void savePlayList(HashMap<Integer, HashMap<String, String>> list, String filename) throws IOException
00171 {
00172 playlist = list;
00173 BufferedWriter buffer = new BufferedWriter(new FileWriter(new File(filename)));
00174 String line = null;
00175 if (!playlist.isEmpty())
00176 {
00177 if (playlist.get(0).get("album") != null)
00178 {
00179 line = "TITLE \"" + playlist.get(0).get("album") + "\"";
00180 buffer.write(line);
00181 buffer.newLine();
00182 }
00183 if (playlist.get(0).get("album_artist") != null)
00184 {
00185 line = "PERFORMER \"" + playlist.get(0).get("album_artist") + "\"";
00186 buffer.write(line);
00187 buffer.newLine();
00188 }
00189 }
00190 String old_media = null;
00191 int track = 1;
00192 for(int i=0; i<playlist.size(); i++)
00193 {
00194 if (playlist.get(i).get("index").equals("00:00:00"))
00195 {
00196
00197 line = "FILE \"" + playlist.get(i).get("media") + "\" ";
00198 String extension = Utils.getExtension(new File(playlist.get(i).get("media")));
00199 if (extension.equals(Utils.mp3)) line += "MP3";
00200 else if (extension.equals(Utils.wav)) line += "WAVE";
00201 buffer.write(line);
00202 buffer.newLine();
00203
00204
00205 track = 1;
00206 line = " TRACK 01 AUDIO";
00207 buffer.write(line);
00208 buffer.newLine();
00209
00210
00211 line = "\tTITLE \"" + playlist.get(i).get("title") + "\"";
00212 buffer.write(line);
00213 buffer.newLine();
00214
00215
00216 line = "\tINDEX 00 " + playlist.get(i).get("index");
00217 buffer.write(line);
00218 buffer.newLine();
00219 } else {
00220 track++;
00221
00222
00223 line = " TRACK ";
00224 if (track < 10) line += "0";
00225 line += track + " AUDIO";
00226 buffer.write(line);
00227 buffer.newLine();
00228
00229
00230 line = "\tTITLE \"" + playlist.get(i).get("title") + "\"";
00231 buffer.write(line);
00232 buffer.newLine();
00233
00234
00235 line = "\tINDEX 00 " + playlist.get(i).get("index");
00236 buffer.write(line);
00237 buffer.newLine();
00238 }
00239 }
00240 buffer.close();
00241 }
00242 }