001package ca.uhn.fhir.util;
002
003/*-
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2020 University Health Network
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import java.io.CharArrayWriter;
024import java.nio.charset.StandardCharsets;
025import java.text.Normalizer;
026import java.util.Arrays;
027
028public class StringUtil {
029
030        public static String normalizeStringForSearchIndexing(String theString) {
031                if (theString == null) {
032                        return null;
033                }
034
035                CharArrayWriter outBuffer = new CharArrayWriter(theString.length());
036
037                /*
038                 * The following block of code is used to strip out diacritical marks from latin script
039                 * and also convert to upper case. E.g. "j?mes" becomes "JAMES".
040                 *
041                 * See http://www.unicode.org/charts/PDF/U0300.pdf for the logic
042                 * behind stripping 0300-036F
043                 *
044                 * See #454 for an issue where we were completely stripping non latin characters
045                 * See #832 for an issue where we normalize korean characters, which are decomposed
046                 */
047                String string = Normalizer.normalize(theString, Normalizer.Form.NFD);
048                for (int i = 0, n = string.length(); i < n; ++i) {
049                        char c = string.charAt(i);
050                        if (c >= '\u0300' && c <= '\u036F') {
051                                continue;
052                        } else {
053                                outBuffer.append(c);
054                        }
055                }
056
057                return new String(outBuffer.toCharArray()).toUpperCase();
058        }
059
060
061        public static String toUtf8String(byte[] theBytes) {
062                byte[] bytes = theBytes;
063                if (theBytes.length >= 3) {
064                        if (theBytes[0] == -17 && theBytes[1] == -69 && theBytes[2] == -65) {
065                                bytes = Arrays.copyOfRange(theBytes, 3, theBytes.length);
066                        }
067                }
068                return new String(bytes, StandardCharsets.UTF_8);
069        }
070
071}