Coverage Report - org.jaudiotagger.audio.asf.util.TagConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
TagConverter
42%
28/66
52%
21/40
5
 
 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.*;
 22  
 import org.jaudiotagger.tag.FieldKey;
 23  
 import org.jaudiotagger.tag.Tag;
 24  
 import org.jaudiotagger.tag.asf.*;
 25  
 import org.jaudiotagger.tag.reference.GenreTypes;
 26  
 
 27  
 import java.util.Iterator;
 28  
 import java.util.List;
 29  
 
 30  
 /**
 31  
  * This class provides functionality to convert
 32  
  * {@link org.jaudiotagger.audio.asf.data.AsfHeader}objects into
 33  
  * {@link org.jaudiotagger.tag.Tag}objects.<br>
 34  
  * 
 35  
  * @author Christian Laireiter (liree)
 36  
  */
 37  4
 public final class TagConverter {
 38  
 
 39  
     /**
 40  
      * This method assigns those tags of <code>tag</code> which are defined to
 41  
      * be common by jaudiotagger. <br>
 42  
      * 
 43  
      * @param tag
 44  
      *            The tag from which the values are gathered. <br>
 45  
      *            Assigned values are: <br>
 46  
      * @param description
 47  
      *            The extended content description which should receive the
 48  
      *            values. <br>
 49  
      *            <b>Warning: </b> the common values will be replaced.
 50  
      */
 51  
     public static void assignCommonTagValues(Tag tag,
 52  
             MetadataContainer description) {
 53  0
         assert description.getContainerType() == ContainerType.EXTENDED_CONTENT;
 54  
         MetadataDescriptor tmp;
 55  0
         if (!Utils.isBlank(tag.getFirst(FieldKey.ALBUM))) {
 56  0
             tmp = new MetadataDescriptor(description.getContainerType(),
 57  
                     AsfFieldKey.ALBUM.getFieldName(),
 58  
                     MetadataDescriptor.TYPE_STRING);
 59  0
             tmp.setStringValue(tag.getFirst(FieldKey.ALBUM));
 60  0
             description.removeDescriptorsByName(tmp.getName());
 61  0
             description.addDescriptor(tmp);
 62  
         } else {
 63  0
             description.removeDescriptorsByName(AsfFieldKey.ALBUM
 64  
                     .getFieldName());
 65  
         }
 66  0
         if (!Utils.isBlank(tag.getFirst(FieldKey.TRACK))) {
 67  0
             tmp = new MetadataDescriptor(description.getContainerType(),
 68  
                     AsfFieldKey.TRACK.getFieldName(),
 69  
                     MetadataDescriptor.TYPE_STRING);
 70  0
             tmp.setStringValue(tag.getFirst(FieldKey.TRACK));
 71  0
             description.removeDescriptorsByName(tmp.getName());
 72  0
             description.addDescriptor(tmp);
 73  
         } else {
 74  0
             description.removeDescriptorsByName(AsfFieldKey.TRACK
 75  
                     .getFieldName());
 76  
         }
 77  0
         if (!Utils.isBlank(tag.getFirst(FieldKey.YEAR))) {
 78  0
             tmp = new MetadataDescriptor(description.getContainerType(),
 79  
                     AsfFieldKey.YEAR.getFieldName(),
 80  
                     MetadataDescriptor.TYPE_STRING);
 81  0
             tmp.setStringValue(tag.getFirst(FieldKey.YEAR));
 82  0
             description.removeDescriptorsByName(tmp.getName());
 83  0
             description.addDescriptor(tmp);
 84  
         } else {
 85  0
             description
 86  
                     .removeDescriptorsByName(AsfFieldKey.YEAR.getFieldName());
 87  
         }
 88  0
         if (!Utils.isBlank(tag.getFirst(FieldKey.GENRE))) {
 89  
             // Write Genre String value
 90  0
             tmp = new MetadataDescriptor(description.getContainerType(),
 91  
                     AsfFieldKey.GENRE.getFieldName(),
 92  
                     MetadataDescriptor.TYPE_STRING);
 93  0
             tmp.setStringValue(tag.getFirst(FieldKey.GENRE));
 94  0
             description.removeDescriptorsByName(tmp.getName());
 95  0
             description.addDescriptor(tmp);
 96  0
             Integer genreNum = GenreTypes.getInstanceOf().getIdForName(
 97  
                     tag.getFirst(FieldKey.GENRE));
 98  
             // ..and if it is one of the standard genre types used the id as
 99  
             // well
 100  0
             if (genreNum != null) {
 101  0
                 tmp = new MetadataDescriptor(description.getContainerType(),
 102  
                         AsfFieldKey.GENRE_ID.getFieldName(),
 103  
                         MetadataDescriptor.TYPE_STRING);
 104  0
                 tmp.setStringValue("(" + genreNum + ")");
 105  0
                 description.removeDescriptorsByName(tmp.getName());
 106  0
                 description.addDescriptor(tmp);
 107  
             } else {
 108  0
                 description.removeDescriptorsByName(AsfFieldKey.GENRE_ID
 109  
                         .getFieldName());
 110  
             }
 111  0
         } else {
 112  0
             description.removeDescriptorsByName(AsfFieldKey.GENRE
 113  
                     .getFieldName());
 114  0
             description.removeDescriptorsByName(AsfFieldKey.GENRE_ID
 115  
                     .getFieldName());
 116  
         }
 117  0
     }
 118  
 
 119  
     /**
 120  
      * This method creates a {@link Tag}and fills it with the contents of the
 121  
      * given {@link AsfHeader}.<br>
 122  
      * 
 123  
      * @param source
 124  
      *            The ASF header which contains the information. <br>
 125  
      * @return A Tag with all its values.
 126  
      */
 127  
     public static AsfTag createTagOf(AsfHeader source) {
 128  
         // TODO do we need to copy here.
 129  222
         AsfTag result = new AsfTag(true);
 130  1332
         for (int i = 0; i < ContainerType.values().length; i++) {
 131  1110
             MetadataContainer current = source
 132  
                     .findMetadataContainer(ContainerType.values()[i]);
 133  1110
             if (current != null) {
 134  468
                 List<MetadataDescriptor> descriptors = current.getDescriptors();
 135  468
                 for (MetadataDescriptor descriptor : descriptors) {
 136  
                     AsfTagField toAdd;
 137  5517
                     if (descriptor.getType() == MetadataDescriptor.TYPE_BINARY) {
 138  84
                         if (descriptor.getName().equals(
 139  
                                 AsfFieldKey.COVER_ART.getFieldName())) {
 140  82
                             toAdd = new AsfTagCoverField(descriptor);
 141  2
                         } else if (descriptor.getName().equals(
 142  
                                 AsfFieldKey.BANNER_IMAGE.getFieldName())) {
 143  0
                             toAdd = new AsfTagBannerField(descriptor);
 144  
                         } else {
 145  2
                             toAdd = new AsfTagField(descriptor);
 146  
                         }
 147  
                     } else {
 148  5433
                         toAdd = new AsfTagTextField(descriptor);
 149  
                     }
 150  5517
                     result.addField(toAdd);
 151  5517
                 }
 152  
             }
 153  
         }
 154  222
         return result;
 155  
     }
 156  
 
 157  
     /**
 158  
      * This method distributes the tags fields among the
 159  
      * {@linkplain ContainerType#getOrdered()} {@linkplain MetadataContainer
 160  
      * containers}.
 161  
      * 
 162  
      * @param tag
 163  
      *            the tag with the fields to distribute.
 164  
      * @return distribution
 165  
      */
 166  
     public static MetadataContainer[] distributeMetadata(final AsfTag tag) {
 167  153
         final Iterator<AsfTagField> asfFields = tag.getAsfFields();
 168  153
         final MetadataContainer[] createContainers = MetadataContainerFactory
 169  
                 .getInstance().createContainers(ContainerType.getOrdered());
 170  
         boolean assigned;
 171  
         AsfTagField current;
 172  2689
         while (asfFields.hasNext()) {
 173  2536
             current = asfFields.next();
 174  2536
             assigned = false;
 175  9833
             for (int i = 0; !assigned && i < createContainers.length; i++) {
 176  7297
                 if (ContainerType.areInCorrectOrder(createContainers[i]
 177  
                         .getContainerType(), AsfFieldKey.getAsfFieldKey(
 178  
                         current.getId()).getHighestContainer())) {
 179  7297
                     if (createContainers[i].isAddSupported(current
 180  
                             .getDescriptor())) {
 181  2536
                         createContainers[i].addDescriptor(current
 182  
                                 .getDescriptor());
 183  2536
                         assigned = true;
 184  
                     }
 185  
                 }
 186  
             }
 187  2536
             assert assigned;
 188  
         }
 189  153
         return createContainers;
 190  
     }
 191  
 
 192  
     /**
 193  
      * Hidden utility class constructor.
 194  
      */
 195  0
     private TagConverter() {
 196  
         // Nothing to do.
 197  0
     }
 198  
 
 199  
 }