| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 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 | |
|
| 41 | |
|
| 42 | |
public abstract class AbstractFrameBodyUrlLink extends AbstractID3v2FrameBody |
| 43 | |
{ |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
protected AbstractFrameBodyUrlLink() |
| 49 | |
{ |
| 50 | 65 | super(); |
| 51 | 65 | } |
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
protected AbstractFrameBodyUrlLink(AbstractFrameBodyUrlLink body) |
| 57 | |
{ |
| 58 | 42 | super(body); |
| 59 | 42 | } |
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
public AbstractFrameBodyUrlLink(String urlLink) |
| 67 | 0 | { |
| 68 | 0 | setObjectValue(DataTypes.OBJ_URLLINK, urlLink); |
| 69 | 0 | } |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
protected AbstractFrameBodyUrlLink(ByteBuffer byteBuffer, int frameSize) throws InvalidTagException |
| 77 | |
{ |
| 78 | 48 | super(byteBuffer, frameSize); |
| 79 | 48 | } |
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 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 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
public String getUrlLink() |
| 101 | |
{ |
| 102 | 76 | return (String) getObjectValue(DataTypes.OBJ_URLLINK); |
| 103 | |
} |
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 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 | |
|
| 115 | |
|
| 116 | 2 | setUrlLink(encodeURL(origUrl)); |
| 117 | |
|
| 118 | |
|
| 119 | 2 | if (!encoder.canEncode(getUrlLink())) |
| 120 | |
{ |
| 121 | 0 | logger.warning(ErrorMessage.MP3_UNABLE_TO_ENCODE_URL.getMsg(origUrl)); |
| 122 | 0 | setUrlLink(""); |
| 123 | |
} |
| 124 | |
|
| 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 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 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 | |
|
| 162 | |
|
| 163 | 0 | logger.warning("Uable to url encode because utf-8 charset not available:" + uee.getMessage()); |
| 164 | 0 | return url; |
| 165 | |
} |
| 166 | |
} |
| 167 | |
} |