001/*
002 * Copyright © 2012, 2013, 2014 Royal Botanic Gardens, Kew.
003 *
004 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
005 *
006 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
007 *
008 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
009 */
010package org.kew.rmf.transformers;
011
012import java.util.Locale;
013import java.util.regex.Matcher;
014import java.util.regex.Pattern;
015
016/**
017 * Converts the input to lower case.
018 */
019public class TitleCaseTransformer extends LowerCaseTransformer {
020
021        Locale locale = Locale.ENGLISH;
022
023        Pattern capitalise = Pattern.compile("\\b(\\p{L}\\p{Mn}*)");
024
025        /**
026         * Converts the input to lower case using the defined locale, default is English.
027         * Then capitalises letters following a word boundary.
028         */
029        @Override
030        public String transform(String s) {
031                StringBuilder sb = new StringBuilder(super.transform(s));
032
033                System.out.println("---");
034                Matcher m = capitalise.matcher(s);
035                while (m.find()) {
036                        System.out.println("Found "+m.group(1));
037                        System.out.println("Length "+m.group(1).length());
038                        System.out.println("Start "+m.start());
039                        System.out.println("End "+m.end());
040                        String match = m.group(1);
041                        System.out.println("Replacing «"+sb.substring(m.start(), m.end())+"» with «"+match.toUpperCase(getLocale())+"»");
042                        sb.delete(m.start(), m.end());
043                        sb.insert(m.start(), match.toUpperCase(getLocale()));
044                }
045
046                return sb.toString();
047        }
048}