The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | * contributor license agreements. See the NOTICE file distributed with |
| 4 | * this work for additional information regarding copyright ownership. |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | * (the "License"); you may not use this file except in compliance with |
| 7 | * the License. You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | package javax.imageio.metadata; |
| 18 | |
| 19 | import java.util.ArrayList; |
| 20 | |
| 21 | import org.apache.harmony.x.imageio.metadata.IIOMetadataUtils; |
| 22 | import org.w3c.dom.Node; |
| 23 | |
| 24 | /** |
| 25 | * The Class IIOMetadata represents the metadata (bundled with an image) |
| 26 | * as a Dom-type tree. |
| 27 | */ |
| 28 | public abstract class IIOMetadata { |
| 29 | |
| 30 | /** Whether the standard metadata format is supported. */ |
| 31 | protected boolean standardFormatSupported; |
| 32 | |
| 33 | /** The native metadata format name. */ |
| 34 | protected String nativeMetadataFormatName; |
| 35 | |
| 36 | /** The native metadata format class name. */ |
| 37 | protected String nativeMetadataFormatClassName; |
| 38 | |
| 39 | /** The extra metadata format names. */ |
| 40 | protected String[] extraMetadataFormatNames; |
| 41 | |
| 42 | /** The extra metadata format class names. */ |
| 43 | protected String[] extraMetadataFormatClassNames; |
| 44 | |
| 45 | /** The default controller. */ |
| 46 | protected IIOMetadataController defaultController; |
| 47 | |
| 48 | /** The controller. */ |
| 49 | protected IIOMetadataController controller; |
| 50 | |
| 51 | /** |
| 52 | * Instantiates a new IIOMetadata with no data set. |
| 53 | */ |
| 54 | protected IIOMetadata() {} |
| 55 | |
| 56 | /** |
| 57 | * Instantiates a new IIOMetadata with the specified data parameters. |
| 58 | * |
| 59 | * @param standardMetadataFormatSupported whether the standard metadata format is supported |
| 60 | * @param nativeMetadataFormatName the native metadata format name |
| 61 | * @param nativeMetadataFormatClassName the native metadata format class name |
| 62 | * @param extraMetadataFormatNames the extra metadata format names |
| 63 | * @param extraMetadataFormatClassNames the extra metadata format class names |
| 64 | */ |
| 65 | protected IIOMetadata(boolean standardMetadataFormatSupported, |
| 66 | String nativeMetadataFormatName, |
| 67 | String nativeMetadataFormatClassName, |
| 68 | String[] extraMetadataFormatNames, |
| 69 | String[] extraMetadataFormatClassNames) { |
| 70 | standardFormatSupported = standardMetadataFormatSupported; |
| 71 | this.nativeMetadataFormatName = nativeMetadataFormatName; |
| 72 | this.nativeMetadataFormatClassName = nativeMetadataFormatClassName; |
| 73 | if (extraMetadataFormatNames == null) { |
| 74 | if (extraMetadataFormatClassNames != null) { |
| 75 | throw new IllegalArgumentException( |
| 76 | "extraMetadataFormatNames == null && extraMetadataFormatClassNames != null!" |
| 77 | ); |
| 78 | } |
| 79 | } else { |
| 80 | if (extraMetadataFormatClassNames == null) { |
| 81 | throw new IllegalArgumentException( |
| 82 | "extraMetadataFormatNames != null && extraMetadataFormatClassNames == null!" |
| 83 | ); |
| 84 | } |
| 85 | if (extraMetadataFormatNames.length == 0) { |
| 86 | throw new IllegalArgumentException("extraMetadataFormatNames.length == 0!"); |
| 87 | } |
| 88 | if (extraMetadataFormatClassNames.length != extraMetadataFormatNames.length) { |
| 89 | throw new IllegalArgumentException( |
| 90 | "extraMetadataFormatClassNames.length != extraMetadataFormatNames.length!" |
| 91 | ); |
| 92 | } |
| 93 | this.extraMetadataFormatNames = extraMetadataFormatNames.clone(); |
| 94 | this.extraMetadataFormatClassNames = extraMetadataFormatClassNames.clone(); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Gets the metadata as tree-type document. |
| 100 | * |
| 101 | * @param formatName the format name |
| 102 | * |
| 103 | * @return the node in tree format |
| 104 | */ |
| 105 | public abstract Node getAsTree(String formatName); |
| 106 | |
| 107 | /** |
| 108 | * Checks if the metadata is read only. |
| 109 | * |
| 110 | * @return true, if the metadata is read only. |
| 111 | */ |
| 112 | public abstract boolean isReadOnly(); |
| 113 | |
| 114 | /** |
| 115 | * Merges the specified tree with this metadata tree. |
| 116 | * |
| 117 | * @param formatName the format of the specified tree |
| 118 | * @param root the root node of the metadata tree |
| 119 | * |
| 120 | * @throws IIOInvalidTreeException if the specified tree |
| 121 | * is incompatible with the this metadata tree. |
| 122 | */ |
| 123 | public abstract void mergeTree(String formatName, Node root) throws IIOInvalidTreeException; |
| 124 | |
| 125 | /** |
| 126 | * Resets the controller. |
| 127 | */ |
| 128 | public abstract void reset(); |
| 129 | |
| 130 | /** |
| 131 | * Gets the controller associated with this metadata document. |
| 132 | * |
| 133 | * @return the controller |
| 134 | */ |
| 135 | public IIOMetadataController getController() { |
| 136 | return controller; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Checks whether this metadata has a controller. |
| 141 | * |
| 142 | * @return true, if this metadata has a controller |
| 143 | */ |
| 144 | public boolean hasController() { |
| 145 | return getController() != null; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Activate the controller. |
| 150 | * |
| 151 | * @return true, if successful |
| 152 | */ |
| 153 | public boolean activateController() { |
| 154 | if (!hasController()) { |
| 155 | throw new IllegalStateException("hasController() == false!"); |
| 156 | } |
| 157 | return getController().activate(this); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Gets the default controller. |
| 162 | * |
| 163 | * @return the default controller |
| 164 | */ |
| 165 | public IIOMetadataController getDefaultController() { |
| 166 | return defaultController; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Gets the extra metadata format names. |
| 171 | * |
| 172 | * @return the extra metadata format names |
| 173 | */ |
| 174 | public String[] getExtraMetadataFormatNames() { |
| 175 | return extraMetadataFormatNames == null ? null : extraMetadataFormatNames.clone(); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Gets the metadata format. |
| 180 | * |
| 181 | * @param formatName the format name |
| 182 | * |
| 183 | * @return the metadata format |
| 184 | */ |
| 185 | public IIOMetadataFormat getMetadataFormat(String formatName) { |
| 186 | return IIOMetadataUtils.instantiateMetadataFormat( |
| 187 | formatName, |
| 188 | standardFormatSupported, |
| 189 | nativeMetadataFormatName, nativeMetadataFormatClassName, |
| 190 | extraMetadataFormatNames, extraMetadataFormatClassNames |
| 191 | ); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Gets the native metadata format name. |
| 196 | * |
| 197 | * @return the native metadata format name |
| 198 | */ |
| 199 | public String getNativeMetadataFormatName() { |
| 200 | return nativeMetadataFormatName; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Checks if the standard metadata format is supported. |
| 205 | * |
| 206 | * @return true, if the standard metadata format is supported |
| 207 | */ |
| 208 | public boolean isStandardMetadataFormatSupported() { |
| 209 | return standardFormatSupported; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Gets the metadata format names. |
| 214 | * |
| 215 | * @return the metadata format names |
| 216 | */ |
| 217 | public String[] getMetadataFormatNames() { |
| 218 | ArrayList<String> res = new ArrayList<String>(); |
| 219 | |
| 220 | String nativeMetadataFormatName = getNativeMetadataFormatName(); |
| 221 | boolean standardFormatSupported = isStandardMetadataFormatSupported(); |
| 222 | String extraMetadataFormatNames[] = getExtraMetadataFormatNames(); |
| 223 | |
| 224 | if (standardFormatSupported) { |
| 225 | res.add(IIOMetadataFormatImpl.standardMetadataFormatName); |
| 226 | } |
| 227 | if (nativeMetadataFormatName != null) { |
| 228 | res.add(nativeMetadataFormatName); |
| 229 | } |
| 230 | if (extraMetadataFormatNames != null) { |
| 231 | for (String extraMetadataFormatName : extraMetadataFormatNames) { |
| 232 | res.add(extraMetadataFormatName); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | return res.size() > 0 ? res.toArray(new String[0]) : null; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Gets the standard chroma node. |
| 241 | * |
| 242 | * @return the standard chroma node |
| 243 | */ |
| 244 | protected IIOMetadataNode getStandardChromaNode() { |
| 245 | return null; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Gets the standard compression node. |
| 250 | * |
| 251 | * @return the standard compression node |
| 252 | */ |
| 253 | protected IIOMetadataNode getStandardCompressionNode() { |
| 254 | return null; |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Gets the standard data node. |
| 259 | * |
| 260 | * @return the standard data node |
| 261 | */ |
| 262 | protected IIOMetadataNode getStandardDataNode() { |
| 263 | return null; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Gets the standard dimension node. |
| 268 | * |
| 269 | * @return the standard dimension node |
| 270 | */ |
| 271 | protected IIOMetadataNode getStandardDimensionNode() { |
| 272 | return null; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Gets the standard document node. |
| 277 | * |
| 278 | * @return the standard document node |
| 279 | */ |
| 280 | protected IIOMetadataNode getStandardDocumentNode() { |
| 281 | return null; |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Gets the standard text node. |
| 286 | * |
| 287 | * @return the standard text node |
| 288 | */ |
| 289 | protected IIOMetadataNode getStandardTextNode() { |
| 290 | return null; |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Gets the standard tile node. |
| 295 | * |
| 296 | * @return the standard tile node |
| 297 | */ |
| 298 | protected IIOMetadataNode getStandardTileNode() { |
| 299 | return null; |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Gets the standard transparency node. |
| 304 | * |
| 305 | * @return the standard transparency node |
| 306 | */ |
| 307 | protected IIOMetadataNode getStandardTransparencyNode() { |
| 308 | return null; |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Gets the metadata as a tree in standard format. |
| 313 | * |
| 314 | * @return the metadata as a tree in standard format |
| 315 | */ |
| 316 | protected final IIOMetadataNode getStandardTree() { |
| 317 | // Create root node |
| 318 | IIOMetadataNode root = new IIOMetadataNode(IIOMetadataFormatImpl.standardMetadataFormatName); |
| 319 | |
| 320 | Node node; |
| 321 | if ((node = getStandardChromaNode()) != null) { |
| 322 | root.appendChild(node); |
| 323 | } |
| 324 | if ((node = getStandardCompressionNode()) != null) { |
| 325 | root.appendChild(node); |
| 326 | } |
| 327 | if ((node = getStandardDataNode()) != null) { |
| 328 | root.appendChild(node); |
| 329 | } |
| 330 | if ((node = getStandardDimensionNode()) != null) { |
| 331 | root.appendChild(node); |
| 332 | } |
| 333 | if ((node = getStandardDocumentNode()) != null) { |
| 334 | root.appendChild(node); |
| 335 | } |
| 336 | if ((node = getStandardTextNode()) != null) { |
| 337 | root.appendChild(node); |
| 338 | } |
| 339 | if ((node = getStandardTileNode()) != null) { |
| 340 | root.appendChild(node); |
| 341 | } |
| 342 | if ((node = getStandardTransparencyNode()) != null) { |
| 343 | root.appendChild(node); |
| 344 | } |
| 345 | |
| 346 | return root; |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Sets the controller. |
| 351 | * |
| 352 | * @param controller the new controller |
| 353 | */ |
| 354 | public void setController(IIOMetadataController controller) { |
| 355 | this.controller = controller; |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * Sets the from tree. |
| 360 | * |
| 361 | * @param formatName the name of the metatdata format of the from tree |
| 362 | * @param root the root node of the from tree |
| 363 | * |
| 364 | * @throws IIOInvalidTreeException if the tree or its format is not compatible with |
| 365 | * this metadata. |
| 366 | */ |
| 367 | public void setFromTree(String formatName, Node root) throws IIOInvalidTreeException { |
| 368 | reset(); |
| 369 | mergeTree(formatName, root); |
| 370 | } |
| 371 | } |