From 3482dc50a916dd95c7afcd74dd14caba7e57381e Mon Sep 17 00:00:00 2001 From: Luke Date: Thu, 15 Jun 2017 12:55:03 -0400 Subject: [PATCH 1/5] Fixed method documentation --- src/com/nikhaldimann/inieditor/IniEditor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/com/nikhaldimann/inieditor/IniEditor.java b/src/com/nikhaldimann/inieditor/IniEditor.java index 7b823df..48ab8d9 100644 --- a/src/com/nikhaldimann/inieditor/IniEditor.java +++ b/src/com/nikhaldimann/inieditor/IniEditor.java @@ -962,7 +962,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 */ From d8148d307615f74f924b18a338bd122be93f03aa Mon Sep 17 00:00:00 2001 From: Luke Date: Thu, 15 Jun 2017 13:03:40 -0400 Subject: [PATCH 2/5] Updated iterated while and for loops to foreach for readability. --- src/com/nikhaldimann/inieditor/IniEditor.java | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/com/nikhaldimann/inieditor/IniEditor.java b/src/com/nikhaldimann/inieditor/IniEditor.java index 48ab8d9..3afe0c1 100644 --- a/src/com/nikhaldimann/inieditor/IniEditor.java +++ b/src/com/nikhaldimann/inieditor/IniEditor.java @@ -750,11 +750,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 +948,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(); @@ -992,8 +989,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; } } From e268b4e9a95e21870cbb94f2f166c7554c281417 Mon Sep 17 00:00:00 2001 From: Luke Date: Thu, 15 Jun 2017 14:03:52 -0400 Subject: [PATCH 3/5] Updated one more for loop to foreach. --- src/com/nikhaldimann/inieditor/IniEditor.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/com/nikhaldimann/inieditor/IniEditor.java b/src/com/nikhaldimann/inieditor/IniEditor.java index 3afe0c1..80197b7 100644 --- a/src/com/nikhaldimann/inieditor/IniEditor.java +++ b/src/com/nikhaldimann/inieditor/IniEditor.java @@ -500,10 +500,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); } From 3114db48a52c2011b17c32835b815fbd02a83322 Mon Sep 17 00:00:00 2001 From: Luke Date: Fri, 16 Jun 2017 11:16:35 -0400 Subject: [PATCH 4/5] Added a getIniMap() method and test --- src/com/nikhaldimann/inieditor/IniEditor.java | 13 +++++++++++++ test/com/nikhaldimann/inieditor/IniEditorTest.java | 12 ++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/com/nikhaldimann/inieditor/IniEditor.java b/src/com/nikhaldimann/inieditor/IniEditor.java index 80197b7..651a214 100644 --- a/src/com/nikhaldimann/inieditor/IniEditor.java +++ b/src/com/nikhaldimann/inieditor/IniEditor.java @@ -286,6 +286,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 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. */ From 2c568d6ca57271414f2edc19088d70c32fc16e17 Mon Sep 17 00:00:00 2001 From: Luke Date: Fri, 16 Jun 2017 12:51:34 -0400 Subject: [PATCH 5/5] Added getIniName() method to get the name of the INI file if it exists --- src/com/nikhaldimann/inieditor/IniEditor.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/com/nikhaldimann/inieditor/IniEditor.java b/src/com/nikhaldimann/inieditor/IniEditor.java index 651a214..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 @@ -530,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)); } @@ -542,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();