View Javadoc

1   /*
2    * Entagged Audio Tag library
3    * Copyright (c) 2003-2005 Raphaël Slinckx <raphael@slinckx.net>
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 entagged.audioformats.mp3.util.id3frames;
20  
21  import java.io.UnsupportedEncodingException;
22  
23  import entagged.audioformats.generic.TagField;
24  import entagged.audioformats.generic.TagTextField;
25  import entagged.audioformats.mp3.Id3v2Tag;
26  
27  public class TextId3Frame extends Id3Frame implements TagTextField {
28  
29  	protected String content;
30  
31  	protected byte encoding;
32  
33  	protected String id;
34  
35  	protected boolean common;
36  
37  	/*
38  	 * 0,1| frame flags 2| encoding 3,..,(0x00(0x00))| text content
39  	 */
40  
41  	public TextId3Frame(String id, String content) {
42  		this.id = id;
43  		checkCommon();
44  		this.content = content;
45  		setEncoding(Id3v2Tag.DEFAULT_ENCODING);
46  	}
47  
48  	public TextId3Frame(String id, byte[] rawContent, byte version)
49  			throws UnsupportedEncodingException {
50  		super(rawContent, version);
51  		this.id = id;
52  		checkCommon();
53  	}
54  
55  	private void checkCommon() {
56  		// TODO on renaming time field, change this too
57  		this.common = id.equals("TIT2") || id.equals("TALB")
58  				|| id.equals("TPE1") || id.equals("TCON") || id.equals("TRCK")
59  				|| id.equals("TDRC") || id.equals("COMM");
60  	}
61  
62  	public String getEncoding() {
63  		if (encoding == 0)
64  			return "ISO-8859-1";
65  		else if (encoding == 1)
66  			return "UTF-16";
67  		else if (encoding == 2)
68  			return "UTF-16BE";
69  		else if (encoding == 3) {
70  			return "UTF-8";
71  		}
72  		return "ISO-8859-1";
73  	}
74  
75  	public void setEncoding(String enc) {
76  		if ("ISO-8859-1".equals(enc))
77  			encoding = 0;
78  		else if ("UTF-16".equals(enc))
79  			encoding = 1;
80  		else if ("UTF-16BE".equals(enc))
81  			encoding = 2;
82  		else if ("UTF-8".equals(enc))
83  			encoding = 3;
84  		else
85  			encoding = 1;
86  	}
87  
88  	public String getContent() {
89  		return content;
90  	}
91  
92  	public boolean isBinary() {
93  		return false;
94  	}
95  
96  	public String getId() {
97  		return this.id;
98  	}
99  
100 	public boolean isCommon() {
101 		return this.common;
102 	}
103 
104 	public void setContent(String s) {
105 		this.content = s;
106 	}
107 
108 	public boolean isEmpty() {
109 		return this.content.equals("");
110 	}
111 
112 	public void copyContent(TagField field) {
113 		if (field instanceof TextId3Frame) {
114 			this.content = ((TextId3Frame) field).getContent();
115 			setEncoding(((TextId3Frame) field).getEncoding());
116 		}
117 	}
118 
119 	protected void populate(byte[] raw) throws UnsupportedEncodingException {
120 		this.encoding = raw[flags.length];
121 		if (this.encoding != 0 && this.encoding != 1 && this.encoding != 2
122 				&& this.encoding != 3)
123 			this.encoding = 0;
124 
125 		this.content = getString(raw, flags.length + 1, raw.length
126 				- flags.length - 1, getEncoding());
127 
128 		int i = this.content.indexOf("\u0000");
129 
130 		if (i != -1)
131 			this.content = this.content.substring(0, i);
132 	}
133 
134 	protected byte[] build() throws UnsupportedEncodingException {
135 		byte[] data = getBytes(this.content, getEncoding());
136 		// the return byte[]
137 		byte[] b = new byte[4 + 4 + flags.length + 1 + data.length];
138 
139 		int offset = 0;
140 		copy(getIdBytes(), b, offset);
141 		offset += 4;
142 		copy(getSize(b.length - 10), b, offset);
143 		offset += 4;
144 		copy(flags, b, offset);
145 		offset += flags.length;
146 
147 		b[offset] = this.encoding;
148 		offset += 1;
149 
150 		copy(data, b, offset);
151 
152 		return b;
153 	}
154 
155 	public String toString() {
156 		return getContent();
157 	}
158 
159 	/*
160 	 * public static void main(String[] args) throws Exception { byte[] bytes =
161 	 * "TeT\u0000TeT".getBytes("ISO-8859-1"); for(int i = 0; i<bytes.length;
162 	 * i++) System.out.print(Integer.toHexString(bytes[i]&0xFF)+"|");
163 	 * System.out.println(); System.out.println("TeT\u0000TeT".length());
164 	 * System.out.println(new String(bytes,"ISO-8859-1").length());
165 	 * 
166 	 * System.out.println("".equals(null));
167 	 * 
168 	 * byte[][] content = { {0x00, 0x00, 0x00, 'T','e','T'}, {0x00, 0x00, 0x00,
169 	 * 'T','e','T',0x00}, {0x00, 0x00, 0x00, 'T','e','T',0x00,'T','e'}, {0x00,
170 	 * 0x00, 0x01, (byte)0xFE, (byte)0xFF,0x00,'T',0x00,'e',0x00,'T'}, {0x00,
171 	 * 0x00, 0x01, (byte)0xFE, (byte)0xFF,0x00,'T',0x00,'e',0x00,'T',0x00,0x00},
172 	 * {0x00, 0x00, 0x01, (byte)0xFE,
173 	 * (byte)0xFF,0x00,'T',0x00,'e',0x00,'T',0x00,0x00,0x00,'t'}, {0x00, 0x00,
174 	 * 0x01, (byte)0xFF, (byte)0xFE,'T',0x00,'e',0x00,'T',0x00}, {0x00, 0x00,
175 	 * 0x01, (byte)0xFF, (byte)0xFE,'T',0x00,'e',0x00,'T',0x00,0x00,0x00},
176 	 * {0x00, 0x00, 0x01, (byte)0xFF,
177 	 * (byte)0xFE,'T',0x00,'e',0x00,'T',0x00,0x00,0x00,'T',0x00} };
178 	 * 
179 	 * for(int i = 0; i<content.length; i++) { TextId3Frame t = new
180 	 * TextId3Frame("TALB", content[i], Id3v2Tag.ID3V23);
181 	 * System.out.println("-------"); System.out.println(t.isBinary());
182 	 * System.out.println(t.getContent()); System.out.println(t.getEncoding());
183 	 * System.out.println(t.getId());
184 	 * 
185 	 * 
186 	 * bytes = t.build(); for(int j = 0; j<bytes.length; j++)
187 	 * System.out.print(Integer.toHexString(bytes[j]&0xFF)+"|"); } }
188 	 */
189 }