1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
| public class XmlConfig {
/*##################################################################
* Attributes
###################################################################*/
//Element ROOT of the xml file.
static private Element ROOT = new Element("CONFIGURATION");
//The object Document is the xml file with a ROOT defined previously.
static private Document XML_CONFIG = new Document(ROOT);
//Configuration file path
static String pathConfigFile = "." + File.separator + "config.xml";
//XML Configuration file
static File configFile = new File(pathConfigFile);
//The cipher key allowing the encryption
private static Cipher encrypt_cipher;
//The cipher key allowing the decryption
private static Cipher decrypt_cipher;
private boolean cryptic;
/*##################################################################
* Constructor
###################################################################*/
/**
* Constructor which create the new xml file.
*/
public XmlConfig() {
//Creation of the instance of saxbuilder.
SAXBuilder Builder = new SAXBuilder();
//The parser has to check the DTD defined in the xmlfile. DTD id TO DO !
//NOW DTD IS NOT CHECK ! Put the validation option to true so as to check it.
Builder.setValidation(false);
//Create key to crypt configuration file
createKeys();
//Create a configuration file if not exist.
if (!configFile.exists()) {
createXmlConfig();
}
try {
InputStream inStream = new FileInputStream(pathConfigFile);
inStream = new CipherInputStream(inStream, decrypt_cipher);
//Get the XML configuration file
XML_CONFIG = Builder.build(inStream);
ROOT = XML_CONFIG.getRootElement();
inStream.close();
} catch (JDOMException ex) {
Logger.getLogger(XmlConfig.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(XmlConfig.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
*
*/
public static void createXmlConfig() {
PrintWriter out = null;
try {
out = new PrintWriter(pathConfigFile, "utf-8");
} catch (FileNotFoundException ex) {
Logger.getLogger(XmlConfig.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(XmlConfig.class.getName()).log(Level.SEVERE, null, ex);
}
//Language is initialize to Default: English
out.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
out.println("<CONFIGURATION>");
out.println("\t<LANGUAGE>snec.language.DefaultMessage</LANGUAGE>");
out.println("</CONFIGURATION>");
out.close();
//Open a stream of the config.ini file
OutputStream outStream;
try {
outStream = new FileOutputStream(pathConfigFile);
outStream = new CipherOutputStream(outStream, encrypt_cipher);
outStream.close();
} catch (IOException ex) {
Logger.getLogger(XmlConfig.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Get a TAG value from XML configuration file
* @param key Tag name
* @return value of the Tag you want to get
*/
public static String getConfigValue(String key) {
Element keyTagElement = ROOT.getChild(key);
List<Element> listTagElement = ROOT.getChildren();
//Get value if TAG exist in configuration file or return null
if (listTagElement.contains(keyTagElement)) {
return ROOT.getChild(key).getText();
} else {
return null;
}
}
public static void modifyConfig(String key, String value) {
Element keyElement = new Element(key);
Iterator IteratorElement = ROOT.getChildren().iterator();
int i = 0, index = 0;
boolean found = false;
while (IteratorElement.hasNext() && index == 0) {
Element el = (Element) IteratorElement.next();
if (el.getName().equals(key)) {
IteratorElement.remove();
index = i;
found = true;
}
i++;
}
if (!found) {
index = ROOT.getChildren().size();
}
List<Element> listE = ROOT.getChildren();
listE.add(index++, keyElement);
keyElement.setText(value);
saveXml();
}
/**
* This class function save the Document in a xml file.
*/
public static void saveXml() {
OutputStream outStream;
try {
XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
FileOutputStream fos = new FileOutputStream(pathConfigFile);
out.output(XML_CONFIG, fos);
fos.close();
//Open a stream of the config.ini file
//outStream = new FileOutputStream(pathConfigFile);
//outStream = new CipherOutputStream(outStream, encrypt_cipher);
//outStream.close();
} catch (java.io.IOException e) {
e.printStackTrace();
}
}
private static void createKeys() {
String algo = "AES";
//password = "SneCtEaM15092008"
byte[] password = new byte[]{0x53, 0x6E, 0x65, 0x43, 0x74, 0x45, 0x61, 0x4D, 0x31, 0x35, 0x30, 0x39, 0x32, 0x30, 0x30, 0x38};
Key key = new SecretKeySpec(password, algo);
try {
encrypt_cipher = Cipher.getInstance(algo);
decrypt_cipher = Cipher.getInstance(algo);
encrypt_cipher.init(Cipher.ENCRYPT_MODE, key);
decrypt_cipher.init(Cipher.DECRYPT_MODE, key);
} catch (Exception ex) {
ex.printStackTrace();
}
}
} |