Source for javax.imageio.metadata.IIOMetadata

   1: /* IIOMetadata.java --
   2:    Copyright (C) 2004  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package javax.imageio.metadata;
  40: 
  41: /**
  42:  * @author Michael Koch (konqueror@gmx.de)
  43:  */
  44: public abstract class IIOMetadata
  45: {
  46:   protected IIOMetadataController controller;
  47:   protected IIOMetadataController defaultController;
  48:   protected String[] extraMetadataFormatClassNames;
  49:   protected String[] extraMetadataFormatNames;
  50:   protected String nativeMetadataFormatClassName;
  51:   protected String nativeMetadataFormatName;
  52:   protected boolean standardFormatSupported;
  53: 
  54:   /**
  55:    * Creates a <code>IIOMetaData</code> object.
  56:    */
  57:   protected IIOMetadata()
  58:   {
  59:     // Do nothing here.
  60:   }
  61: 
  62:   /**
  63:    * Creates a <code>IIOMetaData</code> object with the given arguments.
  64:    *
  65:    * @param standardMetadataFormatSupported
  66:    * @param nativeMetadataFormatName
  67:    * @param nativeMetadataFormatClassName
  68:    * @param extraMetadataFormatNames
  69:    * @param extraMetadataFormatClassNames
  70:    *
  71:    * @throws IllegalArgumentException if extraMetadataFormatNames has length of
  72:    * zero or extraMetadataFormatNames and extraMetadataFormatClassNames are
  73:    * neither both null, not have the same length
  74:    */
  75:   protected IIOMetadata(boolean standardMetadataFormatSupported,
  76:                         String nativeMetadataFormatName,
  77:                         String nativeMetadataFormatClassName,
  78:                         String[] extraMetadataFormatNames,
  79:                         String[] extraMetadataFormatClassNames)
  80:   {
  81:     if (extraMetadataFormatNames != null
  82:         && extraMetadataFormatNames.length == 0)
  83:       throw new IllegalArgumentException
  84:         ("extraMetadataFormatNames may not be empty");
  85: 
  86:     if (((extraMetadataFormatNames == null)
  87:          && (extraMetadataFormatClassNames != null))
  88:         || ((extraMetadataFormatNames != null)
  89:             && (extraMetadataFormatClassNames == null))
  90:         || ((extraMetadataFormatNames != null)
  91:             && (extraMetadataFormatClassNames != null)
  92:             && (extraMetadataFormatNames.length !=
  93:                 extraMetadataFormatClassNames.length)))
  94:       throw new IllegalArgumentException
  95:         ("extraMetadataFormatNames and extraMetadataFormatClassNames " +
  96:          "have different lengths");
  97: 
  98:     this.standardFormatSupported = standardMetadataFormatSupported;
  99:     this.nativeMetadataFormatName = nativeMetadataFormatName;
 100:     this.nativeMetadataFormatClassName = nativeMetadataFormatClassName;
 101:     this.extraMetadataFormatNames = extraMetadataFormatNames;
 102:     this.extraMetadataFormatClassNames = extraMetadataFormatClassNames;
 103:   }
 104: 
 105:   public boolean activateController()
 106:   {
 107:     if (! hasController())
 108:       return false;
 109: 
 110:     return getDefaultController().activate(this);
 111:   }
 112: 
 113:   public IIOMetadataController getController()
 114:   {
 115:     return controller;
 116:   }
 117: 
 118:   public IIOMetadataController getDefaultController()
 119:   {
 120:     return defaultController;
 121:   }
 122: 
 123:   public String[] getExtraMetadataFormatNames()
 124:   {
 125:     return (String[]) extraMetadataFormatNames.clone();
 126:   }
 127: 
 128:   public IIOMetadataFormat getMetadataFormat(String formatName)
 129:   {
 130:     if (formatName == null)
 131:       throw new IllegalArgumentException("formatName may not be null");
 132:     
 133:     String formatClassName = null;
 134: 
 135:     if (isStandardMetadataFormatSupported()
 136:     && formatName.equals(nativeMetadataFormatName))
 137:       formatClassName = nativeMetadataFormatClassName;
 138:     else
 139:       {
 140:     String[] extraFormatNames = getExtraMetadataFormatNames();
 141:     
 142:     for (int i = extraFormatNames.length - 1; i >= 0; --i)
 143:       if (extraFormatNames[i].equals(formatName))
 144:         {
 145:           formatClassName = extraFormatNames[i];
 146:           break;
 147:         }
 148:       }
 149: 
 150:     if (formatClassName == null)
 151:       throw new IllegalArgumentException("unknown format");
 152: 
 153:     IIOMetadataFormat format;
 154:     
 155:     try
 156:       {
 157:     format = (IIOMetadataFormat) Class.forName(formatClassName)
 158:                       .newInstance();
 159:       }
 160:     catch (Exception e)
 161:       {
 162:     IllegalStateException ise = new IllegalStateException();
 163:     ise.initCause(e);
 164:     throw ise;
 165:       }
 166: 
 167:     return format;
 168:   }
 169: 
 170:   public String[] getMetadataFormatNames()
 171:   {
 172:     String[] formatNames = getExtraMetadataFormatNames();
 173:     
 174:     if (isStandardMetadataFormatSupported())
 175:       {
 176:         // Combine native metadata format name and extra metadata format names
 177:     // into one String array.
 178:     String[] tmp = new String[formatNames.length + 1];
 179:     tmp[0] = getNativeMetadataFormatName();
 180: 
 181:     for (int i = 1; i < tmp.length; ++i)
 182:       tmp[i] = formatNames[i - 1];
 183: 
 184:     formatNames = tmp;
 185:       }
 186: 
 187:     return formatNames;
 188:   }
 189: 
 190:   public String getNativeMetadataFormatName()
 191:   {
 192:     return nativeMetadataFormatName;
 193:   }
 194: 
 195:   public boolean hasController()
 196:   {
 197:     return getController() != null;
 198:   }
 199: 
 200:   public abstract boolean isReadOnly();
 201: 
 202:   public boolean isStandardMetadataFormatSupported()
 203:   {
 204:     return standardFormatSupported;
 205:   }
 206: 
 207:   public abstract void reset();
 208: 
 209:   public void setController(IIOMetadataController controller)
 210:   {
 211:     this.controller = controller;
 212:   }
 213: }