Currently showing src/net/jtank/io/IniSection.java
/*
 * Copyright (c) 2002-2003, William Denniss
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright 
 *       notice, this list of conditions and the following disclaimer in the 
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the Tank Software nor the names of its 
 *       contributors may be used to endorse or promote products derived from 
 *       this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

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();
		
		//Collection values = attributes.values();
		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