View Javadoc
1   package ca.uhn.fhir.rest.api;
2   
3   import static org.apache.commons.lang3.StringUtils.isBlank;
4   
5   /*
6    * #%L
7    * HAPI FHIR - Core Library
8    * %%
9    * Copyright (C) 2014 - 2018 University Health Network
10   * %%
11   * Licensed under the Apache License, Version 2.0 (the "License");
12   * you may not use this file except in compliance with the License.
13   * You may obtain a copy of the License at
14   * 
15   *      http://www.apache.org/licenses/LICENSE-2.0
16   * 
17   * Unless required by applicable law or agreed to in writing, software
18   * distributed under the License is distributed on an "AS IS" BASIS,
19   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   * See the License for the specific language governing permissions and
21   * limitations under the License.
22   * #L%
23   */
24  
25  import java.util.ArrayList;
26  import java.util.StringTokenizer;
27  
28  import ca.uhn.fhir.context.FhirContext;
29  import ca.uhn.fhir.model.api.IQueryParameterOr;
30  import ca.uhn.fhir.model.api.IQueryParameterType;
31  
32  public class QualifiedParamList extends ArrayList<String> {
33  
34  	private static final long serialVersionUID = 1L;
35  
36  	private String myQualifier;
37  
38  	public QualifiedParamList() {
39  		super();
40  	}
41  
42  	public QualifiedParamList(int theCapacity) {
43  		super(theCapacity);
44  	}
45  
46  	public QualifiedParamList(IQueryParameterOr<?> theNextOr, FhirContext theContext) {
47  		for (IQueryParameterType next : theNextOr.getValuesAsQueryTokens()) {
48  			if (myQualifier == null) {
49  				myQualifier = next.getQueryParameterQualifier();
50  			}
51  			add(next.getValueAsQueryToken(theContext));
52  		}
53  	}
54  
55  	public String getQualifier() {
56  		return myQualifier;
57  	}
58  
59  	public void setQualifier(String theQualifier) {
60  		myQualifier = theQualifier;
61  	}
62  
63  	public static QualifiedParamList singleton(String theParamValue) {
64  		return singleton(null, theParamValue);
65  	}
66  
67  	public static QualifiedParamList singleton(String theQualifier, String theParamValue) {
68  		QualifiedParamList retVal = new QualifiedParamList(1);
69  		retVal.setQualifier(theQualifier);
70  		retVal.add(theParamValue);
71  		return retVal;
72  	}
73  
74  	public static QualifiedParamList splitQueryStringByCommasIgnoreEscape(String theQualifier, String theParams) {
75  		QualifiedParamList retVal = new QualifiedParamList();
76  		retVal.setQualifier(theQualifier);
77  
78  		StringTokenizer tok = new StringTokenizer(theParams, ",", true);
79  		String prev = null;
80  		while (tok.hasMoreElements()) {
81  			String str = tok.nextToken();
82  			if (isBlank(str)) {
83  				prev = null;
84  				continue;
85  			}
86  			
87  			if (str.equals(",")) {
88  					if (countTrailingSlashes(prev) % 2 == 1) {
89  						int idx = retVal.size() - 1;
90  						String existing = retVal.get(idx);
91  						prev = existing.substring(0, existing.length() - 1) + ',';
92  						retVal.set(idx, prev);
93  					} else {
94  						prev = null;
95  					}
96  				continue;
97  			}
98  
99  			if (prev != null && prev.length() > 0 && prev.charAt(prev.length() - 1) == ',') {
100 				int idx = retVal.size() - 1;
101 				String existing = retVal.get(idx);
102 				prev = existing + str;
103 				retVal.set(idx, prev);
104 			} else {
105 				retVal.add(str);
106 				prev = str;
107 			}
108 
109 		}
110 
111 		return retVal;
112 	}
113 
114 	private static int countTrailingSlashes(String theString) {
115 		if(theString==null) {
116 			return 0;
117 		}
118 		int retVal = 0;
119 		for (int i = theString.length() - 1; i >= 0; i--) {
120 			char nextChar = theString.charAt(i);
121 			if (nextChar != '\\') {
122 				break;
123 			}
124 			retVal++;
125 		}
126 		return retVal;
127 	}
128 
129 }