class LandsatMetadataFactory {

    static LandsatMetadata create(File mtlFile) throws IOException {
        LandsatLegacyMetadata landsatMetadata = new LandsatLegacyMetadata(new FileReader(mtlFile));
        if (landsatMetadata.isLegacyFormat()) {
            // legacy format case
            if (landsatMetadata.isLandsatTM() || landsatMetadata.isLandsatETM_Plus()) {
                return landsatMetadata;
            } else {
                throw new ProductIOException("Product is of a legacy landsat format, not a legacy Landsat5 or Landsat7 ETM+ product.");
            }
        } else {
            // new format case
            BufferedReader reader = null;
            try {
                FileReader fileReader = new FileReader(mtlFile);
                reader = new BufferedReader(fileReader);
                String line = reader.readLine();
                while (line != null) {
                    if (line.contains("SPACECRAFT_ID")) {
                        if (line.contains("LANDSAT_8")) {
                            return new Landsat8Metadata(new FileReader(mtlFile));
                        } else {
                            return new LandsatReprocessedMetadata(new FileReader(mtlFile));
                        }
                    }
                    line = reader.readLine();
                }
            } finally {
                if (reader != null) {
                    reader.close();
                }
            }
            throw new IllegalStateException(
                    "File '" + mtlFile + "' does not contain spacecraft information. (Field 'SPACECRAFT_ID' missing)");
        }
    }

}