001: package it.geosolutions.imageio.plugins.jhdf.aps;
002:
003: import it.geosolutions.imageio.plugins.jhdf.BaseHDFImageReaderSpi;
004: import it.geosolutions.imageio.stream.input.FileImageInputStreamExtImpl;
005:
006: import java.io.File;
007: import java.io.IOException;
008: import java.util.Iterator;
009: import java.util.Locale;
010:
011: import javax.imageio.ImageReader;
012:
013: import ncsa.hdf.object.Attribute;
014: import ncsa.hdf.object.FileFormat;
015: import ncsa.hdf.object.HObject;
016:
017: public class APSImageReaderSpi extends BaseHDFImageReaderSpi {
018:
019: /**
020: * The list of the required attributes was built in compliance with the
021: * information available at:
022: * http://www7333.nrlssc.navy.mil/docs/aps_v3.4/user/aps/ch06.html
023: *
024: */
025: private final static String[] requiredAPSAttributes = { "file",
026: "fileClassification", "fileStatus", "fileTitle",
027: "fileVersion", "createAgency", "createSoftware",
028: "createPlatform", "createTime",
029: /* "createUser" */}; // TODO: Add more attributes??
030:
031: static final String[] suffixes = { "hdf" };
032:
033: static final String[] formatNames = { "HDF", "HDF4", "HDF5" };
034:
035: static final String[] mimeTypes = { "image/hdf" };
036:
037: static final String version = "1.0";
038:
039: static final String readerCN = "it.geosolutions.imageio.plugins.jhdf.aps.APSImageReader";
040:
041: static final String vendorName = "GeoSolutions";
042:
043: // writerSpiNames
044: static final String[] wSN = { null };
045:
046: // StreamMetadataFormatNames and StreamMetadataFormatClassNames
047: static final boolean supportsStandardStreamMetadataFormat = false;
048:
049: static final String nativeStreamMetadataFormatName = "it.geosolutions.imageio.plugins.jhdf.aps.APSHDFStreamMetadata_1.0";
050:
051: static final String nativeStreamMetadataFormatClassName = null;
052:
053: static final String[] extraStreamMetadataFormatNames = { null };
054:
055: static final String[] extraStreamMetadataFormatClassNames = { null };
056:
057: // ImageMetadataFormatNames and ImageMetadataFormatClassNames
058: static final boolean supportsStandardImageMetadataFormat = false;
059:
060: static final String nativeImageMetadataFormatName = "it.geosolutions.imageio.plugins.jhdf.aps.APSHDFImageMetadata_1.0";
061:
062: static final String nativeImageMetadataFormatClassName = null;
063:
064: static final String[] extraImageMetadataFormatNames = { null };
065:
066: static final String[] extraImageMetadataFormatClassNames = { null };
067:
068: public APSImageReaderSpi() {
069: super (
070: vendorName,
071: version,
072: formatNames,
073: suffixes,
074: mimeTypes,
075: readerCN, // readerClassName
076: STANDARD_INPUT_TYPE,
077: wSN, // writer Spi Names
078: supportsStandardStreamMetadataFormat,
079: nativeStreamMetadataFormatName,
080: nativeStreamMetadataFormatClassName,
081: extraStreamMetadataFormatNames,
082: extraStreamMetadataFormatClassNames,
083: supportsStandardImageMetadataFormat,
084: nativeImageMetadataFormatName,
085: nativeImageMetadataFormatClassName,
086: extraImageMetadataFormatNames,
087: extraImageMetadataFormatClassNames);
088: }
089:
090: //
091: // This version of the canDecodeInput checks if all required attributes are
092: // present
093: // in the APS source.
094: //
095: private boolean OldcanDecodeInput(Object input) throws IOException {
096: if (input instanceof FileImageInputStreamExtImpl) {
097: input = ((FileImageInputStreamExtImpl) input).getFile();
098: }
099:
100: if (input instanceof File) {
101: final String filepath = ((File) input).getPath();
102:
103: // TODO: Improve to add HDF5 support.
104: final FileFormat fileFormat = FileFormat
105: .getFileFormat(FileFormat.FILE_TYPE_HDF4);
106: if (fileFormat != null && fileFormat.isThisType(filepath))
107: try {
108: final FileFormat testFile = fileFormat.open(
109: filepath, FileFormat.READ);
110: if (testFile != null) {
111: if (testFile.open() >= 0) {
112:
113: // CHECKS IF APS REQUIRED ATTRIBUTES ARE PRESENT
114: final int requiredAttributes = requiredAPSAttributes.length;
115: int foundAttributes = 0;
116: HObject root = testFile.get("/");
117: final Iterator metadataIt = root
118: .getMetadata().iterator();
119:
120: while (metadataIt.hasNext()) {
121: // get the attribute
122: final Attribute att = (Attribute) metadataIt
123: .next();
124: final String attName = att.getName();
125: for (int i = 0; i < requiredAttributes; i++) {
126: if (attName
127: .equals(requiredAPSAttributes[i])) {
128: foundAttributes++;
129: if (foundAttributes == requiredAttributes) {
130: // Avoid to scan the whole
131: // attributes
132: // list if I already found the
133: // required ones
134: testFile.close();
135: return true;
136: }
137: break;
138: }
139: }
140: }
141:
142: testFile.close();
143: if (foundAttributes == requiredAttributes)
144: return true;
145: return false;
146: }
147: }
148: } catch (IOException e) {
149: throw e;
150: } catch (Exception e) {
151: }
152: }
153: return false;
154: }
155:
156: public boolean canDecodeInput(Object input) throws IOException {
157: synchronized (spiMutex) {
158:
159: if (input instanceof FileImageInputStreamExtImpl) {
160: input = ((FileImageInputStreamExtImpl) input).getFile();
161: }
162:
163: if (input instanceof File) {
164: final String filepath = ((File) input).getPath();
165:
166: // TODO: Improve to add HDF5 support.
167: final FileFormat fileFormat = FileFormat
168: .getFileFormat(FileFormat.FILE_TYPE_HDF4);
169: if (fileFormat != null
170: && fileFormat.isThisType(filepath))
171: try {
172: final FileFormat testFile = fileFormat.open(
173: filepath, FileFormat.READ);
174: if (testFile != null) {
175: if (testFile.open() >= 0) {
176: boolean found = false;
177: HObject root = testFile.get("/");
178: final Iterator metadataIt = root
179: .getMetadata().iterator();
180:
181: while (metadataIt.hasNext()) {
182: // get the attribute
183: final Attribute att = (Attribute) metadataIt
184: .next();
185: final String attName = att
186: .getName();
187:
188: if (attName
189: .equals(APSProperties.STD_FA_CREATESOFTWARE)) {
190: final Object attValue = att
191: .getValue();
192: final String value[] = ((String[]) attValue);
193: if (value[0].startsWith("APS")) {
194: found = true;
195: break;
196: }
197: }
198:
199: }
200:
201: testFile.close();
202: return found;
203: }
204: }
205: } catch (IOException e) {
206: throw e;
207: } catch (Exception e) {
208: // TODO
209: }
210: }
211: return false;
212: }
213: }
214:
215: /**
216: * Returns an instance of the APSImageReader
217: *
218: * @see javax.imageio.spi.ImageReaderSpi#createReaderInstance(java.lang.Object)
219: */
220: public ImageReader createReaderInstance(Object source)
221: throws IOException {
222: return new APSImageReader(this );
223: }
224:
225: /**
226: * @see javax.imageio.spi.IIOServiceProvider#getDescription(java.util.Locale)
227: */
228: public String getDescription(Locale locale) {
229: return new StringBuffer(
230: "APS Compliant HDF Image Reader, version ").append(
231: version).toString();
232: }
233:
234: }
|