Coverage Report - org.jaudiotagger.tag.id3.framebody.AbstractFrameBodyUrlLink
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractFrameBodyUrlLink
73%
25/34
75%
6/8
1.778
 
 1  
 /**
 2  
  *  @author : Paul Taylor
 3  
  *  @author : Eric Farng
 4  
  *
 5  
  *  Version @version:$Id: AbstractFrameBodyUrlLink.java 832 2009-11-12 13:25:38Z paultaylor $
 6  
  *
 7  
  *  MusicTag Copyright (C)2003,2004
 8  
  *
 9  
  *  This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser
 10  
  *  General Public  License as published by the Free Software Foundation; either version 2.1 of the License,
 11  
  *  or (at your option) any later version.
 12  
  *
 13  
  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 14  
  *  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 15  
  *  See the GNU Lesser General Public License for more details.
 16  
  *
 17  
  *  You should have received a copy of the GNU Lesser General Public License along with this library; if not,
 18  
  *  you can get a copy from http://www.opensource.org/licenses/lgpl-license.php or write to the Free Software
 19  
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 20  
  *
 21  
 
 22  
  *
 23  
  */
 24  
 package org.jaudiotagger.tag.id3.framebody;
 25  
 
 26  
 import org.jaudiotagger.logging.ErrorMessage;
 27  
 import org.jaudiotagger.tag.InvalidTagException;
 28  
 import org.jaudiotagger.tag.datatype.DataTypes;
 29  
 import org.jaudiotagger.tag.datatype.StringSizeTerminated;
 30  
 import org.jaudiotagger.tag.id3.valuepair.TextEncoding;
 31  
 
 32  
 import java.io.ByteArrayOutputStream;
 33  
 import java.io.UnsupportedEncodingException;
 34  
 import java.net.URLEncoder;
 35  
 import java.nio.ByteBuffer;
 36  
 import java.nio.charset.Charset;
 37  
 import java.nio.charset.CharsetEncoder;
 38  
 
 39  
 /**
 40  
  * Abstract superclass of all URL Frames
 41  
  */
 42  
 public abstract class AbstractFrameBodyUrlLink extends AbstractID3v2FrameBody
 43  
 {
 44  
 
 45  
     /**
 46  
      * Creates a new FrameBodyUrlLink datatype.
 47  
      */
 48  
     protected AbstractFrameBodyUrlLink()
 49  
     {
 50  272
         super();
 51  272
     }
 52  
 
 53  
     /**
 54  
      * Copy Constructor
 55  
      * @param body
 56  
      */
 57  
     protected AbstractFrameBodyUrlLink(AbstractFrameBodyUrlLink body)
 58  
     {
 59  204
         super(body);
 60  204
     }
 61  
 
 62  
     /**
 63  
      * Creates a new FrameBodyUrlLink datatype., set up with data.
 64  
      *
 65  
      * @param urlLink
 66  
      */
 67  
     public AbstractFrameBodyUrlLink(String urlLink)
 68  0
     {
 69  0
         setObjectValue(DataTypes.OBJ_URLLINK, urlLink);
 70  0
     }
 71  
 
 72  
     /**
 73  
      * Creates a new FrameBodyUrlLink datatype.
 74  
      *
 75  
      * @param byteBuffer
 76  
      * @param frameSize
 77  
      * @throws InvalidTagException if unable to create framebody from buffer
 78  
      */
 79  
     protected AbstractFrameBodyUrlLink(ByteBuffer byteBuffer, int frameSize) throws InvalidTagException
 80  
     {
 81  228
         super(byteBuffer, frameSize);
 82  228
     }
 83  
 
 84  
     /**
 85  
      * Set URL Link
 86  
      *
 87  
      * @param urlLink
 88  
      */
 89  
     public void setUrlLink(String urlLink)
 90  
     {
 91  156
         if (urlLink == null)
 92  
         {
 93  0
             throw new IllegalArgumentException(ErrorMessage.GENERAL_INVALID_NULL_ARGUMENT.getMsg());
 94  
         }
 95  156
         setObjectValue(DataTypes.OBJ_URLLINK, urlLink);
 96  156
     }
 97  
 
 98  
     /**
 99  
      * Get URL Link
 100  
      *
 101  
      * @return the urllink
 102  
      */
 103  
     public String getUrlLink()
 104  
     {
 105  324
         return (String) getObjectValue(DataTypes.OBJ_URLLINK);
 106  
     }
 107  
 
 108  
     /**
 109  
      * If the description cannot be encoded using the current encoding change the encoder
 110  
      */
 111  
     public void write(ByteArrayOutputStream tagBuffer)
 112  
     {
 113  192
         CharsetEncoder encoder = Charset.forName(TextEncoding.CHARSET_ISO_8859_1).newEncoder();
 114  192
         String origUrl = getUrlLink();
 115  192
         if (!encoder.canEncode(origUrl))
 116  
         {
 117  
             //ALL W Frames only support ISO-8859-1 for the url itself, if unable to encode let us assume
 118  
             //the link just needs url encoding
 119  8
             setUrlLink(encodeURL(origUrl));
 120  
 
 121  
             //We still cant convert so just set log error and set to blank to allow save to continue
 122  8
             if (!encoder.canEncode(getUrlLink()))
 123  
             {
 124  0
                 logger.warning(ErrorMessage.MP3_UNABLE_TO_ENCODE_URL.getMsg(origUrl));
 125  0
                 setUrlLink("");
 126  
             }
 127  
             //it was ok, just note the modification made
 128  
             else
 129  
             {
 130  8
                 logger.warning(ErrorMessage.MP3_URL_SAVED_ENCODED.getMsg(origUrl, getUrlLink()));
 131  
             }
 132  
         }
 133  192
         super.write(tagBuffer);
 134  192
     }
 135  
 
 136  
     /**
 137  
      *
 138  
      */
 139  
     protected void setupObjectList()
 140  
     {
 141  116
         objectList.add(new StringSizeTerminated(DataTypes.OBJ_URLLINK, this));
 142  116
     }
 143  
 
 144  
     /**
 145  
      * Encode url because may receive url already encoded or not, but we can only store as ISO8859-1
 146  
      *
 147  
      * @param url
 148  
      * @return
 149  
      */
 150  
     private String encodeURL(String url)
 151  
     {
 152  
         try
 153  
         {
 154  8
             final String[] splitURL = url.split("(?<!/)/(?!/)", -1);
 155  8
             final StringBuffer sb = new StringBuffer(splitURL[0]);
 156  24
             for (int i = 1; i < splitURL.length; i++)
 157  
             {
 158  16
                 sb.append("/").append(URLEncoder.encode(splitURL[i], "utf-8"));
 159  
             }
 160  8
             return sb.toString();
 161  
         }
 162  0
         catch (UnsupportedEncodingException uee)
 163  
         {
 164  
             //Should never happen as utf-8 is always availablebut in case it does we just return the utl
 165  
             //unmodified
 166  0
             logger.warning("Uable to url encode because utf-8 charset not available:" + uee.getMessage());
 167  0
             return url;
 168  
         }
 169  
     }
 170  
 }