@Test public void testTrimTrailingWhitespace() throws Exception { assertEquals(null, StringUtils.trimTrailingWhitespace(null)); assertEquals("", StringUtils.trimTrailingWhitespace("")); assertEquals("", StringUtils.trimTrailingWhitespace(" ")); assertEquals("", StringUtils.trimTrailingWhitespace("\t")); assertEquals("a", StringUtils.trimTrailingWhitespace("a ")); assertEquals(" a", StringUtils.trimTrailingWhitespace(" a")); assertEquals(" a", StringUtils.trimTrailingWhitespace(" a ")); assertEquals(" a b", StringUtils.trimTrailingWhitespace(" a b ")); assertEquals(" a b c", StringUtils.trimTrailingWhitespace(" a b c ")); }
/** * Loads this properties data from this string. * * @param propertiesData The string containing the properties data. * @return The matching properties object. * @throws IOException when the data could not be read. */ public static Properties loadPropertiesFromString(String propertiesData) throws IOException { Properties properties = new Properties(); BufferedReader in = new BufferedReader(new StringReader(propertiesData)); while (true) { String line = in.readLine(); if (line == null) { return properties; } line = StringUtils.trimLeadingWhitespace(line); if (line.length() > 0) { char firstChar = line.charAt(0); if (firstChar != '#' && firstChar != '!') { while (endsWithContinuationMarker(line)) { String nextLine = in.readLine(); line = line.substring(0, line.length() - 1); if (nextLine != null) { line += StringUtils.trimLeadingWhitespace(nextLine); } } int separatorIndex = line.indexOf("="); if (separatorIndex == -1) { separatorIndex = line.indexOf(":"); } String key = (separatorIndex != -1 ? line.substring(0, separatorIndex) : line); String value = (separatorIndex != -1) ? line.substring(separatorIndex + 1) : ""; key = StringUtils.trimTrailingWhitespace(key); value = StringUtils.trimLeadingWhitespace(value); properties.put(unescape(key), unescape(value)); } } } }
/** * load input and close input */ private static Properties load(InputStream in, String exceptionMsg) { Properties prop = new Properties(); try { prop.load(in); Object value; for (Entry<Object, Object> en : prop.entrySet()) { value = en.getValue(); if (value instanceof String) { if (StringUtils.containsWhitespace((CharSequence) value)) { // trim shitespace prop.put(en.getKey(), StringUtils.trimTrailingWhitespace((String) value)); } } } } catch (IOException e) { throw new RuntimeException(exceptionMsg); } finally { try { in.close(); } catch (IOException e) { } } return prop; }