| 1 | package org.farng.mp3.object; |
| 2 | |
| 3 | import java.util.Iterator; |
| 4 | import java.util.LinkedList; |
| 5 | |
| 6 | /** |
| 7 | * ID3v2 and Lyrics3v2 tags have individual fields <code>AbstractMP3Fragment</code>s Then each fragment is broken down |
| 8 | * in to individual <code>AbstractMP3Object</code>s |
| 9 | * |
| 10 | * @author Eric Farng |
| 11 | * @version $Revision: 1.5 $ |
| 12 | */ |
| 13 | public class ObjectLyrics3Line extends AbstractMP3Object { |
| 14 | |
| 15 | private LinkedList timeStamp = new LinkedList(); |
| 16 | private String lyric = ""; |
| 17 | |
| 18 | /** |
| 19 | * Creates a new ObjectLyrics3Line object. |
| 20 | */ |
| 21 | public ObjectLyrics3Line(final String identifier) { |
| 22 | this.identifier = identifier; |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Creates a new ObjectLyrics3Line object. |
| 27 | */ |
| 28 | public ObjectLyrics3Line(final ObjectLyrics3Line copyObject) { |
| 29 | super(copyObject); |
| 30 | this.lyric = new String(copyObject.lyric); |
| 31 | ObjectLyrics3TimeStamp newTimeStamp; |
| 32 | for (int i = 0; i < copyObject.timeStamp.size(); i++) { |
| 33 | newTimeStamp = new ObjectLyrics3TimeStamp((ObjectLyrics3TimeStamp) copyObject.timeStamp.get(i)); |
| 34 | this.timeStamp.add(newTimeStamp); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | public void setLyric(final String lyric) { |
| 39 | this.lyric = lyric; |
| 40 | } |
| 41 | |
| 42 | public void setLyric(final ObjectID3v2LyricLine line) { |
| 43 | this.lyric = line.getText(); |
| 44 | } |
| 45 | |
| 46 | public String getLyric() { |
| 47 | return this.lyric; |
| 48 | } |
| 49 | |
| 50 | public int getSize() { |
| 51 | int size = 0; |
| 52 | for (int i = 0; i < this.timeStamp.size(); i++) { |
| 53 | size += ((ObjectLyrics3TimeStamp) this.timeStamp.get(i)).getSize(); |
| 54 | } |
| 55 | return size + this.lyric.length(); |
| 56 | } |
| 57 | |
| 58 | public void setTimeStamp(final ObjectLyrics3TimeStamp time) { |
| 59 | this.timeStamp.clear(); |
| 60 | this.timeStamp.add(time); |
| 61 | } |
| 62 | |
| 63 | public Iterator getTimeStamp() { |
| 64 | return this.timeStamp.iterator(); |
| 65 | } |
| 66 | |
| 67 | public void addLyric(final String newLyric) { |
| 68 | this.lyric += newLyric; |
| 69 | } |
| 70 | |
| 71 | public void addLyric(final ObjectID3v2LyricLine line) { |
| 72 | this.lyric += line.getText(); |
| 73 | } |
| 74 | |
| 75 | public void addTimeStamp(final ObjectLyrics3TimeStamp time) { |
| 76 | this.timeStamp.add(time); |
| 77 | } |
| 78 | |
| 79 | public boolean equals(final Object obj) { |
| 80 | if ((obj instanceof ObjectLyrics3Line) == false) { |
| 81 | return false; |
| 82 | } |
| 83 | final ObjectLyrics3Line objectLyrics3Line = (ObjectLyrics3Line) obj; |
| 84 | if (this.lyric.equals(objectLyrics3Line.lyric) == false) { |
| 85 | return false; |
| 86 | } |
| 87 | if (this.timeStamp.equals(objectLyrics3Line.timeStamp) == false) { |
| 88 | return false; |
| 89 | } |
| 90 | return super.equals(obj); |
| 91 | } |
| 92 | |
| 93 | public boolean hasTimeStamp() { |
| 94 | if (this.timeStamp.isEmpty()) { |
| 95 | return false; |
| 96 | } |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | public void readString(final String lineString, int offset) { |
| 101 | if (lineString == null) { |
| 102 | throw new NullPointerException("Image is null"); |
| 103 | } |
| 104 | if ((offset < 0) || (offset >= lineString.length())) { |
| 105 | throw new IndexOutOfBoundsException("Offset to line is out of bounds: offset = " + |
| 106 | offset + |
| 107 | ", line.length()" + |
| 108 | lineString.length()); |
| 109 | } |
| 110 | int delim; |
| 111 | ObjectLyrics3TimeStamp time; |
| 112 | this.timeStamp = new LinkedList(); |
| 113 | delim = lineString.indexOf("[", offset); |
| 114 | while (delim >= 0) { |
| 115 | offset = lineString.indexOf("]", delim) + 1; |
| 116 | time = new ObjectLyrics3TimeStamp("Time Stamp"); |
| 117 | time.readString(lineString.substring(delim, offset)); |
| 118 | this.timeStamp.add(time); |
| 119 | delim = lineString.indexOf("[", offset); |
| 120 | } |
| 121 | this.lyric = lineString.substring(offset); |
| 122 | } |
| 123 | |
| 124 | public String toString() { |
| 125 | String str = ""; |
| 126 | for (int i = 0; i < this.timeStamp.size(); i++) { |
| 127 | str += this.timeStamp.get(i).toString(); |
| 128 | } |
| 129 | return "timeStamp = " + str + ", lyric = " + this.lyric + "\n"; |
| 130 | } |
| 131 | |
| 132 | public String writeString() { |
| 133 | String str = ""; |
| 134 | ObjectLyrics3TimeStamp time; |
| 135 | for (int i = 0; i < this.timeStamp.size(); i++) { |
| 136 | time = (ObjectLyrics3TimeStamp) this.timeStamp.get(i); |
| 137 | str += time.writeString(); |
| 138 | } |
| 139 | return str + this.lyric; |
| 140 | } |
| 141 | } |