/** * Removes a section if it exists. * * @param name the section's name * @return <code>true</code> if the section actually existed * @throws IllegalArgumentException when trying to remove the common section */ public boolean removeSection(String name) { String normName = normSection(name); if (this.commonName != null && this.commonName.equals(normName)) { throw new IllegalArgumentException("Can't remove common section"); } if (hasSection(normName)) { this.sections.remove(normName); this.sectionOrder.remove(normName); return true; } else { return false; } }
/** * Checks whether an option exists in a given section. Options in the * common section are assumed to not exist in particular sections, * unless they're overwritten. * * @param section the section's name * @param option the option's name * @return true if the given section has the option */ public boolean hasOption(String section, String option) { return hasSection(section) && getSection(section).hasOption(option); }
/** * Adds a section if it doesn't exist yet. * * @param name the name of the section * @return <code>true</code> if the section didn't already exist * @throws IllegalArgumentException the name is illegal, ie contains one * of the characters '[' and ']' or consists only of white space */ public boolean addSection(String name) { String normName = normSection(name); if (!hasSection(normName)) { // Section constructor might throw IllegalArgumentException Section section = new Section(normName, this.commentDelims, this.isCaseSensitive); section.setOptionFormat(this.optionFormat); this.sections.put(normName, section); this.sectionOrder.add(normName); return true; } else { return false; } }
/** * Adds a blank line to the end of a section. * * @param section the section's name * @throws IniEditor.NoSuchSectionException no section with the given name exists */ public void addBlankLine(String section) { if (hasSection(section)) { getSection(section).addBlankLine(); } else { throw new NoSuchSectionException(section); } }
/** * Removes an option from a section if it exists. Will not remove options * from the common section if it's not directly addressed. * * @param section the section's name * @param option the option's name * @return <code>true</code> if the option was actually removed * @throws IniEditor.NoSuchSectionException no section with the given name exists */ public boolean remove(String section, String option) { if (hasSection(section)) { return getSection(section).remove(option); } else { throw new NoSuchSectionException(section); } }
/** * Returns all option names of a section, not including options from the * common section. * * @param section the section's name * @return list of option names * @throws IniEditor.NoSuchSectionException no section with the given name exists */ public List<String> optionNames(String section) { if (hasSection(section)) { return getSection(section).optionNames(); } else { throw new NoSuchSectionException(section); } }
/** * Adds a comment line to the end of a section. A comment spanning * several lines (ie with line breaks) will be split up, one comment * line for each line. * * @param section the section's name * @param comment the comment * @throws IniEditor.NoSuchSectionException no section with the given name exists */ public void addComment(String section, String comment) { if (hasSection(section)) { getSection(section).addComment(comment); } else { throw new NoSuchSectionException(section); } }
/** * Sets the value of an option in a section, if the option exist, otherwise * adds the option to the section. Trims white space from the start and the * end of the value and deletes newline characters it might contain. * * @param section the section's name * @param option the option's name * @param value the option's value * @throws IniEditor.NoSuchSectionException no section with the given name exists * @throws IllegalArgumentException the option name is illegal, * ie contains a '=' character or consists only of white space * @throws NullPointerException section or option are <code>null</code> */ public void set(String section, String option, String value) { if (hasSection(section)) { getSection(section).set(option, value); } else { throw new NoSuchSectionException(section); } }
/** * Returns the value of a given option in a given section or null if either * the section or the option don't exist. If a common section was defined * options are also looked up there if they're not present in the specific * section. * * @param section the section's name * @param option the option's name * @return the option's value * @throws NullPointerException any of the arguments is <code>null</code> */ public String get(String section, String option) { if (hasSection(section)) { Section sect = getSection(section); if (sect.hasOption(option)) { return sect.get(option); } if (this.commonName != null) { return getSection(this.commonName).get(option); } } return null; }