NetCDFParser extends AbstractParser { public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException { TemporaryResources tmp = TikaInputStream.isTikaInputStream(stream) ? null : new TemporaryResources(); TikaInputStream tis = TikaInputStream.get(stream, tmp); NetcdfFile ncFile = null; try { ncFile = NetcdfFile.open(tis.getFile().getAbsolutePath()); metadata.set("File-Type-Description", ncFile.getFileTypeDescription()); for (Attribute attr : ncFile.getGlobalAttributes()) { Property property = resolveMetadataKey(attr.getFullName()); if (attr.getDataType().isString()) { metadata.add(property, attr.getStringValue()); } else if (attr.getDataType().isNumeric()) { int value = attr.getNumericValue().intValue(); metadata.add(property, String.valueOf(value)); } } XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata); xhtml.startDocument(); xhtml.newline(); xhtml.element("h1", "dimensions"); xhtml.startElement("ul"); xhtml.newline(); for (Dimension dim : ncFile.getDimensions()) { xhtml.element("li", dim.getFullName() + " = " + dim.getLength()); } xhtml.endElement("ul"); xhtml.element("h1", "variables"); xhtml.startElement("ul"); xhtml.newline(); for (Variable var : ncFile.getVariables()) { xhtml.startElement("li"); xhtml.characters(var.getDataType() + " " + var.getNameAndDimensions()); xhtml.newline(); List<Attribute> attributes = var.getAttributes(); if (!attributes.isEmpty()) { xhtml.startElement("ul"); for (Attribute element : attributes) { xhtml.element("li", element.toString()); } xhtml.endElement("ul"); } xhtml.endElement("li"); } xhtml.endElement("ul"); xhtml.endDocument(); } catch (IOException e) { throw new TikaException("NetCDF parse error", e); } finally { if (ncFile != null) { ncFile.close(); } if (tmp != null) { tmp.dispose(); } } }  Set<MediaType> getSupportedTypes(ParseContext context); void parse(InputStream stream, ContentHandler handler,
                      Metadata metadata, ParseContext context);  }
@Test public void testParseGlobalMetadata() throws Exception { Parser parser = new NetCDFParser(); ContentHandler handler = new BodyContentHandler(); Metadata metadata = new Metadata(); try (InputStream stream = NetCDFParser.class .getResourceAsStream("/test-documents/sresa1b_ncar_ccsm3_0_run1_200001.nc")) { parser.parse(stream, handler, metadata, new ParseContext()); } assertEquals(metadata.get(TikaCoreProperties.TITLE), "model output prepared for IPCC AR4"); assertEquals(metadata.get(Metadata.CONTACT), "ccsm@ucar.edu"); assertEquals(metadata.get(Metadata.PROJECT_ID), "IPCC Fourth Assessment"); assertEquals(metadata.get(Metadata.CONVENTIONS), "CF-1.0"); assertEquals(metadata.get(Metadata.REALIZATION), "1"); assertEquals(metadata.get(Metadata.EXPERIMENT_ID), "720 ppm stabilization experiment (SRESA1B)"); assertEquals(metadata.get("File-Type-Description"), "NetCDF-3/CDM"); String content = handler.toString(); assertContains("long_name = \"Surface area\"", content); assertContains("float area(lat=128, lon=256)", content); assertContains("float lat(lat=128)", content); assertContains("double lat_bnds(lat=128, bnds=2)", content); assertContains("double lon_bnds(lon=256, bnds=2)", content); }