Java tutorial
/* * EasyHash - command line utility for creating hashes * Copyright (C) 2013 Alexandru Geana (alegen) * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ package net.alegen.easyhash.utils; import java.io.File; import java.io.FileWriter; import java.io.IOException; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.JDOMException; import org.jdom2.input.SAXBuilder; import org.jdom2.output.Format; import org.jdom2.output.XMLOutputter; public class Settings { private static Settings instance = null; public static Settings getInstance() { if (instance == null) instance = new Settings(); return instance; } private Settings() { } public String algorithm; public boolean echo; public boolean tocb; public boolean caps; public int chain; public int resetTime; public String salt; public void load(String fileName) { try { Element root; root = new SAXBuilder().build(new File(fileName)).getRootElement(); Element elemAlgorithm = root.getChild("algorithm"); if (elemAlgorithm != null) this.algorithm = elemAlgorithm.getText(); else this.algorithm = "md5"; Element elemEcho = root.getChild("echo"); if (elemEcho != null) this.echo = Boolean.parseBoolean(elemEcho.getText()); else this.echo = true; Element elemToClipboard = root.getChild("to-clipboard"); if (elemToClipboard != null) this.tocb = Boolean.parseBoolean(elemToClipboard.getText()); else this.tocb = false; Element elemCaps = root.getChild("caps"); if (elemCaps != null) this.caps = Boolean.parseBoolean(elemCaps.getText()); else this.caps = false; Element elemChain = root.getChild("chain"); if (elemChain != null) this.chain = Integer.parseInt(elemChain.getText()); else this.chain = 1; Element elemResetTime = root.getChild("reset-time"); if (elemResetTime != null) this.resetTime = Integer.parseInt(elemResetTime.getText()); else this.resetTime = 10; Element elemSalt = root.getChild("salt"); if (elemSalt != null) this.salt = elemSalt.getText(); else this.salt = ""; if (this.chain < 1) this.chain = 1; } catch (JDOMException ex) { System.out.println("eh > Settings file contains XML errors. Loading default values."); this.defaults(); } catch (IOException ex) { System.out.println("eh > No settings file has been found. Loading default values."); this.defaults(); } } public void save() { Element elemSettings = new Element("settings"); Document doc = new Document(elemSettings); Element elemAlgorithm = new Element("algorithm"); elemAlgorithm.addContent(this.algorithm); doc.getRootElement().addContent(elemAlgorithm); Element elemEcho = new Element("echo"); elemEcho.addContent(String.valueOf(this.echo)); doc.getRootElement().addContent(elemEcho); Element elemToClipboard = new Element("to-clipboard"); elemToClipboard.addContent(String.valueOf(this.tocb)); doc.getRootElement().addContent(elemToClipboard); Element elemCaps = new Element("caps"); elemCaps.addContent(String.valueOf(this.caps)); doc.getRootElement().addContent(elemCaps); Element elemChain = new Element("chain"); elemChain.addContent(String.valueOf(this.chain)); doc.getRootElement().addContent(elemChain); Element elemResetTime = new Element("reset-time"); elemResetTime.addContent(String.valueOf(this.resetTime)); doc.getRootElement().addContent(elemResetTime); Element elemSalt = new Element("salt"); elemSalt.addContent(this.salt); doc.getRootElement().addContent(elemSalt); XMLOutputter xmlOutput = new XMLOutputter(); xmlOutput.setFormat(Format.getPrettyFormat()); try { xmlOutput.output(doc, new FileWriter("settings.eh.xml")); } catch (IOException ex) { System.out.println("eh > Could not save settings - " + ex.getMessage()); } } private void defaults() { this.algorithm = "md5"; this.echo = true; this.tocb = false; this.caps = false; this.chain = 1; this.resetTime = 10; this.salt = ""; } }