Coverage Report - org.jaudiotagger.audio.generic.GenericTag
 
Classes in this File Line Coverage Branch Coverage Complexity
GenericTag
65%
13/20
50%
4/8
1.7
GenericTag$GenericTagTextField
42%
8/19
0%
0/4
1.7
 
 1  
 /*
 2  
  * Entagged Audio Tag library
 3  
  * Copyright (c) 2003-2005 Raphaël Slinckx <raphael@slinckx.net>
 4  
  * 
 5  
  * This library is free software; you can redistribute it and/or
 6  
  * modify it under the terms of the GNU Lesser General Public
 7  
  * License as published by the Free Software Foundation; either
 8  
  * version 2.1 of the License, or (at your option) any later version.
 9  
  *  
 10  
  * This library is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13  
  * Lesser General Public License for more details.
 14  
  * 
 15  
  * You should have received a copy of the GNU Lesser General Public
 16  
  * License along with this library; if not, write to the Free Software
 17  
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 18  
  */
 19  
 package org.jaudiotagger.audio.generic;
 20  
 
 21  
 import org.jaudiotagger.logging.ErrorMessage;
 22  
 import org.jaudiotagger.tag.*;
 23  
 import org.jaudiotagger.tag.datatype.Artwork;
 24  
 
 25  
 import java.util.Collections;
 26  
 import java.util.EnumSet;
 27  
 import java.util.List;
 28  
 
 29  
 /**
 30  
  * This is a complete example implementation of
 31  
  * {@link AbstractTag} and it currenlty used to provide basic support to audio formats with only read tagging
 32  
  * ability such as Real or Wav files <br>
 33  
  *
 34  
  * @author Raphaël Slinckx
 35  
  */
 36  80
 public abstract class GenericTag extends AbstractTag
 37  
 {
 38  
     private static EnumSet<FieldKey> supportedKeys;
 39  
 
 40  
     static
 41  
     {
 42  4
         supportedKeys = EnumSet.of(FieldKey.ALBUM,FieldKey.ARTIST,FieldKey.TITLE,FieldKey.TRACK,FieldKey.GENRE,FieldKey.COMMENT,FieldKey.YEAR);
 43  4
     }
 44  
     /**
 45  
      * Implementations of {@link TagTextField} for use with
 46  
      * &quot;ISO-8859-1&quot; strings.
 47  
      *
 48  
      * @author Raphaël Slinckx
 49  
      */
 50  80
     private class GenericTagTextField implements TagTextField
 51  
     {
 52  
 
 53  
         /**
 54  
          * Stores the string.
 55  
          */
 56  
         private String content;
 57  
 
 58  
         /**
 59  
          * Stores the identifier.
 60  
          */
 61  
         private final String id;
 62  
 
 63  
         /**
 64  
          * Creates an instance.
 65  
          *
 66  
          * @param fieldId        The identifier.
 67  
          * @param initialContent The string.
 68  
          */
 69  
         public GenericTagTextField(String fieldId, String initialContent)
 70  180
         {
 71  180
             this.id = fieldId;
 72  180
             this.content = initialContent;
 73  180
         }
 74  
 
 75  
         /**
 76  
          * (overridden)
 77  
          *
 78  
          * @see org.jaudiotagger.tag.TagField#copyContent(org.jaudiotagger.tag.TagField)
 79  
          */
 80  
         public void copyContent(TagField field)
 81  
         {
 82  0
             if (field instanceof TagTextField)
 83  
             {
 84  0
                 this.content = ((TagTextField) field).getContent();
 85  
             }
 86  0
         }
 87  
 
 88  
         /**
 89  
          * (overridden)
 90  
          *
 91  
          * @see org.jaudiotagger.tag.TagTextField#getContent()
 92  
          */
 93  
         public String getContent()
 94  
         {
 95  132
             return this.content;
 96  
         }
 97  
 
 98  
         /**
 99  
          * (overridden)
 100  
          *
 101  
          * @see org.jaudiotagger.tag.TagTextField#getEncoding()
 102  
          */
 103  
         public String getEncoding()
 104  
         {
 105  0
             return "ISO-8859-1";
 106  
         }
 107  
 
 108  
         /**
 109  
          * (overridden)
 110  
          *
 111  
          * @see org.jaudiotagger.tag.TagField#getId()
 112  
          */
 113  
         public String getId()
 114  
         {
 115  360
             return id;
 116  
         }
 117  
 
 118  
         /**
 119  
          * (overridden)
 120  
          *
 121  
          * @see org.jaudiotagger.tag.TagField#getRawContent()
 122  
          */
 123  
         public byte[] getRawContent()
 124  
         {
 125  0
             return this.content == null ? new byte[]{} : Utils.getDefaultBytes(this.content, getEncoding());
 126  
         }
 127  
 
 128  
         /**
 129  
          * (overridden)
 130  
          *
 131  
          * @see org.jaudiotagger.tag.TagField#isBinary()
 132  
          */
 133  
         public boolean isBinary()
 134  
         {
 135  0
             return false;
 136  
         }
 137  
 
 138  
         /**
 139  
          * (overridden)
 140  
          *
 141  
          * @see org.jaudiotagger.tag.TagField#isBinary(boolean)
 142  
          */
 143  
         public void isBinary(boolean b)
 144  
         {
 145  
             /* not supported */
 146  0
         }
 147  
 
 148  
         /**
 149  
          * (overridden)
 150  
          *
 151  
          * @see org.jaudiotagger.tag.TagField#isCommon()
 152  
          */
 153  
         public boolean isCommon()
 154  
         {
 155  180
             return true;
 156  
         }
 157  
 
 158  
         /**
 159  
          * (overridden)
 160  
          *
 161  
          * @see org.jaudiotagger.tag.TagField#isEmpty()
 162  
          */
 163  
         public boolean isEmpty()
 164  
         {
 165  0
             return this.content.equals("");
 166  
         }
 167  
 
 168  
         /**
 169  
          * (overridden)
 170  
          *
 171  
          * @see org.jaudiotagger.tag.TagTextField#setContent(java.lang.String)
 172  
          */
 173  
         public void setContent(String s)
 174  
         {
 175  0
             this.content = s;
 176  0
         }
 177  
 
 178  
         /**
 179  
          * (overridden)
 180  
          *
 181  
          * @see org.jaudiotagger.tag.TagTextField#setEncoding(java.lang.String)
 182  
          */
 183  
         public void setEncoding(String s)
 184  
         {
 185  
             /* Not allowed */
 186  0
         }
 187  
 
 188  
         /**
 189  
          * (overridden)
 190  
          *
 191  
          * @see java.lang.Object#toString()
 192  
          */
 193  
         public String toString()
 194  
         {
 195  132
             return getContent();
 196  
         }
 197  
     }
 198  
 
 199  
     /**
 200  
      * (overridden)
 201  
      *
 202  
      * @see org.jaudiotagger.audio.generic.AbstractTag#isAllowedEncoding(java.lang.String)
 203  
      */
 204  
     protected boolean isAllowedEncoding(String enc)
 205  
     {
 206  0
         return true;
 207  
     }
 208  
 
 209  
     public TagField createField(FieldKey genericKey, String value) throws KeyNotFoundException, FieldDataInvalidException
 210  
     {
 211  188
         if(supportedKeys.contains(genericKey))
 212  
         {
 213  180
             return new GenericTagTextField(genericKey.name(),value);
 214  
         }
 215  
         else
 216  
         {
 217  8
             throw new UnsupportedOperationException(ErrorMessage.GENERIC_NOT_SUPPORTED.getMsg());
 218  
         }
 219  
     }
 220  
 
 221  
     /**
 222  
      * @param genericKey
 223  
      * @return
 224  
      * @throws KeyNotFoundException
 225  
      */
 226  
     public String getFirst(FieldKey genericKey) throws KeyNotFoundException
 227  
     {
 228  160
         if(supportedKeys.contains(genericKey))
 229  
         {
 230  160
             return getFirst(genericKey.name());
 231  
         }
 232  
         else
 233  
         {
 234  0
             throw new UnsupportedOperationException(ErrorMessage.GENERIC_NOT_SUPPORTED.getMsg());
 235  
         }
 236  
     }
 237  
 
 238  
     /**
 239  
      * @param genericKey
 240  
      * @throws KeyNotFoundException
 241  
      */
 242  
     public void deleteField(FieldKey genericKey) throws KeyNotFoundException
 243  
     {
 244  8
         if(supportedKeys.contains(genericKey))
 245  
         {
 246  0
             deleteField(genericKey.name());
 247  
         }
 248  
         else
 249  
         {
 250  8
             throw new UnsupportedOperationException(ErrorMessage.GENERIC_NOT_SUPPORTED.getMsg());
 251  
         }
 252  0
     }
 253  
 
 254  
     /**
 255  
      * @param genericKey
 256  
      * @return
 257  
      * @throws KeyNotFoundException
 258  
      */
 259  
     public TagField getFirstField(FieldKey genericKey) throws KeyNotFoundException
 260  
     {
 261  0
         if(supportedKeys.contains(genericKey))
 262  
         {
 263  0
             return getFirstField(genericKey.name());
 264  
         }
 265  
         else
 266  
         {
 267  0
             throw new UnsupportedOperationException(ErrorMessage.GENERIC_NOT_SUPPORTED.getMsg());
 268  
         }
 269  
     }
 270  
 
 271  
     public List<Artwork> getArtworkList()
 272  
     {
 273  8
         return Collections.emptyList();
 274  
     }
 275  
 
 276  
     public TagField createField(Artwork artwork) throws FieldDataInvalidException
 277  
     {
 278  8
         throw new UnsupportedOperationException(ErrorMessage.GENERIC_NOT_SUPPORTED.getMsg());
 279  
     }
 280  
 }