Java String Split splitParameterString(String theInput, boolean theUnescapeComponents)

Here you can find the source of splitParameterString(String theInput, boolean theUnescapeComponents)

Description

split Parameter String

License

Apache License

Declaration

static List<String> splitParameterString(String theInput, boolean theUnescapeComponents) 

Method Source Code


//package com.java2s;
/*/*from  ww  w.j  a v  a2 s .c om*/
 * #%L
 * HAPI FHIR - Core Library
 * %%
 * Copyright (C) 2014 - 2016 University Health Network
 * %%
 * 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.
 * #L%
 */

import java.util.ArrayList;

import java.util.List;

public class Main {
    static List<String> splitParameterString(String theInput, boolean theUnescapeComponents) {
        return splitParameterString(theInput, ',', theUnescapeComponents);
    }

    static List<String> splitParameterString(String theInput, char theDelimiter, boolean theUnescapeComponents) {
        ArrayList<String> retVal = new ArrayList<String>();
        if (theInput != null) {
            StringBuilder b = new StringBuilder();
            for (int i = 0; i < theInput.length(); i++) {
                char next = theInput.charAt(i);
                if (next == theDelimiter) {
                    if (i == 0) {
                        b.append(next);
                    } else {
                        char prevChar = theInput.charAt(i - 1);
                        if (prevChar == '\\') {
                            b.append(next);
                        } else {
                            if (b.length() > 0) {
                                retVal.add(b.toString());
                                b.setLength(0);
                            }
                        }
                    }
                } else {
                    b.append(next);
                }
            }
            if (b.length() > 0) {
                retVal.add(b.toString());
            }
        }

        if (theUnescapeComponents) {
            for (int i = 0; i < retVal.size(); i++) {
                retVal.set(i, unescape(retVal.get(i)));
            }
        }

        return retVal;
    }

    /**
     * Unescapes a string according to the rules for parameter escaping specified in the <a href="http://www.hl7.org/implement/standards/fhir/search.html#escaping">FHIR Specification Escaping
     * Section</a>
     */
    public static String unescape(String theValue) {
        if (theValue == null) {
            return theValue;
        }
        if (theValue.indexOf('\\') == -1) {
            return theValue;
        }

        StringBuilder b = new StringBuilder();

        for (int i = 0; i < theValue.length(); i++) {
            char next = theValue.charAt(i);
            if (next == '\\') {
                if (i == theValue.length() - 1) {
                    b.append(next);
                } else {
                    switch (theValue.charAt(i + 1)) {
                    case '$':
                    case ',':
                    case '|':
                    case '\\':
                        continue;
                    default:
                        b.append(next);
                    }
                }
            } else {
                b.append(next);
            }
        }

        return b.toString();
    }
}

Related

  1. splitPackageName(String packageName)
  2. splitPackages(String packages)
  3. splitParagraphs(String value)
  4. splitParameters(String parameters)
  5. splitParameters(String parameterString)
  6. splitPartitionColStats(String key)
  7. splitResponses(String text)
  8. splitScopes(String aActivityPath)
  9. splitScriptToQueries(String script)