List of usage examples for java.net URL openStream
public final InputStream openStream() throws java.io.IOException
From source file:net.sf.jasperreports.engine.JRPropertiesMap.java
/** * Loads a properties file from a location. * //from w w w . j av a 2 s. c o m * @param location the properties file URL * @return the properties file loaded as a in-memory properties map */ public static JRPropertiesMap loadProperties(URL location) { boolean close = true; InputStream stream = null; try { stream = location.openStream(); Properties props = new Properties(); props.load(stream); close = false; stream.close(); JRPropertiesMap properties = new JRPropertiesMap(); for (Enumeration<?> names = props.propertyNames(); names.hasMoreElements();) { String name = (String) names.nextElement(); String value = props.getProperty(name); properties.setProperty(name, value); } return properties; } catch (IOException e) { throw new JRRuntimeException(e); } finally { if (close && stream != null) { try { stream.close(); } catch (IOException e) { if (log.isWarnEnabled()) { log.warn("Error closing stream for " + location, e); } } } } }
From source file:de.tudarmstadt.ukp.dkpro.wsd.WSDUtils.java
/** * Read key-value pairs of the specified types from the specified columns of * a delimited text file into a Map.// ww w . ja v a2 s . c o m * * @param url * Location of the file to read * @param keyColumn * The index of the column giving the keys * @param keyClass * The class of the key * @param valueColumn * The index of the column giving the values * @param valueClass * The class of the value * @param delimiterRegex * A regular expression for the field delimiter * @return A map of keys to values * @throws IOException * @throws IllegalArgumentException */ public static <K, V> Map<K, V> readMap(final URL url, final int keyColumn, final Class<K> keyClass, final int valueColumn, final Class<V> valueClass, final String delimiterRegex) throws IOException, IllegalArgumentException { Map<K, V> map = new HashMap<K, V>(); InputStream is = url.openStream(); String content = IOUtils.toString(is, "UTF-8"); BufferedReader br = new BufferedReader(new StringReader(content)); Constructor<K> keyConstructor; Constructor<V> valueConstructor; try { keyConstructor = keyClass.getConstructor(String.class); valueConstructor = valueClass.getConstructor(String.class); } catch (Exception e) { throw new IllegalArgumentException(e); } String line; String[] lineParts; while ((line = br.readLine()) != null) { lineParts = line.split(delimiterRegex); K key; V value; try { key = keyConstructor.newInstance(lineParts[keyColumn - 1]); value = valueConstructor.newInstance(lineParts[valueColumn - 1]); } catch (Exception e) { throw new IllegalArgumentException(e); } map.put(key, value); } return map; }
From source file:com.openteach.diamond.container.Container.java
/** * ????/*from w w w . ja v a2 s.c om*/ * * @return Map<String, Class<?>> key: ?? Class<?> Class */ public static Map<String, Class<?>> getExportedClasses() throws Exception { Map<String, Class<?>> exportedClasses = new HashMap<String, Class<?>>(); Properties exportedClassesProps = new Properties(); InputStream is = null; Bundle[] bundles = context.getBundles(); for (int i = 0; i < bundles.length; i++) { String bundleName = bundles[i].getSymbolicName().toLowerCase(); URL url = bundles[i].getResource(BUNDLE_PROPERTY_FILE); if (url != null) { is = url.openStream(); exportedClassesProps.load(is); is.close(); } if (exportedClassesProps.containsKey(bundleName)) { String value = exportedClassesProps.getProperty(bundleName); String[] classes = value.split(","); for (int j = 0; j < classes.length; j++) { exportedClasses.put(classes[j], bundles[i].loadClass(classes[j].trim())); } } } return exportedClasses; }
From source file:cz.lbenda.dataman.db.frm.DbConfigFrmController.java
/** Create new instance return main node and controller of this node and sub-nodes */ public static Tuple2<Parent, DbConfigFrmController> createNewInstance() { URL resource = DbConfigFrmController.class.getResource(FXML_RESOURCE); try {/* ww w . ja v a2 s . c o m*/ FXMLLoader loader = new FXMLLoader(); loader.setLocation(resource); loader.setBuilderFactory(new JavaFXBuilderFactory()); Parent node = loader.load(resource.openStream()); DbConfigFrmController controller = loader.getController(); return new Tuple2<>(node, controller); } catch (IOException e) { LOG.error("Problem with reading FXML", e); throw new RuntimeException("Problem with reading FXML", e); } }
From source file:io.apiman.common.es.util.ApimanEmbeddedElastic.java
public static String getEsBuildVersion() { URL url = ApimanEmbeddedElastic.class.getResource("apiman-embedded-elastic.properties"); if (url == null) { throw new RuntimeException("embedded-elastic.properties missing."); } else {// ww w . ja v a2 s.c om Properties allProperties = new Properties(); try (InputStream is = url.openStream()) { allProperties.load(is); return Optional.ofNullable(allProperties.getProperty("apiman.embedded-es-version")) .orElseThrow(() -> new RuntimeException("apiman.embedded-es-version")); } catch (IOException e) { throw new RuntimeException(e); } } }
From source file:com.projity.configuration.ConfigurationReader.java
public static ProvidesDigesterEvents read(String configurationUrl, ProvidesDigesterEvents root) { URL url = ConfigurationReader.class.getClassLoader().getResource(configurationUrl); if (url == null) { log.fatal("could not find xml configuration file: " + configurationUrl); return null; }/*w w w . j a v a2 s .c o m*/ //log.info("Reading configuration from " + url + " " + new java.util.Date()); ProvidesDigesterEvents result = null; try { result = readStream(url.openStream(), root); } catch (IOException e) { log.error("Could not read field xml configuration file " + url); e.printStackTrace(); } //log.info("Done reading configuration from " + url + " " + new java.util.Date()); return result; }
From source file:de.tudarmstadt.ukp.dkpro.wsd.WSDUtils.java
/** * Read key-value pairs of the specified types from the specified columns of * a delimited text file into a Multimap. * * @param url//from w w w .j av a 2 s . c o m * Location of the file to read * @param keyColumn * The index of the column giving the keys * @param keyClass * The class of the key * @param valueColumn * The index of the column giving the values * @param valueClass * The class of the value * @param delimiterRegex * A regular expression for the field delimiter * @return A map of keys to values * @throws IOException * @throws IllegalArgumentException */ public static <K, V> Multimap<K, V> readMultimap(final URL url, final int keyColumn, final Class<K> keyClass, final int valueColumn, final Class<V> valueClass, final String delimiterRegex) throws IOException, IllegalArgumentException { Multimap<K, V> map = HashMultimap.create(); InputStream is = url.openStream(); String content = IOUtils.toString(is, "UTF-8"); BufferedReader br = new BufferedReader(new StringReader(content)); Constructor<K> keyConstructor; Constructor<V> valueConstructor; try { keyConstructor = keyClass.getConstructor(String.class); valueConstructor = valueClass.getConstructor(String.class); } catch (Exception e) { throw new IllegalArgumentException(e); } String line; String[] lineParts; while ((line = br.readLine()) != null) { lineParts = line.split(delimiterRegex); K key; V value; try { key = keyConstructor.newInstance(lineParts[keyColumn - 1]); value = valueConstructor.newInstance(lineParts[valueColumn - 1]); } catch (Exception e) { throw new IllegalArgumentException(e); } map.put(key, value); } return map; }
From source file:org.esupportail.lecture.domain.DomainTools.java
/** * @param xsltFileURL//from w w w. jav a 2 s . co m * @return cached xsltFile as a streamSource * @throws MalformedURLException * @throws IOException */ public static String getXsltFile(final String xsltFileURL) throws IOException { if (LOG.isDebugEnabled()) { LOG.debug("getXsltFile(" + xsltFileURL + ")"); } String inputXslt; String cacheKey = "XSLT:" + xsltFileURL; Element element = cache.get(cacheKey); if (element == null) { URL url2 = new URL(xsltFileURL); InputStream is = url2.openStream(); BufferedReader in = new BufferedReader(new InputStreamReader(is)); String line; inputXslt = ""; while ((line = in.readLine()) != null) { inputXslt += line; } in.close(); if (LOG.isDebugEnabled()) { LOG.debug("Put xslt in cache : " + cacheKey); LOG.debug("inputXslt : " + inputXslt); } Element cacheElement = new Element(cacheKey, inputXslt); cache.put(cacheElement); } else { if (LOG.isDebugEnabled()) { LOG.debug("xslt already in cache : " + cacheKey); } inputXslt = (String) element.getObjectValue(); LOG.debug("inputXslt : " + inputXslt); } return inputXslt; }
From source file:Characters.java
/** * Returns the declared encoding string from the XML resource at the given URL. * //from ww w . j av a 2 s .c o m * @param url the resource to look at * @return the declared encoding * @throws IOException if there was a problem reading the input stream */ public static String getDeclaredXMLEncoding(URL url) throws IOException { // Look at the input stream using the platform default encoding. InputStream stream = url.openStream(); BufferedReader buffReader = new BufferedReader(new InputStreamReader(stream)); // Read the first line. May throw an IOException. String firstLine = buffReader.readLine(); if (firstLine == null) { return SYSTEM_ENCODING; } // Look for the XML processing instruction. int piStart = firstLine.indexOf("<?xml version=\"1.0\""); if (piStart != -1) { int attributeStart = firstLine.indexOf("encoding=\""); if (attributeStart >= 0) { int nextQuote = firstLine.indexOf('"', attributeStart + 10); if (nextQuote >= 0) { String encoding = firstLine.substring(attributeStart + 10, nextQuote); return encoding.trim(); } } } stream.close(); // If encoding was unspecified, return the system encoding. return SYSTEM_ENCODING; }
From source file:de.cosmocode.palava.core.Palava.java
/** * Creates a new {@link Framework} using a properties url. * <p>/*from ww w . j ava2 s .c om*/ * The loaded properties will be bound using the {@link Settings} annotation. * </p> * * @since 2.4 * @param url the url pointing to the properties file * @return a new configured {@link Framework} instance * @throws NullPointerException if url is null * @throws IllegalArgumentException if reading from the specified url failed * @throws ConfigurationException if guice configuration failed * @throws ProvisionException if providing an instance during creation failed */ public static Framework newFramework(URL url) { Preconditions.checkNotNull(url, "URL"); final InputStream stream; try { stream = url.openStream(); } catch (IOException e) { throw new IllegalArgumentException(e); } try { final Properties properties = new Properties(); properties.load(stream); return newFramework(properties); } catch (IOException e) { throw new IllegalArgumentException(e); } finally { Closeables.closeQuietly(stream); } }