001package ca.uhn.fhir.rest.api; 002 003import static org.apache.commons.lang3.StringUtils.isBlank; 004 005/* 006 * #%L 007 * HAPI FHIR - Core Library 008 * %% 009 * Copyright (C) 2014 - 2017 University Health Network 010 * %% 011 * Licensed under the Apache License, Version 2.0 (the "License"); 012 * you may not use this file except in compliance with the License. 013 * You may obtain a copy of the License at 014 * 015 * http://www.apache.org/licenses/LICENSE-2.0 016 * 017 * Unless required by applicable law or agreed to in writing, software 018 * distributed under the License is distributed on an "AS IS" BASIS, 019 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 020 * See the License for the specific language governing permissions and 021 * limitations under the License. 022 * #L% 023 */ 024 025import java.util.ArrayList; 026import java.util.StringTokenizer; 027 028import ca.uhn.fhir.context.FhirContext; 029import ca.uhn.fhir.model.api.IQueryParameterOr; 030import ca.uhn.fhir.model.api.IQueryParameterType; 031 032public class QualifiedParamList extends ArrayList<String> { 033 034 private static final long serialVersionUID = 1L; 035 036 private String myQualifier; 037 038 public QualifiedParamList() { 039 super(); 040 } 041 042 public QualifiedParamList(int theCapacity) { 043 super(theCapacity); 044 } 045 046 public QualifiedParamList(IQueryParameterOr<?> theNextOr, FhirContext theContext) { 047 for (IQueryParameterType next : theNextOr.getValuesAsQueryTokens()) { 048 if (myQualifier == null) { 049 myQualifier = next.getQueryParameterQualifier(); 050 } 051 add(next.getValueAsQueryToken(theContext)); 052 } 053 } 054 055 public String getQualifier() { 056 return myQualifier; 057 } 058 059 public void setQualifier(String theQualifier) { 060 myQualifier = theQualifier; 061 } 062 063 public static QualifiedParamList singleton(String theParamValue) { 064 return singleton(null, theParamValue); 065 } 066 067 public static QualifiedParamList singleton(String theQualifier, String theParamValue) { 068 QualifiedParamList retVal = new QualifiedParamList(1); 069 retVal.setQualifier(theQualifier); 070 retVal.add(theParamValue); 071 return retVal; 072 } 073 074 public static QualifiedParamList splitQueryStringByCommasIgnoreEscape(String theQualifier, String theParams) { 075 QualifiedParamList retVal = new QualifiedParamList(); 076 retVal.setQualifier(theQualifier); 077 078 StringTokenizer tok = new StringTokenizer(theParams, ",", true); 079 String prev = null; 080 while (tok.hasMoreElements()) { 081 String str = tok.nextToken(); 082 if (isBlank(str)) { 083 prev = null; 084 continue; 085 } 086 087 if (str.equals(",")) { 088 if (countTrailingSlashes(prev) % 2 == 1) { 089 int idx = retVal.size() - 1; 090 String existing = retVal.get(idx); 091 prev = existing.substring(0, existing.length() - 1) + ','; 092 retVal.set(idx, prev); 093 } else { 094 prev = null; 095 } 096 continue; 097 } 098 099 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}