001 package org.util.xml.io;
002
003 import java.io.*;
004 import java.awt.HeadlessException;
005 import javax.swing.*;
006
007 import org.util.xml.parse.*;
008 import org.util.xml.element.*;
009
010 public class XMLConfigManager {
011
012 private File config_file_;
013 private TagElement config_element_;
014
015 public XMLConfigManager() throws IOException {
016 initialize(false);
017 }
018 public XMLConfigManager(String file_name) throws IOException {
019 this(new File(new File(System.getProperty("user.dir")), file_name));
020 }
021 public XMLConfigManager(File config_file) throws IOException {
022 config_file_ = config_file;
023 initialize(true);
024 }
025 public XMLConfigManager(File config_file, boolean b) throws IOException {
026 config_file_ = config_file;
027 initialize(b);
028 }
029 public void initialize(boolean b) throws IOException {
030 if(config_file_ != null && !config_file_.exists()) {
031 if (b) {
032 try {
033 String message = "Config file does not exists.\n\t Do you want to create config file?";
034 int result = JOptionPane.showConfirmDialog(null, message, "select", JOptionPane.YES_NO_OPTION);
035 if(result == JOptionPane.YES_OPTION) {
036 config_file_.createNewFile();
037 }
038 }
039 catch (HeadlessException e){
040 initialize(false);
041 return;
042 }
043 }
044 else {
045 config_file_.createNewFile();
046 }
047 }
048 inputSetting();
049 }
050 public void initialize() throws Exception {
051 if(!config_file_.exists()) {
052 String message = "Config file does not exists.\n\t Do you want to create config file?";
053 int result = JOptionPane.showConfirmDialog(null, message, "select", JOptionPane.YES_NO_OPTION);
054 if(result == JOptionPane.YES_OPTION) {
055 config_file_.createNewFile();
056 }
057 }
058 inputSetting();
059 }
060 public TagElement getTag(String full_key) {
061 String[] keys = full_key.split("/");
062 TagElement tmp = getTag();
063 for(int i=0;i<keys.length;i++) {
064 TagElement ta = null;
065 try{
066 ta = tmp.getTagChild(keys[i]);
067 }catch(Exception e){}
068 if(ta == null) {
069 Element[] tmp_elements = tmp.getChildren();
070 if(tmp_elements != null && tmp_elements.length!=0) {
071 Element[] new_elements = new Element[tmp_elements.length+1];
072 for(int j=0;j<tmp_elements.length;j++)
073 new_elements[j] = tmp_elements[j];
074 try{
075 ta = new TagElement(keys[i]);
076 new_elements[new_elements.length-1] = ta;
077 tmp.setChildren(new_elements);
078 }catch(Exception e){
079 e.printStackTrace();
080 }
081 } else {
082 try{
083 ta = new TagElement(keys[i]);
084 tmp.setChildren(ta);
085 }catch(Exception e){
086 e.printStackTrace();
087 }
088 }
089 try{
090 outputSetting();
091 }catch(Exception e) {
092 e.printStackTrace();
093 }
094 }
095 tmp = ta;
096 }
097 return tmp;
098 }
099
100 public double getDouble(String full_key, double value) {
101 return Double.parseDouble(getValue(full_key, String.valueOf(value)));
102 }
103 public int getInt(String full_key, int value) {
104 return Integer.parseInt(getValue(full_key, String.valueOf(value)));
105 }
106 public boolean getBoolean(String full_key, boolean value) {
107 return "true".equals(getValue(full_key, (value ? "true" : "false") ));
108 }
109
110 public String getValue(String full_key, String value) {
111 String[] keys = full_key.split("/");
112 TagElement tmp = getTag();
113 for(int i=0;i<keys.length;i++) {
114 TagElement ta = null;
115 try{
116 ta = tmp.getTagChild(keys[i]);
117 }catch(Exception e){}
118 if(ta == null) {
119 Element[] tmp_elements = tmp.getChildren();
120 if(tmp_elements != null && tmp_elements.length!=0) {
121 Element[] new_elements = new Element[tmp_elements.length+1];
122 for(int j=0;j<tmp_elements.length;j++)
123 new_elements[j] = tmp_elements[j];
124 try{
125 ta = new TagElement(keys[i]);
126 new_elements[new_elements.length-1] = ta;
127 tmp.setChildren(new_elements);
128 }catch(Exception e){
129 e.printStackTrace();
130 }
131 } else {
132 try{
133 ta = new TagElement(keys[i]);
134 tmp.setChildren(ta);
135 }catch(Exception e){
136 e.printStackTrace();
137 }
138 }
139 try{
140 outputSetting();
141 }catch(Exception e) {
142 e.printStackTrace();
143 }
144 }
145 tmp = ta;
146 }
147 String result_value = tmp.getValue();
148 if(result_value==null || result_value.length()==0) {
149 tmp.setValue(value);
150 try{
151 outputSetting();
152 }catch(Exception e) {
153 e.printStackTrace();
154 }
155 }
156 return tmp.getValue();
157 }
158
159 public void setValue(String full_key, String value) {
160 String[] keys = full_key.split("/");
161 TagElement tmp = getTag();
162 for(int i=0;i<keys.length;i++) {
163 TagElement ta = null;
164 try{
165 ta = tmp.getTagChild(keys[i]);
166 }catch(Exception e){}
167 if(ta == null) {
168 Element[] tmp_elements = tmp.getChildren();
169 if(tmp_elements != null && tmp_elements.length!=0) {
170 Element[] new_elements = new Element[tmp_elements.length+1];
171 for(int j=0;j<tmp_elements.length;j++)
172 new_elements[j] = tmp_elements[j];
173 try{
174 ta = new TagElement(keys[i]);
175 new_elements[new_elements.length-1] = ta;
176 tmp.setChildren(new_elements);
177 }catch(Exception e){
178 e.printStackTrace();
179 }
180 } else {
181 try{
182 ta = new TagElement(keys[i]);
183 tmp.setChildren(ta);
184 }catch(Exception e){
185 e.printStackTrace();
186 }
187 }
188 try{
189 outputSetting();
190 }catch(Exception e) {
191 e.printStackTrace();
192 }
193 }
194 tmp = ta;
195 }
196 tmp.setValue(value);
197 try{
198 outputSetting();
199 }catch(Exception e) {
200 e.printStackTrace();
201 }
202 }
203
204
205 public TagElement getTag() {
206 return config_element_;
207 }
208
209 public void inputSetting() throws IOException {
210 if(config_file_!=null && config_file_.exists())
211 try{
212 ElementParser parser = new ElementParser(new FileInputStream(config_file_));
213 parser.parse();
214 config_element_ = parser.getFirstPlainTagElement();
215 }
216 catch(IOException e) {
217 e.printStackTrace();
218 }
219 catch(XMLParseException e) {
220 e.printStackTrace();
221 }
222
223 if(config_element_ == null || !config_element_.getKey().equals("config")) {
224 config_element_ = new TagElement("config");
225 try {
226 outputSetting();
227 } catch(IOException e){}
228 }
229 config_element_.setTabText("\t");
230 }
231 public void outputSetting() throws IOException {
232 if(config_file_ == null || !config_file_.exists()) return ;
233 String config_text = config_element_.toString();
234 System.out.println(config_text);
235 BufferedWriter bw = new BufferedWriter(new FileWriter(config_file_));
236 bw.write(config_text);
237 bw.flush();
238 bw.close();
239 }
240 public String toString() {
241 return "file: " + config_file_ + "\n" + config_element_.toString();
242 }
243 }