View Javadoc
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 org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
22  
23  import java.util.ArrayList;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  
28  import javax.servlet.http.HttpServletRequest;
29  
30  
31  /**
32   * Parses HTML form controls.
33   */
34  public class ControlParser
35  {
36      private final HttpServletRequest request;
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      {
43          this.request = request;
44          parse(  );
45      }
46  
47      @SuppressWarnings( "unchecked" )
48      private void parse(  )
49      {
50          // gather all controls
51          Map<String, String[]> controls = request.getParameterMap(  );
52  
53          for ( Map.Entry<String, String[]> control : controls.entrySet(  ) )
54          {
55              String controlName = control.getKey(  ).trim(  ).toLowerCase(  );
56  
57              int firstIndex = getFirstIndex( controlName );
58  
59              if ( firstIndex == -1 )
60              {
61                  zeroDim.put( controlName, control.getValue(  )[0] );
62              }
63              else
64              {
65                  String strippedControlName = controlName.substring( 0, controlName.indexOf( '[' ) );
66                  int secondIndex = getSecondIndex( controlName );
67  
68                  if ( secondIndex == -1 )
69                  {
70                      Map<Integer, String> values = oneDim.get( strippedControlName );
71  
72                      if ( values == null )
73                      {
74                          values = new HashMap<Integer, String>(  );
75                          oneDim.put( strippedControlName, values );
76                      }
77  
78                      values.put( firstIndex, control.getValue(  )[0] );
79                  }
80                  else
81                  {
82                      Map<Integer, Map<Integer, String>> values = twoDim.get( strippedControlName );
83  
84                      if ( values == null )
85                      {
86                          values = new HashMap<Integer, Map<Integer, String>>(  );
87                          twoDim.put( strippedControlName, values );
88                      }
89  
90                      Map<Integer, String> list = values.get( firstIndex );
91  
92                      if ( list == null )
93                      {
94                          list = new HashMap<Integer, String>(  );
95                          values.put( firstIndex, list );
96                      }
97  
98                      list.put( secondIndex, control.getValue(  )[0] );
99                  }
100             }
101         }
102     }
103 
104     private static int getFirstIndex( String controlName )
105     {
106         int result = -1;
107 
108         int open = controlName.indexOf( '[' );
109         int close = controlName.indexOf( ']' );
110 
111         if ( ( open == -1 ) || ( close == -1 ) || ( close < open ) )
112         {
113             return result;
114         }
115 
116         String indexStr = controlName.substring( open + 1, close );
117 
118         try
119         {
120             result = Integer.parseInt( indexStr );
121 
122             if ( result < 0 )
123             {
124                 result = -1;
125             }
126         }
127         catch ( NumberFormatException e )
128         {
129         }
130 
131         return result;
132     }
133 
134     private static int getSecondIndex( String controlName )
135     {
136         int result = -1;
137 
138         int open = controlName.indexOf( "][" );
139         int close = controlName.lastIndexOf( ']' );
140 
141         if ( ( open == -1 ) || ( close == -1 ) || ( close < open ) )
142         {
143             return result;
144         }
145 
146         String indexStr = controlName.substring( open + 2, close );
147 
148         try
149         {
150             result = Integer.parseInt( indexStr );
151 
152             if ( result < 0 )
153             {
154                 result = -1;
155             }
156         }
157         catch ( NumberFormatException e )
158         {
159         }
160 
161         return result;
162     }
163 
164     private static List<String> convertToList( String controlName, Map<Integer, String> map )
165     {
166         if ( map == null )
167         {
168             return null;
169         }
170 
171         int count = map.size(  );
172         List<String> result = new ArrayList<String>( count );
173 
174         for ( int i = 0; i < count; i++ )
175         {
176             String value = map.get( i );
177 
178             if ( value == null )
179             {
180                 throw new CmisInvalidArgumentException( controlName + " has gaps!" );
181             }
182 
183             result.add( value );
184         }
185 
186         return result;
187     }
188 
189     public String getValue( String controlName )
190     {
191         if ( controlName == null )
192         {
193             throw new IllegalArgumentException( "controlName must not be null!" );
194         }
195 
196         return zeroDim.get( controlName.toLowerCase(  ) );
197     }
198 
199     public List<String> getValues( String controlName )
200     {
201         if ( controlName == null )
202         {
203             throw new IllegalArgumentException( "controlName must not be null!" );
204         }
205 
206         return convertToList( controlName, oneDim.get( controlName.toLowerCase(  ) ) );
207     }
208 
209     public List<String> getValues( String controlName, int index )
210     {
211         if ( controlName == null )
212         {
213             throw new IllegalArgumentException( "controlName must not be null!" );
214         }
215 
216         Map<Integer, Map<Integer, String>> map = twoDim.get( controlName.toLowerCase(  ) );
217 
218         if ( map == null )
219         {
220             return null;
221         }
222 
223         return convertToList( controlName, map.get( index ) );
224     }
225 
226     public Map<Integer, String> getOneDimMap( String controlName )
227     {
228         if ( controlName == null )
229         {
230             throw new IllegalArgumentException( "controlName must not be null!" );
231         }
232 
233         return oneDim.get( controlName.toLowerCase(  ) );
234     }
235 
236     public Map<Integer, Map<Integer, String>> getTwoDimMap( String controlName )
237     {
238         if ( controlName == null )
239         {
240             throw new IllegalArgumentException( "controlName must not be null!" );
241         }
242 
243         return twoDim.get( controlName.toLowerCase(  ) );
244     }
245 }