package xml.config.tool;
import java.io.File;
import java.io.IOException;
import xml.config.XMLConfigManager;
import xml.config.exceptions.ConfigTagAlreadyExistsException;
import xml.config.exceptions.EmptyXMLConfigException;
import xml.config.exceptions.NotWellFormedXMLConfigFileException;
import xml.config.exceptions.UnexpectedException;
public class Maker {
private static XMLConfigManager xmlConfigManager = null;
// ** INTERNAL METHODS ** //
// ********************** //
private static boolean checkXMLConfigNotNull(){
if(xmlConfigManager==null)
return false;
if(xmlConfigManager.getXmlConfig().size()<1)
return false;
return true;
}
// ** METHODS ** //
// ************* //
public static void makeExit(){
System.exit(0);
}
public static void makeNewXMLConfig(){
xmlConfigManager = new XMLConfigManager();
System.out.println("New XMLConfig SUCCESSFULLY created :)");
}
public static void makeLoadFromFile(){
try{
xmlConfigManager = XMLConfigManager.fromFile(Asker.askFile());
}catch(IOException ioex){
System.err.println("Could not access to given file :(");
}catch(NotWellFormedXMLConfigFileException nwfxcfex){
System.err.println("Given file was not a well-formed XMLConfig file");
}
}
public static void makeSaveToFile(){
try{
xmlConfigManager.toFile(Asker.askFile());
}catch(EmptyXMLConfigException excex){
System.err.println("You cannot save an empty XMLConfig to a file");
}catch(UnexpectedException uex){
System.err.println("SOMETHING REALLY BAD AND UNEXPECTED OCURRED!");
}
}
public static void makeRemoveFile(){
if(Asker.askFile().delete())
System.out.println("Successfully removed :)");
else
System.err.println("Could not remove :(");
}
public static void makeListKeys(){
if(checkXMLConfigNotNull()){
String list[] = xmlConfigManager.getXmlConfig().list();
for(String str : list){
System.out.println("\t"+str);
}
}
}
public static void makeDeleteElement(){
if(checkXMLConfigNotNull()){
String key = Asker.askElementKey();
xmlConfigManager.getXmlConfig().del(key);
}
}
public static void makeModifyElement(){
if(checkXMLConfigNotNull()){
String key = Asker.askElementKey();
String value = Asker.askValue();
xmlConfigManager.getXmlConfig().set(key,value);
}
}
public static void makeInsertElement(){
if(xmlConfigManager!=null){
String key = Asker.askElementKey();
String value = Asker.askValue();
try{
xmlConfigManager.getXmlConfig().add(key,value);
}catch(ConfigTagAlreadyExistsException ex){
System.err.println("The element you are trying to add already exists.");
}
}
}
public static void makeQueryElement(){
if(checkXMLConfigNotNull()){
String key = Asker.askElementKey();
System.out.println(key+": '"+xmlConfigManager.getXmlConfig().get(key)+"'");
}
}
public static void makeHelp(){
Asker.printOptions();
}
public static void makeClear(){
for(int i = 0 ; i < 100 ; i++){
System.out.println();
}
}
public static void makeInstruction(){
String cmd = Asker.lastInstruction.substring(1);
if(cmd.contains(" "))
cmd = Asker.lastInstruction.substring(1,Asker.lastInstruction.indexOf(" "));
cmd = cmd.toUpperCase();
try{
if(Asker.lastInstruction.length()==2 ||
(Asker.lastInstruction.contains(" ") &&
Asker.lastInstruction.substring(0,Asker.lastInstruction.indexOf(" ")
).length()==2)){
if(Asker.lastInstruction.toUpperCase().charAt(1)=='E')
cmd = Asker.getCMDFromOptions(Asker.OPTION_EXIT);
else if(Asker.lastInstruction.toUpperCase().charAt(1)=='N')
cmd = Asker.getCMDFromOptions(Asker.OPTION_NEW_XMLCONFIG);
else if(Asker.lastInstruction.toUpperCase().charAt(1)=='O')
cmd = Asker.getCMDFromOptions(Asker.OPTION_LOAD_FROM_FILE_XMLCONFIG);
else if(Asker.lastInstruction.toUpperCase().charAt(1)=='S')
cmd = Asker.getCMDFromOptions(Asker.OPTION_SAVE_TO_FILE_XMLCONFIG);
else if(Asker.lastInstruction.toUpperCase().charAt(1)=='R')
cmd = Asker.getCMDFromOptions(Asker.OPTION_REMOVE_XMLCONFIG_FILE);
else if(Asker.lastInstruction.toUpperCase().charAt(1)=='L')
cmd = Asker.getCMDFromOptions(Asker.OPTION_LIST_XMLCONFIG_KEYS);
else if(Asker.lastInstruction.toUpperCase().charAt(1)=='D')
cmd = Asker.getCMDFromOptions(Asker.OPTION_DELETE_XMLCONFIG_ELEMENT);
else if(Asker.lastInstruction.toUpperCase().charAt(1)=='M')
cmd = Asker.getCMDFromOptions(Asker.OPTION_MODIFY_XMLCONFIG_ELEMENT);
else if(Asker.lastInstruction.toUpperCase().charAt(1)=='I')
cmd = Asker.getCMDFromOptions(Asker.OPTION_INSERT_XMLCONFIG_ELEMENT);
else if(Asker.lastInstruction.toUpperCase().charAt(1)=='Q')
cmd = Asker.getCMDFromOptions(Asker.OPTION_QUERY_XMLCONFIG_ELEMENT);
else if(Asker.lastInstruction.toUpperCase().charAt(1)=='H')
cmd = Asker.getCMDFromOptions(Asker.OPTION_HELP);
else if(Asker.lastInstruction.toUpperCase().charAt(1)=='C')
cmd = Asker.getCMDFromOptions(Asker.OPTION_CLEAR);
}
}catch(Exception ex){
}
try{
switch(Asker.getOptionFromCMD(cmd)){
case Asker.OPTION_EXIT:
Maker.makeExit();
case Asker.OPTION_NEW_XMLCONFIG:
Maker.makeNewXMLConfig();
break;
case Asker.OPTION_LOAD_FROM_FILE_XMLCONFIG:
Maker.makeLoadFromFileInstruction(
new File(Asker.lastInstruction.substring(
Asker.lastInstruction.indexOf(" ")+1)));
break;
case Asker.OPTION_SAVE_TO_FILE_XMLCONFIG:
Maker.makeSaveToFileInstruction(
new File(Asker.lastInstruction.substring(
Asker.lastInstruction.indexOf(" ")+1)));
break;
case Asker.OPTION_REMOVE_XMLCONFIG_FILE:
Maker.makeRemoveFileInstruction(
new File(Asker.lastInstruction.substring(
Asker.lastInstruction.indexOf(" ")+1)));
break;
case Asker.OPTION_LIST_XMLCONFIG_KEYS:
Maker.makeListKeys();
break;
case Asker.OPTION_DELETE_XMLCONFIG_ELEMENT:
Maker.makeDeleteElementInstruction(
Asker.lastInstruction.substring(
Asker.lastInstruction.indexOf(" ")+1));
break;
case Asker.OPTION_MODIFY_XMLCONFIG_ELEMENT:
String modifyKey = Asker.lastInstruction.substring(
Asker.lastInstruction.indexOf(" ")+1,
Asker.lastInstruction.indexOf(" ",Asker.lastInstruction.indexOf(" ")+1));
String modifyValue = Asker.lastInstruction.substring(
Asker.lastInstruction.indexOf(" ",
Asker.lastInstruction.indexOf(" ")+1)+1);
Maker.makeModifyElementInstruction(modifyKey, modifyValue);
break;
case Asker.OPTION_INSERT_XMLCONFIG_ELEMENT:
String insertKey = Asker.lastInstruction.substring(
Asker.lastInstruction.indexOf(" ")+1,
Asker.lastInstruction.indexOf(" ",Asker.lastInstruction.indexOf(" ")+1));
String insertValue = Asker.lastInstruction.substring(
Asker.lastInstruction.indexOf(" ",
Asker.lastInstruction.indexOf(" ")+1)+1);
Maker.makeInsertElementInstruction(insertKey, insertValue);
break;
case Asker.OPTION_QUERY_XMLCONFIG_ELEMENT:
Maker.makeQueryElementInstruction(
Asker.lastInstruction.substring(
Asker.lastInstruction.indexOf(" ")+1));
break;
case Asker.OPTION_HELP:
Maker.makeHelp();
break;
case Asker.OPTION_CLEAR:
Maker.makeClear();
break;
}
}catch(Exception ex){
System.err.println("Instruction error :(\n"
+"You probably have a syntax error.\n"
+"Please note that instructions are made for advanced users "
+"and they still experimental yet.\n");
}
}
// ** INSTRUCTION METHODS ** //
// ************************* //
public static void makeLoadFromFileInstruction(File file){
try{
xmlConfigManager = XMLConfigManager.fromFile(file);
}catch(IOException ioex){
System.err.println("Could not access to given file :(");
}catch(NotWellFormedXMLConfigFileException nwfxcfex){
System.err.println("Given file was not a well-formed XMLConfig file");
}
}
public static void makeSaveToFileInstruction(File file){
try{
xmlConfigManager.toFile(file);
}catch(EmptyXMLConfigException excex){
System.err.println("You cannot save an empty XMLConfig to a file");
}catch(UnexpectedException uex){
System.err.println("SOMETHING REALLY BAD AND UNEXPECTED OCURRED!");
}
}
public static void makeRemoveFileInstruction(File file){
if(file.delete())
System.out.println("Successfully removed :)");
else
System.err.println("Could not remove :(");
}
public static void makeDeleteElementInstruction(String key){
if(checkXMLConfigNotNull()){
xmlConfigManager.getXmlConfig().del(key);
}
}
public static void makeModifyElementInstruction(String key, String value){
if(checkXMLConfigNotNull()){
xmlConfigManager.getXmlConfig().set(key,value);
}
}
public static void makeInsertElementInstruction(String key, String value){
if(xmlConfigManager!=null){
try{
xmlConfigManager.getXmlConfig().add(key,value);
}catch(ConfigTagAlreadyExistsException ex){
System.err.println("The element you are trying to add already exists.");
}
}
}
public static void makeQueryElementInstruction(String key){
if(checkXMLConfigNotNull()){
System.out.println(key+": '"+xmlConfigManager.getXmlConfig().get(key)+"'");
}
}
}