All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mitchellbosecke.pebble.extension.core.TitleFilter Maven / Gradle / Ivy

/*******************************************************************************
 * This file is part of Pebble.
 * 
 * Copyright (c) 2014 by Mitchell Bösecke
 * 
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 ******************************************************************************/
package com.mitchellbosecke.pebble.extension.core;

import java.util.List;
import java.util.Map;

import com.mitchellbosecke.pebble.extension.Filter;

public class TitleFilter implements Filter {

    @Override
    public List getArgumentNames() {
        return null;
    }

    @Override
    public Object apply(Object input, Map args) {
        if (input == null) {
            return null;
        }
        String value = (String) input;

        if (value.length() == 0) {
            return value;
        }

        StringBuilder result = new StringBuilder();

        boolean capitalizeNextCharacter = true;

        for (char c : value.toCharArray()) {
            if (Character.isWhitespace(c)) {
                capitalizeNextCharacter = true;
            } else if (capitalizeNextCharacter) {
                c = Character.toTitleCase(c);
                capitalizeNextCharacter = false;
            }
            result.append(c);
        }

        return result.toString();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy