Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 32 additions & 13 deletions src/com/nikhaldimann/inieditor/IniEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<String , Map<String, String>> getIniMap() {
Map<String, Map<String, String>> iniMap = new HashMap<String, Map<String, String>>();
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
Expand Down Expand Up @@ -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<String> 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);
}
Expand All @@ -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));
}

Expand All @@ -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();
Expand Down Expand Up @@ -750,11 +772,9 @@ public void setOptionFormat(OptionFormat format) {
*/
public List<String> optionNames() {
List<String> optNames = new LinkedList<String>();
Iterator<Line> 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;
Expand Down Expand Up @@ -950,9 +970,8 @@ else if (delimIndex < 0) {
* @throws IOException at an I/O problem
*/
public void save(PrintWriter writer) throws IOException {
Iterator<Line> 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();
Expand All @@ -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
*/
Expand Down Expand Up @@ -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;
}
}
Expand Down
12 changes: 12 additions & 0 deletions test/com/nikhaldimann/inieditor/IniEditorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,18 @@ public void testGetSectionMap() {
assertEquals(i.getSectionMap("common"), new HashMap<String, String>());
}

public void testGetIniMap() {
IniEditor i = new IniEditor();
Map<String, Map<String, String>> temp = new HashMap<String, Map<String, String>>();
assertEquals(i.getIniMap(), temp);
i.addSection("test");
temp.put("test", new HashMap<String, String>());
assertEquals(i.getIniMap(), temp);
i.set("test", "hallo", "bike");
temp.get("test").put("hallo", "bike");
assertEquals(i.getIniMap(), temp);
}

/**
* Setting options with illegal names.
*/
Expand Down