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