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

com.flowcentraltech.flowcentral.connect.common.util.ConnectStringUtil Maven / Gradle / Ivy

There is a newer version: 3.0.2
Show newest version
/*
 * Copyright 2021-2022 FlowCentral Technologies Limited.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.flowcentraltech.flowcentral.connect.common.util;

import java.text.DecimalFormat;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;

import com.tcdng.unify.common.constants.StandardFormatType;
import com.tcdng.unify.common.util.ParamToken;
import com.tcdng.unify.common.util.StringToken;
import com.tcdng.unify.common.util.StringTokenUtils;

/**
 * Connect string utilities.
 * 
 * @author FlowCentral Technologies Limited
 * @since 1.0
 */
public final class ConnectStringUtil {

    private ConnectStringUtil() {

    }
    
    public static String applyBeanToTemplate(String template, Object bean) throws Exception {
        List _template = StringTokenUtils.breakdownParameterizedString(template);
        return ConnectStringUtil.applyBeanToTemplate(_template, bean);
    }
    
    public static String applyBeanToTemplate(List template, Object bean) throws Exception {
        StringBuilder sb = new StringBuilder();
        if (template != null && !template.isEmpty()) {
            TemplateContext ctx = new TemplateContext();
            for (StringToken token : template) {
                Object val = null;
                switch(token.getType()) {
                    case FORMATTED_PARAM:
                        val = PropertyUtils.getProperty(bean, ((ParamToken) token).getParam());
                        if (val != null) {
                            Format format = ctx.getFormat(((ParamToken) token).getFormatType());
                            val = format.format(val);
                        }
                        break;
                    case GENERATOR_PARAM:
                        break;
                    case NEWLINE:
                        val = "\n";
                        break;
                    case PARAM:
                        val = PropertyUtils.getProperty(bean, ((ParamToken) token).getParam());
                        break;
                    case TEXT:
                        val = token.getToken();
                        break;
                    default:
                        break;                  
                }
                
                if (val != null) {
                    sb.append(val);
                }
            }
        }

        return sb.toString();
    }
    
    private static class TemplateContext {
        
        private Map formats;
        
        public TemplateContext() {
            this.formats = new HashMap();
        }
        
        public Format getFormat(StandardFormatType type) {
            Format format = formats.get(type);
            if (format == null) {
                if (type.isDateType()) {
                    format = new SimpleDateFormat(type.format());
                } else {
                    format = new DecimalFormat(type.format());
                }
                formats.put(type, format);
            }

            return format;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy