com.springer.omelet.data.PropertyMapping.java Source code

Java tutorial

Introduction

Here is the source code for com.springer.omelet.data.PropertyMapping.java

Source

/*******************************************************************************
 *
 *    Copyright 2014 Springer Science+Business Media Deutschland GmbH
 *    
 *    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.springer.omelet.data;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;

import com.springer.omelet.exception.FrameworkException;

/***
 * Load properties file
 * 
 * @author kapilA
 * 
 */
public class PropertyMapping implements IProperty {

    private Properties prop = new Properties();
    private Map<String, String> propertiesValue = Collections.synchronizedMap(new HashMap<String, String>());
    private Set<String> classEnumCheck = new HashSet<String>();
    private FileInputStream fis;
    boolean isEnumMappingChecked;
    private static final Logger LOGGER = Logger.getLogger(PropertyMapping.class);

    public PropertyMapping(Properties prop) {
        this.prop = prop;
        createHashMap(prop);
    }

    @SuppressWarnings("unused")
    private PropertyMapping() {

    }

    private void loadFile(String filePath) {
        try {
            fis = new FileInputStream(new File(filePath));
            prop.load(fis);
            fis.close();
            createHashMap(prop);
        } catch (IOException e) {
            LOGGER.error(e);
        } catch (NullPointerException aE) {
            // TODO: handle exception
            LOGGER.error(aE);
        } finally {
            IOUtils.closeQuietly(fis);
        }
    }

    public PropertyMapping(String filePath) {
        loadFile(filePath);
    }

    public PropertyMapping(String[] filePaths) {
        for (String filePath : filePaths) {
            loadFile(filePath);
        }
    }

    public PropertyMapping(Map<String, String> data) {
        propertiesValue = data;
    }

    /***
     * Return values of the Key
     */
    public <E extends Enum<E>> String getValue(E key) {
        // TODO : check if it is not producing unexpected
        // checkEnumMapping(key);
        String value;
        try {
            value = propertiesValue.get(key.toString());
            if (value == null) {
                throw new NullPointerException();
            }
            return value;
        } catch (NullPointerException e) {
            LOGGER.error(e);
            throw new FrameworkException("Value for key: " + key + " not specified in Data file");
        }
    }

    /***
     * return the value of the key passed for the properties file passed in
     * constructor of the class
     */
    public String getValue(String key) {
        String value;
        try {
            value = propertiesValue.get(key);
            if (value == null) {
                throw new NullPointerException();
            }
            return value;
        } catch (NullPointerException e) {
            LOGGER.error(e);
            throw new FrameworkException("Value for key: " + key + " not specified in Data file");
        }
    }

    /***
     * Create hash map for all the loaded properties file
     * 
     * @param prop
     */
    private void createHashMap(Properties prop) {
        String key;
        Iterator<Object> i = prop.keySet().iterator();
        while (i.hasNext()) {
            key = (String) i.next();
            propertiesValue.put(key, prop.getProperty(key));
        }
    }

    /***
     * Compares the Enum with loaded properties file and prevents Null in Run
     * time
     * 
     * @param key
     */
    @SuppressWarnings("unused")
    private <E extends Enum<E>> void checkEnumMapping(E key) {
        String lineSeprator = System.getProperty("line.separator");
        if (!classEnumCheck.contains(key.getClass().getName())) {
            Set<String> misMatchEnum = new HashSet<String>();
            for (Enum<?> value : key.getClass().getEnumConstants()) {
                if (propertiesValue.get(value.toString()) == null) {
                    misMatchEnum.add("Key with text=" + value.toString() + " in class=" + key.getClass().getName()
                            + "" + " is not present in the loaded  file");
                }
            }
            if (!misMatchEnum.isEmpty()) {
                StringBuilder sb = new StringBuilder();
                Iterator<String> mmI = misMatchEnum.iterator();
                while (mmI.hasNext()) {
                    sb.append(mmI.next());
                    sb.append(lineSeprator);
                }
                throw new FrameworkException(sb.toString());
            }
            classEnumCheck.add(key.getClass().getName());
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("<script language='JavaScript'>");
        sb.append("function change(text){testData.innerHTML=text}");
        sb.append("</script>");

        StringBuilder newSb = new StringBuilder();
        for (String key : propertiesValue.keySet()) {
            newSb.append(key + ":" + propertiesValue.get(key) + "<br>");
        }
        sb.append("<a href='Data' onmouseover=\"javascript:change('" + newSb.toString()
                + "')\" onmouseout=\"javascript:change('')\">Data</a>");
        sb.append("<div id='testData'></div>");
        return sb.toString();
    }
}