Coverage Report - org.jaudiotagger.tag.lyrics3.FieldFrameBodyLYR
 
Classes in this File Line Coverage Branch Coverage Complexity
FieldFrameBodyLYR
0%
0/120
0%
0/44
2.136
 
 1  
 /**
 2  
  *  @author : Paul Taylor
 3  
  *  @author : Eric Farng
 4  
  *
 5  
  *  Version @version:$Id: FieldFrameBodyLYR.java 836 2009-11-12 15:44:07Z paultaylor $
 6  
  *
 7  
  *  MusicTag Copyright (C)2003,2004
 8  
  *
 9  
  *  This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser
 10  
  *  General Public  License as published by the Free Software Foundation; either version 2.1 of the License,
 11  
  *  or (at your option) any later version.
 12  
  *
 13  
  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 14  
  *  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 15  
  *  See the GNU Lesser General Public License for more details.
 16  
  *
 17  
  *  You should have received a copy of the GNU Lesser General Public License along with this library; if not,
 18  
  *  you can get a copy from http://www.opensource.org/licenses/lgpl-license.php or write to the Free Software
 19  
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 20  
  *
 21  
  * Description:
 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  
      * Creates a new FieldBodyLYR datatype.
 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  
      * Creates a new FieldBodyLYR datatype.
 69  
      *
 70  
      * @param line
 71  
      */
 72  
     public FieldFrameBodyLYR(String line)
 73  0
     {
 74  0
         readString(line);
 75  0
     }
 76  
 
 77  
     /**
 78  
      * Creates a new FieldBodyLYR datatype.
 79  
      *
 80  
      * @param sync
 81  
      */
 82  
     public FieldFrameBodyLYR(FrameBodySYLT sync)
 83  0
     {
 84  0
         addLyric(sync);
 85  0
     }
 86  
 
 87  
     /**
 88  
      * Creates a new FieldBodyLYR datatype.
 89  
      *
 90  
      * @param unsync
 91  
      */
 92  
     public FieldFrameBodyLYR(FrameBodyUSLT unsync)
 93  0
     {
 94  0
         addLyric(unsync);
 95  0
     }
 96  
 
 97  
     /**
 98  
      * Creates a new FieldBodyLYR datatype.
 99  
      * @param byteBuffer
 100  
      * @throws org.jaudiotagger.tag.InvalidTagException
 101  
      */
 102  
     public FieldFrameBodyLYR(ByteBuffer byteBuffer) throws InvalidTagException
 103  0
     {
 104  
 
 105  0
         this.read(byteBuffer);
 106  
 
 107  0
     }
 108  
 
 109  
     /**
 110  
      * @return
 111  
      */
 112  
     public String getIdentifier()
 113  
     {
 114  0
         return "LYR";
 115  
     }
 116  
 
 117  
     /**
 118  
      * @param str
 119  
      */
 120  
     public void setLyric(String str)
 121  
     {
 122  0
         readString(str);
 123  0
     }
 124  
 
 125  
     /**
 126  
      * @return
 127  
      */
 128  
     public String getLyric()
 129  
     {
 130  0
         return writeString();
 131  
     }
 132  
 
 133  
     /**
 134  
      * @return
 135  
      */
 136  
     public int getSize()
 137  
     {
 138  0
         int size = 0;
 139  
         Lyrics3Line line;
 140  
 
 141  0
         for (Object line1 : lines)
 142  
         {
 143  0
             line = (Lyrics3Line) line1;
 144  0
             size += (line.getSize() + 2);
 145  
         }
 146  
 
 147  0
         return size;
 148  
 
 149  
         //return size - 2; // cut off the last crlf pair
 150  
     }
 151  
 
 152  
     /**
 153  
      * @param obj
 154  
      * @return
 155  
      */
 156  
     public boolean isSubsetOf(Object obj)
 157  
     {
 158  0
         if (!(obj instanceof FieldFrameBodyLYR))
 159  
         {
 160  0
             return false;
 161  
         }
 162  
 
 163  0
         ArrayList<Lyrics3Line> superset = ((FieldFrameBodyLYR) obj).lines;
 164  
 
 165  0
         for (Object line : lines)
 166  
         {
 167  0
             if (!superset.contains(line))
 168  
             {
 169  0
                 return false;
 170  
             }
 171  
         }
 172  
 
 173  0
         return super.isSubsetOf(obj);
 174  
     }
 175  
 
 176  
     /**
 177  
      * @param sync
 178  
      */
 179  
     public void addLyric(FrameBodySYLT sync)
 180  
     {
 181  
         // SYLT frames are made of individual lines
 182  0
         Iterator<ID3v2LyricLine> iterator = sync.iterator();
 183  
         Lyrics3Line newLine;
 184  
         ID3v2LyricLine currentLine;
 185  
         Lyrics3TimeStamp timeStamp;
 186  0
         HashMap<String, Lyrics3Line> lineMap = new HashMap<String, Lyrics3Line>();
 187  
 
 188  0
         while (iterator.hasNext())
 189  
         {
 190  0
             currentLine = iterator.next();
 191  
 
 192  
             // createField copy to use in new tag
 193  0
             currentLine = new ID3v2LyricLine(currentLine);
 194  0
             timeStamp = new Lyrics3TimeStamp("Time Stamp", this);
 195  0
             timeStamp.setTimeStamp(currentLine.getTimeStamp(), (byte) sync.getTimeStampFormat());
 196  
 
 197  0
             if (lineMap.containsKey(currentLine.getText()))
 198  
             {
 199  0
                 newLine = lineMap.get(currentLine.getText());
 200  0
                 newLine.addTimeStamp(timeStamp);
 201  
             }
 202  
             else
 203  
             {
 204  0
                 newLine = new Lyrics3Line("Lyric Line", this);
 205  0
                 newLine.setLyric(currentLine);
 206  0
                 newLine.setTimeStamp(timeStamp);
 207  0
                 lineMap.put(currentLine.getText(), newLine);
 208  0
                 lines.add(newLine);
 209  
             }
 210  
         }
 211  0
     }
 212  
 
 213  
     /**
 214  
      * @param unsync
 215  
      */
 216  
     public void addLyric(FrameBodyUSLT unsync)
 217  
     {
 218  
         // USLT frames are just long text string;
 219  0
         Lyrics3Line line = new Lyrics3Line("Lyric Line", this);
 220  0
         line.setLyric(unsync.getLyric());
 221  0
         lines.add(line);
 222  0
     }
 223  
 
 224  
     /**
 225  
      * @param obj
 226  
      * @return
 227  
      */
 228  
     public boolean equals(Object obj)
 229  
     {
 230  0
         if (!(obj instanceof FieldFrameBodyLYR))
 231  
         {
 232  0
             return false;
 233  
         }
 234  
 
 235  0
         FieldFrameBodyLYR object = (FieldFrameBodyLYR) obj;
 236  
 
 237  0
         return this.lines.equals(object.lines) && super.equals(obj);
 238  
 
 239  
     }
 240  
 
 241  
     /**
 242  
      * @return
 243  
      */
 244  
     public boolean hasTimeStamp()
 245  
     {
 246  0
         boolean present = false;
 247  
 
 248  0
         for (Object line : lines)
 249  
         {
 250  0
             if (((Lyrics3Line) line).hasTimeStamp())
 251  
             {
 252  0
                 present = true;
 253  
             }
 254  
         }
 255  
 
 256  0
         return present;
 257  
     }
 258  
 
 259  
     /**
 260  
      * @return
 261  
      */
 262  
     public Iterator<Lyrics3Line> iterator()
 263  
     {
 264  0
         return lines.iterator();
 265  
     }
 266  
 
 267  
     /**
 268  
      *
 269  
      *
 270  
      *
 271  
      */
 272  
     public void read(ByteBuffer byteBuffer) throws InvalidTagException
 273  
     {
 274  
         String lineString;
 275  
 
 276  0
         byte[] buffer = new byte[5];
 277  
 
 278  
         // read the 5 character size
 279  0
         byteBuffer.get(buffer, 0, 5);
 280  
 
 281  0
         int size = Integer.parseInt(new String(buffer, 0, 5));
 282  
 
 283  0
         if ((size == 0) && (!TagOptionSingleton.getInstance().isLyrics3KeepEmptyFieldIfRead()))
 284  
         {
 285  0
             throw new InvalidTagException("Lyircs3v2 Field has size of zero.");
 286  
         }
 287  
 
 288  0
         buffer = new byte[size];
 289  
 
 290  
         // read the SIZE length description
 291  0
         byteBuffer.get(buffer);
 292  0
         lineString = new String(buffer);
 293  0
         readString(lineString);
 294  0
     }
 295  
 
 296  
     /**
 297  
      * @return
 298  
      */
 299  
     public String toString()
 300  
     {
 301  0
         String str = getIdentifier() + " : ";
 302  
 
 303  0
         for (Object line : lines)
 304  
         {
 305  0
             str += line.toString();
 306  
         }
 307  
 
 308  0
         return str;
 309  
     }
 310  
 
 311  
     /**
 312  
      * @param file
 313  
      * @throws java.io.IOException
 314  
      */
 315  
     public void write(RandomAccessFile file) throws java.io.IOException
 316  
     {
 317  
         int size;
 318  0
         int offset = 0;
 319  0
         byte[] buffer = new byte[5];
 320  
         String str;
 321  
 
 322  0
         size = getSize();
 323  0
         str = Integer.toString(size);
 324  
 
 325  0
         for (int i = 0; i < (5 - str.length()); i++)
 326  
         {
 327  0
             buffer[i] = (byte) '0';
 328  
         }
 329  
 
 330  0
         offset += (5 - str.length());
 331  
 
 332  0
         for (int i = 0; i < str.length(); i++)
 333  
         {
 334  0
             buffer[i + offset] = (byte) str.charAt(i);
 335  
         }
 336  
 
 337  0
         offset += str.length();
 338  0
         file.write(buffer, 0, 5);
 339  
 
 340  0
         if (size > 0)
 341  
         {
 342  0
             str = writeString();
 343  0
             buffer = new byte[str.length()];
 344  
 
 345  0
             for (int i = 0; i < str.length(); i++)
 346  
             {
 347  0
                 buffer[i] = (byte) str.charAt(i);
 348  
             }
 349  
 
 350  0
             file.write(buffer);
 351  
         }
 352  0
     }
 353  
 
 354  
     /**
 355  
      * @param lineString
 356  
      */
 357  
     private void readString(String lineString)
 358  
     {
 359  
         // now readString each line and put in the vector;
 360  
         String token;
 361  0
         int offset = 0;
 362  0
         int delim = lineString.indexOf(Lyrics3v2Fields.CRLF);
 363  0
         lines = new ArrayList<Lyrics3Line>();
 364  
 
 365  
         Lyrics3Line line;
 366  
 
 367  0
         while (delim >= 0)
 368  
         {
 369  0
             token = lineString.substring(offset, delim);
 370  0
             line = new Lyrics3Line("Lyric Line", this);
 371  0
             line.setLyric(token);
 372  0
             lines.add(line);
 373  0
             offset = delim + Lyrics3v2Fields.CRLF.length();
 374  0
             delim = lineString.indexOf(Lyrics3v2Fields.CRLF, offset);
 375  
         }
 376  
 
 377  0
         if (offset < lineString.length())
 378  
         {
 379  0
             token = lineString.substring(offset);
 380  0
             line = new Lyrics3Line("Lyric Line", this);
 381  0
             line.setLyric(token);
 382  0
             lines.add(line);
 383  
         }
 384  0
     }
 385  
 
 386  
     /**
 387  
      * @return
 388  
      */
 389  
     private String writeString()
 390  
     {
 391  
         Lyrics3Line line;
 392  0
         String str = "";
 393  
 
 394  0
         for (Object line1 : lines)
 395  
         {
 396  0
             line = (Lyrics3Line) line1;
 397  0
             str += (line.writeString() + Lyrics3v2Fields.CRLF);
 398  
         }
 399  
 
 400  0
         return str;
 401  
 
 402  
         //return str.substring(0,str.length()-2); // cut off the last CRLF pair
 403  
     }
 404  
 
 405  
     /**
 406  
      * TODO
 407  
      */
 408  
     protected void setupObjectList()
 409  
     {
 410  
 
 411  0
     }
 412  
 }