Coverage Report - org.jaudiotagger.tag.id3.framebody.AbstractFrameBodyUrlLink
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractFrameBodyUrlLink
74%
25/34
75%
6/8
1.778
 
 1  
 /**
 2  
  *  @author : Paul Taylor
 3  
  *  @author : Eric Farng
 4  
  *
 5  
  *  Version @version:$Id: AbstractFrameBodyUrlLink.java,v 1.13 2008/07/21 10:45:42 paultaylor Exp $
 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  65
         super();
 51  65
     }
 52  
 
 53  
     /**
 54  
      * Copy Constructor
 55  
      */
 56  
     protected AbstractFrameBodyUrlLink(AbstractFrameBodyUrlLink body)
 57  
     {
 58  42
         super(body);
 59  42
     }
 60  
 
 61  
     /**
 62  
      * Creates a new FrameBodyUrlLink datatype., set up with data.
 63  
      *
 64  
      * @param urlLink
 65  
      */
 66  
     public AbstractFrameBodyUrlLink(String urlLink)
 67  0
     {
 68  0
         setObjectValue(DataTypes.OBJ_URLLINK, urlLink);
 69  0
     }
 70  
 
 71  
     /**
 72  
      * Creates a new FrameBodyUrlLink datatype.
 73  
      *
 74  
      * @throws InvalidTagException if unable to create framebody from buffer
 75  
      */
 76  
     protected AbstractFrameBodyUrlLink(ByteBuffer byteBuffer, int frameSize) throws InvalidTagException
 77  
     {
 78  48
         super(byteBuffer, frameSize);
 79  48
     }
 80  
 
 81  
     /**
 82  
      * Set URL Link
 83  
      *
 84  
      * @param urlLink
 85  
      */
 86  
     public void setUrlLink(String urlLink)
 87  
     {
 88  36
         if (urlLink == null)
 89  
         {
 90  0
             throw new IllegalArgumentException(ErrorMessage.GENERAL_INVALID_NULL_ARGUMENT.getMsg());
 91  
         }
 92  36
         setObjectValue(DataTypes.OBJ_URLLINK, urlLink);
 93  36
     }
 94  
 
 95  
     /**
 96  
      * Get URL Link
 97  
      *
 98  
      * @return the urllink
 99  
      */
 100  
     public String getUrlLink()
 101  
     {
 102  76
         return (String) getObjectValue(DataTypes.OBJ_URLLINK);
 103  
     }
 104  
 
 105  
     /**
 106  
      * If the description cannot be encoded using the current encoding change the encoder
 107  
      */
 108  
     public void write(ByteArrayOutputStream tagBuffer)
 109  
     {
 110  43
         CharsetEncoder encoder = Charset.forName(TextEncoding.CHARSET_ISO_8859_1).newEncoder();
 111  43
         String origUrl = getUrlLink();
 112  43
         if (!encoder.canEncode(origUrl))
 113  
         {
 114  
             //ALL W Frames only support ISO-8859-1 for the url itself, if unable to encode let us assume
 115  
             //the link just needs url encoding
 116  2
             setUrlLink(encodeURL(origUrl));
 117  
 
 118  
             //We still cant convert so just set log error and set to blank to allow save to continue
 119  2
             if (!encoder.canEncode(getUrlLink()))
 120  
             {
 121  0
                 logger.warning(ErrorMessage.MP3_UNABLE_TO_ENCODE_URL.getMsg(origUrl));
 122  0
                 setUrlLink("");
 123  
             }
 124  
             //it was ok, just note the modification made
 125  
             else
 126  
             {
 127  2
                 logger.warning(ErrorMessage.MP3_URL_SAVED_ENCODED.getMsg(origUrl, getUrlLink()));
 128  
             }
 129  
         }
 130  43
         super.write(tagBuffer);
 131  43
     }
 132  
 
 133  
     /**
 134  
      *
 135  
      */
 136  
     protected void setupObjectList()
 137  
     {
 138  29
         objectList.add(new StringSizeTerminated(DataTypes.OBJ_URLLINK, this));
 139  29
     }
 140  
 
 141  
     /**
 142  
      * Encode url because may receive url already encoded or not, but we can only store as ISO8859-1
 143  
      *
 144  
      * @param url
 145  
      * @return
 146  
      */
 147  
     private String encodeURL(String url)
 148  
     {
 149  
         try
 150  
         {
 151  2
             final String[] splitURL = url.split("(?<!/)/(?!/)", -1);
 152  2
             final StringBuffer sb = new StringBuffer(splitURL[0]);
 153  6
             for (int i = 1; i < splitURL.length; i++)
 154  
             {
 155  4
                 sb.append("/").append(URLEncoder.encode(splitURL[i], "utf-8"));
 156  
             }
 157  2
             return sb.toString();
 158  
         }
 159  0
         catch (UnsupportedEncodingException uee)
 160  
         {
 161  
             //Should never happen as utf-8 is always availablebut in case it does we just return the utl
 162  
             //unmodified
 163  0
             logger.warning("Uable to url encode because utf-8 charset not available:" + uee.getMessage());
 164  0
             return url;
 165  
         }
 166  
     }
 167  
 }