Math2mat
|
00001 00021 package m2m.frontend; 00022 00023 import java.io.BufferedWriter; 00024 import java.io.File; 00025 import java.io.FileWriter; 00026 import java.io.StringWriter; 00027 00028 import org.eclipse.core.runtime.Platform; 00029 import org.w3c.dom.Document; 00030 import org.w3c.dom.Element; 00031 import org.w3c.dom.NodeList; 00032 import org.w3c.dom.Text; 00033 import org.xml.sax.SAXException; 00034 00035 import javax.xml.parsers.*; 00036 import javax.xml.transform.OutputKeys; 00037 import javax.xml.transform.Transformer; 00038 import javax.xml.transform.TransformerConfigurationException; 00039 import javax.xml.transform.TransformerException; 00040 import javax.xml.transform.TransformerFactory; 00041 import javax.xml.transform.dom.DOMSource; 00042 import javax.xml.transform.stream.StreamResult; 00043 00044 //import org.xml.sax.SAXException; 00045 import java.io.IOException; 00046 00047 import m2m.backend.utils.XMLUtils; 00048 00049 public class GUIProperties { 00050 00051 private static GUIProperties singleProp = null; 00052 00053 static public GUIProperties getReference() { 00054 if (singleProp==null) 00055 singleProp=new GUIProperties(); 00056 return singleProp; 00057 } 00058 00059 00060 private String validateDir; 00061 00062 public String getValidateDir() { 00063 return validateDir; 00064 } 00065 00066 public void setValidateDir(String dir) { 00067 validateDir = dir; 00068 } 00069 00070 private String[] reOpenFile = new String[5]; 00071 00072 public String getXmlTagName() { 00073 return "ReOpenFile"; 00074 } 00075 00076 public boolean fromXml(Element fstNmElmnt) { 00077 setValidateDir(XMLUtils.getTextValue(fstNmElmnt, "validateDir","")); 00078 for(int findex=1;findex<6;findex++) { 00079 this.setReOpenFileAtIndex(XMLUtils.getTextValue(fstNmElmnt,"File"+findex,"No file"),findex-1); 00080 } 00081 return true; 00082 } 00083 00084 public Element toXml(Document dom) { 00085 Element el = dom.createElement(getXmlTagName()); 00086 el.appendChild(XMLUtils.createTextElement(dom, "validateDir", validateDir)); 00087 00088 for(int i=1;i<6;i++) { 00089 Element fEl = dom.createElement("File"+i); 00090 Text path = dom.createTextNode(reOpenFile[i-1]); 00091 fEl.appendChild(path); 00092 el.appendChild(fEl); 00093 } 00094 return el; 00095 } 00096 00103 public String getReOpenFileAtIndex(int index) { 00104 return reOpenFile[index]; 00105 } 00106 00112 public String[] getReOpenFile() { 00113 return reOpenFile; 00114 } 00115 00122 public void setReOpenFileAtIndex(String value, int index) { 00123 reOpenFile[index] = value; 00124 } 00125 00126 00130 private GUIProperties() { 00131 this.validateDir=new String(""); 00132 for(int i=0;i<5;i++) 00133 reOpenFile[i]=new String("No file"); 00134 readSettings(); 00135 } 00136 00137 protected void finalize() { 00138 00139 } 00140 00141 public boolean writeSettings() { 00142 //load the GUI property file 00143 File file = Platform.getBundle("m2mgui").getBundleContext().getDataFile("m2mprop"); 00144 00145 // File file = new File(new File("").getAbsolutePath()+"/M2MProperties.prop"); 00146 return writeSettings(file); 00147 } 00148 00149 public boolean readSettings() { 00150 File file = Platform.getBundle("m2mgui").getBundleContext().getDataFile("m2mprop"); 00151 // String location=Platform.getBundle("m2mgui").getLocation(); 00152 // int pos=location.indexOf("file:"); 00153 // location=location.substring(pos+5); 00154 // System.out.println("GetLocation2: "+location); 00155 00156 00157 if (file==null) 00158 return false; 00159 if (file.length()==0) 00160 return false; 00161 return readSettings(file); 00162 } 00163 00164 public boolean readSettings(File f) { 00165 00166 //get the factory 00167 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 00168 00169 try { 00170 00171 //Using factory get an instance of document builder 00172 DocumentBuilder db = dbf.newDocumentBuilder(); 00173 00174 //parse using builder to get DOM representation of the XML file 00175 Document dom = db.parse(f); 00176 00177 dom.getDocumentElement().normalize(); 00178 System.out.println("Root element " + dom.getDocumentElement().getNodeName()); 00179 00180 Element docEl = dom.getDocumentElement(); 00181 00182 NodeList reOpenLst = docEl.getElementsByTagName(this.getXmlTagName()); 00183 if (reOpenLst != null && reOpenLst.getLength()>0) { 00184 Element fstNmElmnt = (Element) reOpenLst.item(0); 00185 this.fromXml(fstNmElmnt); 00186 } 00187 00188 00189 00190 00191 }catch(ParserConfigurationException pce) { 00192 pce.printStackTrace(); 00193 return false; 00194 }catch(SAXException se) { 00195 se.printStackTrace(); 00196 return false; 00197 }catch(IOException ioe) { 00198 ioe.printStackTrace(); 00199 return false; 00200 } 00201 00202 return true; 00203 } 00204 00205 public boolean writeSettings(File f) { 00206 //get an instance of factory 00207 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 00208 try { 00209 //get an instance of builder 00210 DocumentBuilder db = dbf.newDocumentBuilder(); 00211 00212 //create an instance of DOM 00213 Document dom = db.newDocument(); 00214 00215 00216 00217 //create the root element 00218 Element rootEl = dom.createElement("M2MSettings"); 00219 dom.appendChild(rootEl); 00220 00221 00222 rootEl.appendChild(this.toXml(dom)); 00223 00225 //Output the XML 00226 00227 //set up a transformer 00228 TransformerFactory transfac = TransformerFactory.newInstance(); 00229 Transformer trans = transfac.newTransformer(); 00230 trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 00231 trans.setOutputProperty(OutputKeys.INDENT, "yes"); 00232 00233 //create string from xml tree 00234 StringWriter sw = new StringWriter(); 00235 StreamResult result = new StreamResult(sw); 00236 DOMSource source = new DOMSource(dom); 00237 trans.transform(source, result); 00238 String xmlString = sw.toString(); 00239 00240 try{ 00241 // Create file 00242 FileWriter fstream = new FileWriter(f); 00243 BufferedWriter out = new BufferedWriter(fstream); 00244 out.write(xmlString); 00245 //Close the output stream 00246 out.close(); 00247 }catch (Exception e){//Catch exception if any 00248 System.err.println("Error: " + e.getMessage()); 00249 } 00250 00251 } 00252 catch(ParserConfigurationException pce) { 00253 //dump it 00254 System.out.println("Error while trying to instantiate DocumentBuilder " + pce); 00255 return false; 00256 } catch (TransformerConfigurationException e) { 00257 // TODO Auto-generated catch block 00258 e.printStackTrace(); 00259 return false; 00260 } catch (TransformerException e) { 00261 // TODO Auto-generated catch block 00262 e.printStackTrace(); 00263 return false; 00264 } 00265 00266 return true; 00267 } 00268 00269 00270 }