001 package org.util.xml.io; 002 003 import java.io.*; 004 import javax.swing.*; 005 006 import org.util.xml.parse.*; 007 import org.util.xml.element.*; 008 009 public class XMLConfigManager { 010 011 private File config_file_; 012 private TagElement config_element_; 013 014 public XMLConfigManager(String file_name) throws Exception { 015 this(new File(new File(System.getProperty("user.dir")), file_name)); 016 } 017 public XMLConfigManager(File config_file) throws Exception { 018 config_file_ = config_file; 019 initialize(); 020 } 021 public void initialize() throws Exception { 022 if(!config_file_.exists()) { 023 String message = "Config file does not exists.\n\t Do you want to create config file?"; 024 int result = JOptionPane.showConfirmDialog(null, message, "select", JOptionPane.YES_NO_OPTION); 025 if(result == JOptionPane.YES_OPTION) 026 config_file_.createNewFile(); 027 } 028 inputSetting(); 029 } 030 public TagElement getTag(String full_key) { 031 String[] keys = full_key.split("/"); 032 TagElement tmp = getTag(); 033 for(int i=0;i<keys.length;i++) { 034 TagElement ta = null; 035 try{ 036 ta = tmp.getTagChild(keys[i]); 037 }catch(Exception e){} 038 if(ta == null) { 039 Element[] tmp_elements = tmp.getChildren(); 040 if(tmp_elements != null && tmp_elements.length!=0) { 041 Element[] new_elements = new Element[tmp_elements.length+1]; 042 for(int j=0;j<tmp_elements.length;j++) 043 new_elements[j] = tmp_elements[j]; 044 try{ 045 ta = new TagElement(keys[i]); 046 new_elements[new_elements.length-1] = ta; 047 tmp.setChildren(new_elements); 048 }catch(Exception e){ 049 e.printStackTrace(); 050 } 051 } else { 052 try{ 053 ta = new TagElement(keys[i]); 054 tmp.setChildren(ta); 055 }catch(Exception e){ 056 e.printStackTrace(); 057 } 058 } 059 try{ 060 outputSetting(); 061 }catch(Exception e) { 062 e.printStackTrace(); 063 } 064 } 065 tmp = ta; 066 } 067 return tmp; 068 } 069 070 public double getDouble(String full_key, double value) { 071 return Double.parseDouble(getValue(full_key, String.valueOf(value))); 072 } 073 public int getInt(String full_key, int value) { 074 return Integer.parseInt(getValue(full_key, String.valueOf(value))); 075 } 076 public boolean getBoolean(String full_key, boolean value) { 077 return "true".equals(getValue(full_key, (value ? "true" : "false") )); 078 } 079 public String getValue(String full_key, String value) { 080 String[] keys = full_key.split("/"); 081 TagElement tmp = getTag(); 082 for(int i=0;i<keys.length;i++) { 083 TagElement ta = null; 084 try{ 085 ta = tmp.getTagChild(keys[i]); 086 }catch(Exception e){} 087 if(ta == null) { 088 Element[] tmp_elements = tmp.getChildren(); 089 if(tmp_elements != null && tmp_elements.length!=0) { 090 Element[] new_elements = new Element[tmp_elements.length+1]; 091 for(int j=0;j<tmp_elements.length;j++) 092 new_elements[j] = tmp_elements[j]; 093 try{ 094 ta = new TagElement(keys[i]); 095 new_elements[new_elements.length-1] = ta; 096 tmp.setChildren(new_elements); 097 }catch(Exception e){ 098 e.printStackTrace(); 099 } 100 } else { 101 try{ 102 ta = new TagElement(keys[i]); 103 tmp.setChildren(ta); 104 }catch(Exception e){ 105 e.printStackTrace(); 106 } 107 } 108 try{ 109 outputSetting(); 110 }catch(Exception e) { 111 e.printStackTrace(); 112 } 113 } 114 tmp = ta; 115 } 116 String result_value = tmp.getValue(); 117 if(result_value==null || result_value.length()==0) { 118 tmp.setValue(value); 119 try{ 120 outputSetting(); 121 }catch(Exception e) { 122 e.printStackTrace(); 123 } 124 } 125 return tmp.getValue(); 126 } 127 128 public TagElement getTag() { 129 return config_element_; 130 } 131 132 public void inputSetting() throws Exception { 133 if(config_file_!=null && config_file_.exists()) 134 try{ 135 ElementParser parser = new ElementParser(new FileInputStream(config_file_)); 136 parser.parse(); 137 config_element_ = parser.getFirstPlainTagElement(); 138 } catch(Exception e) { 139 e.printStackTrace(); 140 } 141 142 if(config_element_ == null || !config_element_.getKey().equals("config")) { 143 config_element_ = new TagElement("config"); 144 try{ 145 outputSetting(); 146 }catch(Exception e){} 147 } 148 config_element_.setTabText("\t"); 149 } 150 public void outputSetting() throws Exception { 151 if(!config_file_.exists()) return ; 152 String config_text = config_element_.toString(); 153 System.out.println(config_text); 154 BufferedWriter bw = new BufferedWriter(new FileWriter(config_file_)); 155 bw.write(config_text); 156 bw.flush(); 157 bw.close(); 158 } 159 160 }