diff --git a/src/com/nikhaldimann/inieditor/IniEditor.java b/src/com/nikhaldimann/inieditor/IniEditor.java index 7b823df..78fe884 100644 --- a/src/com/nikhaldimann/inieditor/IniEditor.java +++ b/src/com/nikhaldimann/inieditor/IniEditor.java @@ -120,6 +120,7 @@ public class IniEditor { private char[] commentDelims; private boolean isCaseSensitive; private OptionFormat optionFormat; + private String iniName = null; /** * Constructs new bare IniEditor instance. @@ -242,6 +243,13 @@ public void setOptionFormatString(String formatString) { this.optionFormat = new OptionFormat(formatString); } + /** + * Returns the name of the INI file if it exists, or null otherwise. + */ + public String getName() { + return iniName; + } + /** * 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 @@ -286,6 +294,19 @@ public Map< String, String > getSectionMap(String section) { return null; } + /** + * Returns a nested map of the entire Ini file for simple reading. + * + * @return HashMap of section/option/value for entire ini file + */ + public Map> getIniMap() { + Map> iniMap = new HashMap>(); + for(String section: sectionNames()) { + iniMap.put(section, getSectionMap(section)); + } + return iniMap; + } + /** * 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 @@ -500,10 +521,9 @@ public void save(OutputStream stream) throws IOException { * @throws IOException at an I/O problem */ public void save(OutputStreamWriter streamWriter) throws IOException { - Iterator it = this.sectionOrder.iterator(); PrintWriter writer = new PrintWriter(streamWriter, true); - while (it.hasNext()) { - Section sect = getSection(it.next()); + for (String sectionName : sectionOrder) { + Section sect = getSection(sectionName); writer.println(sect.header()); sect.save(writer); } @@ -518,6 +538,7 @@ public void save(OutputStreamWriter streamWriter) throws IOException { * @throws IOException at an I/O problem */ public void load(String filename) throws IOException { + iniName = filename; load(new File(filename)); } @@ -530,6 +551,7 @@ public void load(String filename) throws IOException { * @throws IOException at an I/O problem */ public void load(File file) throws IOException { + iniName = file.getName(); InputStream in = new FileInputStream(file); load(in); in.close(); @@ -750,11 +772,9 @@ public void setOptionFormat(OptionFormat format) { */ public List optionNames() { List optNames = new LinkedList(); - Iterator it = this.lines.iterator(); - while (it.hasNext()) { - Line line = it.next(); + for (Line line : this.lines) { if (line instanceof Option) { - optNames.add(((Option)line).name()); + optNames.add(((Option) line).name()); } } return optNames; @@ -950,9 +970,8 @@ else if (delimIndex < 0) { * @throws IOException at an I/O problem */ public void save(PrintWriter writer) throws IOException { - Iterator it = this.lines.iterator(); - while (it.hasNext()) { - writer.println(it.next().toString()); + for (Line line : this.lines) { + writer.println(line.toString()); } if (writer.checkError()) { throw new IOException(); @@ -962,7 +981,7 @@ public void save(PrintWriter writer) throws IOException { /** * Returns an actual Option instance. * - * @param option the name of the option, assumed to be normed already (!) + * @param name the name of the option, assumed to be normed already (!) * @return the requested Option instance * @throws NullPointerException if no option with the specified name exists */ @@ -992,8 +1011,8 @@ private static boolean validName(String name) { if (name.trim().equals("")) { return false; } - for (int i = 0; i < INVALID_NAME_CHARS.length; i++) { - if (name.indexOf(INVALID_NAME_CHARS[i]) >= 0) { + for (char INVALID_NAME_CHAR : INVALID_NAME_CHARS) { + if (name.indexOf(INVALID_NAME_CHAR) >= 0) { return false; } } diff --git a/test/com/nikhaldimann/inieditor/IniEditorTest.java b/test/com/nikhaldimann/inieditor/IniEditorTest.java index 0e9573c..27d0f2b 100644 --- a/test/com/nikhaldimann/inieditor/IniEditorTest.java +++ b/test/com/nikhaldimann/inieditor/IniEditorTest.java @@ -150,6 +150,18 @@ public void testGetSectionMap() { assertEquals(i.getSectionMap("common"), new HashMap()); } + public void testGetIniMap() { + IniEditor i = new IniEditor(); + Map> temp = new HashMap>(); + assertEquals(i.getIniMap(), temp); + i.addSection("test"); + temp.put("test", new HashMap()); + assertEquals(i.getIniMap(), temp); + i.set("test", "hallo", "bike"); + temp.get("test").put("hallo", "bike"); + assertEquals(i.getIniMap(), temp); + } + /** * Setting options with illegal names. */