public TableConfig readConfigXML( String fileLocation, FeatureType wantFeatureType, NetcdfDataset ds, Formatter errlog) throws IOException { org.jdom2.Document doc; try { SAXBuilder builder = new SAXBuilder(false); if (debugURL) System.out.println(" PointConfig URL = <" + fileLocation + ">"); doc = builder.build(fileLocation); } catch (JDOMException e) { throw new IOException(e.getMessage()); } if (debugXML) System.out.println(" SAXBuilder done"); if (showParsedXML) { XMLOutputter xmlOut = new XMLOutputter(); System.out.println( "*** PointConfig/showParsedXML = \n" + xmlOut.outputString(doc) + "\n*******"); } Element configElem = doc.getRootElement(); String featureType = configElem.getAttributeValue("featureType"); Element tableElem = configElem.getChild("table"); TableConfig tc = parseTableConfig(ds, tableElem, null); tc.featureType = FeatureType.valueOf(featureType); return tc; }
public TableConfig readConfigXMLfromResource( String resourceLocation, FeatureType wantFeatureType, NetcdfDataset ds, Formatter errlog) throws IOException { ClassLoader cl = this.getClass().getClassLoader(); try (InputStream is = cl.getResourceAsStream(resourceLocation)) { if (is == null) throw new FileNotFoundException(resourceLocation); if (debugXML) { System.out.println(" PointConfig URL = <" + resourceLocation + ">"); try (InputStream is2 = cl.getResourceAsStream(resourceLocation)) { System.out.println(" contents=\n" + IO.readContents(is2)); } } org.jdom2.Document doc; try { SAXBuilder builder = new SAXBuilder(false); if (debugURL) System.out.println(" PointConfig URL = <" + resourceLocation + ">"); doc = builder.build(is); } catch (JDOMException e) { throw new IOException(e.getMessage()); } if (debugXML) System.out.println(" SAXBuilder done"); if (showParsedXML) { XMLOutputter xmlOut = new XMLOutputter(); System.out.println( "*** PointConfig/showParsedXML = \n" + xmlOut.outputString(doc) + "\n*******"); } Element configElem = doc.getRootElement(); String featureType = configElem.getAttributeValue("featureType"); Element tableElem = configElem.getChild("table"); assert tableElem != null; TableConfig tc = parseTableConfig(ds, tableElem, null); tc.featureType = FeatureType.valueOf(featureType); return tc; } }
public static void read(org.jdom2.Element root, StringBuilder errlog) { List children = root.getChildren(); for (int i = 0; i < children.size(); i++) { Element elem = (Element) children.get(i); if (elem.getName().equals("ioServiceProvider")) { String className = elem.getAttributeValue("class"); try { ucar.nc2.NetcdfFile.registerIOProvider(className); } catch (ClassNotFoundException e) { errlog.append( "CoordSysBuilder class not found= " + className + "; check your classpath\n"); } catch (Exception e) { errlog.append("IOServiceProvider " + className + " failed= " + e.getMessage() + "\n"); } } else if (elem.getName().equals("coordSysBuilder")) { String conventionName = elem.getAttributeValue("convention"); String className = elem.getAttributeValue("class"); try { ucar.nc2.dataset.CoordSysBuilder.registerConvention(conventionName, className); } catch (ClassNotFoundException e) { errlog.append( "CoordSysBuilder class not found= " + className + "; check your classpath\n"); } catch (Exception e) { errlog.append("CoordSysBuilder " + className + " failed= " + e.getMessage() + "\n"); } } else if (elem.getName().equals("coordTransBuilder")) { String transformName = elem.getAttributeValue("name"); String className = elem.getAttributeValue("class"); try { ucar.nc2.dataset.CoordTransBuilder.registerTransform(transformName, className); } catch (ClassNotFoundException e) { errlog.append( "CoordSysBuilder class not found= " + className + "; check your classpath\n"); } catch (Exception e) { errlog.append("CoordTransBuilder " + className + " failed= " + e.getMessage() + "\n"); } } else if (elem.getName().equals("typedDatasetFactory")) { String typeName = elem.getAttributeValue("datatype"); String className = elem.getAttributeValue("class"); FeatureType datatype = FeatureType.valueOf(typeName.toUpperCase()); if (null == datatype) { errlog.append( "TypedDatasetFactory " + className + " unknown datatype= " + typeName + "\n"); continue; } try { ucar.nc2.dt.TypedDatasetFactory.registerFactory(datatype, className); } catch (ClassNotFoundException e) { errlog.append( "CoordSysBuilder class not found= " + className + "; check your classpath\n"); } catch (Exception e) { errlog.append("TypedDatasetFactory " + className + " failed= " + e.getMessage() + "\n"); } } else if (elem.getName().equals("gribParameterTable")) { String editionS = elem.getAttributeValue("edition"); String centerS = elem.getAttributeValue("center"); String subcenterS = elem.getAttributeValue("subcenter"); String versionS = elem.getAttributeValue("version"); String filename = elem.getText(); if ((centerS == null) || (versionS == null) || (filename == null)) { errlog.append("table element must center, version and filename attributes\n"); continue; } // Grib1ParamTables.addParameterTable(int center, int subcenter, int tableVersion, String // filename) { // use reflection to decouple from the grib package try { int center = Integer.parseInt(centerS); int subcenter = (subcenterS == null) ? -1 : Integer.parseInt(subcenterS); int version = Integer.parseInt(versionS); // ucar.nc2.grib.grib1.tables.Grib1ParamTables.addParameterTable(int center, int // subcenter, int tableVersion, String tableFilename) Class c = RuntimeConfigParser.class .getClassLoader() .loadClass("ucar.nc2.grib.grib1.tables.Grib1ParamTables"); Method m = c.getMethod("addParameterTable", int.class, int.class, int.class, String.class); m.invoke(null, center, subcenter, version, filename); } catch (Exception e) { e.printStackTrace(); } String strictS = elem.getAttributeValue("strict"); if (strictS != null) { boolean notStrict = strictS.equalsIgnoreCase("false"); try { Class c = RuntimeConfigParser.class .getClassLoader() .loadClass("ucar.grib.grib1.tables.Grib1ParamTables"); Method m = c.getMethod("setStrict", boolean.class); m.invoke(null, !notStrict); } catch (Exception e) { e.printStackTrace(); } continue; } } else if (elem.getName().equals("gribParameterTableLookup")) { String editionS = elem.getAttributeValue("edition"); String filename = elem.getText(); // ucar.nc2.grib.grib1.tables.Grib1ParamTables.addParameterTableLookup(String // lookupFilename) try { Class c = RuntimeConfigParser.class .getClassLoader() .loadClass("ucar.nc2.grib.grib1.tables.Grib1ParamTables"); Method m = c.getMethod("addParameterTableLookup", String.class); m.invoke(null, filename); } catch (Exception e) { e.printStackTrace(); } } else if (elem.getName().equals("table")) { String type = elem.getAttributeValue("type"); String filename = elem.getAttributeValue("filename"); if ((type == null) || (filename == null)) { errlog.append("table element must have both type and filename attributes\n"); continue; } try { if (type.equalsIgnoreCase("GRIB1")) { // ucar.grib.grib1.GribPDSParamTable.addParameterUserLookup( filename); // use reflection instead to decouple from the grib package try { Class c = RuntimeConfigParser.class .getClassLoader() .loadClass("ucar.grib.grib1.GribPDSParamTable"); Method m = c.getMethod("addParameterUserLookup", String.class); m.invoke(null, filename); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } else if (type.equalsIgnoreCase("GRIB2")) { // ucar.grib.grib2.ParameterTable.addParametersUser( filename); try { Class c = RuntimeConfigParser.class .getClassLoader() .loadClass(" ucar.grib.grib2.ParameterTable"); Method m = c.getMethod("addParametersUser", String.class); m.invoke(null, filename); } catch (Exception e) { e.printStackTrace(); } } else { errlog.append("Unknown table type " + type + "\n"); continue; } } catch (Exception e) { errlog.append("table read failed on " + filename + " = " + e.getMessage() + "\n"); } } else if (elem.getName().equals("bufrtable")) { String filename = elem.getAttributeValue("filename"); if (filename == null) { errlog.append("bufrtable must have filename attribute\n"); continue; } // reflection is used to decouple optional jars Class bufrTablesClass; try { bufrTablesClass = RuntimeConfigParser.class .getClassLoader() .loadClass( "ucar.nc2.iosp.bufr.tables.BufrTables"); // only load if bufr.jar is present java.lang.Class[] params = new Class[1]; params[0] = String.class; Method method = bufrTablesClass.getMethod("addLookupFile", params); Object[] args = new Object[1]; args[0] = filename; method.invoke(null, args); // static method has null for object } catch (Throwable e) { if (e instanceof FileNotFoundException) errlog.append("bufrtable read failed on " + filename + " = " + e.getMessage() + "\n"); else errlog.append("bufr.jar is not on classpath\n"); } } else if (elem.getName().equals("Netcdf4Clibrary")) { // cdm does not have a dependency on netcdf4 (and we don't want to introduce one), // so we cannot refer to the Nc4Iosp.class object. String nc4IospClassName = "ucar.nc2.jni.netcdf.Nc4Iosp"; /* <Netcdf4Clibrary> <libraryPath>/usr/local/lib</libraryPath> <libraryName>netcdf</libraryName> <useForReading>false</useForReading> </Netcdf4Clibrary> */ String path = elem.getChildText("libraryPath"); String name = elem.getChildText("libraryName"); if (path != null && name != null) { // reflection is used to decouple optional jars try { Class nc4IospClass = RuntimeConfigParser.class.getClassLoader().loadClass(nc4IospClassName); Method method = nc4IospClass.getMethod( "setLibraryAndPath", new Class[] {String.class, String.class}); method.invoke(null, path, name); // static method has null for object } catch (Throwable e) { errlog.append(nc4IospClassName + " is not on classpath\n"); } } boolean useForReading = Boolean.parseBoolean(elem.getChildText("useForReading")); if (useForReading) { try { // Registers Nc4Iosp in front of all the other IOSPs already registered in // NetcdfFile.<clinit>(). // Crucially, this means that we'll try to open a file with Nc4Iosp before we try it // with H5iosp. NetcdfFile.registerIOProvider(nc4IospClassName); } catch (IllegalAccessException | InstantiationException | ClassNotFoundException e) { errlog.append( String.format( "Could not register IOSP '%s': %s%n", nc4IospClassName, e.getMessage())); } } } } }