| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.jaudiotagger.audio.generic; |
| 19 | |
|
| 20 | |
import org.jaudiotagger.tag.*; |
| 21 | |
import org.jaudiotagger.tag.datatype.Artwork; |
| 22 | |
|
| 23 | |
import java.util.*; |
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | 1218 | public abstract class AbstractTag implements Tag |
| 33 | |
{ |
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | 1218 | protected int commonNumber = 0; |
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | 1218 | protected Map<String, List<TagField>> fields = new LinkedHashMap<String, List<TagField>>(); |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
public void addField(TagField field) |
| 56 | |
{ |
| 57 | 14768 | if (field == null) |
| 58 | |
{ |
| 59 | 0 | return; |
| 60 | |
} |
| 61 | |
|
| 62 | 14768 | List<TagField> list = fields.get(field.getId()); |
| 63 | |
|
| 64 | |
|
| 65 | 14768 | if (list == null) |
| 66 | |
{ |
| 67 | 14488 | list = new ArrayList<TagField>(); |
| 68 | 14488 | list.add(field); |
| 69 | 14488 | fields.put(field.getId(), list); |
| 70 | 14488 | if (field.isCommon()) |
| 71 | |
{ |
| 72 | 3278 | commonNumber++; |
| 73 | |
} |
| 74 | |
} |
| 75 | |
else |
| 76 | |
{ |
| 77 | |
|
| 78 | 280 | list.add(field); |
| 79 | |
} |
| 80 | 14768 | } |
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
public List<TagField> get(String id) |
| 89 | |
{ |
| 90 | 9003 | List<TagField> list = fields.get(id); |
| 91 | |
|
| 92 | 9003 | if (list == null) |
| 93 | |
{ |
| 94 | 367 | return new ArrayList<TagField>(); |
| 95 | |
} |
| 96 | |
|
| 97 | 8636 | return list; |
| 98 | |
} |
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
public List<TagField> getFields(FieldKey id) throws KeyNotFoundException |
| 108 | |
{ |
| 109 | 0 | List<TagField> list = fields.get(id.name()); |
| 110 | 0 | if (list == null) |
| 111 | |
{ |
| 112 | 0 | return new ArrayList<TagField>(); |
| 113 | |
} |
| 114 | 0 | return list; |
| 115 | |
} |
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
public String getFirst(String id) |
| 123 | |
{ |
| 124 | 6435 | List<TagField> l = get(id); |
| 125 | 6435 | return (l.size() != 0) ? l.get(0).toString() : ""; |
| 126 | |
} |
| 127 | |
|
| 128 | |
public TagField getFirstField(String id) |
| 129 | |
{ |
| 130 | 176 | List<TagField> l = get(id); |
| 131 | 176 | return (l.size() != 0) ? l.get(0) : null; |
| 132 | |
} |
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
public Iterator<TagField> getFields() |
| 139 | |
{ |
| 140 | 980 | final Iterator<Map.Entry<String, List<TagField>>> it = this.fields.entrySet().iterator(); |
| 141 | 980 | return new Iterator<TagField>() |
| 142 | |
{ |
| 143 | |
private Iterator<TagField> fieldsIt; |
| 144 | |
|
| 145 | |
private void changeIt() |
| 146 | |
{ |
| 147 | 14623 | if (!it.hasNext()) |
| 148 | |
{ |
| 149 | 137 | return; |
| 150 | |
} |
| 151 | |
|
| 152 | 14486 | Map.Entry<String, List<TagField>> e = it.next(); |
| 153 | 14486 | List<TagField> l = e.getValue(); |
| 154 | 14486 | fieldsIt = l.iterator(); |
| 155 | 14486 | } |
| 156 | |
|
| 157 | |
public boolean hasNext() |
| 158 | |
{ |
| 159 | 15671 | if (fieldsIt == null) |
| 160 | |
{ |
| 161 | 980 | changeIt(); |
| 162 | |
} |
| 163 | 15671 | return it.hasNext() || (fieldsIt != null && fieldsIt.hasNext()); |
| 164 | |
} |
| 165 | |
|
| 166 | |
public TagField next() |
| 167 | |
{ |
| 168 | 14691 | if (!fieldsIt.hasNext()) |
| 169 | |
{ |
| 170 | 13643 | changeIt(); |
| 171 | |
} |
| 172 | |
|
| 173 | 14691 | return fieldsIt.next(); |
| 174 | |
} |
| 175 | |
|
| 176 | 15671 | public void remove() |
| 177 | |
{ |
| 178 | 0 | fieldsIt.remove(); |
| 179 | 0 | } |
| 180 | |
}; |
| 181 | |
} |
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
public int getFieldCount() |
| 191 | |
{ |
| 192 | 209 | Iterator it = getFields(); |
| 193 | 209 | int count = 0; |
| 194 | 1786 | while (it.hasNext()) |
| 195 | |
{ |
| 196 | 1577 | count++; |
| 197 | 1577 | it.next(); |
| 198 | |
} |
| 199 | 209 | return count; |
| 200 | |
} |
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
public boolean hasCommonFields() |
| 209 | |
{ |
| 210 | 0 | return commonNumber != 0; |
| 211 | |
} |
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
public boolean hasField(String id) |
| 219 | |
{ |
| 220 | 16 | return get(id).size() != 0; |
| 221 | |
} |
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
protected abstract boolean isAllowedEncoding(String enc); |
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
public boolean isEmpty() |
| 238 | |
{ |
| 239 | 282 | return fields.size() == 0; |
| 240 | |
} |
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
public void setField(FieldKey genericKey, String value) throws KeyNotFoundException, FieldDataInvalidException |
| 251 | |
{ |
| 252 | 510 | TagField tagfield = createField(genericKey,value); |
| 253 | 494 | setField(tagfield); |
| 254 | 494 | } |
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
public void addField(FieldKey genericKey, String value) throws KeyNotFoundException, FieldDataInvalidException |
| 267 | |
{ |
| 268 | 189 | TagField tagfield = createField(genericKey,value); |
| 269 | 189 | addField(tagfield); |
| 270 | 189 | } |
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | |
public void setField(TagField field) |
| 281 | |
{ |
| 282 | 4646 | if (field == null) |
| 283 | |
{ |
| 284 | 0 | return; |
| 285 | |
} |
| 286 | |
|
| 287 | |
|
| 288 | |
|
| 289 | 4646 | List<TagField> list = fields.get(field.getId()); |
| 290 | 4646 | if (list != null) |
| 291 | |
{ |
| 292 | 600 | list.set(0, field); |
| 293 | 600 | return; |
| 294 | |
} |
| 295 | |
|
| 296 | |
|
| 297 | 4046 | list = new ArrayList<TagField>(); |
| 298 | 4046 | list.add(field); |
| 299 | 4046 | fields.put(field.getId(), list); |
| 300 | 4046 | if (field.isCommon()) |
| 301 | |
{ |
| 302 | 1041 | commonNumber++; |
| 303 | |
} |
| 304 | 4046 | } |
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
public boolean setEncoding(String enc) |
| 312 | |
{ |
| 313 | 0 | if (!isAllowedEncoding(enc)) |
| 314 | |
{ |
| 315 | 0 | return false; |
| 316 | |
} |
| 317 | |
|
| 318 | 0 | Iterator it = getFields(); |
| 319 | 0 | while (it.hasNext()) |
| 320 | |
{ |
| 321 | 0 | TagField field = (TagField) it.next(); |
| 322 | 0 | if (field instanceof TagTextField) |
| 323 | |
{ |
| 324 | 0 | ((TagTextField) field).setEncoding(enc); |
| 325 | |
} |
| 326 | 0 | } |
| 327 | |
|
| 328 | 0 | return true; |
| 329 | |
} |
| 330 | |
|
| 331 | |
|
| 332 | |
|
| 333 | |
|
| 334 | |
|
| 335 | |
|
| 336 | |
public String toString() |
| 337 | |
{ |
| 338 | 153 | StringBuffer out = new StringBuffer(); |
| 339 | 153 | out.append("Tag content:\n"); |
| 340 | 153 | Iterator it = getFields(); |
| 341 | 3212 | while (it.hasNext()) |
| 342 | |
{ |
| 343 | 3059 | TagField field = (TagField) it.next(); |
| 344 | 3059 | out.append("\t"); |
| 345 | 3059 | out.append(field.getId()); |
| 346 | 3059 | out.append(":"); |
| 347 | 3059 | out.append(field.toString()); |
| 348 | 3059 | out.append("\n"); |
| 349 | 3059 | } |
| 350 | 153 | return out.toString().substring(0, out.length() - 1); |
| 351 | |
} |
| 352 | |
|
| 353 | |
|
| 354 | |
|
| 355 | |
|
| 356 | |
|
| 357 | |
|
| 358 | |
|
| 359 | |
|
| 360 | |
|
| 361 | |
public abstract TagField createField(FieldKey genericKey, String value) throws KeyNotFoundException, FieldDataInvalidException; |
| 362 | |
|
| 363 | |
|
| 364 | |
|
| 365 | |
|
| 366 | |
|
| 367 | |
|
| 368 | |
|
| 369 | |
public abstract String getFirst(FieldKey genericKey) throws KeyNotFoundException; |
| 370 | |
|
| 371 | |
|
| 372 | |
|
| 373 | |
|
| 374 | |
|
| 375 | |
|
| 376 | |
|
| 377 | |
public abstract TagField getFirstField(FieldKey genericKey) throws KeyNotFoundException; |
| 378 | |
|
| 379 | |
|
| 380 | |
|
| 381 | |
|
| 382 | |
|
| 383 | |
|
| 384 | |
public abstract void deleteField(FieldKey fieldKey) throws KeyNotFoundException; |
| 385 | |
|
| 386 | |
|
| 387 | |
|
| 388 | |
|
| 389 | |
|
| 390 | |
|
| 391 | |
|
| 392 | |
protected void deleteField(String key) |
| 393 | |
{ |
| 394 | 71 | Object removed = fields.remove(key); |
| 395 | |
|
| 396 | |
|
| 397 | 71 | } |
| 398 | |
|
| 399 | |
public Artwork getFirstArtwork() |
| 400 | |
{ |
| 401 | 24 | List<Artwork> artwork = getArtworkList(); |
| 402 | 24 | if(artwork.size()>0) |
| 403 | |
{ |
| 404 | 24 | return artwork.get(0); |
| 405 | |
} |
| 406 | 0 | return null; |
| 407 | |
} |
| 408 | |
|
| 409 | |
|
| 410 | |
|
| 411 | |
|
| 412 | |
|
| 413 | |
|
| 414 | |
|
| 415 | |
public void setField(Artwork artwork) throws FieldDataInvalidException |
| 416 | |
{ |
| 417 | 20 | this.setField(createField(artwork)); |
| 418 | 12 | } |
| 419 | |
|
| 420 | |
|
| 421 | |
|
| 422 | |
|
| 423 | |
|
| 424 | |
|
| 425 | |
|
| 426 | |
public void addField(Artwork artwork) throws FieldDataInvalidException |
| 427 | |
{ |
| 428 | 0 | this.addField(createField(artwork)); |
| 429 | 0 | } |
| 430 | |
|
| 431 | |
|
| 432 | |
|
| 433 | |
|
| 434 | |
|
| 435 | |
|
| 436 | |
|
| 437 | |
public void deleteArtworkField() throws KeyNotFoundException |
| 438 | |
{ |
| 439 | 16 | this.deleteField(FieldKey.COVER_ART); |
| 440 | 8 | } |
| 441 | |
} |