Map<String, PropertySet> sectionMap = new LinkedHashMap<>(); for (ResourceLocator resource : resources) { IniFile file = IniFile.of(resource.getCharSource()); if (file.contains(CHAIN_SECTION)) { PropertySet chainSection = file.section(CHAIN_SECTION); for (String sectionName : file.asMap().keySet()) { if (!sectionName.equals(CHAIN_SECTION)) { sectionMap.merge(sectionName, file.section(sectionName), PropertySet::overrideWith); return IniFile.of(sectionMap);
@VisibleForTesting static ImmutableMap<String, HolidayCalendar> loadFromIni(String filename) { List<ResourceLocator> resources = ResourceConfig.orderedResources(filename); Map<String, HolidayCalendar> map = new HashMap<>(); for (ResourceLocator resource : resources) { try { IniFile ini = IniFile.of(resource.getCharSource()); for (String sectionName : ini.sections()) { PropertySet section = ini.section(sectionName); HolidayCalendar parsed = parseHolidayCalendar(sectionName, section); map.put(parsed.getName(), parsed); map.putIfAbsent(parsed.getName().toUpperCase(Locale.ENGLISH), parsed); } } catch (RuntimeException ex) { log.log(Level.SEVERE, "Error processing resource as Holiday Calendar INI file: " + resource, ex); return ImmutableMap.of(); } } return ImmutableMap.copyOf(map); }
/** * Obtains an instance, specifying the map of section to properties. * * @param sectionMap the map of sections * @return the INI file */ public static IniFile of(Map<String, PropertySet> sectionMap) { return new IniFile(ImmutableMap.copyOf(sectionMap)); }
private static ImmutableMap<String, ImmutableMap<String, String>> parseExternals(IniFile config) { ImmutableMap.Builder<String, ImmutableMap<String, String>> builder = ImmutableMap.builder(); for (String sectionName : config.sections()) { if (sectionName.startsWith(EXTERNALS_SECTION)) { String group = sectionName.substring(EXTERNALS_SECTION.length()); builder.put(group, config.section(sectionName).asMap()); } } return builder.build(); }
private static ImmutableMap<String, String> parseAlternates(IniFile config) { if (!config.contains(ALTERNATES_SECTION)) { return ImmutableMap.of(); } Map<String, String> alternates = new HashMap<>(); for (Entry<String, String> entry : config.section(ALTERNATES_SECTION).asMap().entrySet()) { alternates.put(entry.getKey(), entry.getValue()); alternates.putIfAbsent(entry.getKey().toUpperCase(Locale.ENGLISH), entry.getValue()); } return ImmutableMap.copyOf(alternates); }
@VisibleForTesting static ImmutableMap<Currency, HolidayCalendarId> loadDefaultsFromIni(String filename) { List<ResourceLocator> resources = ResourceConfig.orderedResources(filename); Map<Currency, HolidayCalendarId> map = new HashMap<>(); for (ResourceLocator resource : resources) { try { IniFile ini = IniFile.of(resource.getCharSource()); PropertySet section = ini.section("defaultByCurrency"); for (String currencyCode : section.keys()) { map.put(Currency.of(currencyCode), HolidayCalendarId.of(section.value(currencyCode))); } } catch (RuntimeException ex) { log.log(Level.SEVERE, "Error processing resource as Holiday Calendar Defaults INI file: " + resource, ex); return ImmutableMap.of(); } } return ImmutableMap.copyOf(map); }
@Test(expectedExceptions = IllegalArgumentException.class) public void test_of_invalid_propertyAtStart() { String invalid = "a = x\n"; IniFile.of(CharSource.wrap(invalid)); }
private static ImmutableMap<String, FloatingRateName> parseIndices(IniFile ini) { HashMap<String, ImmutableFloatingRateName> map = new HashMap<>(); parseSection(ini.section("ibor"), "-", FloatingRateType.IBOR, map); parseFixingDateOffset(ini.section("iborFixingDateOffset"), map); parseSection(ini.section("overnightCompounded"), "", FloatingRateType.OVERNIGHT_COMPOUNDED, map); parseSection(ini.section("overnightAveraged"), "", FloatingRateType.OVERNIGHT_AVERAGED, map); parseSection(ini.section("price"), "", FloatingRateType.PRICE, map); return ImmutableMap.copyOf(map); }
private static ImmutableMap<CurrencyPair, Integer> parsePairs(IniFile ini) { ImmutableMap.Builder<CurrencyPair, Integer> builder = ImmutableMap.builder(); for (Entry<String, PropertySet> entry : ini.asMap().entrySet()) { String pairStr = entry.getKey(); if (CurrencyPair.REGEX_FORMAT.matcher(pairStr).matches()) { CurrencyPair pair = CurrencyPair.parse(pairStr); PropertySet properties = entry.getValue(); Integer rateDigits = Integer.parseInt(properties.value("rateDigits")); builder.put(pair, rateDigits); } } return builder.build(); }
public void test_equalsHashCode() { IniFile a1 = IniFile.of(CharSource.wrap(INI1)); IniFile a2 = IniFile.of(CharSource.wrap(INI1)); IniFile b = IniFile.of(CharSource.wrap(INI2)); assertEquals(a1.equals(a1), true); assertEquals(a1.equals(a2), true); assertEquals(a1.equals(b), false); assertEquals(a1.equals(null), false); assertEquals(a1.equals(ANOTHER_TYPE), false); assertEquals(a1.hashCode(), a2.hashCode()); }
/** * Gets a single section of this INI file. * <p> * This returns the section associated with the specified name. * If the section does not exist an exception is thrown. * * @param name the section name * @return the INI file section * @throws IllegalArgumentException if the section does not exist */ public PropertySet section(String name) { ArgChecker.notNull(name, "name"); if (contains(name) == false) { throw new IllegalArgumentException("Unknown INI file section: " + name); } return sectionMap.get(name); }
/** * Parses the specified source as an INI file. * <p> * This parses the specified character source expecting an INI file format. * The resulting instance can be queried for each section in the file. * <p> * INI files sometimes contain a Unicode Byte Order Mark. * Callers are responsible for handling this, such as by using {@link UnicodeBom}. * * @param source the INI file resource * @return the INI file * @throws UncheckedIOException if an IO exception occurs * @throws IllegalArgumentException if the file cannot be parsed */ public static IniFile of(CharSource source) { ArgChecker.notNull(source, "source"); ImmutableList<String> lines = Unchecked.wrap(() -> source.readLines()); ImmutableMap<String, ImmutableListMultimap<String, String>> parsedIni = parse(lines); ImmutableMap.Builder<String, PropertySet> builder = ImmutableMap.builder(); parsedIni.forEach((sectionName, sectionData) -> builder.put(sectionName, PropertySet.of(sectionData))); return new IniFile(builder.build()); }
@Override public TradeReportTemplate load(IniFile iniFile) { List<TradeReportColumn> reportColumns = new ArrayList<TradeReportColumn>(); for (String columnName : iniFile.sections()) { if (columnName.toLowerCase(Locale.ENGLISH).equals(SETTINGS_SECTION)) { continue; } PropertySet properties = iniFile.section(columnName); reportColumns.add(parseColumn(columnName, properties)); } return TradeReportTemplate.builder() .columns(reportColumns) .build(); }
private static ImmutableList<Pair<Pattern, String>> parseLenientPatterns(IniFile config) { if (!config.contains(LENIENT_PATTERNS_SECTION)) { return ImmutableList.of(); } List<Pair<Pattern, String>> alternates = new ArrayList<>(); for (Entry<String, String> entry : config.section(LENIENT_PATTERNS_SECTION).asMap().entrySet()) { alternates.add(Pair.of(Pattern.compile(entry.getKey(), Pattern.CASE_INSENSITIVE), entry.getValue())); } return ImmutableList.copyOf(alternates); }
public void test_equalsHashCode_section() { IniFile a1 = IniFile.of(CharSource.wrap(INI1)); IniFile a2 = IniFile.of(CharSource.wrap(INI1)); IniFile b = IniFile.of(CharSource.wrap(INI2)); assertEquals(a1.section("name").equals(a1.section("name")), true); assertEquals(a1.section("name").equals(a2.section("name")), true); assertEquals(a1.section("name").equals(b.section("section")), false); assertEquals(a1.section("name").equals(null), false); assertEquals(a1.section("name").equals(ANOTHER_TYPE), false); assertEquals(a1.section("name").hashCode(), a2.section("name").hashCode()); }
@Test(expectedExceptions = IllegalArgumentException.class) public void test_of_invalid_badSection() { String invalid = "" + "[section\n" + "b\n"; IniFile.of(CharSource.wrap(invalid)); }
private static ImmutableMap<Currency, FloatingRateName> parseIborDefaults( IniFile ini, ImmutableMap<String, FloatingRateName> names) { ImmutableMap.Builder<Currency, FloatingRateName> map = ImmutableMap.builder(); PropertySet section = ini.section("currencyDefaultIbor"); for (String key : section.keys()) { FloatingRateName frname = names.get(section.value(key)); if (frname == null) { throw new IllegalArgumentException("Invalid default Ibor index for currency " + key); } map.put(Currency.of(key), frname); } return map.build(); }
private static ImmutableMap<String, Currency> parseCurrencies(IniFile ini, boolean loadHistoric) { ImmutableMap.Builder<String, Currency> builder = ImmutableMap.builder(); for (Entry<String, PropertySet> entry : ini.asMap().entrySet()) { String currencyCode = entry.getKey(); if (currencyCode.length() == 3 && Currency.CODE_MATCHER.matchesAllOf(currencyCode)) { PropertySet properties = entry.getValue(); boolean isHistoric = (properties.keys().contains("historic") && Boolean.parseBoolean(properties.value("historic"))); if (isHistoric == loadHistoric) { int minorUnits = Integer.parseInt(properties.value("minorUnitDigits")); String triangulationCurrency = properties.value("triangulationCurrency"); builder.put(currencyCode, new Currency(currencyCode, minorUnits, triangulationCurrency)); } } } return builder.build(); }
public void test_of_propertyNoEquals() { IniFile test = IniFile.of(CharSource.wrap("[section]\na\n")); Multimap<String, String> keyValues1 = ImmutableListMultimap.of("a", ""); assertEquals(test.asMap(), ImmutableMap.of("section", PropertySet.of(keyValues1))); assertEquals(test.section("section"), PropertySet.of(keyValues1)); assertEquals(test.section("section").contains("a"), true); assertEquals(test.section("section").valueList("a"), ImmutableList.of("")); assertEquals(test.section("section").contains("b"), false); assertEquals(test.section("section").keys(), ImmutableSet.of("a")); assertEquals(test.section("section").asMultimap(), ImmutableListMultimap.of("a", "")); assertEquals(test.toString(), "{section={a=[]}}"); }