Currently showing src/net/jtank/io/IniSection.java
package net.jtank.io;
import java.util.*;
/**
* Ini reading package.
*
* @author William Denniss
* @version 2.3, 12th October 2003
*/
public class IniSection {
private String title;
private String comment;
private Map attributes;
private Map comments;
/**
* Creates a new section with the given title.
*
* @param title the title of the new section
*/
public IniSection (String title) {
this.title = title;
attributes = new HashMap();
comments = new HashMap();
this.comment = "";
}
/**
* Returns the title of the section.
*
* @return the title of the section.
*/
public String getTitle () {
return title;
}
/**
* Returns the attribute value for the given key
*
* @param key The key to search for
* @return The value associated with the given key or null if the attribute
* does not exist.
*/
public String getAttribute (String key) {
return (String) attributes.get(key);
}
/**
* Returns the comment associated with the attribute
* of the key
*
* @param key The key to search for
* @return the comment associated with this attribute or the empty string "" if
* no comment exists.
*/
public String getAttributeComment (String key) {
String attribComment = (String) comments.get(key);
if (attribComment == null) {
return "";
} else {
return attribComment;
}
}
/**
* Removes the passed attribute from the Section
*
* @param key The key whose attribute to remove.
*/
public void removeAttribute (String key) {
attributes.remove(key);
}
/**
* Removes the passed attribute's comment from the Section
*
* @param key The key whose comment to remove.
*/
public void removeAttributeComment (String key) {
comments.remove(key);
}
/**
* Returns an array of keys whose value match passed
* String.
*
* @param value the value to search for.
* @return an array of keys whose value match passed
* String.
*/
public String [] findKeys (String value) {
List foundKeys = new LinkedList();
Iterator keys = attributes.keySet().iterator();
while (keys.hasNext()) {
String currentKey = (String) keys.next();
if (getAttribute(currentKey).equals(value)) {
foundKeys.add(currentKey);
}
}
return (String[]) foundKeys.toArray(new String[0]);
}
/**
* Returns the attribute value for the given key
* If it doesn't exist the default is returned
*
* @param key The key to search for
* @param default The default value
* @return The value associated with the given key
*/
public String getAttribute (String key, String defaultValue) {
if (attributes.get(key) == null)
return defaultValue;
if (((String) attributes.get(key)).equals(""))
return defaultValue;
return (String) attributes.get(key);
}
/**
* Returns the attribute value (as an int) for the given key
* If it doesn't exist (or is invalid) the default is returned
*
* @param key The key to search for
* @param defaultValue The default value
* @return The value associated with the given key
*/
public int getAttribute (String key, int defaultValue) {
try {
return Integer.parseInt(getAttribute(key, defaultValue + ""));
} catch (NumberFormatException e) {
return defaultValue;
}
}
/**
* Returns the attribute value (as an boolean) for the given key
* If it doesn't exist the default is returned
*
* @param key The key to search for
* @param defaultValue The default value
* @return The value associated with the given key
*/
public boolean getAttribute (String key, boolean defaultValue) {
return getAttribute(key, defaultValue + "").equalsIgnoreCase("true");
}
/**
* Sets the given attribute with a String value
*
* @param the attribute to set.
* @param value the value of the attribute.
*/
public void setAttribute (String key, String value) {
setAttribute(key, value, "");
}
/**
* Sets the given attribute with a String value
*
* @param the attribute to set.
* @param value the value of the attribute.
*/
public void setAttribute (String key, String value, String attribComment) {
attributes.remove(key);
attributes.put(key, value);
if (!attribComment.equals("")) {
comments.remove(key);
comments.put(key, attribComment);
}
}
/**
* Sets the given attribute with a String value
*
* @param the attribute to set.
* @param value the value of the attribute.
*/
public void setAttribute (String key, int value) {
setAttribute(key, value+"");
}
/**
* Returns the Ini String representation of this section.
*
* @return the Ini String representation of this section.
*/
public String toString () {
String toReturn = "";
String [] keys = (String []) attributes.keySet().toArray(new String [0]);
toReturn += comment;
toReturn += "[" + title + "]" + "\n";
for (int i = 0; i < keys.length; i++) {
String attrib = (String) getAttribute(keys[i]);
if (attrib != null) {
toReturn += getAttributeComment(keys[i]);
toReturn += keys[i] + "=" + formatAttribute(attrib) + "\n";
}
}
return toReturn;
}
/**
* Formats an attribute for output
*
* @param attrib the attribute for formatting
* @return the attribute formatted for output
*/
private String formatAttribute(String attrib) {
if (attrib.lastIndexOf("\n") != -1) {
return "{\n" + attrib + "\n}";
} else {
return attrib;
}
}
/**
* Sets the comment for this section.
*
* @param comment the comment of this section
*/
public void setComment (String comment) {
this.comment = comment;
}
/**
* Gets the comment for this section
*
* @return the comment for this section
*/
public String getComment () {
return comment;
}
}
Total 278 Lines of Code.
|
Source code formatted using showsrc by William Denniss
|