summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/certsrv/common/NameValuePairs.java
blob: 678ccfee2e6170d086a26d219c94065c71565efd (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package com.netscape.certsrv.common;


import java.util.Enumeration;
import java.util.Hashtable;
import java.util.StringTokenizer;
import java.util.Vector;


/**
 * A class represents an ordered list of name 
 * value pairs.
 *
 * @version $Revision$, $Date$
 */
public class NameValuePairs {

    private Vector mPairs = new Vector();

    // an index to speed up searching
    // The key is the name.  The element is the NameValuePair.
    private Hashtable index = new Hashtable();

    /**
     * Constructs name value pairs.
     */	 
    public NameValuePairs() {
    }

    /**
     * Adds a name value pair into this set.
     * if the name already exist, the value will
     * be replaced.
     *
     * @param name name
     * @param value value
     */
    public void add(String name, String value) {
        NameValuePair pair = getPair(name);

        if (pair == null) {
            pair = new NameValuePair(name, value);
            mPairs.addElement(pair);
            index.put(name, pair);
        } else {
            pair.setValue(value);
        }
    }

    /**
     * Retrieves name value pair from this set.
     *
     * @param name name
     * @return name value pair
     */
    public NameValuePair getPair(String name) {
        return (NameValuePair) index.get(name);
    }

    /**
     * Returns number of pairs in this set.
     *
     * @return size
     */
    public int size() {
        return mPairs.size();
    }

    /**
     * Retrieves name value pairs in specific position.
     *
     * @param pos position of the value
     * @return name value pair
     */
    public NameValuePair elementAt(int pos) {
        return (NameValuePair) mPairs.elementAt(pos);
    }

    /**
     * Removes all name value pairs in this set.
     */
    public void removeAllPairs() {
        mPairs.removeAllElements();
        index.clear();
    }

    /**
     * Retrieves value of the name value pairs that matches
     * the given name.
     *
     * @param name name
     * @return value
     */
    public String getValue(String name) {
        NameValuePair p = getPair(name);

        if (p != null) {
            return p.getValue();
        }
        return null;
    }

    /**
     * Retrieves a list of names.
     *
     * @return a list of names
     */
    public Enumeration getNames() {
        Vector v = new Vector();
        int size = mPairs.size(); 

        for (int i = 0; i < size; i++) { 
            NameValuePair p = (NameValuePair) mPairs.elementAt(i);

            v.addElement(p.getName());
        }
        //System.out.println("getNames: "+v.size());
        return v.elements();
    }
	
    /**
     * Show the content of this name value container as
     * string representation.
     *
     * @return string representation
     */
    public String toString() {
        StringBuffer buf = new StringBuffer();

        for (int i = 0; i < mPairs.size(); i++) {
            NameValuePair p = (NameValuePair) mPairs.elementAt(i);

            buf.append(p.getName() + "=" + p.getValue());
            buf.append("\n");
        }
        return buf.toString();
    }

    /**
     * Parses a string into name value pairs.
     *
     * @param s string
     * @param nvp name value pairs
     * @return true if successful
     */
    public static boolean parseInto(String s, NameValuePairs nvp) {
        StringTokenizer st = new StringTokenizer(s, "&");

        while (st.hasMoreTokens()) {
            String t = st.nextToken();
            int i = t.indexOf("=");

            if (i == -1) {
                return false;
            }
            String n = t.substring(0, i);
            String v = t.substring(i + 1);

            nvp.add(n, v);
        }	
        return true;
    }

    /**
     * Returns a list of name value pair object.
     *
     * @return name value objects
     */
    public Enumeration elements() {
        return mPairs.elements();
    }
}