Coverage Report - org.jaudiotagger.audio.asf.util.TagConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
TagConverter
94%
63/67
83%
25/30
0
 
 1  
 /*
 2  
  * Entagged Audio Tag library
 3  
  * Copyright (c) 2004-2005 Christian Laireiter <liree@web.de>
 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.asf.util;
 20  
 
 21  
 import org.jaudiotagger.audio.asf.data.AsfHeader;
 22  
 import org.jaudiotagger.audio.asf.data.ContentDescription;
 23  
 import org.jaudiotagger.audio.asf.data.ContentDescriptor;
 24  
 import org.jaudiotagger.audio.asf.data.ExtendedContentDescription;
 25  
 import org.jaudiotagger.audio.asf.tag.*;
 26  
 import org.jaudiotagger.tag.Tag;
 27  
 import org.jaudiotagger.tag.reference.GenreTypes;
 28  
 
 29  
 import java.util.Iterator;
 30  
 
 31  
 /**
 32  
  * This class provides functionality to convert
 33  
  * {@link org.jaudiotagger.audio.asf.data.AsfHeader}objects into
 34  
  * {@link org.jaudiotagger.tag.Tag}objects (More extract information and
 35  
  * create a {@link org.jaudiotagger.audio.generic.GenericTag}).<br>
 36  
  *
 37  
  * @author Christian Laireiter (liree)
 38  
  */
 39  5
 public class TagConverter
 40  
 {
 41  
 
 42  
     /**
 43  
      * This method assigns those tags of <code>tag</code> which are defined to
 44  
      * be common by entagged. <br>
 45  
      *
 46  
      * @param tag         The tag from which the values are gathered. <br>
 47  
      *                    Assigned values are: <br>
 48  
      * @param description The extended content description which should receive the
 49  
      *                    values. <br>
 50  
      *                    <b>Warning: </b> the common values will be replaced.
 51  
      * @see Tag#getAlbum() <br>
 52  
      * @see Tag#getTrack() <br>
 53  
      * @see Tag#getYear() <br>
 54  
      * @see Tag#getGenre() <br>
 55  
      */
 56  
     public static void assignCommonTagValues(Tag tag, ExtendedContentDescription description)
 57  
     {
 58  13
         ContentDescriptor tmp = null;
 59  13
         if (!Utils.isBlank(tag.getFirstAlbum()))
 60  
         {
 61  8
             tmp = new ContentDescriptor(AsfFieldKey.ALBUM.getFieldName(), ContentDescriptor.TYPE_STRING);
 62  8
             tmp.setStringValue(tag.getFirstAlbum());
 63  8
             description.addOrReplace(tmp);
 64  
         }
 65  
         else
 66  
         {
 67  5
             description.remove(AsfFieldKey.ALBUM.getFieldName());
 68  
         }
 69  13
         if (!Utils.isBlank(tag.getFirstTrack()))
 70  
         {
 71  8
             tmp = new ContentDescriptor(AsfFieldKey.TRACK.getFieldName(), ContentDescriptor.TYPE_STRING);
 72  8
             tmp.setStringValue(tag.getFirstTrack());
 73  8
             description.addOrReplace(tmp);
 74  
         }
 75  
         else
 76  
         {
 77  5
             description.remove(AsfFieldKey.TRACK.getFieldName());
 78  
         }
 79  13
         if (!Utils.isBlank(tag.getFirstYear()))
 80  
         {
 81  8
             tmp = new ContentDescriptor(AsfFieldKey.YEAR.getFieldName(), ContentDescriptor.TYPE_STRING);
 82  8
             tmp.setStringValue(tag.getFirstYear());
 83  8
             description.addOrReplace(tmp);
 84  
         }
 85  
         else
 86  
         {
 87  5
             description.remove(AsfFieldKey.YEAR.getFieldName());
 88  
         }
 89  13
         if (!Utils.isBlank(tag.getFirstGenre()))
 90  
         {
 91  8
             tmp = new ContentDescriptor(AsfFieldKey.GENRE.getFieldName(), ContentDescriptor.TYPE_STRING);
 92  8
             tmp.setStringValue(tag.getFirstGenre());
 93  8
             description.addOrReplace(tmp);
 94  8
             Integer genreNum = GenreTypes.getInstanceOf().getIdForName(tag.getFirstGenre());
 95  8
             if (genreNum != null)
 96  
             {
 97  0
                 tmp = new ContentDescriptor(AsfFieldKey.GENRE_ID.getFieldName(), ContentDescriptor.TYPE_STRING);
 98  0
                 tmp.setStringValue("(" + genreNum + ")");
 99  0
                 description.addOrReplace(tmp);
 100  
             }
 101  
             else
 102  
             {
 103  8
                 description.remove(AsfFieldKey.GENRE_ID.getFieldName());
 104  
             }
 105  8
         }
 106  
         else
 107  
         {
 108  5
             description.remove(AsfFieldKey.GENRE.getFieldName());
 109  5
             description.remove(AsfFieldKey.GENRE_ID.getFieldName());
 110  
         }
 111  13
     }
 112  
 
 113  
     /**
 114  
      * This method will add or replace all values of tag are not defined as
 115  
      * common by entagged.
 116  
      *
 117  
      * @param tag        The tag containing the values.
 118  
      * @param descriptor the extended content description.
 119  
      */
 120  
     public static void assignOptionalTagValues(AsfTag tag, ExtendedContentDescription descriptor)
 121  
     {
 122  13
         assert tag.isCopyingFields();
 123  13
         Iterator<AsfTagField> it = tag.getAsfFields();
 124  378
         while (it.hasNext())
 125  
         {
 126  365
             ContentDescriptor contentDesc = it.next().getDescriptor();
 127  
             /*
 128  
              * If assignCommonTagValues(..) was called prior to this method, some values have already
 129  
              * been worked up. So here those values should not be overridden.
 130  
              */
 131  365
             if (!descriptor.containsDescriptor(contentDesc.getName()))
 132  
             {
 133  333
                 descriptor.addOrReplace(contentDesc);
 134  
             }
 135  365
         }
 136  13
     }
 137  
 
 138  
     /**
 139  
      * This method creates a new {@link ContentDescription}object, filled with
 140  
      * the corresponding values of the given <code>tag</code>.<br>
 141  
      * <b>Warning </b>: <br>
 142  
      * Only the first values can be stored in ASF files, because the content
 143  
      * description is limited.
 144  
      *
 145  
      * @param tag The tag from which the values are taken. <br>
 146  
      * @return A new content description object filled with <code>tag</code>.
 147  
      * @see Tag#getFirstArtist() <br>
 148  
      * @see Tag#getFirstTitle() <br>
 149  
      * @see Tag#getFirstComment() <br>
 150  
      * @see AsfTag#getFirstCopyright() <br>
 151  
      */
 152  
     public static ContentDescription createContentDescription(AsfTag tag)
 153  
     {
 154  17
         ContentDescription result = new ContentDescription();
 155  17
         result.setAuthor(tag.getFirstArtist());
 156  17
         result.setTitle(tag.getFirstTitle());
 157  17
         result.setComment(tag.getFirstComment());
 158  17
         result.setCopyRight(tag.getFirstCopyright());
 159  17
         result.setRating(tag.getFirstRating());
 160  17
         return result;
 161  
     }
 162  
 
 163  
     /**
 164  
      * This method creates a {@link Tag}and fills it with the contents of the
 165  
      * given {@link AsfHeader}.<br>
 166  
      *
 167  
      * @param source The ASF header which contains the information. <br>
 168  
      * @return A Tag with all its values.
 169  
      */
 170  
     //TODO do we need to copy all the tag fields really
 171  
     public static AsfTag createTagOf(AsfHeader source)
 172  
     {        
 173  44
         AsfTag result = new AsfTag(true);
 174  44
         final ContentDescription contentDescription = source.findContentDescription();
 175  44
         final ExtendedContentDescription extDesc = source.findExtendedContentDescription();
 176  
         /*
 177  
          * It is possible that the file contains no content description, since
 178  
          * that some informations aren't available.
 179  
          */
 180  
         
 181  44
         if (source.findContentDescription() != null)
 182  
         {
 183  35
             result.setArtist(contentDescription.getAuthor());
 184  35
             result.setComment(contentDescription.getComment());
 185  35
             result.setTitle(contentDescription.getTitle());
 186  35
             result.setCopyright(contentDescription.getCopyRight());
 187  35
             result.setRating(contentDescription.getRating());
 188  
         }
 189  
         // It is possible that the file contains no extended content
 190  
         // description. In that case some informations cannot be provided.
 191  44
         if (extDesc != null)
 192  
         {
 193  
             // Now any properties, which don't belong to the common section
 194  30
             Iterator<ContentDescriptor> it = extDesc.getDescriptors().iterator();
 195  885
             while (it.hasNext())
 196  
             {
 197  855
                 ContentDescriptor current = it.next();
 198  855
                 if (!AsfTag.storesDescriptor(current))
 199  
                 {
 200  816
                     if (current.getType() == ContentDescriptor.TYPE_BINARY)
 201  
                     {
 202  16
                         if(current.getName().equals(AsfFieldKey.COVER_ART.getFieldName()))
 203  
                         {
 204  16
                             result.add(new AsfTagCoverField(current));
 205  
                         }
 206  
                         else
 207  
                         {
 208  0
                             result.add(new AsfTagField(current));
 209  
                         }
 210  
                     }
 211  
                     else
 212  
                     {
 213  800
                         result.add(new AsfTagTextField(current));
 214  
                     }
 215  
                 }
 216  855
             }
 217  
         }
 218  44
         return result;
 219  
     }
 220  
 
 221  
 }