1 14 15 package webEditor.core; 16 17 import java.io.*; 18 import java.util.*; 19 20 import org.w3c.dom.*; 21 import org.apache.xerces.dom.*; 22 import org.apache.xml.serialize.*; 23 24 import webEditor.util.xml_parser; 25 26 27 32 33 public class configuration 34 { 35 38 private static Hashtable configTable; 39 40 43 private static xml_parser myParser; 44 45 46 public configuration(String confFile) 47 { 48 myParser = new xml_parser (); 50 configTable = myParser.parseConfFile (confFile); 51 } 52 53 public configuration(Hashtable givenTable) 54 { 55 configTable = givenTable; 57 } 58 59 public configuration() 60 { 61 configTable = new Hashtable(); 63 } 64 65 71 public String readValue (String category, String key) 72 { 73 String value = null; 74 Hashtable myTable = null; 75 76 myTable = (Hashtable) this.configTable.get (category); 77 if ( myTable == null ) { 78 return (null); 79 } 80 81 value = (String ) myTable.get (key); 82 if ( value == null ) { 83 return (null); 84 } 85 86 return (value); 87 } 88 89 100 public void storeValue (String category, String key, String value) 101 { 102 Hashtable myTable = null; 103 104 myTable = (Hashtable) this.configTable.get (category); 105 if ( myTable == null ) { 106 myTable = new Hashtable(); 108 } 109 myTable.put (key, (String ) value); 110 this.configTable.put (category, (Hashtable) myTable); 111 return; 112 } 113 114 119 public Hashtable readCategory (String category) 120 { 121 Hashtable myTable = new Hashtable(); 122 123 if ( category == null ) { 124 return (myTable); 125 } 126 myTable = (Hashtable) this.configTable.get (category); 127 return (myTable); 128 } 129 130 140 public void storeCategory (String category, Hashtable values) 141 { 142 this.configTable.put (category, (Hashtable) values); 143 return; 144 } 145 146 152 public Document writeDoc (Hashtable dataStream, Document configDoc) 153 { 154 String rootTag = null; 155 String tagName = null; 156 String tagValue = null; 157 158 Element root = configDoc.getDocumentElement(); 159 rootTag = "category"; 161 NodeList list = configDoc.getElementsByTagName(rootTag).item(0).getChildNodes(); 162 163 for (Enumeration e = dataStream.keys() ; e.hasMoreElements() ;) { 166 tagName = (String ) e.nextElement(); 167 tagValue = (String ) dataStream.get (tagName); 168 for (int i=0; i < list.getLength(); i++) { 169 if (list.item(i).getAttributes().getNamedItem("code").getNodeValue().equals(tagName)) { 170 Node source = list.item(i); 171 Node myDocNode = source.cloneNode (true); 172 myDocNode.getLastChild().setNodeValue (tagValue); 173 Node parent = source.getParentNode(); 174 Node temp = parent.replaceChild (myDocNode, source); 175 } 176 } 177 } 178 179 return (configDoc); 180 } 181 182 188 public void saveConfig (String filePath, Document doc) 189 { 190 if (filePath == null) { 191 return; 192 } 193 try { 194 OutputFormat format = new OutputFormat (doc, "ISO-8859-1" ,true); 195 format.setIndent (2); 196 format.setLineWidth (80); 197 format.setPreserveSpace (true); 198 StringWriter stringOut = new StringWriter (); 199 XMLSerializer serial = new XMLSerializer (stringOut, format); 200 serial.asDOMSerializer (); 201 202 serial.serialize (doc.getDocumentElement()); 203 204 File fd = new File (filePath); 206 if ( fd.exists() ) { 207 fd.delete(); 208 } 209 RandomAccessFile myFile = new RandomAccessFile (filePath, "rw"); 210 myFile.write (stringOut.toString().getBytes()); 211 myFile.close(); 212 } 213 catch (Exception e) { 214 e.printStackTrace(); 215 } 216 } 217 218 219 223 public Hashtable returnData () 224 { 225 return (configTable); 226 } 227 228 } 229 | Popular Tags |