// Path: src/main/java/org/centralperf/model/dao/ScriptVariable.java
// @Entity
// public class ScriptVariable {
// 
// 	@Id
// 	@GeneratedValue
// 	private Long id;
// 	
// 	private String name;
// 	private String defaultValue;
// 	private String value;
// 	private String description;
// 	
// 	public String getName() {
// 		return name;
// 	}
// 	public void setName(String name) {
// 		this.name = name;
// 	}
// 	public String getDefaultValue() {
// 		return defaultValue;
// 	}
// 	public void setDefaultValue(String defaultValue) {
// 		this.defaultValue = defaultValue;
// 	}
// 	public String getValue() {
// 		return value;
// 	}
// 	public void setValue(String value) {
// 		this.value = value;
// 	}
// 	public String getDescription() {
// 		return description;
// 	}
// 	public void setDescription(String description) {
// 		this.description = description;
// 	}
// 	
// 	@Override
// 	public ScriptVariable clone() {
// 		ScriptVariable scriptVariable = new ScriptVariable();
// 		scriptVariable.setName(this.getName());
// 		scriptVariable.setValue(this.getValue());
// 		scriptVariable.setDescription(this.getDescription());
// 		scriptVariable.setDefaultValue(this.getDefaultValue());
// 		return scriptVariable;
// 	};
// 	
// 	@Override
// 	public String toString() {
// 		return "ScriptVariable. Name = " + this.getName() 
// 				+ ", value = " + this.getValue()				
// 				+ ", default value = " + this.getDefaultValue() 
// 				+ ", description = " + this.getDescription();
// 	}
// 	
// }
// 
// Path: src/main/java/org/centralperf/model/dao/ScriptVariableSet.java
// @Entity
// public class ScriptVariableSet {
// 
// 	@Id
// 	@GeneratedValue
// 	private Long id;
// 	
// 	private String name;
// 	
// 	@OneToMany(cascade=CascadeType.ALL)
// 	private List<ScriptVariable> scriptVariables = new ArrayList<ScriptVariable>();
// 	
// 	public List<ScriptVariable> getScriptVariables() {
// 		return scriptVariables;
// 	}
// 	public void setScriptVariables(List<ScriptVariable> scriptsVariable) {
// 		this.scriptVariables = scriptsVariable;
// 	}
// 	public String getName() {
// 		return name;
// 	}
// 	public void setName(String name) {
// 		this.name = name;
// 	}
// 	
// 	@Override
// 	public String toString() {
// 		StringBuilder builder = new StringBuilder();
// 		builder.append("Script variable set. Name = " + this.getName() + "\r\n");
// 		builder.append("==================\r\n");
// 		for (ScriptVariable scriptsVariable : this.getScriptVariables()) {
// 			builder.append(scriptsVariable.toString() + "\r\n");
// 		}
// 		builder.append("==================\r\n");
// 		return builder.toString();
// 	}
// 	
// }


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.centralperf.model.dao.ScriptVariable;
import org.centralperf.model.dao.ScriptVariableSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
			log.error("SAX Error on variable extraction:"+e.getMessage(), e);
		} catch (IOException e) {
			log.error("IO Error on variable extraction:"+e.getMessage(), e);
		} catch (XPathExpressionException e) {
			log.error("XPath Error on variable extraction:"+e.getMessage(), e);
		}
		return null;

	}
	
	/**
	 * Parse a JMX file to extract all variables declared in it
	 * @param jmxFile
	 * @return
	 */	
	public static String replaceVariables(String jmxString, List<ScriptVariable> scriptVariables) {
		return replaceVariables(new InputSource(new StringReader(jmxString)), scriptVariables);
	}	
	
	/**
	 * Replaces variables by their new values in JMX File
	 * @param jmxFileContent
	 * @param scriptVariables
	 * @return
	 */
	public static String replaceVariables(InputSource jmxFileContent, List<ScriptVariable> scriptVariables){
		
		Map<String, ScriptVariable> scriptVariableMap = new HashMap<String, ScriptVariable>();
		
		// build a hashtable of scriptVariable
// Path: src/main/java/org/centralperf/model/dao/ScriptVariable.java
// @Entity
// public class ScriptVariable {
// 
// 	@Id
// 	@GeneratedValue
// 	private Long id;
// 	
// 	private String name;
// 	private String defaultValue;
// 	private String value;
// 	private String description;
// 	
// 	public String getName() {
// 		return name;
// 	}
// 	public void setName(String name) {
// 		this.name = name;
// 	}
// 	public String getDefaultValue() {
// 		return defaultValue;
// 	}
// 	public void setDefaultValue(String defaultValue) {
// 		this.defaultValue = defaultValue;
// 	}
// 	public String getValue() {
// 		return value;
// 	}
// 	public void setValue(String value) {
// 		this.value = value;
// 	}
// 	public String getDescription() {
// 		return description;
// 	}
// 	public void setDescription(String description) {
// 		this.description = description;
// 	}
// 	
// 	@Override
// 	public ScriptVariable clone() {
// 		ScriptVariable scriptVariable = new ScriptVariable();
// 		scriptVariable.setName(this.getName());
// 		scriptVariable.setValue(this.getValue());
// 		scriptVariable.setDescription(this.getDescription());
// 		scriptVariable.setDefaultValue(this.getDefaultValue());
// 		return scriptVariable;
// 	};
// 	
// 	@Override
// 	public String toString() {
// 		return "ScriptVariable. Name = " + this.getName() 
// 				+ ", value = " + this.getValue()				
// 				+ ", default value = " + this.getDefaultValue() 
// 				+ ", description = " + this.getDescription();
// 	}
// 	
// }
// 
// Path: src/main/java/org/centralperf/model/dao/ScriptVariableSet.java
// @Entity
// public class ScriptVariableSet {
// 
// 	@Id
// 	@GeneratedValue
// 	private Long id;
// 	
// 	private String name;
// 	
// 	@OneToMany(cascade=CascadeType.ALL)
// 	private List<ScriptVariable> scriptVariables = new ArrayList<ScriptVariable>();
// 	
// 	public List<ScriptVariable> getScriptVariables() {
// 		return scriptVariables;
// 	}
// 	public void setScriptVariables(List<ScriptVariable> scriptsVariable) {
// 		this.scriptVariables = scriptsVariable;
// 	}
// 	public String getName() {
// 		return name;
// 	}
// 	public void setName(String name) {
// 		this.name = name;
// 	}
// 	
// 	@Override
// 	public String toString() {
// 		StringBuilder builder = new StringBuilder();
// 		builder.append("Script variable set. Name = " + this.getName() + "\r\n");
// 		builder.append("==================\r\n");
// 		for (ScriptVariable scriptsVariable : this.getScriptVariables()) {
// 			builder.append(scriptsVariable.toString() + "\r\n");
// 		}
// 		builder.append("==================\r\n");
// 		return builder.toString();
// 	}
// 	
// }


// Path: src/main/java/org/centralperf/sampler/driver/jmeter/helper/JMXScriptVariableExtractor.java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.centralperf.model.dao.ScriptVariable;
import org.centralperf.model.dao.ScriptVariableSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

			log.error("SAX Error on variable extraction:"+e.getMessage(), e);
		} catch (IOException e) {
			log.error("IO Error on variable extraction:"+e.getMessage(), e);
		} catch (XPathExpressionException e) {
			log.error("XPath Error on variable extraction:"+e.getMessage(), e);
		}
		return null;

	}
	
	/**
	 * Parse a JMX file to extract all variables declared in it
	 * @param jmxFile
	 * @return
	 */	
	public static String replaceVariables(String jmxString, List<ScriptVariable> scriptVariables) {
		return replaceVariables(new InputSource(new StringReader(jmxString)), scriptVariables);
	}	
	
	/**
	 * Replaces variables by their new values in JMX File
	 * @param jmxFileContent
	 * @param scriptVariables
	 * @return
	 */
	public static String replaceVariables(InputSource jmxFileContent, List<ScriptVariable> scriptVariables){
		
		Map<String, ScriptVariable> scriptVariableMap = new HashMap<String, ScriptVariable>();
		
		// build a hashtable of scriptVariable
		for(ScriptVariable scriptVariable : scriptVariables){