KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Ostermiller > util > NameValuePair


1 /*
2  * Name Value Pair
3  * Copyright (C) 2004 Stephen Ostermiller
4  * http://ostermiller.org/contact.pl?regarding=Java+Utilities
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * See COPYING.TXT for details.
17  */

18
19 package com.Ostermiller.util;
20
21 import java.io.UnsupportedEncodingException JavaDoc;
22 import java.net.URLEncoder JavaDoc;
23
24 /**
25  * Represents a name value pair as would be used as a CGI parameter.
26  * <p>
27  * More information about this class is available from <a target="_top" HREF=
28  * "http://ostermiller.org/utils/">ostermiller.org</a>.
29  *
30  * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
31  * @since ostermillerutils 1.03.00
32  */

33 public class NameValuePair {
34
35     /**
36      * Name of the pair.
37      */

38     private String JavaDoc name;
39
40     /**
41      * Value of the pair.
42      */

43     private String JavaDoc value;
44
45     /**
46      * Construct a name value pair.
47      *
48      * @param name name of the pair.
49      * @param value value of the pair.
50      *
51      * @since ostermillerutils 1.03.00
52      */

53     public NameValuePair (String JavaDoc name, String JavaDoc value){
54         if (name == null) name = "";
55         if (value == null) value = "";
56         this.name = name;
57         this.value = value;
58     }
59
60     /**
61      * Get the name of the pair.
62      *
63      * @return the name of the pair.
64      *
65      * @since ostermillerutils 1.03.00
66      */

67     public String JavaDoc getName(){
68         return name;
69     }
70
71     /**
72      * Get the value of the pair.
73      *
74      * @return the value of the pair.
75      *
76      * @since ostermillerutils 1.03.00
77      */

78     public String JavaDoc getValue(){
79         return value;
80     }
81
82     /**
83      * Get the name and value as CGI parameters, URL Encoded to UTF-8.
84      *
85      * @return CGI appropriate representation of the pair.
86      *
87      * @since ostermillerutils 1.03.00
88      */

89     public String JavaDoc toString(){
90         try {
91             return toString("UTF-8");
92         } catch (UnsupportedEncodingException JavaDoc uex){
93             // UTF-8 Should be acceptable
94
throw new RuntimeException JavaDoc(uex);
95         }
96     }
97
98     /**
99      * Get the name and value as CGI parameters, URL Encoded to the given encoding.
100      *
101      * @param charset Character set to use when URL Encoding.
102      * @return CGI appropriate representation of the pair.
103      *
104      * @since ostermillerutils 1.03.00
105      */

106     public String JavaDoc toString(String JavaDoc charset) throws UnsupportedEncodingException JavaDoc {
107         return URLEncoder.encode(name, charset) + "=" + URLEncoder.encode(value, charset);
108     }
109 }
110
Popular Tags