1 22 23 package org.cofax; 24 25 import java.util.*; 26 27 import org.xml.sax.InputSource ; 28 import org.w3c.dom.*; 29 import javax.xml.parsers.*; 30 31 43 44 public final class XMLConfig { 45 46 HashMap config; 48 String xmlFileName; 50 String lastError; 52 private Document doc; 54 57 public XMLConfig() { 58 59 this.config = new HashMap(); 60 61 } 63 66 public String getLastError() { 67 68 return lastError; 69 70 } 71 72 75 public void setXMLFileName(String xmlFileName) { 76 77 this.xmlFileName = "file:///" + xmlFileName.replace('\\', '/'); 78 79 } 81 84 public void empty() { 85 86 config.clear(); 87 88 } 90 93 public void refresh() { 94 95 this.empty(); 96 this.load(); 97 98 } 100 103 public boolean set(String fieldName, Object fieldValue) { 104 105 if (!fieldName.equals("")) { 106 config.put(fieldName, fieldValue); 107 return true; 108 } else 109 return false; 110 111 } 113 116 public Object get(String fieldName) { 117 118 if (config.containsKey(fieldName)) 119 return config.get(fieldName); 120 else 121 return null; 122 123 } 125 128 public String getString(String fieldName) { 129 130 String value = null; 131 132 try { 133 value = config.get(fieldName).toString(); 134 } catch (Exception e) { 135 value = (String ) config.get(fieldName); 136 } 137 138 if (value == null) 139 return ""; 140 else 141 return value; 142 143 } 145 148 public boolean load() { 149 150 NodeList xmlConfigNodeTree; Node docNode; NodeList docNodesChildren; Node docNodeChild; String fieldName = ""; String fieldValue = ""; 157 158 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 159 DocumentBuilder parser; 160 try { 161 parser = factory.newDocumentBuilder(); } catch (Exception e) { 163 this.lastError = "ERROR in XMLConfig: parse error on: " + this.xmlFileName + "\n" + "EXCEPTION: " + e.toString(); 164 return false; 165 } 166 167 int i; 168 Document doc; 170 try { 171 doc = parser.parse(new InputSource (this.xmlFileName)); 172 } catch (Exception e) { 173 this.lastError = "ERROR in XMLConfig: parse error on: " + this.xmlFileName + "\n" + "EXCEPTION: " + e.toString(); 174 return false; 175 } 176 177 try { 178 xmlConfigNodeTree = doc.getElementsByTagName("XMLConfig"); 179 docNode = xmlConfigNodeTree.item(0); 180 docNodesChildren = docNode.getChildNodes(); 181 for (i = 0; i < docNodesChildren.getLength(); i++) { 182 fieldName = ""; 183 fieldValue = ""; 184 docNodeChild = docNodesChildren.item(i); 185 if (docNodeChild.getNodeType() == Node.ELEMENT_NODE) { 186 fieldName = docNodeChild.getNodeName(); 187 188 NodeList fieldChildren = docNodeChild.getChildNodes(); 189 190 if (fieldChildren != null && fieldChildren.getLength() > 0) { 192 193 fieldValue = docNodeChild.getFirstChild().getNodeValue(); 194 if (!fieldName.equals("")) { 195 this.set(fieldName, fieldValue); 196 } 197 } 198 199 } 200 } 201 } catch (Exception e) { 202 this.lastError = "ERROR in XMLConfig : failed on: " + this.xmlFileName + " XML TAG: " + fieldName + " VALUE: " + fieldValue + "\n" + "EXCEPTION: " 203 + e.toString(); 204 return false; 205 } 206 return true; 207 208 } 210 215 public ArrayList getList(String listName) { 216 ArrayList keyValues = new ArrayList(); 217 218 NodeList keyValueList = doc.getElementsByTagName(listName); 219 220 for (int forOne = 0; forOne < keyValueList.getLength(); forOne++) { 221 222 Node keyNode = keyValueList.item(forOne); 223 NodeList keyRow = keyNode.getChildNodes(); 224 HashMap keyValuePair = new HashMap(); 225 keyValuePair.clear(); 226 227 for (int forTwo = 0; forTwo < keyRow.getLength(); forTwo++) { 228 229 Node fieldNode = keyRow.item(forTwo); 230 int type = fieldNode.getNodeType(); 231 if (type == Node.ELEMENT_NODE) { 232 233 String tagName = ""; 234 String tagValue = ""; 235 236 tagName = fieldNode.getParentNode().getNodeName() + ":" + fieldNode.getNodeName(); 237 238 NodeList children = fieldNode.getChildNodes(); 240 241 if (children != null && children.getLength() > 0) { 243 244 if (children.item(0).getNodeType() == Node.TEXT_NODE) { 249 250 tagValue = children.item(0).getNodeValue() + ""; 251 252 if (tagValue != null && !tagValue.equals("") && !tagName.equals("")) { 253 keyValuePair.put(tagName, tagValue); 254 } else { 255 keyValuePair.put(tagName, ""); 256 } 257 258 } 261 } 263 } 265 } 267 if (!keyValuePair.isEmpty()) { 268 keyValues.add(keyValuePair); 269 } 270 271 } 273 return keyValues; 274 } 275 276 } 277 | Popular Tags |