/* $Id$ */
package org.noreply.fancydress.directory.parser;
import java.util.*;
/**
* Hold entries of a directory section.
*
* This class holds all entries of a directory section. All entries are
* key:value pairs.
*
* The values themselves are not yet parsed, nor is the content
* cryptographically verified.
*
* A DirectoryMessage
is made up of instances of this class.
*
* @see org.noreply.fancydress.directory.Directory
* @see DirectoryParser
* @see DirectoryMessage
* @see DirectoryEntry
*/
public class DirectorySection {
/**
* name of this section
*/
private String name;
/**
* List of entries
*/
private ArrayList entries;
/**
* Default constructor.
*
* Creates a new DirectorySection with the name passed as parameter.
*
* @param name name of this section
*/
public DirectorySection(String name) {
this.name = name;
entries = new ArrayList();
}
/**
* Copy constructor.
*
* Creates a new DirectorySection copying all values (recursively) from
* section
.
*
* @param section section to copy from.
*/
public DirectorySection(DirectorySection section) {
name = new String(section.name);
entries = new ArrayList();
for (Iterator i = section.entries.iterator(); i.hasNext(); )
entries.add(new DirectoryEntry((DirectoryEntry)i.next()));
}
/**
* Add an entry to this section.
*
* @param entry Entry to add. It is used directly, not copied.
*/
public void addEntry(DirectoryEntry entry) {
entries.add(entry);
}
/**
* Blint a value in order to verify or create a signature.
*
* @param entry Entry to blind.
* @return The number of entries blinded.
*/
public int blindValue(String entry) {
int count = 0;
for (Iterator i = entries.iterator(); i.hasNext(); ) {
DirectoryEntry e = (DirectoryEntry) i.next();
if (e.getName().equals(entry)) {
e.blindValue();
count ++;
}
}
return count;
}
/**
* Return the first entry with that name
*
* @param entry Entry to return.
* @return first DirectoryEntry that matches the name or null.
*/
public DirectoryEntry getEntry(String entry) {
int count = 0;
for (Iterator i = entries.iterator(); i.hasNext(); ) {
DirectoryEntry e = (DirectoryEntry) i.next();
if (e.getName().equals(entry))
return e;
};
return null;
}
/**
* Returns a string representation of this section.
*
* @return a string representation of the object.
*/
public String toString() {
StringBuffer strbuf = new StringBuffer();
strbuf.append("[");
strbuf.append(name);
strbuf.append("]\n");
for (Iterator i = entries.iterator(); i.hasNext(); ) {
Object o = i.next();
strbuf.append(o.toString());
strbuf.append("\n");
}
return strbuf.toString();
}
/**
* Returns the name of this section.
*
* @return name of this section.
*/
public String getName() {
return name;
}
}