This project has retired. For details please refer to its Attic page.
ControlParser xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.server.impl.browser;
20  
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import javax.servlet.http.HttpServletRequest;
27  
28  import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
29  
30  /**
31   * Parses HTML form controls.
32   */
33  public class ControlParser {
34  
35      private final HttpServletRequest request;
36  
37      private final Map<String, String> zeroDim = new HashMap<String, String>();
38      private final Map<String, Map<Integer, String>> oneDim = new HashMap<String, Map<Integer, String>>();
39      private final Map<String, Map<Integer, Map<Integer, String>>> twoDim = new HashMap<String, Map<Integer, Map<Integer, String>>>();
40  
41      public ControlParser(HttpServletRequest request) {
42          this.request = request;
43          parse();
44      }
45  
46      @SuppressWarnings("unchecked")
47      private void parse() {
48          // gather all controls
49          Map<String, String[]> controls = request.getParameterMap();
50          for (Map.Entry<String, String[]> control : controls.entrySet()) {
51              String controlName = control.getKey().trim().toLowerCase();
52  
53              int firstIndex = getFirstIndex(controlName);
54  
55              if (firstIndex == -1) {
56                  zeroDim.put(controlName, control.getValue()[0]);
57              } else {
58                  String strippedControlName = controlName.substring(0, controlName.indexOf('['));
59                  int secondIndex = getSecondIndex(controlName);
60  
61                  if (secondIndex == -1) {
62                      Map<Integer, String> values = oneDim.get(strippedControlName);
63                      if (values == null) {
64                          values = new HashMap<Integer, String>();
65                          oneDim.put(strippedControlName, values);
66                      }
67  
68                      values.put(firstIndex, control.getValue()[0]);
69                  } else {
70                      Map<Integer, Map<Integer, String>> values = twoDim.get(strippedControlName);
71                      if (values == null) {
72                          values = new HashMap<Integer, Map<Integer, String>>();
73                          twoDim.put(strippedControlName, values);
74                      }
75  
76                      Map<Integer, String> list = values.get(firstIndex);
77                      if (list == null) {
78                          list = new HashMap<Integer, String>();
79                          values.put(firstIndex, list);
80                      }
81  
82                      list.put(secondIndex, control.getValue()[0]);
83                  }
84              }
85          }
86      }
87  
88      private static int getFirstIndex(String controlName) {
89          int result = -1;
90  
91          int open = controlName.indexOf('[');
92          int close = controlName.indexOf(']');
93  
94          if (open == -1 || close == -1 || close < open) {
95              return result;
96          }
97  
98          String indexStr = controlName.substring(open + 1, close);
99          try {
100             result = Integer.parseInt(indexStr);
101             if (result < 0) {
102                 result = -1;
103             }
104         } catch (NumberFormatException e) {
105         }
106 
107         return result;
108     }
109 
110     private static int getSecondIndex(String controlName) {
111         int result = -1;
112 
113         int open = controlName.indexOf("][");
114         int close = controlName.lastIndexOf(']');
115 
116         if (open == -1 || close == -1 || close < open) {
117             return result;
118         }
119 
120         String indexStr = controlName.substring(open + 2, close);
121         try {
122             result = Integer.parseInt(indexStr);
123             if (result < 0) {
124                 result = -1;
125             }
126         } catch (NumberFormatException e) {
127         }
128 
129         return result;
130     }
131 
132     private static List<String> convertToList(String controlName, Map<Integer, String> map) {
133         if (map == null) {
134             return null;
135         }
136 
137         int count = map.size();
138         List<String> result = new ArrayList<String>(count);
139 
140         for (int i = 0; i < count; i++) {
141             String value = map.get(i);
142             if (value == null) {
143                 throw new CmisInvalidArgumentException(controlName + " has gaps!");
144             }
145             result.add(value);
146         }
147 
148         return result;
149     }
150 
151     public String getValue(String controlName) {
152         if (controlName == null) {
153             throw new IllegalArgumentException("controlName must not be null!");
154         }
155 
156         return zeroDim.get(controlName.toLowerCase());
157     }
158 
159     public List<String> getValues(String controlName) {
160         if (controlName == null) {
161             throw new IllegalArgumentException("controlName must not be null!");
162         }
163 
164         return convertToList(controlName, oneDim.get(controlName.toLowerCase()));
165     }
166 
167     public List<String> getValues(String controlName, int index) {
168         if (controlName == null) {
169             throw new IllegalArgumentException("controlName must not be null!");
170         }
171 
172         Map<Integer, Map<Integer, String>> map = twoDim.get(controlName.toLowerCase());
173         if (map == null) {
174             return null;
175         }
176 
177         return convertToList(controlName, map.get(index));
178     }
179 
180     public Map<Integer, String> getOneDimMap(String controlName) {
181         if (controlName == null) {
182             throw new IllegalArgumentException("controlName must not be null!");
183         }
184 
185         return oneDim.get(controlName.toLowerCase());
186     }
187 
188     public Map<Integer, Map<Integer, String>> getTwoDimMap(String controlName) {
189         if (controlName == null) {
190             throw new IllegalArgumentException("controlName must not be null!");
191         }
192 
193         return twoDim.get(controlName.toLowerCase());
194     }
195 }