| 1 | package org.farng.mp3.lyrics3; |
| 2 | |
| 3 | import org.farng.mp3.InvalidTagException; |
| 4 | import org.farng.mp3.TagConstant; |
| 5 | import org.farng.mp3.TagOptionSingleton; |
| 6 | import org.farng.mp3.id3.FrameBodySYLT; |
| 7 | import org.farng.mp3.id3.FrameBodyUSLT; |
| 8 | import org.farng.mp3.object.AbstractMP3Object; |
| 9 | import org.farng.mp3.object.ObjectID3v2LyricLine; |
| 10 | import org.farng.mp3.object.ObjectLyrics3Line; |
| 11 | import org.farng.mp3.object.ObjectLyrics3TimeStamp; |
| 12 | |
| 13 | import java.io.RandomAccessFile; |
| 14 | import java.util.ArrayList; |
| 15 | import java.util.HashMap; |
| 16 | import java.util.Iterator; |
| 17 | |
| 18 | /** |
| 19 | * Lyrics multi line text. Timestamps can be used anywhere in the text in any order. Timestamp format is [mm:ss] (no |
| 20 | * spaces allowed in the timestamps). |
| 21 | * |
| 22 | * @author Eric Farng |
| 23 | * @version $Revision: 1.6 $ |
| 24 | */ |
| 25 | public class FieldBodyLYR extends AbstractLyrics3v2FieldBody { |
| 26 | |
| 27 | private ArrayList lines = new ArrayList(); |
| 28 | |
| 29 | /** |
| 30 | * Creates a new FieldBodyLYR object. |
| 31 | */ |
| 32 | public FieldBodyLYR() { |
| 33 | super(); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Creates a new FieldBodyLYR object. |
| 38 | */ |
| 39 | public FieldBodyLYR(final FieldBodyLYR copyObject) { |
| 40 | super(copyObject); |
| 41 | ObjectLyrics3Line oldObject; |
| 42 | for (int i = 0; i < copyObject.lines.size(); i++) { |
| 43 | oldObject = (ObjectLyrics3Line) copyObject.lines.get(i); |
| 44 | AbstractMP3Object newObject = new ObjectLyrics3Line(oldObject); |
| 45 | this.lines.add(newObject); |
| 46 | // appendToObjectList(newObject); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Creates a new FieldBodyLYR object. |
| 52 | */ |
| 53 | public FieldBodyLYR(final String line) { |
| 54 | readString(line); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Creates a new FieldBodyLYR object. |
| 59 | */ |
| 60 | public FieldBodyLYR(final FrameBodySYLT sync) { |
| 61 | addLyric(sync); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Creates a new FieldBodyLYR object. |
| 66 | */ |
| 67 | public FieldBodyLYR(final FrameBodyUSLT unsync) { |
| 68 | addLyric(unsync); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Creates a new FieldBodyLYR object. |
| 73 | */ |
| 74 | public FieldBodyLYR(final RandomAccessFile file) throws InvalidTagException, java.io.IOException { |
| 75 | this.read(file); |
| 76 | } |
| 77 | |
| 78 | public String getIdentifier() { |
| 79 | return "LYR"; |
| 80 | } |
| 81 | |
| 82 | public void setLyric(final String str) { |
| 83 | readString(str); |
| 84 | } |
| 85 | |
| 86 | public String getLyric() { |
| 87 | return writeString(); |
| 88 | } |
| 89 | |
| 90 | public int getSize() { |
| 91 | int size = 0; |
| 92 | ObjectLyrics3Line line; |
| 93 | for (int i = 0; i < this.lines.size(); i++) { |
| 94 | line = (ObjectLyrics3Line) this.lines.get(i); |
| 95 | size += (line.getSize() + 2); |
| 96 | } |
| 97 | return size; |
| 98 | } |
| 99 | |
| 100 | public boolean isSubsetOf(final Object object) { |
| 101 | if ((object instanceof FieldBodyLYR) == false) { |
| 102 | return false; |
| 103 | } |
| 104 | final ArrayList superset = ((FieldBodyLYR) object).lines; |
| 105 | for (int i = 0; i < this.lines.size(); i++) { |
| 106 | if (superset.contains(this.lines.get(i)) == false) { |
| 107 | return false; |
| 108 | } |
| 109 | } |
| 110 | return super.isSubsetOf(object); |
| 111 | } |
| 112 | |
| 113 | public void addLyric(final FrameBodySYLT sync) { |
| 114 | // SYLT frames are made of individual lines |
| 115 | final Iterator iterator = sync.iterator(); |
| 116 | ObjectLyrics3Line newLine; |
| 117 | ObjectID3v2LyricLine currentLine; |
| 118 | ObjectLyrics3TimeStamp timeStamp; |
| 119 | final HashMap lineMap = new HashMap(); |
| 120 | while (iterator.hasNext()) { |
| 121 | currentLine = (ObjectID3v2LyricLine) iterator.next(); |
| 122 | |
| 123 | // create copy to use in new tag |
| 124 | currentLine = new ObjectID3v2LyricLine(currentLine); |
| 125 | timeStamp = new ObjectLyrics3TimeStamp("Time Stamp"); |
| 126 | timeStamp.setTimeStamp(currentLine.getTimeStamp(), sync.getTimeStampFormat()); |
| 127 | if (lineMap.containsKey(currentLine.getText())) { |
| 128 | newLine = (ObjectLyrics3Line) lineMap.get(currentLine.getText()); |
| 129 | newLine.addTimeStamp(timeStamp); |
| 130 | } else { |
| 131 | newLine = new ObjectLyrics3Line("Lyric Line"); |
| 132 | newLine.setLyric(currentLine); |
| 133 | newLine.setTimeStamp(timeStamp); |
| 134 | lineMap.put(currentLine.getText(), newLine); |
| 135 | this.lines.add(newLine); |
| 136 | // appendToObjectList(newLine); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | public void addLyric(final FrameBodyUSLT unsync) { |
| 142 | // USLT frames are just long text string; |
| 143 | final ObjectLyrics3Line line = new ObjectLyrics3Line("Lyric Line"); |
| 144 | line.setLyric(new String(unsync.getLyric())); |
| 145 | this.lines.add(line); |
| 146 | appendToObjectList(line); |
| 147 | } |
| 148 | |
| 149 | public boolean equals(final Object obj) { |
| 150 | if ((obj instanceof FieldBodyLYR) == false) { |
| 151 | return false; |
| 152 | } |
| 153 | final FieldBodyLYR fieldBodyLYR = (FieldBodyLYR) obj; |
| 154 | if (this.lines.equals(fieldBodyLYR.lines) == false) { |
| 155 | return false; |
| 156 | } |
| 157 | // return true; |
| 158 | // we dont' want to call super here. super looks for equal object list |
| 159 | // we don't care about object lists for LYR field |
| 160 | return super.equals(obj); |
| 161 | } |
| 162 | |
| 163 | public boolean hasTimeStamp() { |
| 164 | boolean present = false; |
| 165 | for (int i = 0; i < this.lines.size(); i++) { |
| 166 | if (((ObjectLyrics3Line) this.lines.get(i)).hasTimeStamp()) { |
| 167 | present = true; |
| 168 | } |
| 169 | } |
| 170 | return present; |
| 171 | } |
| 172 | |
| 173 | public Iterator iterator() { |
| 174 | return this.lines.iterator(); |
| 175 | } |
| 176 | |
| 177 | protected void setupObjectList() { |
| 178 | // throw new UnsupportedOperationException(); |
| 179 | } |
| 180 | |
| 181 | public void read(final RandomAccessFile file) throws InvalidTagException, java.io.IOException { |
| 182 | final String lineString; |
| 183 | byte[] buffer = new byte[5]; |
| 184 | |
| 185 | // read the 5 character size |
| 186 | file.read(buffer, 0, 5); |
| 187 | final int size = Integer.parseInt(new String(buffer, 0, 5)); |
| 188 | if ((size == 0) && (TagOptionSingleton.getInstance().isLyrics3KeepEmptyFieldIfRead() == false)) { |
| 189 | throw new InvalidTagException("Lyircs3v2 Field has size of zero."); |
| 190 | } |
| 191 | buffer = new byte[size]; |
| 192 | |
| 193 | // read the SIZE length description |
| 194 | file.read(buffer); |
| 195 | lineString = new String(buffer); |
| 196 | readString(lineString); |
| 197 | } |
| 198 | |
| 199 | public String toString() { |
| 200 | String str = getIdentifier() + " : "; |
| 201 | for (int i = 0; i < this.lines.size(); i++) { |
| 202 | str += this.lines.get(i).toString(); |
| 203 | } |
| 204 | return str; |
| 205 | } |
| 206 | |
| 207 | public void write(final RandomAccessFile file) throws java.io.IOException { |
| 208 | final int size; |
| 209 | int offset = 0; |
| 210 | byte[] buffer = new byte[5]; |
| 211 | String str; |
| 212 | size = getSize(); |
| 213 | str = Integer.toString(size); |
| 214 | for (int i = 0; i < (5 - str.length()); i++) { |
| 215 | buffer[i] = (byte) '0'; |
| 216 | } |
| 217 | offset += (5 - str.length()); |
| 218 | for (int i = 0; i < str.length(); i++) { |
| 219 | buffer[i + offset] = (byte) str.charAt(i); |
| 220 | } |
| 221 | offset += str.length(); |
| 222 | file.write(buffer, 0, 5); |
| 223 | if (size > 0) { |
| 224 | str = writeString(); |
| 225 | buffer = new byte[str.length()]; |
| 226 | for (int i = 0; i < str.length(); i++) { |
| 227 | buffer[i] = (byte) str.charAt(i); |
| 228 | } |
| 229 | file.write(buffer); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | private void readString(final String lineString) { |
| 234 | // now readString each line and put in the vector; |
| 235 | String token; |
| 236 | int offset = 0; |
| 237 | int delim = lineString.indexOf(TagConstant.SEPERATOR_LINE); |
| 238 | this.lines = new ArrayList(); |
| 239 | ObjectLyrics3Line line; |
| 240 | while (delim >= 0) { |
| 241 | token = lineString.substring(offset, delim); |
| 242 | line = new ObjectLyrics3Line("Lyric Line"); |
| 243 | line.setLyric(token); |
| 244 | this.lines.add(line); |
| 245 | appendToObjectList(line); |
| 246 | offset = delim + TagConstant.SEPERATOR_LINE.length(); |
| 247 | delim = lineString.indexOf(TagConstant.SEPERATOR_LINE, offset); |
| 248 | } |
| 249 | if (offset < lineString.length()) { |
| 250 | token = lineString.substring(offset); |
| 251 | line = new ObjectLyrics3Line("Lyric Line"); |
| 252 | line.setLyric(token); |
| 253 | this.lines.add(line); |
| 254 | appendToObjectList(line); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | private String writeString() { |
| 259 | ObjectLyrics3Line line; |
| 260 | String str = ""; |
| 261 | for (int i = 0; i < this.lines.size(); i++) { |
| 262 | line = (ObjectLyrics3Line) this.lines.get(i); |
| 263 | str += (line.writeString() + TagConstant.SEPERATOR_LINE); |
| 264 | } |
| 265 | return str; |
| 266 | } |
| 267 | } |