summaryrefslogtreecommitdiffstats
path: root/simple/simple-http/src/main/java/org/simpleframework/http/core/QueryCombiner.java
blob: ed4c92ee943660648114b204aef6a0a3f416ef8b (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
/*
 * QueryCombiner.java May 2003
 *
 * Copyright (C) 2003, Niall Gallagher <niallg@users.sf.net>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing 
 * permissions and limitations under the License.
 */
  
package org.simpleframework.http.core;

import java.util.List;
import java.util.Set;

import org.simpleframework.http.Query;
import org.simpleframework.http.parse.QueryParser;

/**
 * The <code>QueryCombimer</code> is used to parse several strings
 * as a complete URL encoded parameter string. This will do the
 * following concatenations.
 *
 * <pre>
 * null + "a=b&amp;c=d&amp;e=f" = "a=b&amp;c=d&amp;e=f"
 * "a=b" + "e=f&amp;g=h" = "a=b&amp;e=f&amp;g=h";
 * "a=b&amp;c=d&amp;e=f" + "" = "a=b&amp;c=d&amp;e=f"
 * </pre>
 *
 * This ensures that the <code>QueryForm</code> can parse the list
 * of strings as a single URL encoded parameter string. This can
 * parse any number of parameter strings.
 *
 * @author Niall Gallagher
 */
class QueryCombiner extends QueryParser {
   
   /**
    * Constructor that allows a list of string objects to be
    * parsed as a single parameter string. This will check
    * each string to see if it is empty, that is, is either
    * null or the zero length string.
    * 
    * @param list this is a list of query values to be used
    */
   public QueryCombiner(String... list) {
      this.parse(list);
   }

   /**
    * Constructor that allows an array of string objects to 
    * be parsed as a single parameter string. This will check
    * each string to see if it is empty, that is, is either
    * null or the zero length string.
    * 
    * @param query this is the query from the HTTP header     
    * @param list this is the list of strings to be parsed
    */
   public QueryCombiner(Query query, String... list) {
      this.add(query);
      this.parse(list);      
   }
   
   /**
    * Constructor that allows an array of string objects to 
    * be parsed as a single parameter string. This will check
    * each string to see if it is empty, that is, is either
    * null or the zero length string.
    * 
    * @param query this is the query from the HTTP header
    * @param post this is the query from the HTTP post body
    */
   public QueryCombiner(Query query, Query post) {
      this.add(query);
      this.add(post);
   }

   /**
    * This will concatenate the list of parameter strings as a 
    * single parameter string, before handing it to be parsed
    * by the <code>parse(String)</code> method. This method 
    * will ignore any null or zero length strings in the array.    
    *
    * @param list this is the list of strings to be parsed
    */
   public void parse(String[] list) {
      StringBuilder text = new StringBuilder();
      
      for(int i = 0; i < list.length; i++) {
         if(list[i] == null) {
            continue;
         } else if(list[i].length()==0){
            continue;
         } else if(text.length() > 0){
            text.append("&");
         }
         text.append(list[i]);        
      }      
      parse(text);
   }
   
   /**
    * This is used to perform a parse of the form data that is in
    * the provided string builder. This will simply convert the
    * data in to a string and parse it in the normal fashion.
    * 
    * @param text this is the buffer to be converted to a string
    */
   private void parse(StringBuilder text) {
      if(text != null){ 
         ensureCapacity(text.length());
         count = text.length();
         text.getChars(0, count, buf,0);
         parse();
      }
   }   
   
   /**
    * This method is used to insert a collection of tokens into 
    * the parsers map. This is used when another source of tokens
    * is required to populate the connection currently maintained
    * within this parsers internal map. Any tokens that currently
    * exist with similar names will be overwritten by this.
    * 
    * @param query this is the collection of tokens to be added
    */
   private void add(Query query) {
      Set<String> keySet = query.keySet();
      
      for(String key : keySet) {
         List<String> list = query.getAll(key);
         String first = query.get(key);
         
         if(first != null) {
            all.put(key, list);
            map.put(key, first);
         }
      }                 
   }
}