Created
January 12, 2012 04:49
-
-
Save Vineeth-Mohan/1598844 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.algoTree.Utils.Helper; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.util.Enumeration; | |
import java.util.HashMap; | |
import java.util.Iterator; | |
import java.util.Map; | |
import java.util.Properties; | |
import org.apache.log4j.Level; | |
import org.apache.log4j.Logger; | |
/** | |
* The Class PropertyTemplate. | |
* | |
* @author vineethmohan | |
*/ | |
public class PropertyTemplate { | |
/** The properties. */ | |
Map<String,Map<String,String>> properties=null; | |
/** The logger. */ | |
Logger logger=Logger.getLogger(PropertyTemplate.class.getName()); | |
public Map<String,Map<String,String>> templatePropertyHmapCreation(String templateFileName,String propertyFileName) throws Exception | |
{ | |
//TODO - logger | |
properties=new HashMap<String,Map<String,String>>(); | |
HashMap<String,String> hMap=null; | |
//to check whether the property file path is file or directory | |
File propertyFile=new File(propertyFileName); | |
// logger.info("Final Output "); | |
logger.info("Template File Path "+templateFileName); | |
logger.info("Property File Path "+propertyFileName); | |
if(propertyFile.isFile()) | |
{ | |
// TODO - give info if its file or folder in logger | |
//to get property file name | |
String[] propertyFileNameArray=propertyFileName.split("/"); | |
String tmpFileNam=null; | |
tmpFileNam=propertyFileNameArray[propertyFileNameArray.length-1]; | |
String fileNam=tmpFileNam.substring(0,tmpFileNam.indexOf(".")); | |
logger.info("File Name "+fileNam); | |
hmap=generateHmap(templateFileName,propertyFileName); | |
properties.put(fileNam,hmap); | |
} | |
else if(propertyFile.isDirectory()) | |
{ | |
File propertyFolder= new File(propertyFileName); | |
File[] propertyFileArray=propertyFolder.listFiles(); | |
if(propertyFileArray.length>0) | |
{ | |
//iteration for property file | |
for (int i=0;i<propertyFileArray.length;i++) | |
{ | |
String propertyFileNam=propertyFileArray[i].getPath(); | |
//to get property file name | |
String tmpFileNam=propertyFileArray[i].getName(); | |
//System.out.println("file name"+tmpFileNam); | |
String fileNam=tmpFileNam.substring(0,tmpFileNam.indexOf(".")); | |
logger.info("File name"+fileNam); | |
// | |
hmap=generateHmap(templateFileName,propertyFileNam); | |
properties.put(fileNam,hmap); | |
} | |
} | |
else | |
{ // TODO - Throw exception | |
logger.error("Property File is Empty"); | |
} | |
} | |
return properties; | |
} | |
public HashMap<String,String> generateHmap(String templateFilePath,String propertyFilePath) throws IOException | |
{ | |
HashMap<String,String> hmap =null; | |
logger.info("Path---------->"+propertyFilePath); | |
File propertyFile=new File(propertyFilePath); | |
FileInputStream propertyFileInputStream=new FileInputStream(propertyFile); | |
Properties propertyFileProperties=new Properties(); | |
propertyFileProperties.load(propertyFileInputStream); | |
Enumeration<?> propertyEnum; | |
propertyEnum=propertyFileProperties.propertyNames(); | |
hmap=new HashMap<String,String>(); | |
File templateFile= new File(templateFilePath); | |
FileInputStream templateFileInputStream=new FileInputStream(templateFile); | |
Properties templateFileProperties=new Properties(); | |
templateFileProperties.load(templateFileInputStream); | |
Enumeration<?> templateEnum; | |
templateEnum=templateFileProperties.propertyNames(); | |
while(templateEnum.hasMoreElements()) | |
{ | |
String templateKey=(String)templateEnum.nextElement(); | |
if(propertyFileProperties.containsKey(templateKey)) | |
{ | |
String finalMapValue=""; | |
String propertyValue=null; | |
propertyValue=templateFileProperties.getProperty(templateKey);//template | |
String propvalue2=null; | |
propvalue2=propertyFileProperties.getProperty(templateKey);//property file | |
finalMapValue=propvalue2; | |
if(propertyValue.equals("")) | |
{ | |
finalMapValue=propvalue2; | |
} | |
if(propvalue2.equals("")) | |
{ | |
finalMapValue=propertyValue; | |
} | |
hmap.put(templateKey,finalMapValue); | |
}//if close | |
else | |
{ | |
hmap.put(templateKey,templateFileProperties.getProperty(templateKey)); | |
} | |
//travel through property file fully | |
while(propertyEnum.hasMoreElements()) | |
{ | |
String propertyKey=(String)propertyEnum.nextElement(); | |
hmap.put(propertyKey,propertyFileProperties.getProperty(propertyKey)); | |
} | |
//end | |
}//while end | |
propertyFileInputStream.close(); | |
templateFileInputStream.close(); | |
return hmap; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment