List of usage examples for java.util Properties getOrDefault
@Override
public Object getOrDefault(Object key, Object defaultValue)
From source file:com.esri.geoportal.commons.pdf.PdfUtils.java
/** * Generates a Dublin-Core XML string from the given PDF's metadata. * //from ww w . j a v a2 s.c om * @param pdfBytes the PDF file to parse * @param fileName the name of the PDF file. Used if the PDF metadata doesn't specify a title. * @param url the source location of the PDF file. Used to set the XML's "resource URL". * @param geometryServiceUrl url of a <a href="https://developers.arcgis.com/rest/services-reference/geometry-service.htm">geometry service</a> for reprojecting coordinates. * * @return Dublin-Core XML metadata */ public static byte[] generateMetadataXML(byte[] pdfBytes, String fileName, String url, String geometryServiceUrl) throws IOException { byte[] bytes = null; // Read in the PDF metadata. Properties metaProps = readMetadata(pdfBytes, fileName, geometryServiceUrl); // Build out the XML metadata if (metaProps != null) { Properties props = new Properties(); props.put(WKAConstants.WKA_TITLE, metaProps.get(PdfUtils.PROP_TITLE)); props.put(WKAConstants.WKA_DESCRIPTION, metaProps.get(PdfUtils.PROP_SUBJECT)); props.put(WKAConstants.WKA_MODIFIED, metaProps.get(PdfUtils.PROP_MODIFICATION_DATE)); props.put(WKAConstants.WKA_BBOX, metaProps.getOrDefault(PdfUtils.PROP_BBOX, DEFAULT_BBOX)); props.put(WKAConstants.WKA_RESOURCE_URL, url); try { MapAttribute attr = AttributeUtils.fromProperties(props); Document document = new SimpleDcMetaBuilder().create(attr); bytes = XmlUtils.toString(document).getBytes("UTF-8"); } catch (MetaException | TransformerException ex) { throw new IOException(ex); } } return bytes; }
From source file:org.pentaho.big.data.impl.shim.mapreduce.PentahoMapReduceJobBuilderImpl.java
/** * Gets a property from the configuration. If it is missing it will load it from the properties provided. If it * cannot/*w ww. ja v a2 s . c o m*/ * be found there the default value provided will be used. * * @param conf Configuration to check for property first. * @param properties Properties to check for property second. * @param propertyName Name of the property to return * @param defaultValue Default value to use if no property by the given name could be found in {@code conf} or {@code * properties} * @return Value of {@code propertyName} */ public static String getProperty(Configuration conf, Properties properties, String propertyName, String defaultValue) { String fromConf = conf.get(propertyName); if (Utils.isEmpty(fromConf)) { Object objectValue = properties.getOrDefault(propertyName, null); if (objectValue != null) { if (objectValue instanceof String) { return objectValue.toString(); } else if (objectValue instanceof List) { // it should contain strings only ArrayList<String> values = new ArrayList<String>((List) objectValue); StringBuilder stringBuilder = new StringBuilder(""); for (int i = 0; i < values.size(); i++) { String value = values.get(i); if (value != null && !value.isEmpty()) { if (i != 0) { stringBuilder.append(","); } stringBuilder.append(value); } } if (stringBuilder.toString().equals("")) { return defaultValue; } else { return stringBuilder.toString(); } } else { // shouldn't happen return defaultValue; } } else { return defaultValue; } } return fromConf; }
From source file:org.pentaho.big.data.impl.shim.mapreduce.PentahoMapReduceJobBuilderImplTest.java
@Test public void testGetPropertyFromProperties() { Configuration configuration = mock(Configuration.class); Properties properties = mock(Properties.class); ArrayList<String> values = new ArrayList<>(); values.add("value1"); values.add("value2"); when(properties.getOrDefault("property1", null)).thenReturn(values); assertEquals("value1,value2", pentahoMapReduceJobBuilder.getProperty(configuration, properties, "property1", null)); when(properties.getOrDefault("property2", null)).thenReturn("value"); assertEquals("value", pentahoMapReduceJobBuilder.getProperty(configuration, properties, "property2", null)); }