/** * Resolves all variables. */ public static void resolveAllVariables(Properties prop) { for (Object o : prop.keySet()) { String key = (String) o; String value = resolveProperty(prop, key); prop.setProperty(key, value); } }
/** * Create properties from the file. * * @param fileName properties file name to load */ public static Properties createFromFile(String fileName) throws IOException { return createFromFile(new File(fileName)); }
/** * Returns String property from a map. If key is not found, or if value is not a String, returns <code>null</code>. * Mimics <code>Property.getProperty</code> but on map. */ public static String getProperty(Map map, String key) { return getProperty(map, key, null); }
@Test void testResolve() throws IOException { Properties p = PropertiesUtil.createFromString( "foo=foo\n" + "boo.foo=*${foo}*\n" + "zoo=${boo.${foo}}"); assertEquals(3, p.size()); assertNull(p.getProperty("xxx")); assertEquals("foo", p.getProperty("foo")); assertEquals("*${foo}*", p.getProperty("boo.foo")); assertNull(resolveProperty(p, "xxx")); assertEquals("foo", resolveProperty(p, "foo")); assertEquals("*foo*", resolveProperty(p, "boo.foo")); assertEquals("*foo*", resolveProperty(p, "zoo")); PropertiesUtil.resolveAllVariables(p); assertEquals(3, p.size()); assertEquals("foo", p.getProperty("foo")); assertEquals("*foo*", p.getProperty("boo.foo")); assertEquals("*foo*", p.getProperty("zoo")); }
@Test void testEscape() throws IOException { Properties p = PropertiesUtil.createFromString( "foo=foo\n" + "boo.foo=*\\\\${foo}*\n" + "zoo=\\\\${boo.\\\\${foo}}\n" + "doo=\\\\\\\\${foo}"); assertEquals(4, p.size()); assertNull(p.getProperty("xxx")); assertEquals("foo", p.getProperty("foo")); assertEquals("*\\${foo}*", p.getProperty("boo.foo")); assertEquals("\\${boo.\\${foo}}", p.getProperty("zoo")); assertEquals("\\\\${foo}", p.getProperty("doo")); assertNull(resolveProperty(p, "xxx")); assertEquals("foo", resolveProperty(p, "foo")); assertEquals("*${foo}*", resolveProperty(p, "boo.foo")); assertEquals("${boo.${foo}}", resolveProperty(p, "zoo")); assertEquals("\\foo", resolveProperty(p, "doo")); }
/** * Writes properties to a file. * * @param p properties to write to file * @param file destination file */ public static void writeToFile(Properties p, File file) throws IOException { writeToFile(p, file, null); }
/** * Create properties from the file. * * @param file properties file to load */ public static Properties createFromFile(File file) throws IOException { Properties prop = new Properties(); loadFromFile(prop, file); return prop; }
/** * Creates properties from string. */ public static Properties createFromString(String data) throws IOException { Properties p = new Properties(); loadFromString(p, data); return p; }
/** * Creates properties from classpath. */ public static Properties createFromClasspath(String... rootTemplate) { Properties p = new Properties(); return loadFromClasspath(p, rootTemplate); }
/** * Writes properties to a file. * * @param p properties to write to file * @param file destination file */ public static void writeToFile(final Properties p, final File file) throws IOException { writeToFile(p, file, null); }
/** * Loads properties from the file. Properties are appended to the existing * properties object. * * @param p properties to fill in * @param fileName properties file name to load */ public static void loadFromFile(Properties p, String fileName) throws IOException { loadFromFile(p, new File(fileName)); }
/** * Creates properties from string. */ public static Properties createFromString(final String data) throws IOException { Properties p = new Properties(); loadFromString(p, data); return p; }
/** * Creates properties from classpath. */ public static Properties createFromClasspath(final String... rootTemplate) { Properties p = new Properties(); return loadFromClasspath(p, rootTemplate); }
/** * Resolves all variables. */ public static void resolveAllVariables(final Properties prop) { for (Object o : prop.keySet()) { String key = (String) o; String value = resolveProperty(prop, key); prop.setProperty(key, value); } }
/** * Writes properties to a file. * * @param p properties to write to file * @param fileName destination file name */ public static void writeToFile(Properties p, String fileName) throws IOException { writeToFile(p, new File(fileName), null); }
/** * Create properties from the file. * * @param fileName properties file name to load */ public static Properties createFromFile(final String fileName) throws IOException { return createFromFile(new File(fileName)); }
public String resolve(String macroName) { return getProperty(map, macroName); } });
/** * Create properties from the file. * * @param file properties file to load */ public static Properties createFromFile(final File file) throws IOException { Properties prop = new Properties(); loadFromFile(prop, file); return prop; }
/** * Creates properties from string. */ public static Properties createFromString(final String data) throws IOException { Properties p = new Properties(); loadFromString(p, data); return p; }
/** * Creates properties from classpath. */ public static Properties createFromClasspath(final String... rootTemplate) { Properties p = new Properties(); return loadFromClasspath(p, rootTemplate); }