summaryrefslogtreecommitdiff
path: root/src/org/noreply/fancydress/directory/parser/DirectoryMessage.java
blob: 81ce6e26b88315084c45dfe5526d16e246e1c4de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package org.noreply.fancydress.directory.parser;

import java.util.*;

/**
 * Hold all sections and entries that are in a directory.
 *
 * This class holds all sections and entries of a directory.  All data
 * are stored in sections, which in turn hold entries of key:value pairs.
 *
 * The values themselves are not yet parsed, nor is the content
 * cryptographically verified.
 *
 * The <code>DirectoryParser</code> returns an object of this class.
 *
 * @see org.noreply.fancydress.directory.Directory
 * @see DirectoryParser
 * @see DirectorySection
 * @see DirectoryEntry
 */
public class DirectoryMessage implements Cloneable {
	/**
	 * List of directory sections
	 */
	private ArrayList sections;

	/**
	 * Default constructor.
	 *
	 * Creates a new DirectoryMessage, adding <code>section</code> to
	 * the section list.
	 *
	 * @param section First section of the directory.  It is used directly, not copied.
	 */
	public DirectoryMessage(DirectorySection section) {
		this();
		sections.add(section);
	}

	/**
	 * Emty constructor.
	 *
	 * Creates a new DirectoryMessage.
	 */
	public DirectoryMessage() {
		sections = new ArrayList();
	}

	/**
	 * Copy constructor.
	 *
	 * Creates a new DirectoryMessage copying all values (recursively) from
	 * <code>message</code>.
	 *
	 * @param message  message to copy from.
	 */
	public DirectoryMessage(DirectoryMessage message) {
		this();
		for (Iterator i = message.sections.iterator(); i.hasNext(); )
			sections.add(new DirectorySection((DirectorySection)i.next()));
	}

	/**
	 * Add a section to this message.
	 *
	 * @param section Section to add.  It is used directly, not copied.
	 */
	public void addSection(DirectorySection section) {
		sections.add(section);
	}

	/**
	 * Blind a value in order to verify or create a signature.
	 *
	 * @param section Section in which to blind the entry named.
	 * @param entry   Entry to blind.
	 * @return        The number of entries blinded.
	 */
	public int blindValue(String section, String entry) {
		int count = 0;
		for (Iterator i = sections.iterator(); i.hasNext(); ) {
			DirectorySection s = (DirectorySection) i.next();
			if (s.getName().equals(section))
				count += s.blindValue(entry);
		};
		return count;
	}

	/**
	 * Return the first section with that name
	 *
	 * @param section Section to return.
	 * @return        first DirectorySection that matches the name or null.
	 */
	public DirectorySection getSection(String section) {
		int count = 0;
		for (Iterator i = sections.iterator(); i.hasNext(); ) {
			DirectorySection s = (DirectorySection) i.next();
			if (s.getName().equals(section))
				return s;
		};
		return null;
	}

	/**
	 * Return an iterator over the sections.
	 *
	 * @return        Iterator over sections.
	 */
	public Iterator getSectionsIterator() {
		return sections.iterator();
	}

	/**
	 * Returns a string representation of the directory.
	 * 
	 * This representation can be parsed again using the directory parser.
	 * 
	 * @return a string representation of the object.
	 */
	public String toString() {
		StringBuffer strbuf = new StringBuffer();
		
		for (Iterator i = sections.iterator(); i.hasNext(); ) {
			Object o = i.next();
			strbuf.append(o.toString());
		}
		return strbuf.toString();
	}
}