001package ca.uhn.fhir.rest.server.interceptor.s13n.standardizers;
002
003/*-
004 * #%L
005 * HAPI FHIR - Server Framework
006 * %%
007 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
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 org.apache.commons.text.WordUtils;
024
025import java.util.Arrays;
026import java.util.HashSet;
027import java.util.Set;
028
029/**
030 * Standardizes last names by capitalizing all characters following a separators (e.g. -, '), capitalizing "Mac" and "Mc"
031 * prefixes and keeping name particles in lower case.
032 */
033public class LastNameStandardizer extends FirstNameStandardizer {
034
035        private Set<String> myParticles = new HashSet<>(Arrays.asList("van", "der", "ter", "de", "da", "la"));
036        private Set<String> myPrefixes = new HashSet<>(Arrays.asList("mac", "mc"));
037        private Set<String> myPrefixExcludes = new HashSet<>(Arrays.asList("machi"));
038
039        public LastNameStandardizer() {
040                super();
041        }
042
043        protected LastNameStandardizer addDelimiters(String... theDelimiters) {
044                super.addDelimiters(theDelimiters);
045                return this;
046        }
047
048        protected String standardizeNameToken(String theToken) {
049                if (theToken.isEmpty()) {
050                        return theToken;
051                }
052
053                if (myParticles.contains(theToken.toLowerCase())) {
054                        return theToken.toLowerCase();
055                }
056
057                String retVal = super.standardizeNameToken(theToken);
058                return handlePrefix(retVal);
059        }
060
061        protected String handlePrefix(String theToken) {
062                String lowerCaseToken = theToken.toLowerCase();
063                for (String exclude : myPrefixExcludes) {
064                        if (lowerCaseToken.startsWith(exclude)) {
065                                return theToken;
066                        }
067                }
068
069                for (String prefix : myPrefixes) {
070                        if (!lowerCaseToken.startsWith(prefix)) {
071                                continue;
072                        }
073
074                        String capitalizedPrefix = WordUtils.capitalize(prefix);
075                        String capitalizedSuffix = WordUtils.capitalize(lowerCaseToken.replaceFirst(prefix, ""));
076                        return capitalizedPrefix.concat(capitalizedSuffix);
077                }
078                return theToken;
079        }
080
081}