diff options
author | Endi Sukma Dewata <edewata@redhat.com> | 2012-03-24 02:08:08 -0500 |
---|---|---|
committer | Matthew Harmsen <mharmsen@redhat.com> | 2012-03-26 15:10:41 -0700 |
commit | 4a7ec07c942544b7ca27718a11dac00505c4de7b (patch) | |
tree | cceeb31a92d7b8b307300305fba77e642b7a6495 /base/console/src/com/netscape/certsrv/common/NameValuePairs.java | |
parent | 007bc68f666ef5658274a1e27989d75f2a681a20 (diff) | |
download | pki-4a7ec07c942544b7ca27718a11dac00505c4de7b.tar.gz pki-4a7ec07c942544b7ca27718a11dac00505c4de7b.tar.xz pki-4a7ec07c942544b7ca27718a11dac00505c4de7b.zip |
Removed unnecessary pki folder.
Previously the source code was located inside a pki folder.
This folder was created during svn migration and is no longer
needed. This folder has now been removed and the contents have
been moved up one level.
Ticket #131
Diffstat (limited to 'base/console/src/com/netscape/certsrv/common/NameValuePairs.java')
-rw-r--r-- | base/console/src/com/netscape/certsrv/common/NameValuePairs.java | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/base/console/src/com/netscape/certsrv/common/NameValuePairs.java b/base/console/src/com/netscape/certsrv/common/NameValuePairs.java new file mode 100644 index 000000000..2dd913ea7 --- /dev/null +++ b/base/console/src/com/netscape/certsrv/common/NameValuePairs.java @@ -0,0 +1,164 @@ +// --- 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.io.*; +import java.util.*; +import java.net.*; +import java.util.*; +import java.text.*; +import java.math.*; + + +/** + * A class represents an ordered list of name + * value pairs. + * + * @author thomask + * @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. + */ + 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. + */ + public NameValuePair getPair(String name) { + return (NameValuePair) index.get(name); + } + + /** + * Returns number of pairs in this set. + */ + public int size() { + return mPairs.size(); + } + + /** + * Retrieves name value pairs in specific position. + */ + 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. + */ + public String getValue(String name) { + NameValuePair p = getPair(name); + + if (p != null) { + return p.getValue(); + } + return null; + } + + /** + * Retrieves 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(); + } + + 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; + } + + public Enumeration elements() { + return mPairs.elements(); + } +} |