Coverage Report - org.jaudiotagger.tag.lyrics3.Lyrics3v2Field
 
Classes in this File Line Coverage Branch Coverage Complexity
Lyrics3v2Field
0%
0/82
0%
0/60
4.091
 
 1  
 /**
 2  
  *  @author : Paul Taylor
 3  
  *  @author : Eric Farng
 4  
  *
 5  
  *  Version @version:$Id: Lyrics3v2Field.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  
 
 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 = ((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 createField 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  
      * @param byteBuffer
 136  
      */
 137  
     public Lyrics3v2Field(ByteBuffer byteBuffer) throws InvalidTagException
 138  0
     {
 139  0
         this.read(byteBuffer);
 140  0
     }
 141  
 
 142  
     /**
 143  
      * @return
 144  
      */
 145  
     public String getIdentifier()
 146  
     {
 147  0
         if (frameBody == null)
 148  
         {
 149  0
             return "";
 150  
         }
 151  0
         return frameBody.getIdentifier();
 152  
     }
 153  
 
 154  
     /**
 155  
      * @return
 156  
      */
 157  
     public int getSize()
 158  
     {
 159  0
         return frameBody.getSize() + 5 + getIdentifier().length();
 160  
     }
 161  
 
 162  
     /**
 163  
      * @param byteBuffer
 164  
      * @throws InvalidTagException
 165  
      * @throws IOException
 166  
      */
 167  
     public void read(ByteBuffer byteBuffer) throws InvalidTagException
 168  
     {
 169  0
         byte[] buffer = new byte[6];
 170  
         // lets scan for a non-zero byte;
 171  
         long filePointer;
 172  
         byte b;
 173  
         do
 174  
         {
 175  0
             b = byteBuffer.get();
 176  
         }
 177  0
         while (b == 0);
 178  0
         byteBuffer.position(byteBuffer.position() - 1);
 179  
         // read the 3 character ID
 180  0
         byteBuffer.get(buffer, 0, 3);
 181  0
         String identifier = new String(buffer, 0, 3);
 182  
         // is this a valid identifier?
 183  0
         if (!Lyrics3v2Fields.isLyrics3v2FieldIdentifier(identifier))
 184  
         {
 185  0
             throw new InvalidTagException(identifier + " is not a valid ID3v2.4 frame");
 186  
         }
 187  0
         frameBody = readBody(identifier, byteBuffer);
 188  0
     }
 189  
 
 190  
     /**
 191  
      * @return
 192  
      */
 193  
     public String toString()
 194  
     {
 195  0
         if (frameBody == null)
 196  
         {
 197  0
             return "";
 198  
         }
 199  0
         return frameBody.toString();
 200  
     }
 201  
 
 202  
     /**
 203  
      * @param file
 204  
      * @throws IOException
 205  
      */
 206  
     public void write(RandomAccessFile file) throws IOException
 207  
     {
 208  0
         if ((frameBody.getSize() > 0) || TagOptionSingleton.getInstance().isLyrics3SaveEmptyField())
 209  
         {
 210  0
             byte[] buffer = new byte[3];
 211  0
             String str = getIdentifier();
 212  0
             for (int i = 0; i < str.length(); i++)
 213  
             {
 214  0
                 buffer[i] = (byte) str.charAt(i);
 215  
             }
 216  0
             file.write(buffer, 0, str.length());
 217  
             //body.write(file);
 218  
         }
 219  0
     }
 220  
 
 221  
     /**
 222  
      * Read a Lyrics3 Field from a file.
 223  
      *
 224  
      * @param identifier
 225  
      * @param byteBuffer
 226  
      * @return
 227  
      * @throws InvalidTagException
 228  
      */
 229  
     private AbstractLyrics3v2FieldFrameBody readBody(String identifier, ByteBuffer byteBuffer) throws InvalidTagException
 230  
     {
 231  
         AbstractLyrics3v2FieldFrameBody newBody;
 232  0
         if (identifier.equals(Lyrics3v2Fields.FIELD_V2_AUTHOR))
 233  
         {
 234  0
             newBody = new FieldFrameBodyAUT(byteBuffer);
 235  
         }
 236  0
         else if (identifier.equals(Lyrics3v2Fields.FIELD_V2_ALBUM))
 237  
         {
 238  0
             newBody = new FieldFrameBodyEAL(byteBuffer);
 239  
         }
 240  0
         else if (identifier.equals(Lyrics3v2Fields.FIELD_V2_ARTIST))
 241  
         {
 242  0
             newBody = new FieldFrameBodyEAR(byteBuffer);
 243  
         }
 244  0
         else if (identifier.equals(Lyrics3v2Fields.FIELD_V2_TRACK))
 245  
         {
 246  0
             newBody = new FieldFrameBodyETT(byteBuffer);
 247  
         }
 248  0
         else if (identifier.equals(Lyrics3v2Fields.FIELD_V2_IMAGE))
 249  
         {
 250  0
             newBody = new FieldFrameBodyIMG(byteBuffer);
 251  
         }
 252  0
         else if (identifier.equals(Lyrics3v2Fields.FIELD_V2_INDICATIONS))
 253  
         {
 254  0
             newBody = new FieldFrameBodyIND(byteBuffer);
 255  
         }
 256  0
         else if (identifier.equals(Lyrics3v2Fields.FIELD_V2_ADDITIONAL_MULTI_LINE_TEXT))
 257  
         {
 258  0
             newBody = new FieldFrameBodyINF(byteBuffer);
 259  
         }
 260  0
         else if (identifier.equals(Lyrics3v2Fields.FIELD_V2_LYRICS_MULTI_LINE_TEXT))
 261  
         {
 262  0
             newBody = new FieldFrameBodyLYR(byteBuffer);
 263  
         }
 264  
         else
 265  
         {
 266  0
             newBody = new FieldFrameBodyUnsupported(byteBuffer);
 267  
         }
 268  0
         return newBody;
 269  
     }
 270  
 }