public void parse() throws IOException { File confFile = new File(conf); if (!confFile.exists()) { System.err.println("File does not exist: " + conf); return; } IniSection config = new IniSection(); // So called Latin-1 is the default, but most are UTF-8 config.load(confFile, "UTF-8"); String encoding = config.get("Encoding"); if (encoding == null || "Latin-1".equalsIgnoreCase(encoding)) { config.clear(); config.load(confFile, "WINDOWS-1252"); } SwordBookMetaData.report(config); SwordBookMetaData.normalize(new PrintWriter(System.out), config, CANONICAL_ORDER); }
/** * Replace the value(s) for the key with a new value. * * @param key the key for the section * @param value the value for the key * @return whether the replace happened */ public boolean replace(String key, String value) { if (!allowed(key, value)) { return false; } Collection<String> values = getOrCreateValues(key); values.clear(); return values.add(value); }
/** * Load the conf from a buffer. This is used to load conf entries from the cached mods.d.tar.gz or mods.d.zip file. * * @param buffer * the buffer to load * @throws IOException */ private void loadBuffer(byte[] buffer, Filter<String> keepers) throws IOException { filtered = keepers != null; configAll.clear(); configAll.load(buffer, ENCODING_UTF8, keepers); String encoding = configAll.get(KEY_ENCODING); if (!ENCODING_UTF8.equalsIgnoreCase(encoding)) { configAll.clear(); configAll.load(buffer, ENCODING_LATIN1, keepers); } }
private void adjustName() { // If there is no name then use the initials name if (configAll.get(KEY_DESCRIPTION) == null) { LOGGER.error("Malformed conf file: missing [{}]{}=. Using {}", configAll.getName(), KEY_DESCRIPTION, configAll.getName()); setProperty(KEY_DESCRIPTION, configAll.getName()); } }
String line = advance(bin); if (line == null) { break; if (isSectionLine(line)) { int splitPos = getSplitPos(line); if (splitPos < 0) { warnings.append("Skipping: Expected to see '=' in: ").append(line).append('\n'); String value = more(bin, line.substring(splitPos + 1).trim()); if (filter == null || filter.test(key)) { add(key, value);
writer = new PrintWriter(out); IniSection copy = new IniSection(config); List<String> knownKeys = new ArrayList(copy.getKeys()); writer.print("["); writer.print(copy.getName()); writer.print("]"); writer.println(); if (!copy.containsKey(key)) { continue; Collection<String> values = copy.getValues(key); Iterator<String> iter = values.iterator(); String value; while (keys.hasNext()) { String key = keys.next(); Collection<String> values = copy.getValues(key); Iterator<String> iter = values.iterator(); String value;
public static void report(final IniSection config) { StringBuilder buf = new StringBuilder(config.report()); for (String key : config.getKeys()) { ConfigEntryType type = ConfigEntryType.fromString(key); int count = config.size(key); for (int i = 0; i < count; i++) { String value = config.get(key, i); LOGGER.info("Conf report for [{}]\n{}", config.getName(), buf.toString());
private void mergeConfig(IniSection config) { for (String key : config.getKeys()) { ConfigEntryType type = ConfigEntryType.fromString(key); for (String value : config.getValues(key)) { if (type != null && type.mayRepeat()) { if (!configAll.containsValue(key, value)) { configAll.add(key, value); } } else { setProperty(key, value); } } } }
private static void adjustHistory(IniSection config) { // Iterate over a copy of the keys so that we don't get // a concurrent modification exception when we remove matching keys // and when we add new keys List<String> keys = new ArrayList(config.getKeys()); for (String key : keys) { String value = config.get(key); ConfigEntryType type = ConfigEntryType.fromString(key); if (ConfigEntryType.HISTORY.equals(type)) { config.remove(key); int pos = key.indexOf('_'); value = key.substring(pos + 1) + ' ' + value; config.add(KEY_HISTORY, value); } } }
case FRONTEND: if (this.configFrontend == null) { this.configFrontend = new IniSection(configAll.getName()); case JSWORD: if (this.configJSword == null) { this.configJSword = new IniSection(configAll.getName()); config.replace(key, value); try { config.save(new File(writeLocation, bookConf), getBookCharset()); } catch (IOException ex) { LOGGER.error("Unable to save {}={}: conf file for [{}]; error={}", key, value, configAll.getName(), ex);
/** * Get continuation lines, if any. */ private String more(BufferedReader bin, String value) throws IOException { boolean moreCowBell = false; String line = value; StringBuilder buf = new StringBuilder(); do { moreCowBell = more(line); if (moreCowBell) { line = line.substring(0, line.length() - 1).trim(); } buf.append(line); if (moreCowBell) { buf.append('\n'); line = advance(bin); // Is this new line a potential key line? // It cannot both continue the prior // and also be a key line. int splitPos = getSplitPos(line); if (splitPos >= 0) { warnings.append("Possible trailing continuation on previous line. Found: ").append(line).append('\n'); } } } while (moreCowBell && line != null); String cowBell = buf.toString(); buf = null; line = null; return cowBell; }
private IniSection addConfig(MetaDataLocator locator) { // The write location supersedes the read location File conf = new File(locator.getWriteLocation(), bookConf); if (!conf.exists()) { conf = new File(locator.getReadLocation(), bookConf); } if (conf.exists()) { // The additional confs have the same encoding as the SWORD conf. String encoding = getProperty(KEY_ENCODING); try { IniSection config = new IniSection(); config.load(conf, encoding); mergeConfig(config); return config; } catch (IOException e) { LOGGER.error("Unable to load conf {}:{}", conf, e); } } return null; }
/** * Get the first value for the key specified by the index and the section. * * @param sectionName the name of the section * @param key the key for the section * @return the value at the specified index * @throws ArrayIndexOutOfBoundsException when the index is out of bounds */ public String getValue(String sectionName, String key) { IniSection section = doGetSection(sectionName); return section == null ? null : section.get(key, 0); }
private Element toRow(OSISUtil.OSISFactory factory, String key) { int size = configAll.size(key); if (size == 0) { return null; } // See if it is a predefined type ConfigEntryType type = ConfigEntryType.fromString(key); Element nameEle = toKeyCell(factory, key); Element valueElement = factory.createCell(); for (int j = 0; j < size; j++) { if (j > 0) { valueElement.addContent(factory.createLB()); } String text = configAll.get(key, j); if (type != null && !type.isText() && type.isAllowed(text)) { text = type.convert(text).toString(); } text = XMLUtil.escape(text); if (type != null && type.allowsRTF()) { valueElement.addContent(OSISUtil.rtfToOsis(text)); } else { valueElement.addContent(text); } } // Each key gets one row. Element rowEle = factory.createRow(); rowEle.addContent(nameEle); rowEle.addContent(valueElement); return rowEle; }
/** * Get a section, creating it if necessary. * * @param sectionName * @return the found or created section */ private IniSection getOrCreateSection(final String sectionName) { IniSection section = sectionMap.get(sectionName); if (section == null) { section = new IniSection(sectionName); sectionMap.put(sectionName, section); list.add(sectionName); } return section; }
/** * Add a value for the key. Duplicate values are not allowed. * * @param key the key for the section * @param value the value for the key * @return whether the value was added or is already present. */ public boolean addValue(String key, String value) { IniSection section = getSection(); return section == null || section.add(key, value); }
/** * Load the INI from an InputStream using the given encoding. Filter keys as specified. * * @param is the InputStream to read from * @param encoding the encoding of the file * @param filter the filter, possibly null, for the desired keys * @throws IOException */ public void load(InputStream is, String encoding, Filter<String> filter) throws IOException { Reader in = null; try { in = new InputStreamReader(is, encoding); doLoad(in, filter); } finally { if (in != null) { in.close(); in = null; } } }
@Override public boolean hasFeature(FeatureType feature) { String name = feature.toString(); // Features are a positive statement. // If we find it mentioned anywhere, then it true if (configAll.containsValue(KEY_FEATURE, name)) { return true; } // Many "features" are GlobalOptionFilters, which in the Sword C++ API // indicate a class to use for filtering. // These mostly have the source type prepended to the feature StringBuilder buffer = new StringBuilder(getProperty(KEY_SOURCE_TYPE)); buffer.append(name); if (configAll.containsValue(KEY_GLOBAL_OPTION_FILTER, buffer.toString())) { return true; } // Check for the alias prefixed by the source type String alias = feature.getAlias(); buffer.setLength(0); buffer.append(getProperty(KEY_SOURCE_TYPE)); buffer.append(alias); // But some do not return configAll.containsValue(KEY_GLOBAL_OPTION_FILTER, name) || configAll.containsValue(KEY_GLOBAL_OPTION_FILTER, buffer.toString()); }
writer.print(key); writer.print(" = "); writer.print(format(value)); writer.println();