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

com.soento.core.util.PropertyUtilBean Maven / Gradle / Ivy

package com.soento.core.util;

import org.apache.commons.beanutils.PropertyUtilsBean;
import org.apache.commons.beanutils.expression.DefaultResolver;
import org.apache.commons.beanutils.expression.Resolver;
import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author soento
 */
public class PropertyUtilBean extends PropertyUtilsBean {

    public PropertyUtilBean() {
        this.resolverWrapper = new ResolverWrapper();
    }

    @Override
    public ResolverExt getResolver() {
        return this.resolverWrapper;
    }


    private ResolverExt resolverWrapper;


    private class ResolverWrapper implements PropertyUtilBean.ResolverExt {
        final Pattern underLinePattern = Pattern.compile("_[a-z]");
        final Pattern humpPattern = Pattern.compile("[A-Z]");
        private Resolver resolver = new DefaultResolver();

        private ResolverWrapper() {
        }

        @Override
        public int getIndex(String expression) {
            return this.resolver.getIndex(expression);
        }

        @Override
        public String getKey(String expression) {
            return this.resolver.getKey(expression);
        }

        @Override
        public String getProperty(String expression) {
            return this.resolver.getProperty(expression);
        }

        @Override
        public boolean hasNested(String expression) {
            return this.resolver.hasNested(expression);
        }

        @Override
        public boolean isIndexed(String expression) {
            return this.resolver.isIndexed(expression);
        }

        @Override
        public boolean isMapped(String expression) {
            return this.resolver.isMapped(expression);
        }

        @Override
        public String next(String expression) {
            return this.resolver.next(expression);
        }

        @Override
        public String remove(String expression) {
            return this.resolver.remove(expression);
        }

        @Override
        public String getUnderlineProperty(String expression) {
            Matcher matcher = this.humpPattern.matcher(expression);
            if (StringUtils.isNotEmpty(expression)) {
                StringBuffer sb = new StringBuffer();
                while (matcher.find()) {
                    String group = matcher.group();
                    matcher.appendReplacement(sb, group.replace(group, "_" + group.toLowerCase()));
                }
                matcher.appendTail(sb);
                if (sb.length() > 0) {
                    return sb.toString();
                }
            }
            return null;
        }

        @Override
        public String getHumpProperty(String expression) {
            Matcher matcher = this.underLinePattern.matcher(expression);
            if (StringUtils.isNotEmpty(expression)) {
                StringBuffer sb = new StringBuffer();
                while (matcher.find()) {
                    String group = matcher.group();
                    matcher.appendReplacement(sb, group.replace("_", "").toUpperCase());
                }
                matcher.appendTail(sb);
                if (sb.length() > 0) {
                    return sb.toString();
                }
            }
            return null;
        }
    }

    public interface ResolverExt extends Resolver {
        String getUnderlineProperty(String paramString);

        String getHumpProperty(String paramString);
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy