| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
package org.jaudiotagger.tag.lyrics3; |
| 24 | |
|
| 25 | |
import org.jaudiotagger.tag.InvalidTagException; |
| 26 | |
import org.jaudiotagger.tag.TagOptionSingleton; |
| 27 | |
import org.jaudiotagger.tag.datatype.ID3v2LyricLine; |
| 28 | |
import org.jaudiotagger.tag.datatype.Lyrics3Line; |
| 29 | |
import org.jaudiotagger.tag.datatype.Lyrics3TimeStamp; |
| 30 | |
import org.jaudiotagger.tag.id3.framebody.FrameBodySYLT; |
| 31 | |
import org.jaudiotagger.tag.id3.framebody.FrameBodyUSLT; |
| 32 | |
|
| 33 | |
import java.io.RandomAccessFile; |
| 34 | |
import java.nio.ByteBuffer; |
| 35 | |
import java.util.ArrayList; |
| 36 | |
import java.util.HashMap; |
| 37 | |
import java.util.Iterator; |
| 38 | |
|
| 39 | |
|
| 40 | |
public class FieldFrameBodyLYR extends AbstractLyrics3v2FieldFrameBody |
| 41 | |
{ |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | 0 | private ArrayList<Lyrics3Line> lines = new ArrayList<Lyrics3Line>(); |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
public FieldFrameBodyLYR() |
| 51 | 0 | { |
| 52 | 0 | } |
| 53 | |
|
| 54 | |
public FieldFrameBodyLYR(FieldFrameBodyLYR copyObject) |
| 55 | |
{ |
| 56 | 0 | super(copyObject); |
| 57 | |
|
| 58 | |
Lyrics3Line old; |
| 59 | |
|
| 60 | 0 | for (int i = 0; i < copyObject.lines.size(); i++) |
| 61 | |
{ |
| 62 | 0 | old = copyObject.lines.get(i); |
| 63 | 0 | this.lines.add(new Lyrics3Line(old)); |
| 64 | |
} |
| 65 | 0 | } |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
public FieldFrameBodyLYR(String line) |
| 73 | 0 | { |
| 74 | 0 | readString(line); |
| 75 | 0 | } |
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
public FieldFrameBodyLYR(FrameBodySYLT sync) |
| 83 | 0 | { |
| 84 | 0 | addLyric(sync); |
| 85 | 0 | } |
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
public FieldFrameBodyLYR(FrameBodyUSLT unsync) |
| 93 | 0 | { |
| 94 | 0 | addLyric(unsync); |
| 95 | 0 | } |
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
public FieldFrameBodyLYR(ByteBuffer byteBuffer) throws InvalidTagException |
| 101 | 0 | { |
| 102 | |
|
| 103 | 0 | this.read(byteBuffer); |
| 104 | |
|
| 105 | 0 | } |
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
public String getIdentifier() |
| 111 | |
{ |
| 112 | 0 | return "LYR"; |
| 113 | |
} |
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
public void setLyric(String str) |
| 119 | |
{ |
| 120 | 0 | readString(str); |
| 121 | 0 | } |
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
public String getLyric() |
| 127 | |
{ |
| 128 | 0 | return writeString(); |
| 129 | |
} |
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
public int getSize() |
| 135 | |
{ |
| 136 | 0 | int size = 0; |
| 137 | |
Lyrics3Line line; |
| 138 | |
|
| 139 | 0 | for (Object line1 : lines) |
| 140 | |
{ |
| 141 | 0 | line = (Lyrics3Line) line1; |
| 142 | 0 | size += (line.getSize() + 2); |
| 143 | |
} |
| 144 | |
|
| 145 | 0 | return size; |
| 146 | |
|
| 147 | |
|
| 148 | |
} |
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
public boolean isSubsetOf(Object obj) |
| 155 | |
{ |
| 156 | 0 | if ((obj instanceof FieldFrameBodyLYR) == false) |
| 157 | |
{ |
| 158 | 0 | return false; |
| 159 | |
} |
| 160 | |
|
| 161 | 0 | ArrayList<Lyrics3Line> superset = ((FieldFrameBodyLYR) obj).lines; |
| 162 | |
|
| 163 | 0 | for (Object line : lines) |
| 164 | |
{ |
| 165 | 0 | if (superset.contains(line) == false) |
| 166 | |
{ |
| 167 | 0 | return false; |
| 168 | |
} |
| 169 | |
} |
| 170 | |
|
| 171 | 0 | return super.isSubsetOf(obj); |
| 172 | |
} |
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
public void addLyric(FrameBodySYLT sync) |
| 178 | |
{ |
| 179 | |
|
| 180 | 0 | Iterator<ID3v2LyricLine> iterator = sync.iterator(); |
| 181 | |
Lyrics3Line newLine; |
| 182 | |
ID3v2LyricLine currentLine; |
| 183 | |
Lyrics3TimeStamp timeStamp; |
| 184 | 0 | HashMap<String, Lyrics3Line> lineMap = new HashMap<String, Lyrics3Line>(); |
| 185 | |
|
| 186 | 0 | while (iterator.hasNext()) |
| 187 | |
{ |
| 188 | 0 | currentLine = iterator.next(); |
| 189 | |
|
| 190 | |
|
| 191 | 0 | currentLine = new ID3v2LyricLine(currentLine); |
| 192 | 0 | timeStamp = new Lyrics3TimeStamp("Time Stamp", this); |
| 193 | 0 | timeStamp.setTimeStamp(currentLine.getTimeStamp(), (byte) sync.getTimeStampFormat()); |
| 194 | |
|
| 195 | 0 | if (lineMap.containsKey(currentLine.getText())) |
| 196 | |
{ |
| 197 | 0 | newLine = lineMap.get(currentLine.getText()); |
| 198 | 0 | newLine.addTimeStamp(timeStamp); |
| 199 | |
} |
| 200 | |
else |
| 201 | |
{ |
| 202 | 0 | newLine = new Lyrics3Line("Lyric Line", this); |
| 203 | 0 | newLine.setLyric(currentLine); |
| 204 | 0 | newLine.setTimeStamp(timeStamp); |
| 205 | 0 | lineMap.put(currentLine.getText(), newLine); |
| 206 | 0 | lines.add(newLine); |
| 207 | |
} |
| 208 | |
} |
| 209 | 0 | } |
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
public void addLyric(FrameBodyUSLT unsync) |
| 215 | |
{ |
| 216 | |
|
| 217 | 0 | Lyrics3Line line = new Lyrics3Line("Lyric Line", this); |
| 218 | 0 | line.setLyric(new String(unsync.getLyric())); |
| 219 | 0 | lines.add(line); |
| 220 | 0 | } |
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
public boolean equals(Object obj) |
| 227 | |
{ |
| 228 | 0 | if ((obj instanceof FieldFrameBodyLYR) == false) |
| 229 | |
{ |
| 230 | 0 | return false; |
| 231 | |
} |
| 232 | |
|
| 233 | 0 | FieldFrameBodyLYR object = (FieldFrameBodyLYR) obj; |
| 234 | |
|
| 235 | 0 | if (this.lines.equals(object.lines) == false) |
| 236 | |
{ |
| 237 | 0 | return false; |
| 238 | |
} |
| 239 | |
|
| 240 | 0 | return super.equals(obj); |
| 241 | |
} |
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
public boolean hasTimeStamp() |
| 247 | |
{ |
| 248 | 0 | boolean present = false; |
| 249 | |
|
| 250 | 0 | for (Object line : lines) |
| 251 | |
{ |
| 252 | 0 | if (((Lyrics3Line) line).hasTimeStamp()) |
| 253 | |
{ |
| 254 | 0 | present = true; |
| 255 | |
} |
| 256 | |
} |
| 257 | |
|
| 258 | 0 | return present; |
| 259 | |
} |
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
public Iterator<Lyrics3Line> iterator() |
| 265 | |
{ |
| 266 | 0 | return lines.iterator(); |
| 267 | |
} |
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
public void read(ByteBuffer byteBuffer) throws InvalidTagException |
| 275 | |
{ |
| 276 | |
String lineString; |
| 277 | |
|
| 278 | 0 | byte[] buffer = new byte[5]; |
| 279 | |
|
| 280 | |
|
| 281 | 0 | byteBuffer.get(buffer, 0, 5); |
| 282 | |
|
| 283 | 0 | int size = Integer.parseInt(new String(buffer, 0, 5)); |
| 284 | |
|
| 285 | 0 | if ((size == 0) && (TagOptionSingleton.getInstance().isLyrics3KeepEmptyFieldIfRead() == false)) |
| 286 | |
{ |
| 287 | 0 | throw new InvalidTagException("Lyircs3v2 Field has size of zero."); |
| 288 | |
} |
| 289 | |
|
| 290 | 0 | buffer = new byte[size]; |
| 291 | |
|
| 292 | |
|
| 293 | 0 | byteBuffer.get(buffer); |
| 294 | 0 | lineString = new String(buffer); |
| 295 | 0 | readString(lineString); |
| 296 | 0 | } |
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
public String toString() |
| 302 | |
{ |
| 303 | 0 | String str = getIdentifier() + " : "; |
| 304 | |
|
| 305 | 0 | for (Object line : lines) |
| 306 | |
{ |
| 307 | 0 | str += line.toString(); |
| 308 | |
} |
| 309 | |
|
| 310 | 0 | return str; |
| 311 | |
} |
| 312 | |
|
| 313 | |
|
| 314 | |
|
| 315 | |
|
| 316 | |
|
| 317 | |
public void write(RandomAccessFile file) throws java.io.IOException |
| 318 | |
{ |
| 319 | 0 | int size = 0; |
| 320 | 0 | int offset = 0; |
| 321 | 0 | byte[] buffer = new byte[5]; |
| 322 | 0 | String str = ""; |
| 323 | |
|
| 324 | 0 | size = getSize(); |
| 325 | 0 | str = Integer.toString(size); |
| 326 | |
|
| 327 | 0 | for (int i = 0; i < (5 - str.length()); i++) |
| 328 | |
{ |
| 329 | 0 | buffer[i] = (byte) '0'; |
| 330 | |
} |
| 331 | |
|
| 332 | 0 | offset += (5 - str.length()); |
| 333 | |
|
| 334 | 0 | for (int i = 0; i < str.length(); i++) |
| 335 | |
{ |
| 336 | 0 | buffer[i + offset] = (byte) str.charAt(i); |
| 337 | |
} |
| 338 | |
|
| 339 | 0 | offset += str.length(); |
| 340 | 0 | file.write(buffer, 0, 5); |
| 341 | |
|
| 342 | 0 | if (size > 0) |
| 343 | |
{ |
| 344 | 0 | str = writeString(); |
| 345 | 0 | buffer = new byte[str.length()]; |
| 346 | |
|
| 347 | 0 | for (int i = 0; i < str.length(); i++) |
| 348 | |
{ |
| 349 | 0 | buffer[i] = (byte) str.charAt(i); |
| 350 | |
} |
| 351 | |
|
| 352 | 0 | file.write(buffer); |
| 353 | |
} |
| 354 | 0 | } |
| 355 | |
|
| 356 | |
|
| 357 | |
|
| 358 | |
|
| 359 | |
private void readString(String lineString) |
| 360 | |
{ |
| 361 | |
|
| 362 | 0 | String token = ""; |
| 363 | 0 | int offset = 0; |
| 364 | 0 | int delim = lineString.indexOf(Lyrics3v2Fields.CRLF); |
| 365 | 0 | lines = new ArrayList<Lyrics3Line>(); |
| 366 | |
|
| 367 | |
Lyrics3Line line; |
| 368 | |
|
| 369 | 0 | while (delim >= 0) |
| 370 | |
{ |
| 371 | 0 | token = lineString.substring(offset, delim); |
| 372 | 0 | line = new Lyrics3Line("Lyric Line", this); |
| 373 | 0 | line.setLyric(token); |
| 374 | 0 | lines.add(line); |
| 375 | 0 | offset = delim + Lyrics3v2Fields.CRLF.length(); |
| 376 | 0 | delim = lineString.indexOf(Lyrics3v2Fields.CRLF, offset); |
| 377 | |
} |
| 378 | |
|
| 379 | 0 | if (offset < lineString.length()) |
| 380 | |
{ |
| 381 | 0 | token = lineString.substring(offset); |
| 382 | 0 | line = new Lyrics3Line("Lyric Line", this); |
| 383 | 0 | line.setLyric(token); |
| 384 | 0 | lines.add(line); |
| 385 | |
} |
| 386 | 0 | } |
| 387 | |
|
| 388 | |
|
| 389 | |
|
| 390 | |
|
| 391 | |
private String writeString() |
| 392 | |
{ |
| 393 | |
Lyrics3Line line; |
| 394 | 0 | String str = ""; |
| 395 | |
|
| 396 | 0 | for (Object line1 : lines) |
| 397 | |
{ |
| 398 | 0 | line = (Lyrics3Line) line1; |
| 399 | 0 | str += (line.writeString() + Lyrics3v2Fields.CRLF); |
| 400 | |
} |
| 401 | |
|
| 402 | 0 | return str; |
| 403 | |
|
| 404 | |
|
| 405 | |
} |
| 406 | |
|
| 407 | |
|
| 408 | |
|
| 409 | |
|
| 410 | |
protected void setupObjectList() |
| 411 | |
{ |
| 412 | |
|
| 413 | 0 | } |
| 414 | |
} |