View Javadoc
1   /********************************************************************************
2    * $Header: /cvsroot/sinon/sinon/src/java/eteg/util/HTTPQueryStringParser.java,v 1.1.1.1 2005/06/17 14:10:29 thiagohp Exp $
3    * $Revision: 1.1.1.1 $
4    * $Date: 2005/06/17 14:10:29 $
5    * =============================================================================
6    * (c) ETEG Internet Ltda - Todos os direitos reservados
7    *
8    * A utilização deste código-fonte está regida por acordos contratuais. Sua
9    * distribuição é proibida sem a autorização do proprietário, cabendo aos
10   * infratores as penalidades previstas na lei.
11   *
12   * Exceto nos casos explícitos pela lei ou acertados em contrato, este SOFTWARE
13   * é provido "COMO É" ("AS IS"), não incluindo nenhum tipo de garantia expressa
14   * ou implícita, incluindo, sem limitações, garantias de comercialização,
15   * continuidade do SOFTWARE, que o mesmo esteja livre de erros, que os defeitos
16   * serão corrigidos ou materiais de terceiros acessados através do SOFTWARE,
17   * estarão livres de vírus ou de outros componentes prejudiciais, adequação a
18   * um propósito específico, titularidade e não violação de direitos autorais.
19   *
20   ******************************************************************************/
21  /*
22   * Arquivo criado em 14/09/2004 às 21:43:22.
23   */
24  
25  package eteg.util;
26  
27  import java.io.UnsupportedEncodingException;
28  import java.net.URLDecoder;
29  import java.util.Enumeration;
30  import java.util.Properties;
31  import java.util.StringTokenizer;
32  
33  /***
34   * Class that provides some methods related to HTTP query string parsing. 
35   *
36   *
37   * @author <a href="mailto:thiagohp at eteg.com.br">Thiago H. de Paula Figueiredo</a>
38   * @author Última modificação feita por $Author: thiagohp $
39   * @version $Revision: 1.1.1.1 $
40   * @since 0.0
41   */
42  final public class HTTPQueryStringParser {
43  
44      public static void print(String queryString) {
45          print(queryString, "ISO_8859_1");
46      }
47  
48      public static void printAsParameterTags(String queryString) {
49          printAsParameteryTags(queryString, "ISO_8859_1");
50      }
51  
52      public static void printAsParameteryTags(String queryString, String encoding) {
53  
54          Properties properties = parse(queryString, encoding);
55          Enumeration keys = properties.keys();
56  
57          while(keys.hasMoreElements()) {
58  
59              String name = (String) keys.nextElement();
60              String value = properties.getProperty(name);
61  
62              System.out.print("<parameter name=\"");
63              System.out.print(name);
64              System.out.print("\" value=\"");
65              System.out.print(value);
66              System.out.println("\"/>");
67  
68          }
69  
70      }
71  
72      public static void print(String queryString, String encoding) {
73  
74          Properties properties = parse(queryString, encoding);
75          Enumeration keys = properties.keys();
76  
77          while(keys.hasMoreElements()) {
78  
79              String name = (String) keys.nextElement();
80              String value = properties.getProperty(name);
81  
82              System.out.println(name + " : " + value);
83  
84          }
85  
86      }
87  
88      public static Properties parse(String queryString) {
89          return parse(queryString, "ISO_8859_1");
90      }
91  
92      public static Properties parse(String queryString, String encoding) {
93  
94          StringTokenizer firstTokenizer = new StringTokenizer(queryString, "&");
95          Properties properties = new Properties();
96          String name;
97          String value;
98  
99          while (firstTokenizer.hasMoreTokens()) {
100 
101             String string = firstTokenizer.nextToken();
102             StringTokenizer tokenizer = new StringTokenizer(string, "=");
103 
104             while (tokenizer.hasMoreTokens()) {
105 
106                 try {
107 
108                     name = URLDecoder.decode(tokenizer.nextToken(), encoding);
109 
110                     if (tokenizer.hasMoreTokens()) {
111 
112                         value = URLDecoder.decode(
113                                 tokenizer.nextToken(), encoding);
114 
115                     }
116                     else {
117                         value = "";
118                     }
119 
120                     properties.put(name, value);
121 
122                 } catch (UnsupportedEncodingException e) {
123 
124                     throw new IllegalArgumentException(
125                             "Encoding " + encoding + " is not supported.");
126 
127                 }
128 
129             }
130 
131         }
132 
133         return properties;
134 
135     }
136 
137     private HTTPQueryStringParser() {
138     }
139 
140 }