Coverage Report - org.jaudiotagger.tag.lyrics3.FieldFrameBodyLYR
 
Classes in this File Line Coverage Branch Coverage Complexity
FieldFrameBodyLYR
0%
0/125
0%
0/42
0
 
 1  
 /**
 2  
  *  @author : Paul Taylor
 3  
  *  @author : Eric Farng
 4  
  *
 5  
  *  Version @version:$Id: FieldFrameBodyLYR.java,v 1.11 2008/07/21 10:45:49 paultaylor Exp $
 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  
      */
 100  
     public FieldFrameBodyLYR(ByteBuffer byteBuffer) throws InvalidTagException
 101  0
     {
 102  
 
 103  0
         this.read(byteBuffer);
 104  
 
 105  0
     }
 106  
 
 107  
     /**
 108  
      * @return
 109  
      */
 110  
     public String getIdentifier()
 111  
     {
 112  0
         return "LYR";
 113  
     }
 114  
 
 115  
     /**
 116  
      * @param str
 117  
      */
 118  
     public void setLyric(String str)
 119  
     {
 120  0
         readString(str);
 121  0
     }
 122  
 
 123  
     /**
 124  
      * @return
 125  
      */
 126  
     public String getLyric()
 127  
     {
 128  0
         return writeString();
 129  
     }
 130  
 
 131  
     /**
 132  
      * @return
 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  
         //return size - 2; // cut off the last crlf pair
 148  
     }
 149  
 
 150  
     /**
 151  
      * @param obj
 152  
      * @return
 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  
      * @param sync
 176  
      */
 177  
     public void addLyric(FrameBodySYLT sync)
 178  
     {
 179  
         // SYLT frames are made of individual lines
 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  
             // create copy to use in new tag
 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  
      * @param unsync
 213  
      */
 214  
     public void addLyric(FrameBodyUSLT unsync)
 215  
     {
 216  
         // USLT frames are just long text string;
 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  
      * @param obj
 224  
      * @return
 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  
      * @return
 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  
      * @return
 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  
         // read the 5 character size
 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  
         // read the SIZE length description
 293  0
         byteBuffer.get(buffer);
 294  0
         lineString = new String(buffer);
 295  0
         readString(lineString);
 296  0
     }
 297  
 
 298  
     /**
 299  
      * @return
 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  
      * @param file
 315  
      * @throws java.io.IOException
 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  
      * @param lineString
 358  
      */
 359  
     private void readString(String lineString)
 360  
     {
 361  
         // now readString each line and put in the vector;
 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  
      * @return
 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  
         //return str.substring(0,str.length()-2); // cut off the last CRLF pair
 405  
     }
 406  
 
 407  
     /**
 408  
      * TODO
 409  
      */
 410  
     protected void setupObjectList()
 411  
     {
 412  
 
 413  0
     }
 414  
 }