Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package isjexecact; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.JDOMException; import org.jdom2.input.SAXBuilder; /** * * @author glauber.duma */ public class AppSettings { private static final Map<String, String> setupApp = new HashMap<>(); private static String appFileName; private static boolean caseSensitive; /** * Construtor padro. * */ public void AppSettings() { appFileName = "AppConfig.xml"; caseSensitive = true; } /** * Construtor sobrecarregado. * * @param appfilename Nome do arquivo incluindo o path */ AppSettings(String appfilename) { appFileName = appfilename; caseSensitive = true; } /** * Construtor sobrecarregado. * * @param appfilename Nome do arquivo incluindo o path * @param casesensitive Parmetro que indica se as pesquisas chave sero case sensitive. */ AppSettings(String appfilename, boolean casesensitive) { appFileName = appfilename; caseSensitive = casesensitive; } /** * Carrega os valores do arquivo de configurao para AppSeting ambiente. * * @throws org.jdom2.JDOMException * @throws java.io.IOException */ public void loadDefaultSetings() throws JDOMException, IOException { SAXBuilder builder = new SAXBuilder(); //Document doc = builder.build("c:/TMPGED/teste.xml"); Document doc = builder.build(appFileName); Element xmllei = doc.getRootElement(); List<Element> lista = xmllei.getChildren(); for (Element e : lista) { setValue(e.getAttributeValue("id"), e.getChildText("valor")); // System.out.println("Chave: "+ e.getAttributeValue("id")); // System.out.println("Valor: " + e.getChildText("valor")); } } /** * Atribui um valor para uma chave. * * @param chave Nome da chave. * @param valor Valor a ser atribuido para chave. */ public static void setValue(String chave, String valor) { if (!chave.isEmpty() && !valor.isEmpty()) { setupApp.put(caseSensitive == true ? chave : chave.toUpperCase(), valor); } } /** * Retorna o valor configurado para uma chave. * * @param key Nome da chave a ser pesquisada. * * @return Valor da chave. */ public static String getValue(String key) { String retorno = ""; if (!key.isEmpty()) { if (setupApp.containsKey(caseSensitive == true ? key : key.toUpperCase())) { retorno = setupApp.get(caseSensitive == true ? key : key.toUpperCase()); } } return retorno; } /** * Retorna a quantidade de elementos. * @return Quantidade de elementos */ public static int len() { return setupApp.size(); } /** * Esvazia a pilha do Map. */ public static void clear() { setupApp.clear(); } /** * Remove uma chave da pilha. * @param key Chave a ser removida. */ public static void deleteKey(String key) { if (!key.isEmpty()) { setupApp.remove(caseSensitive == true ? key : key.toUpperCase()); } } }