001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package javax.imageio.metadata;
018:
019: import java.util.ArrayList;
020:
021: import org.apache.harmony.x.imageio.metadata.IIOMetadataUtils;
022: import org.w3c.dom.Node;
023:
024: public abstract class IIOMetadata {
025:
026: protected boolean standardFormatSupported;
027: protected String nativeMetadataFormatName;
028: protected String nativeMetadataFormatClassName;
029: protected String[] extraMetadataFormatNames;
030: protected String[] extraMetadataFormatClassNames;
031: protected IIOMetadataController defaultController;
032: protected IIOMetadataController controller;
033:
034: protected IIOMetadata() {
035: }
036:
037: protected IIOMetadata(boolean standardMetadataFormatSupported,
038: String nativeMetadataFormatName,
039: String nativeMetadataFormatClassName,
040: String[] extraMetadataFormatNames,
041: String[] extraMetadataFormatClassNames) {
042: standardFormatSupported = standardMetadataFormatSupported;
043: this .nativeMetadataFormatName = nativeMetadataFormatName;
044: this .nativeMetadataFormatClassName = nativeMetadataFormatClassName;
045: if (extraMetadataFormatNames == null) {
046: if (extraMetadataFormatClassNames != null) {
047: throw new IllegalArgumentException(
048: "extraMetadataFormatNames == null && extraMetadataFormatClassNames != null!");
049: }
050: } else {
051: if (extraMetadataFormatClassNames == null) {
052: throw new IllegalArgumentException(
053: "extraMetadataFormatNames != null && extraMetadataFormatClassNames == null!");
054: }
055: if (extraMetadataFormatNames.length == 0) {
056: throw new IllegalArgumentException(
057: "extraMetadataFormatNames.length == 0!");
058: }
059: if (extraMetadataFormatClassNames.length != extraMetadataFormatNames.length) {
060: throw new IllegalArgumentException(
061: "extraMetadataFormatClassNames.length != extraMetadataFormatNames.length!");
062: }
063: this .extraMetadataFormatNames = extraMetadataFormatNames
064: .clone();
065: this .extraMetadataFormatClassNames = extraMetadataFormatClassNames
066: .clone();
067: }
068: }
069:
070: public abstract Node getAsTree(String formatName);
071:
072: public abstract boolean isReadOnly();
073:
074: public abstract void mergeTree(String formatName, Node root)
075: throws IIOInvalidTreeException;
076:
077: public abstract void reset();
078:
079: public IIOMetadataController getController() {
080: return controller;
081: }
082:
083: public boolean hasController() {
084: return getController() != null;
085: }
086:
087: public boolean activateController() {
088: if (!hasController()) {
089: throw new IllegalStateException("hasController() == false!");
090: }
091: return getController().activate(this );
092: }
093:
094: public IIOMetadataController getDefaultController() {
095: return defaultController;
096: }
097:
098: public String[] getExtraMetadataFormatNames() {
099: return extraMetadataFormatNames == null ? null
100: : extraMetadataFormatNames.clone();
101: }
102:
103: public IIOMetadataFormat getMetadataFormat(String formatName) {
104: return IIOMetadataUtils
105: .instantiateMetadataFormat(formatName,
106: standardFormatSupported,
107: nativeMetadataFormatName,
108: nativeMetadataFormatClassName,
109: extraMetadataFormatNames,
110: extraMetadataFormatClassNames);
111: }
112:
113: public String getNativeMetadataFormatName() {
114: return nativeMetadataFormatName;
115: }
116:
117: public boolean isStandardMetadataFormatSupported() {
118: return standardFormatSupported;
119: }
120:
121: public String[] getMetadataFormatNames() {
122: ArrayList<String> res = new ArrayList<String>();
123:
124: String nativeMetadataFormatName = getNativeMetadataFormatName();
125: boolean standardFormatSupported = isStandardMetadataFormatSupported();
126: String extraMetadataFormatNames[] = getExtraMetadataFormatNames();
127:
128: if (standardFormatSupported) {
129: res.add(IIOMetadataFormatImpl.standardMetadataFormatName);
130: }
131: if (nativeMetadataFormatName != null) {
132: res.add(nativeMetadataFormatName);
133: }
134: if (extraMetadataFormatNames != null) {
135: for (String extraMetadataFormatName : extraMetadataFormatNames) {
136: res.add(extraMetadataFormatName);
137: }
138: }
139:
140: return res.size() > 0 ? res.toArray(new String[0]) : null;
141: }
142:
143: protected IIOMetadataNode getStandardChromaNode() {
144: return null;
145: }
146:
147: protected IIOMetadataNode getStandardCompressionNode() {
148: return null;
149: }
150:
151: protected IIOMetadataNode getStandardDataNode() {
152: return null;
153: }
154:
155: protected IIOMetadataNode getStandardDimensionNode() {
156: return null;
157: }
158:
159: protected IIOMetadataNode getStandardDocumentNode() {
160: return null;
161: }
162:
163: protected IIOMetadataNode getStandardTextNode() {
164: return null;
165: }
166:
167: protected IIOMetadataNode getStandardTileNode() {
168: return null;
169: }
170:
171: protected IIOMetadataNode getStandardTransparencyNode() {
172: return null;
173: }
174:
175: protected final IIOMetadataNode getStandardTree() {
176: // Create root node
177: IIOMetadataNode root = new IIOMetadataNode(
178: IIOMetadataFormatImpl.standardMetadataFormatName);
179:
180: Node node;
181: if ((node = getStandardChromaNode()) != null) {
182: root.appendChild(node);
183: }
184: if ((node = getStandardCompressionNode()) != null) {
185: root.appendChild(node);
186: }
187: if ((node = getStandardDataNode()) != null) {
188: root.appendChild(node);
189: }
190: if ((node = getStandardDimensionNode()) != null) {
191: root.appendChild(node);
192: }
193: if ((node = getStandardDocumentNode()) != null) {
194: root.appendChild(node);
195: }
196: if ((node = getStandardTextNode()) != null) {
197: root.appendChild(node);
198: }
199: if ((node = getStandardTileNode()) != null) {
200: root.appendChild(node);
201: }
202: if ((node = getStandardTransparencyNode()) != null) {
203: root.appendChild(node);
204: }
205:
206: return root;
207: }
208:
209: public void setController(IIOMetadataController controller) {
210: this .controller = controller;
211: }
212:
213: public void setFromTree(String formatName, Node root)
214: throws IIOInvalidTreeException {
215: reset();
216: mergeTree(formatName, root);
217: }
218: }
|