1 7 8 package javax.imageio.metadata; 9 10 import org.w3c.dom.Node ; 11 import java.lang.reflect.Method ; 12 13 49 public abstract class IIOMetadata { 50 51 55 protected boolean standardFormatSupported; 56 57 61 protected String nativeMetadataFormatName = null; 62 63 68 protected String nativeMetadataFormatClassName = null; 69 70 75 protected String [] extraMetadataFormatNames = null; 76 77 83 protected String [] extraMetadataFormatClassNames = null; 84 85 98 protected IIOMetadataController defaultController = null; 99 100 112 protected IIOMetadataController controller = null; 113 114 121 protected IIOMetadata() {} 122 123 155 protected IIOMetadata(boolean standardMetadataFormatSupported, 156 String nativeMetadataFormatName, 157 String nativeMetadataFormatClassName, 158 String [] extraMetadataFormatNames, 159 String [] extraMetadataFormatClassNames) { 160 this.standardFormatSupported = standardMetadataFormatSupported; 161 this.nativeMetadataFormatName = nativeMetadataFormatName; 162 this.nativeMetadataFormatClassName = nativeMetadataFormatClassName; 163 if (extraMetadataFormatNames != null) { 164 if (extraMetadataFormatNames.length == 0) { 165 throw new IllegalArgumentException 166 ("extraMetadataFormatNames.length == 0!"); 167 } 168 if (extraMetadataFormatClassNames == null) { 169 throw new IllegalArgumentException 170 ("extraMetadataFormatNames != null && extraMetadataFormatClassNames == null!"); 171 } 172 if (extraMetadataFormatClassNames.length != 173 extraMetadataFormatNames.length) { 174 throw new IllegalArgumentException 175 ("extraMetadataFormatClassNames.length != extraMetadataFormatNames.length!"); 176 } 177 this.extraMetadataFormatNames = 178 (String []) extraMetadataFormatNames.clone(); 179 this.extraMetadataFormatClassNames = 180 (String []) extraMetadataFormatClassNames.clone(); 181 } else { 182 if (extraMetadataFormatClassNames != null) { 183 throw new IllegalArgumentException 184 ("extraMetadataFormatNames == null && extraMetadataFormatClassNames != null!"); 185 } 186 } 187 } 188 189 206 public boolean isStandardMetadataFormatSupported() { 207 return standardFormatSupported; 208 } 209 210 218 public abstract boolean isReadOnly(); 219 220 243 public String getNativeMetadataFormatName() { 244 return nativeMetadataFormatName; 245 } 246 247 267 public String [] getExtraMetadataFormatNames() { 268 if (extraMetadataFormatNames == null) { 269 return null; 270 } 271 return (String [])extraMetadataFormatNames.clone(); 272 } 273 274 293 public String [] getMetadataFormatNames() { 294 String nativeName = getNativeMetadataFormatName(); 295 String standardName = isStandardMetadataFormatSupported() ? 296 IIOMetadataFormatImpl.standardMetadataFormatName : null; 297 String [] extraNames = getExtraMetadataFormatNames(); 298 299 int numFormats = 0; 300 if (nativeName != null) { 301 ++numFormats; 302 } 303 if (standardName != null) { 304 ++numFormats; 305 } 306 if (extraNames != null) { 307 numFormats += extraNames.length; 308 } 309 if (numFormats == 0) { 310 return null; 311 } 312 313 String [] formats = new String [numFormats]; 314 int index = 0; 315 if (nativeName != null) { 316 formats[index++] = nativeName; 317 } 318 if (standardName != null) { 319 formats[index++] = standardName; 320 } 321 if (extraNames != null) { 322 for (int i = 0; i < extraNames.length; i++) { 323 formats[index++] = extraNames[i]; 324 } 325 } 326 327 return formats; 328 } 329 330 359 public IIOMetadataFormat getMetadataFormat(String formatName) { 360 if (formatName == null) { 361 throw new IllegalArgumentException ("formatName == null!"); 362 } 363 if (standardFormatSupported 364 && formatName.equals 365 (IIOMetadataFormatImpl.standardMetadataFormatName)) { 366 return IIOMetadataFormatImpl.getStandardFormatInstance(); 367 } 368 String formatClassName = null; 369 if (formatName.equals(nativeMetadataFormatName)) { 370 formatClassName = nativeMetadataFormatClassName; 371 } else if (extraMetadataFormatNames != null) { 372 for (int i = 0; i < extraMetadataFormatNames.length; i++) { 373 if (formatName.equals(extraMetadataFormatNames[i])) { 374 formatClassName = extraMetadataFormatClassNames[i]; 375 break; } 377 } 378 } 379 if (formatClassName == null) { 380 throw new IllegalArgumentException ("Unsupported format name"); 381 } 382 try { 383 Class cls = null; 384 final Object o = this; 385 386 ClassLoader loader = (ClassLoader ) 389 java.security.AccessController.doPrivileged( 390 new java.security.PrivilegedAction () { 391 public Object run() { 392 return o.getClass().getClassLoader(); 393 } 394 }); 395 396 try { 397 cls = Class.forName(formatClassName, true, 398 loader); 399 } catch (ClassNotFoundException e) { 400 loader = (ClassLoader ) 404 java.security.AccessController.doPrivileged( 405 new java.security.PrivilegedAction () { 406 public Object run() { 407 return Thread.currentThread().getContextClassLoader(); 408 } 409 }); 410 try { 411 cls = Class.forName(formatClassName, true, 412 loader); 413 } catch (ClassNotFoundException e1) { 414 cls = Class.forName(formatClassName, true, 418 ClassLoader.getSystemClassLoader()); 419 } 420 } 421 422 Method meth = cls.getMethod("getInstance", null); 423 return (IIOMetadataFormat ) meth.invoke(null, null); 424 } catch (Exception e) { 425 RuntimeException ex = 426 new IllegalStateException ("Can't obtain format"); 427 ex.initCause(e); 428 throw ex; 429 } 430 431 } 432 433 455 public abstract Node getAsTree(String formatName); 456 457 489 public abstract void mergeTree(String formatName, Node root) 490 throws IIOInvalidTreeException ; 491 492 509 protected IIOMetadataNode getStandardChromaNode() { 510 return null; 511 } 512 513 531 protected IIOMetadataNode getStandardCompressionNode() { 532 return null; 533 } 534 535 553 protected IIOMetadataNode getStandardDataNode() { 554 return null; 555 } 556 557 575 protected IIOMetadataNode getStandardDimensionNode() { 576 return null; 577 } 578 579 596 protected IIOMetadataNode getStandardDocumentNode() { 597 return null; 598 } 599 600 617 protected IIOMetadataNode getStandardTextNode() { 618 return null; 619 } 620 621 638 protected IIOMetadataNode getStandardTileNode() { 639 return null; 640 } 641 642 658 protected IIOMetadataNode getStandardTransparencyNode() { 659 return null; 660 } 661 662 666 private void append(IIOMetadataNode root, IIOMetadataNode node) { 667 if (node != null) { 668 root.appendChild(node); 669 } 670 } 671 672 698 protected final IIOMetadataNode getStandardTree() { 699 IIOMetadataNode root = new IIOMetadataNode 700 (IIOMetadataFormatImpl.standardMetadataFormatName); 701 append(root, getStandardChromaNode()); 702 append(root, getStandardCompressionNode()); 703 append(root, getStandardDataNode()); 704 append(root, getStandardDimensionNode()); 705 append(root, getStandardDocumentNode()); 706 append(root, getStandardTextNode()); 707 append(root, getStandardTileNode()); 708 append(root, getStandardTransparencyNode()); 709 return root; 710 } 711 712 739 public void setFromTree(String formatName, Node root) 740 throws IIOInvalidTreeException { 741 reset(); 742 mergeTree(formatName, root); 743 } 744 745 759 public abstract void reset(); 760 761 782 public void setController(IIOMetadataController controller) { 783 this.controller = controller; 784 } 785 786 804 public IIOMetadataController getController() { 805 return controller; 806 } 807 808 825 public IIOMetadataController getDefaultController() { 826 return defaultController; 827 } 828 829 845 public boolean hasController() { 846 return (getController() != null); 847 } 848 849 877 public boolean activateController() { 878 if (!hasController()) { 879 throw new IllegalStateException ("hasController() == false!"); 880 } 881 return getController().activate(this); 882 } 883 } 884 | Popular Tags |