Coverage Report - org.jaudiotagger.audio.asf.data.ContentDescription
 
Classes in this File Line Coverage Branch Coverage Complexity
ContentDescription
87%
48/55
75%
3/4
1.062
 
 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.data;
 20  
 
 21  
 import org.jaudiotagger.audio.asf.util.Utils;
 22  
 
 23  
 import java.io.IOException;
 24  
 import java.io.OutputStream;
 25  
 import java.math.BigInteger;
 26  
 import java.util.Arrays;
 27  
 import java.util.HashSet;
 28  
 import java.util.Set;
 29  
 
 30  
 /**
 31  
  * This class represents the data of a chunk which contains title, author,
 32  
  * copyright, description and the rating of the file. <br>
 33  
  * It is optional within ASF files. But if, exists only once.
 34  
  * 
 35  
  * @author Christian Laireiter
 36  
  */
 37  
 public final class ContentDescription extends MetadataContainer {
 38  
     /**
 39  
      * Stores the only allowed keys of this metadata container.
 40  
      */
 41  
     public final static Set<String> ALLOWED;
 42  
 
 43  
     /**
 44  
      * Field key for author.
 45  
      */
 46  
     public final static String KEY_AUTHOR = "AUTHOR";
 47  
 
 48  
     /**
 49  
      * Field key for copyright.
 50  
      */
 51  
     public final static String KEY_COPYRIGHT = "COPYRIGHT";
 52  
 
 53  
     /**
 54  
      * Field key for description.
 55  
      */
 56  
     public final static String KEY_DESCRIPTION = "DESCRIPTION";
 57  
 
 58  
     /**
 59  
      * Field key for rating.
 60  
      */
 61  
     public final static String KEY_RATING = "RATING";
 62  
 
 63  
     /**
 64  
      * Field key for title.
 65  
      */
 66  
     public final static String KEY_TITLE = "TITLE";
 67  
 
 68  
     static {
 69  4
         ALLOWED = new HashSet<String>(Arrays.asList(KEY_AUTHOR,
 70  
                 KEY_COPYRIGHT, KEY_DESCRIPTION, KEY_RATING, KEY_TITLE));
 71  4
     }
 72  
 
 73  
     /**
 74  
      * Creates an instance. <br>
 75  
      */
 76  
     public ContentDescription() {
 77  4
         this(0, BigInteger.ZERO);
 78  4
     }
 79  
 
 80  
     /**
 81  
      * Creates an instance.
 82  
      * 
 83  
      * @param pos
 84  
      *            Position of content description within file or stream
 85  
      * @param chunkLen
 86  
      *            Length of content description.
 87  
      */
 88  
     public ContentDescription(final long pos,final BigInteger chunkLen) {
 89  504
         super(ContainerType.CONTENT_DESCRIPTION, pos, chunkLen);
 90  488
     }
 91  
 
 92  
     /**
 93  
      * @return Returns the author.
 94  
      */
 95  
     public String getAuthor() {
 96  473
         return getValueFor(KEY_AUTHOR);
 97  
     }
 98  
 
 99  
     /**
 100  
      * @return Returns the comment.
 101  
      */
 102  
     public String getComment() {
 103  473
         return getValueFor(KEY_DESCRIPTION);
 104  
     }
 105  
 
 106  
     /**
 107  
      * @return Returns the copyRight.
 108  
      */
 109  
     public String getCopyRight() {
 110  473
         return getValueFor(KEY_COPYRIGHT);
 111  
     }
 112  
 
 113  
     /**
 114  
      * {@inheritDoc}
 115  
      */
 116  
     @Override
 117  
     public long getCurrentAsfChunkSize() {
 118  279
         long result = 44; // GUID + UINT64 for size + 5 times string length
 119  
         // (each
 120  
         // 2 bytes) + 5 times zero term char (2 bytes each).
 121  279
         result += getAuthor().length() * 2; // UTF-16LE
 122  279
         result += getComment().length() * 2;
 123  279
         result += getRating().length() * 2;
 124  279
         result += getTitle().length() * 2;
 125  279
         result += getCopyRight().length() * 2;
 126  279
         return result;
 127  
     }
 128  
 
 129  
     /**
 130  
      * @return returns the rating.
 131  
      */
 132  
     public String getRating() {
 133  473
         return getValueFor(KEY_RATING);
 134  
     }
 135  
 
 136  
     /**
 137  
      * @return Returns the title.
 138  
      */
 139  
     public String getTitle() {
 140  473
         return getValueFor(KEY_TITLE);
 141  
     }
 142  
 
 143  
     /**
 144  
      * {@inheritDoc}
 145  
      */
 146  
     @Override
 147  
     public boolean isAddSupported(final MetadataDescriptor descriptor) {
 148  4174
         return ALLOWED.contains(descriptor.getName())
 149  
                 && super.isAddSupported(descriptor);
 150  
     }
 151  
 
 152  
     /**
 153  
      * 
 154  
      * {@inheritDoc}
 155  
      */
 156  
     @Override
 157  
     public String prettyPrint(final String prefix) {
 158  0
         final StringBuilder result = new StringBuilder(super.prettyPrint(prefix));
 159  0
         result.append(prefix).append("  |->Title      : ").append(getTitle())
 160  
                 .append(Utils.LINE_SEPARATOR);
 161  0
         result.append(prefix).append("  |->Author     : ").append(getAuthor())
 162  
                 .append(Utils.LINE_SEPARATOR);
 163  0
         result.append(prefix).append("  |->Copyright  : ").append(
 164  
                 getCopyRight()).append(Utils.LINE_SEPARATOR);
 165  0
         result.append(prefix).append("  |->Description: ").append(getComment())
 166  
                 .append(Utils.LINE_SEPARATOR);
 167  0
         result.append(prefix).append("  |->Rating     :").append(getRating())
 168  
                 .append(Utils.LINE_SEPARATOR);
 169  0
         return result.toString();
 170  
     }
 171  
 
 172  
     /**
 173  
      * @param fileAuthor
 174  
      *            The author to set.
 175  
      * @throws IllegalArgumentException
 176  
      *             If "UTF-16LE"-byte-representation would take more than 65535
 177  
      *             bytes.
 178  
      */
 179  
     public void setAuthor(final String fileAuthor) throws IllegalArgumentException {
 180  307
         setStringValue(KEY_AUTHOR, fileAuthor);
 181  303
     }
 182  
 
 183  
     /**
 184  
      * @param tagComment
 185  
      *            The comment to set.
 186  
      * @throws IllegalArgumentException
 187  
      *             If "UTF-16LE"-byte-representation would take more than 65535
 188  
      *             bytes.
 189  
      */
 190  
     public void setComment(final String tagComment) throws IllegalArgumentException {
 191  295
         setStringValue(KEY_DESCRIPTION, tagComment);
 192  291
     }
 193  
 
 194  
     /**
 195  
      * @param cpright
 196  
      *            The copyRight to set.
 197  
      * @throws IllegalArgumentException
 198  
      *             If "UTF-16LE"-byte-representation would take more than 65535
 199  
      *             bytes.
 200  
      */
 201  
     public void setCopyright(final String cpright) throws IllegalArgumentException {
 202  293
         setStringValue(KEY_COPYRIGHT, cpright);
 203  289
     }
 204  
 
 205  
     /**
 206  
      * @param ratingText
 207  
      *            The rating to be set.
 208  
      * @throws IllegalArgumentException
 209  
      *             If "UTF-16LE"-byte-representation would take more than 65535
 210  
      *             bytes.
 211  
      */
 212  
     public void setRating(final String ratingText) throws IllegalArgumentException {
 213  157
         setStringValue(KEY_RATING, ratingText);
 214  153
     }
 215  
 
 216  
     /**
 217  
      * @param songTitle
 218  
      *            The title to set.
 219  
      * @throws IllegalArgumentException
 220  
      *             If "UTF-16LE"-byte-representation would take more than 65535
 221  
      *             bytes.
 222  
      */
 223  
     public void setTitle(final String songTitle) throws IllegalArgumentException {
 224  307
         setStringValue(KEY_TITLE, songTitle);
 225  303
     }
 226  
 
 227  
     /**
 228  
      * {@inheritDoc}
 229  
      */
 230  
     @Override
 231  
     public long writeInto(final OutputStream out) throws IOException {
 232  93
         final long chunkSize = getCurrentAsfChunkSize();
 233  
 
 234  93
         out.write(this.getGuid().getBytes());
 235  93
         Utils.writeUINT64(getCurrentAsfChunkSize(), out);
 236  
         // write the sizes of the string representations plus 2 bytes zero term
 237  
         // character
 238  93
         Utils.writeUINT16(getTitle().length() * 2 + 2, out);
 239  93
         Utils.writeUINT16(getAuthor().length() * 2 + 2, out);
 240  93
         Utils.writeUINT16(getCopyRight().length() * 2 + 2, out);
 241  93
         Utils.writeUINT16(getComment().length() * 2 + 2, out);
 242  93
         Utils.writeUINT16(getRating().length() * 2 + 2, out);
 243  
         // write the Strings
 244  93
         out.write(Utils.getBytes(getTitle(), AsfHeader.ASF_CHARSET));
 245  93
         out.write(AsfHeader.ZERO_TERM);
 246  93
         out.write(Utils.getBytes(getAuthor(), AsfHeader.ASF_CHARSET));
 247  93
         out.write(AsfHeader.ZERO_TERM);
 248  93
         out.write(Utils.getBytes(getCopyRight(), AsfHeader.ASF_CHARSET));
 249  93
         out.write(AsfHeader.ZERO_TERM);
 250  93
         out.write(Utils.getBytes(getComment(), AsfHeader.ASF_CHARSET));
 251  93
         out.write(AsfHeader.ZERO_TERM);
 252  93
         out.write(Utils.getBytes(getRating(), AsfHeader.ASF_CHARSET));
 253  93
         out.write(AsfHeader.ZERO_TERM);
 254  93
         return chunkSize;
 255  
     }
 256  
 }