Last active
August 29, 2015 14:07
-
-
Save ryupold/aa09462832b43c56ef9d to your computer and use it in GitHub Desktop.
static Config class for writing simple xml key value pairs
This file contains 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
public final class Config{ | |
private Config(){} | |
private final static String configPath; | |
private final static File configFile; | |
private static Document document; | |
private static Element doc; | |
static | |
{ | |
configPath = System.getProperty("user.home") + File.pathSeparator +".PROGRAMNAME"+File.pathSeparator+"config.xml"; | |
configFile = new File(configPath); | |
} | |
public static void loadConfig() throws ParserConfigurationException, SAXException, IOException { | |
if (configFile.exists() && configFile.isFile()) { | |
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); | |
document = db.parse(configFile); | |
doc = document.getDocumentElement(); | |
} else if (configFile.exists()) { //config file is not a file, probably a directory | |
configFile.delete(); | |
writeDefaultConfigFile(); | |
} else { //file does not exist | |
configFile.getParentFile().mkdirs(); | |
writeDefaultConfigFile(); | |
} | |
} | |
private static void writeDefaultConfigFile() throws ParserConfigurationException { | |
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); | |
document = db.newDocument(); | |
doc = document.createElement("config"); | |
document.appendChild(doc); | |
Element property = document.createElement("property_1"); | |
property.setTextContent("foo"); | |
doc.appendChild(property); | |
property = document.createElement("property_2"); | |
property.setTextContent("bar"); | |
doc.appendChild(property); | |
property = document.createElement("property_3"); | |
property.setTextContent("qwerty"); | |
doc.appendChild(property); | |
save(); | |
} | |
public static boolean save() { | |
if (document != null) { | |
try { | |
TransformerFactory transformerFactory = TransformerFactory.newInstance(); | |
Transformer transformer = transformerFactory.newTransformer(); | |
DOMSource domSource = new DOMSource(document); | |
StreamResult streamResult = new StreamResult(configFile); | |
transformer.transform(domSource, streamResult); | |
return true; | |
} catch (TransformerConfigurationException ex) { | |
return false; | |
} catch (TransformerException ex) { | |
return false; | |
} | |
} else { | |
return false; | |
} | |
} | |
public static void setProperty(String name, String value) { | |
NodeList nl = doc.getElementsByTagName(name); | |
if (nl.getLength() > 0) { | |
Node ele = nl.item(0); | |
ele.setTextContent(value); | |
} | |
save(); | |
} | |
public static String getProperty(String name) { | |
NodeList nl = doc.getElementsByTagName(name); | |
if (nl.getLength() > 0) { | |
Node ele = nl.item(0); | |
return ele.getTextContent(); | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment