Coverage Report - org.jaudiotagger.tag.id3.framebody.FrameBodyCOMM
 
Classes in this File Line Coverage Branch Coverage Complexity
FrameBodyCOMM
91%
39/43
50%
4/8
1.462
 
 1  
 /*
 2  
  *  MusicTag Copyright (C)2003,2004
 3  
  *
 4  
  *  This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser
 5  
  *  General Public  License as published by the Free Software Foundation; either version 2.1 of the License,
 6  
  *  or (at your option) any later version.
 7  
  *
 8  
  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 9  
  *  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 10  
  *  See the GNU Lesser General Public License for more details.
 11  
  *
 12  
  *  You should have received a copy of the GNU Lesser General Public License along with this library; if not,
 13  
  *  you can get a copy from http://www.opensource.org/licenses/lgpl-license.php or write to the Free Software
 14  
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 15  
  */
 16  
 package org.jaudiotagger.tag.id3.framebody;
 17  
 
 18  
 import org.jaudiotagger.logging.ErrorMessage;
 19  
 import org.jaudiotagger.tag.InvalidTagException;
 20  
 import org.jaudiotagger.tag.datatype.*;
 21  
 import org.jaudiotagger.tag.id3.ID3TextEncodingConversion;
 22  
 import org.jaudiotagger.tag.id3.ID3v24Frames;
 23  
 import org.jaudiotagger.tag.id3.valuepair.TextEncoding;
 24  
 import org.jaudiotagger.tag.reference.Languages;
 25  
 
 26  
 import java.io.ByteArrayOutputStream;
 27  
 import java.nio.ByteBuffer;
 28  
 
 29  
 /**
 30  
  * Comments frame.
 31  
  * <p/>
 32  
  * <p/>
 33  
  * This frame is indended for any kind of full text information that
 34  
  * does not fit in any other frame. It consists of a frame header
 35  
  * followed by encoding, language and content descriptors and is ended
 36  
  * with the actual comment as a text string. Newline characters are
 37  
  * allowed in the comment text string. There may be more than one
 38  
  * comment frame in each tag, but only one with the same language and
 39  
  * content descriptor.
 40  
  * </p><p><table border=0 width="70%">
 41  
  * <tr><td colspan=2>&lt;Header for 'Comment', ID: "COMM"&gt;</td></tr>
 42  
  * <tr><td>Text encoding   </td><td width="80%">$xx          </td></tr>
 43  
  * <tr><td>Language        </td><td>$xx xx xx                </td></tr>
 44  
  * <tr><td>Short content descrip.</td><td>&lt;text string according to encoding&gt; $00 (00)</td></tr>
 45  
  * <tr><td>The actual text </td><td>&lt;full text string according to encoding&gt;</td></tr>
 46  
  * </table></p>
 47  
  * <p/>
 48  
  * <p>For more details, please refer to the ID3 specifications:
 49  
  * <ul>
 50  
  * <li><a href="http://www.id3.org/id3v2.3.0.txt">ID3 v2.3.0 Spec</a>
 51  
  * </ul>
 52  
  *
 53  
  * @author : Paul Taylor
 54  
  * @author : Eric Farng
 55  
  * @version $Id: FrameBodyCOMM.java,v 1.23 2008/07/21 10:45:42 paultaylor Exp $
 56  
  */
 57  
 public class FrameBodyCOMM extends AbstractID3v2FrameBody implements ID3v24FrameBody, ID3v23FrameBody
 58  
 {
 59  
     /**
 60  
      * Creates a new FrameBodyCOMM datatype.
 61  
      */
 62  
     public FrameBodyCOMM()
 63  21
     {
 64  21
         setObjectValue(DataTypes.OBJ_TEXT_ENCODING, TextEncoding.ISO_8859_1);
 65  21
         setObjectValue(DataTypes.OBJ_LANGUAGE, Languages.DEFAULT_ID);
 66  21
         setObjectValue(DataTypes.OBJ_DESCRIPTION, "");
 67  21
         setObjectValue(DataTypes.OBJ_TEXT, "");
 68  21
     }
 69  
 
 70  
     public FrameBodyCOMM(FrameBodyCOMM body)
 71  
     {
 72  104
         super(body);
 73  104
     }
 74  
 
 75  
     /**
 76  
      * Creates a new FrameBodyCOMM datatype.
 77  
      *
 78  
      * @param textEncoding
 79  
      * @param language
 80  
      * @param description
 81  
      * @param text
 82  
      */
 83  
     public FrameBodyCOMM(byte textEncoding, String language, String description, String text)
 84  4
     {
 85  4
         setObjectValue(DataTypes.OBJ_TEXT_ENCODING, textEncoding);
 86  4
         setObjectValue(DataTypes.OBJ_LANGUAGE, language);
 87  4
         setObjectValue(DataTypes.OBJ_DESCRIPTION, description);
 88  4
         setObjectValue(DataTypes.OBJ_TEXT, text);
 89  4
     }
 90  
 
 91  
     /**
 92  
      * Construct a Comment frame body from the buffer
 93  
      *
 94  
      * @param byteBuffer
 95  
      * @param frameSize
 96  
      * @throws InvalidTagException if unable to create framebody from buffer
 97  
      */
 98  
     public FrameBodyCOMM(ByteBuffer byteBuffer, int frameSize) throws InvalidTagException
 99  
     {
 100  127
         super(byteBuffer, frameSize);
 101  125
     }
 102  
 
 103  
 
 104  
     /**
 105  
      * Set the description field, which describes the type of comment
 106  
      *
 107  
      * @param description
 108  
      */
 109  
     public void setDescription(String description)
 110  
     {
 111  5
         if (description == null)
 112  
         {
 113  0
             throw new IllegalArgumentException(ErrorMessage.GENERAL_INVALID_NULL_ARGUMENT.getMsg());
 114  
         }
 115  5
         setObjectValue(DataTypes.OBJ_DESCRIPTION, description);
 116  5
     }
 117  
 
 118  
     /**
 119  
      * Get the description field, which describes the type of comment
 120  
      *
 121  
      * @return description field
 122  
      */
 123  
     public String getDescription()
 124  
     {
 125  35
         return (String) getObjectValue(DataTypes.OBJ_DESCRIPTION);
 126  
     }
 127  
 
 128  
     /**
 129  
      * The ID3v2 frame identifier
 130  
      *
 131  
      * @return the ID3v2 frame identifier  for this frame type
 132  
      */
 133  
     public String getIdentifier()
 134  
     {
 135  414
         return ID3v24Frames.FRAME_ID_COMMENT;
 136  
     }
 137  
 
 138  
     /**
 139  
      * Sets the language the comment is written in
 140  
      *
 141  
      * @param language
 142  
      */
 143  
     public void setLanguage(String language)
 144  
     {
 145  
         //TODO not sure if this might break existing code
 146  
         /*if(language==null)
 147  
         {
 148  
              throw new IllegalArgumentException(ErrorMessage.GENERAL_INVALID_NULL_ARGUMENT.getMsg());
 149  
         } */
 150  4
         setObjectValue(DataTypes.OBJ_LANGUAGE, language);
 151  4
     }
 152  
 
 153  
     /**
 154  
      * Get the language the comment is written in
 155  
      *
 156  
      * @return the language
 157  
      */
 158  
     public String getLanguage()
 159  
     {
 160  6
         return (String) getObjectValue(DataTypes.OBJ_LANGUAGE);
 161  
     }
 162  
 
 163  
     /**
 164  
      * @param text
 165  
      */
 166  
     public void setText(String text)
 167  
     {
 168  17
         if (text == null)
 169  
         {
 170  0
             throw new IllegalArgumentException(ErrorMessage.GENERAL_INVALID_NULL_ARGUMENT.getMsg());
 171  
         }
 172  17
         setObjectValue(DataTypes.OBJ_TEXT, text);
 173  17
     }
 174  
 
 175  
     /**
 176  
      * Returns the the text field which holds the comment, adjusted to ensure does not return trailing null
 177  
      * which is due to a iTunes bug.
 178  
      *
 179  
      * @return the text field
 180  
      */
 181  
     public String getText()
 182  
     {
 183  20
         TextEncodedStringSizeTerminated text = (TextEncodedStringSizeTerminated) getObject(DataTypes.OBJ_TEXT);
 184  20
         return text.getValueAtIndex(0);
 185  
     }
 186  
 
 187  
     /**
 188  
      *
 189  
      */
 190  
     protected void setupObjectList()
 191  
     {
 192  152
         objectList.add(new NumberHashMap(DataTypes.OBJ_TEXT_ENCODING, this, TextEncoding.TEXT_ENCODING_FIELD_SIZE));
 193  152
         objectList.add(new StringHashMap(DataTypes.OBJ_LANGUAGE, this, Languages.LANGUAGE_FIELD_SIZE));
 194  152
         objectList.add(new TextEncodedStringNullTerminated(DataTypes.OBJ_DESCRIPTION, this));
 195  152
         objectList.add(new TextEncodedStringSizeTerminated(DataTypes.OBJ_TEXT, this));
 196  152
     }
 197  
 
 198  
     /**
 199  
      * Because COMM have a text encoding we need to check the text String does
 200  
      * not contain characters that cannot be encoded in current encoding before
 201  
      * we write data. If there are we change the encoding.
 202  
      */
 203  
     public void write(ByteArrayOutputStream tagBuffer)
 204  
     {
 205  
         //Ensure valid for type
 206  81
         setTextEncoding(ID3TextEncodingConversion.getTextEncoding(getHeader(), getTextEncoding()));
 207  
 
 208  
         //Ensure valid for data
 209  81
         if (((AbstractString) getObject(DataTypes.OBJ_TEXT)).canBeEncoded() == false)
 210  
         {
 211  0
             this.setTextEncoding(ID3TextEncodingConversion.getUnicodeTextEncoding(getHeader()));
 212  
         }
 213  81
         if (((AbstractString) getObject(DataTypes.OBJ_DESCRIPTION)).canBeEncoded() == false)
 214  
         {
 215  0
             this.setTextEncoding(ID3TextEncodingConversion.getUnicodeTextEncoding(getHeader()));
 216  
         }
 217  81
         super.write(tagBuffer);
 218  81
     }
 219  
 
 220  
 }