Coverage Report - org.jaudiotagger.tag.lyrics3.Lyrics3v2Field
 
Classes in this File Line Coverage Branch Coverage Complexity
Lyrics3v2Field
0%
0/83
0%
0/60
3.636
 
 1  
 /**
 2  
  *  @author : Paul Taylor
 3  
  *  @author : Eric Farng
 4  
  *
 5  
  *  Version @version:$Id: Lyrics3v2Field.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  
 
 24  
 package org.jaudiotagger.tag.lyrics3;
 25  
 
 26  
 import org.jaudiotagger.tag.InvalidTagException;
 27  
 import org.jaudiotagger.tag.TagException;
 28  
 import org.jaudiotagger.tag.TagOptionSingleton;
 29  
 import org.jaudiotagger.tag.id3.AbstractID3v2Frame;
 30  
 import org.jaudiotagger.tag.id3.AbstractTagFrame;
 31  
 import org.jaudiotagger.tag.id3.framebody.AbstractFrameBodyTextInfo;
 32  
 import org.jaudiotagger.tag.id3.framebody.FrameBodyCOMM;
 33  
 import org.jaudiotagger.tag.id3.framebody.FrameBodySYLT;
 34  
 import org.jaudiotagger.tag.id3.framebody.FrameBodyUSLT;
 35  
 
 36  
 import java.io.IOException;
 37  
 import java.io.RandomAccessFile;
 38  
 import java.nio.ByteBuffer;
 39  
 
 40  
 
 41  
 public class Lyrics3v2Field extends AbstractTagFrame
 42  
 {
 43  
     /**
 44  
      * Creates a new Lyrics3v2Field datatype.
 45  
      */
 46  
     public Lyrics3v2Field()
 47  0
     {
 48  0
     }
 49  
 
 50  
     public Lyrics3v2Field(Lyrics3v2Field copyObject)
 51  
     {
 52  0
         super(copyObject);
 53  0
     }
 54  
 
 55  
     /**
 56  
      * Creates a new Lyrics3v2Field datatype.
 57  
      *
 58  
      * @param body
 59  
      */
 60  
     public Lyrics3v2Field(AbstractLyrics3v2FieldFrameBody body)
 61  0
     {
 62  0
         this.frameBody = body;
 63  0
     }
 64  
 
 65  
     /**
 66  
      * Creates a new Lyrics3v2Field datatype.
 67  
      *
 68  
      * @param frame
 69  
      * @throws TagException
 70  
      */
 71  
     public Lyrics3v2Field(AbstractID3v2Frame frame) throws TagException
 72  0
     {
 73  
         AbstractFrameBodyTextInfo textFrame;
 74  
         String text;
 75  0
         String frameIdentifier = frame.getIdentifier();
 76  0
         if (frameIdentifier.startsWith("USLT"))
 77  
         {
 78  0
             frameBody = new FieldFrameBodyLYR("");
 79  0
             ((FieldFrameBodyLYR) frameBody).addLyric((FrameBodyUSLT) frame.getBody());
 80  
         }
 81  0
         else if (frameIdentifier.startsWith("SYLT"))
 82  
         {
 83  0
             frameBody = new FieldFrameBodyLYR("");
 84  0
             ((FieldFrameBodyLYR) frameBody).addLyric((FrameBodySYLT) frame.getBody());
 85  
         }
 86  0
         else if (frameIdentifier.startsWith("COMM"))
 87  
         {
 88  0
             text = new String(((FrameBodyCOMM) frame.getBody()).getText());
 89  0
             frameBody = new FieldFrameBodyINF(text);
 90  
         }
 91  0
         else if (frameIdentifier.equals("TCOM"))
 92  
         {
 93  0
             textFrame = (AbstractFrameBodyTextInfo) frame.getBody();
 94  0
             frameBody = new FieldFrameBodyAUT("");
 95  0
             if ((textFrame != null) && (textFrame.getText().length() > 0))
 96  
             {
 97  0
                 frameBody = new FieldFrameBodyAUT(textFrame.getText());
 98  
             }
 99  
         }
 100  0
         else if (frameIdentifier.equals("TALB"))
 101  
         {
 102  0
             textFrame = (AbstractFrameBodyTextInfo) frame.getBody();
 103  0
             if ((textFrame != null) && (textFrame.getText().length() > 0))
 104  
             {
 105  0
                 frameBody = new FieldFrameBodyEAL(textFrame.getText());
 106  
             }
 107  
         }
 108  0
         else if (frameIdentifier.equals("TPE1"))
 109  
         {
 110  0
             textFrame = (AbstractFrameBodyTextInfo) frame.getBody();
 111  0
             if ((textFrame != null) && (textFrame.getText().length() > 0))
 112  
             {
 113  0
                 frameBody = new FieldFrameBodyEAR(textFrame.getText());
 114  
             }
 115  
         }
 116  0
         else if (frameIdentifier.equals("TIT2"))
 117  
         {
 118  0
             textFrame = (AbstractFrameBodyTextInfo) frame.getBody();
 119  0
             if ((textFrame != null) && (textFrame.getText().length() > 0))
 120  
             {
 121  0
                 frameBody = new FieldFrameBodyETT(textFrame.getText());
 122  
             }
 123  
         }
 124  
         else
 125  
         {
 126  0
             throw new TagException("Cannot create Lyrics3v2 field from given ID3v2 frame");
 127  
         }
 128  0
     }
 129  
 
 130  
     /**
 131  
      * Creates a new Lyrics3v2Field datatype.
 132  
      *
 133  
      * @param file
 134  
      * @throws InvalidTagException
 135  
      */
 136  
     public Lyrics3v2Field(ByteBuffer byteBuffer) throws InvalidTagException
 137  0
     {
 138  0
         this.read(byteBuffer);
 139  0
     }
 140  
 
 141  
     /**
 142  
      * @return
 143  
      */
 144  
     public String getIdentifier()
 145  
     {
 146  0
         if (frameBody == null)
 147  
         {
 148  0
             return "";
 149  
         }
 150  0
         return frameBody.getIdentifier();
 151  
     }
 152  
 
 153  
     /**
 154  
      * @return
 155  
      */
 156  
     public int getSize()
 157  
     {
 158  0
         return frameBody.getSize() + 5 + getIdentifier().length();
 159  
     }
 160  
 
 161  
     /**
 162  
      * @param byteBuffer
 163  
      * @throws InvalidTagException
 164  
      * @throws IOException
 165  
      */
 166  
     public void read(ByteBuffer byteBuffer) throws InvalidTagException
 167  
     {
 168  0
         byte[] buffer = new byte[6];
 169  
         // lets scan for a non-zero byte;
 170  
         long filePointer;
 171  
         byte b;
 172  
         do
 173  
         {
 174  0
             b = byteBuffer.get();
 175  
         }
 176  0
         while (b == 0);
 177  0
         byteBuffer.position(byteBuffer.position() - 1);
 178  
         // read the 3 character ID
 179  0
         byteBuffer.get(buffer, 0, 3);
 180  0
         String identifier = new String(buffer, 0, 3);
 181  
         // is this a valid identifier?
 182  0
         if (Lyrics3v2Fields.isLyrics3v2FieldIdentifier(identifier) == false)
 183  
         {
 184  0
             throw new InvalidTagException(identifier + " is not a valid ID3v2.4 frame");
 185  
         }
 186  0
         frameBody = readBody(identifier, byteBuffer);
 187  0
     }
 188  
 
 189  
     /**
 190  
      * @return
 191  
      */
 192  
     public String toString()
 193  
     {
 194  0
         if (frameBody == null)
 195  
         {
 196  0
             return "";
 197  
         }
 198  0
         return frameBody.toString();
 199  
     }
 200  
 
 201  
     /**
 202  
      * @param file
 203  
      * @throws IOException
 204  
      */
 205  
     public void write(RandomAccessFile file) throws IOException
 206  
     {
 207  0
         if ((((AbstractLyrics3v2FieldFrameBody) frameBody).getSize() > 0) || TagOptionSingleton.getInstance().isLyrics3SaveEmptyField())
 208  
         {
 209  0
             byte[] buffer = new byte[3];
 210  0
             String str = getIdentifier();
 211  0
             for (int i = 0; i < str.length(); i++)
 212  
             {
 213  0
                 buffer[i] = (byte) str.charAt(i);
 214  
             }
 215  0
             file.write(buffer, 0, str.length());
 216  
             //body.write(file);
 217  
         }
 218  0
     }
 219  
 
 220  
     /**
 221  
      * Read a Lyrics3 Field from a file.
 222  
      *
 223  
      * @param identifier
 224  
      * @return
 225  
      * @throws InvalidTagException
 226  
      */
 227  
     private AbstractLyrics3v2FieldFrameBody readBody(String identifier, ByteBuffer byteBuffer) throws InvalidTagException
 228  
     {
 229  0
         AbstractLyrics3v2FieldFrameBody newBody = null;
 230  0
         if (identifier.equals(Lyrics3v2Fields.FIELD_V2_AUTHOR))
 231  
         {
 232  0
             newBody = new FieldFrameBodyAUT(byteBuffer);
 233  
         }
 234  0
         else if (identifier.equals(Lyrics3v2Fields.FIELD_V2_ALBUM))
 235  
         {
 236  0
             newBody = new FieldFrameBodyEAL(byteBuffer);
 237  
         }
 238  0
         else if (identifier.equals(Lyrics3v2Fields.FIELD_V2_ARTIST))
 239  
         {
 240  0
             newBody = new FieldFrameBodyEAR(byteBuffer);
 241  
         }
 242  0
         else if (identifier.equals(Lyrics3v2Fields.FIELD_V2_TRACK))
 243  
         {
 244  0
             newBody = new FieldFrameBodyETT(byteBuffer);
 245  
         }
 246  0
         else if (identifier.equals(Lyrics3v2Fields.FIELD_V2_IMAGE))
 247  
         {
 248  0
             newBody = new FieldFrameBodyIMG(byteBuffer);
 249  
         }
 250  0
         else if (identifier.equals(Lyrics3v2Fields.FIELD_V2_INDICATIONS))
 251  
         {
 252  0
             newBody = new FieldFrameBodyIND(byteBuffer);
 253  
         }
 254  0
         else if (identifier.equals(Lyrics3v2Fields.FIELD_V2_ADDITIONAL_MULTI_LINE_TEXT))
 255  
         {
 256  0
             newBody = new FieldFrameBodyINF(byteBuffer);
 257  
         }
 258  0
         else if (identifier.equals(Lyrics3v2Fields.FIELD_V2_LYRICS_MULTI_LINE_TEXT))
 259  
         {
 260  0
             newBody = new FieldFrameBodyLYR(byteBuffer);
 261  
         }
 262  
         else
 263  
         {
 264  0
             newBody = new FieldFrameBodyUnsupported(byteBuffer);
 265  
         }
 266  0
         return newBody;
 267  
     }
 268  
 }