001: package it.geosolutions.imageio.plugins.jhdf.tovs;
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 TOVSImageReaderSpi extends BaseHDFImageReaderSpi {
018:
019: static final String[] suffixes = { "hdf" };
020:
021: static final String[] formatNames = { "HDF", "HDF4", "HDF5" };
022:
023: static final String[] mimeTypes = { "image/hdf" };
024:
025: static final String version = "1.0";
026:
027: static final String readerCN = "it.geosolutions.imageio.plugins.jhdf.tovs.TOVSImageReader";
028:
029: static final String vendorName = "GeoSolutions";
030:
031: // writerSpiNames
032: static final String[] wSN = { null };
033:
034: // StreamMetadataFormatNames and StreamMetadataFormatClassNames
035: static final boolean supportsStandardStreamMetadataFormat = false;
036:
037: static final String nativeStreamMetadataFormatName = "it.geosolutions.imageio.plugins.jhdf.tovs.TOVSStreamMetadata_1.0";
038:
039: static final String nativeStreamMetadataFormatClassName = null;
040:
041: static final String[] extraStreamMetadataFormatNames = { null };
042:
043: static final String[] extraStreamMetadataFormatClassNames = { null };
044:
045: // ImageMetadataFormatNames and ImageMetadataFormatClassNames
046: static final boolean supportsStandardImageMetadataFormat = false;
047:
048: static final String nativeImageMetadataFormatName = "it.geosolutions.imageio.plugins.jhdf.tovs.TOVSImageMetadata_1.0";
049:
050: static final String nativeImageMetadataFormatClassName = null;
051:
052: static final String[] extraImageMetadataFormatNames = { null };
053:
054: static final String[] extraImageMetadataFormatClassNames = { null };
055:
056: public TOVSImageReaderSpi() {
057: super (
058: vendorName,
059: version,
060: formatNames,
061: suffixes,
062: mimeTypes,
063: readerCN, // readerClassName
064: STANDARD_INPUT_TYPE,
065: wSN, // writer Spi Names
066: supportsStandardStreamMetadataFormat,
067: nativeStreamMetadataFormatName,
068: nativeStreamMetadataFormatClassName,
069: extraStreamMetadataFormatNames,
070: extraStreamMetadataFormatClassNames,
071: supportsStandardImageMetadataFormat,
072: nativeImageMetadataFormatName,
073: nativeImageMetadataFormatClassName,
074: extraImageMetadataFormatNames,
075: extraImageMetadataFormatClassNames);
076: }
077:
078: public boolean canDecodeInput(Object input) throws IOException {
079: synchronized (spiMutex) {
080: if (input instanceof FileImageInputStreamExtImpl) {
081: input = ((FileImageInputStreamExtImpl) input).getFile();
082: }
083:
084: if (input instanceof File) {
085: final String filepath = ((File) input).getPath();
086:
087: // TODO: Improve to add HDF5 support.
088: final FileFormat fileFormat = FileFormat
089: .getFileFormat(FileFormat.FILE_TYPE_HDF4);
090: if (fileFormat != null
091: && fileFormat.isThisType(filepath))
092: try {
093: final FileFormat testFile = fileFormat.open(
094: filepath, FileFormat.READ);
095: if (testFile != null) {
096: if (testFile.open() >= 0) {
097: boolean found = false;
098: HObject root = testFile.get("/");
099: final Iterator metadataIt = root
100: .getMetadata().iterator();
101:
102: while (metadataIt.hasNext()) {
103: // get the attribute
104: final Attribute att = (Attribute) metadataIt
105: .next();
106: final String attName = att
107: .getName();
108:
109: if (attName
110: .startsWith("File Description")) {
111: final Object attValue = att
112: .getValue();
113: final String value = (String) attValue;
114: if (value
115: .contains("TOVS PATHFINDER")) {
116: found = true;
117: break;
118: }
119: }
120:
121: }
122:
123: testFile.close();
124: return found;
125: }
126: }
127: } catch (IOException e) {
128: throw e;
129: } catch (Exception e) {
130: // TODO
131: }
132: }
133: }
134: return false;
135: }
136:
137: public ImageReader createReaderInstance(Object source)
138: throws IOException {
139: return new TOVSImageReader(this );
140: }
141:
142: public String getDescription(Locale locale) {
143: return new StringBuffer(
144: "TOVS Compliant HDF Image Reader, version ").append(
145: version).toString();
146: }
147:
148: }
|