Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/media/hyperpad/conf/ConfigHandler.java


1   /*
2    * $COPYRIGHT$
3    * $Id: ConfigHandler.java,v 1.1 2002/09/13 18:47:56 borzy Exp $
4    *
5    * Date        Author               Changes
6    * Apr 18 2001 Borsos Szabolcs      Created
7    */   
8   package org.media.hyperpad.conf;
9   
10  import java.awt.Font;
11  import java.awt.Color;
12  import java.awt.Toolkit;
13  import java.io.File;
14  import java.io.IOException;
15  import java.io.InputStream;
16  import java.io.PrintWriter;
17  import java.io.FileInputStream;
18  import java.io.FileOutputStream;
19  import java.io.OutputStreamWriter;
20  import org.w3c.dom.*;
21  import org.xml.sax.InputSource;
22  import org.xml.sax.SAXException;
23  import org.xml.sax.SAXParseException;
24  import javax.xml.parsers.DocumentBuilderFactory;  
25  import javax.xml.parsers.FactoryConfigurationError;  
26  import javax.xml.parsers.ParserConfigurationException;
27  import javax.xml.parsers.DocumentBuilder;
28  /**
29   * Class meant to handle the configuration XML for HyperPad.
30   * @author <a href="mailto:borzy@nolimits.ro">Borsos Szabolcs</a>
31   * @version $Revision: 1.1 $ $Date: 2002/09/13 18:47:56 $
32   * @see String
33   */  
34  public class ConfigHandler {
35      Document document;
36      InputSource is;
37      Element e;
38          
39      public ConfigHandler(String path) {
40          try {
41              DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
42              InputStream ins = getXML(path);
43              if (ins != null) {
44                  is = new InputSource(ins);
45                  DocumentBuilder builder = factory.newDocumentBuilder();
46                  document = builder.parse(is); 
47                  e = document.getDocumentElement();
48              }
49          }
50          catch (Exception ex) {
51              ex.printStackTrace();
52          }
53      }
54      
55      private InputStream getXML(String path) throws IOException {
56          try {
57              Class _this = Class.forName("org.media.hyperpad.conf.ConfigHandler");
58              InputStream ins = (InputStream)new FileInputStream(path);
59              if (ins != null) {
60                  return ins;
61              }
62              else {
63                  System.out.println("Wrong path given to ConfigHandler. "+path);
64                  return null;
65              }
66          }
67          catch (ClassNotFoundException cnfex) {
68              System.out.println("Can't find the configuration XML file! Loading default values...");
69              cnfex.printStackTrace();
70              return null;
71          }
72      }
73  
74      public boolean getNavigationBarStatus() {
75          return getMode("NavigationBarStatus", "", "status");
76      }
77  
78      public void setNavigationBarStatus(boolean new_status) {
79          setMode("NavigationBarStatus", "", "status", new_status);
80      }
81  
82      public String getButtonDisplay() {
83          if (getFirstChildValue("NavigationBarStatus", "show").toLowerCase().trim().equals("text only")) {
84              return "TO";
85          }
86          else if (getFirstChildValue("NavigationBarStatus", "show").toLowerCase().trim().equals("text and icons")) {
87              return "T+I";
88          }
89          else if (getFirstChildValue("NavigationBarStatus", "show").toLowerCase().trim().equals("icons only")) {
90              return "IO";
91          }
92          else {
93              System.out.println("Wrong Navigation Button show value entered. Please correct the errors " +
94                                 "in this file: conf/hyperpad.xml. Continue loading" +
95                                 "with default setting: show text and icons.");
96              return "T+I";
97          }
98      }
99  
100     public void setButtonDisplay(String new_display) {
101         if (new_display.equals("TO")) {
102             setFirstChildValue("NavigationBarStatus", "show", "text only");
103         }
104         else if (new_display.equals("T+I")) {
105             setFirstChildValue("NavigationBarStatus", "show", "text and icons");
106         }
107         else if (new_display.equals("IO")) {
108             setFirstChildValue("NavigationBarStatus", "show", "icons only");
109         }
110     }
111 
112     public boolean getReplaceMode() {
113         return getMode("TabWidth", "", "replace");
114     }
115     
116     public void setReplaceMode(boolean new_mode) {
117         setMode("TabWidth", "", "replace", new_mode);
118     }
119 
120     public String getSpaceNumber() {
121         return getFirstChildValue("TabWidth", "spaceNR");
122     }
123     
124     public void setSpaceNumber(String number) {
125         setFirstChildValue("TabWidth", "spaceNR", number);
126     }
127 
128     public String getHistoryLength() {
129         return getFirstChildValue("HistoryLength", "length");
130     }
131 
132     public void setHistoryLength(String length) {
133         setFirstChildValue("HistoryLength", "length", length);
134     }
135 
136     public String getRecentListLength() {
137         return getFirstChildValue("RecentListLength", "length");
138     }
139 
140     public void setRecentListLength(String length) {
141         setFirstChildValue("RecentListLength", "length", length);
142     }
143 
144     public boolean getHTMLMode() {
145         return getMode("HTMLMode", "", "mode");
146     }
147 
148     public void setHTMLMode(boolean new_mode) {
149         setMode("HTMLMode", "", "mode", new_mode);
150     }
151 
152     public boolean getHighlight() {
153         return getMode("Highlight", "", "mode");
154     }
155 
156     public void setHighlight(boolean new_mode) {
157         setMode("Highlight", "", "mode", new_mode);
158     }
159 
160     public String getLookAndFeel() {
161         return getFirstChildValue("LookAndFeel", "current");
162     }
163 
164     public void setLookAndFeel(String new_LF) {
165         setFirstChildValue("LookAndFeel", "current", new_LF);
166     }
167 
168     public boolean getAntiAliasingStatus() {
169         return getMode("AntiAliasing", "", "on");
170     }
171 
172     public void setAntiAliasingStatus(boolean new_status) {
173         setMode("AntiAliasing", "", "on", new_status);
174     }
175 
176     public boolean getLineWrapStatus() {
177         return getMode("LineWrap", "", "on");
178     }
179 
180     public void setLineWrapStatus(boolean new_status) {
181         setMode("LineWrap", "", "on", new_status);
182     }
183 
184     public String getBrowser() {
185         return getSecondChildValue("navigation", "Browser", "path");
186     }
187 
188     public void setBrowser(String new_path) {
189         setSecondChildValue("navigation", "Browser", "path", new_path);
190     }
191     
192     public boolean getProxyStatus() {
193         return getMode("navigation", "Proxy", "enabled");
194     }
195 
196     public void setProxyStatus(boolean new_status) {
197         setMode("navigation", "Proxy", "enabled", new_status);
198     }
199 
200     public String getHttpProxyHost() {
201         return getSecondChildValue("navigation", "HttpProxy", "host");
202     }
203 
204     public void setHttpProxyHost(String new_value) {
205         setSecondChildValue("navigation", "HttpProxy", "host", new_value);
206     }
207 
208     public String getHttpProxyPort() {
209         return getSecondChildValue("navigation", "HttpProxy", "port");
210     }
211 
212     public void setHttpProxyPort(String new_value) {
213         setSecondChildValue("navigation", "HttpProxy", "port", new_value);
214     }
215 
216     public String getFtpProxyHost() {
217         return getSecondChildValue("navigation", "FtpProxy", "host");
218     }
219 
220     public void setFtpProxyHost(String new_value) {
221         setSecondChildValue("navigation", "FtpProxy", "host", new_value);
222     }
223 
224     public String getFtpProxyPort() {
225         return getSecondChildValue("navigation", "FtpProxy", "port");
226     }
227 
228     public void setFtpProxyPort(String new_value) {
229         setSecondChildValue("navigation", "FtpProxy", "port", new_value);
230     }
231 
232     public Font getMenuFont() {
233         return getFont("editing", "menu", "font");
234     }
235 
236     public Font getGeneralFont() {
237         return getFont("editing", "general", "font");
238     }
239 
240     public void setGeneralFont(String new_name, int new_style, int new_size) {
241         setFont("editing", "general", "font", new_name, new_style, new_size);
242     }
243 
244     public Color getGeneralColor() {
245         return getColor("editing", "general", "color");
246     }
247 
248     public void setGeneralColor(int new_r, int new_g, int new_b) {
249         setColor("editing", "general", "color", new_r, new_g, new_b);
250     }
251 
252     public Color getGeneralBgColor() {
253         return getColor("editing", "general", "backgroundcolor");
254     }
255 
256     public void setGeneralBgColor(int new_r, int new_g, int new_b) {
257         setColor("editing", "general", "backgroundcolor", new_r, new_g, new_b);
258     }
259 
260     public Font getCommentFont() {
261         return getFont("editing", "comment", "font");
262     }
263 
264     public void setCommentFont(String new_name, int new_style, int new_size) {
265         setFont("editing", "comment", "font", new_name, new_style, new_size);
266     }
267 
268     public Color getCommentColor() {
269         return getColor("editing", "comment", "color");
270     }
271 
272     public void setCommentColor(int new_r, int new_g, int new_b) {
273         setColor("editing", "comment", "color", new_r, new_g, new_b);
274     }
275 
276     public Font getKeywordFont() {
277         return getFont("editing", "keyword", "font");
278     }
279 
280     public void setKeywordFont(String new_name, int new_style, int new_size) {
281         setFont("editing", "keyword", "font", new_name, new_style, new_size);
282     }
283 
284     public Color getKeywordColor() {
285         return getColor("editing", "keyword", "color");
286     }
287 
288     public void setKeywordColor(int new_r, int new_g, int new_b) {
289         setColor("editing", "keyword", "color", new_r, new_g, new_b);
290     }
291 
292     public Font getStringFont() {
293         return getFont("editing", "string", "font");
294     }
295 
296     public void setStringFont(String new_name, int new_style, int new_size) {
297         setFont("editing", "string", "font", new_name, new_style, new_size);
298     }
299 
300     public Color getStringColor() {
301         return getColor("editing", "string", "color");
302     }
303 
304     public void setStringColor(int new_r, int new_g, int new_b) {
305         setColor("editing", "string", "color", new_r, new_g, new_b);
306     }
307 
308     public Font getNumericFont() {
309         return getFont("editing", "numeric", "font");
310     }
311 
312     public void setNumericFont(String new_name, int new_style, int new_size) {
313         setFont("editing", "numeric", "font", new_name, new_style, new_size);
314     }
315 
316     public Color getNumericColor() {
317         return getColor("editing", "numeric", "color");
318     }
319 
320     public void setNumericColor(int new_r, int new_g, int new_b) {
321         setColor("editing", "numeric", "color", new_r, new_g, new_b);
322     }
323 
324     public Font getCharacterFont() {
325         return getFont("editing", "character", "font");
326     }
327 
328     public void setCharacterFont(String new_name, int new_style, int new_size) {
329         setFont("editing", "character", "font", new_name, new_style, new_size);
330     }
331 
332     public Color getCharacterColor() {
333         return getColor("editing", "character", "color");
334     }
335 
336     public void setCharacterColor(int new_r, int new_g, int new_b) {
337         setColor("editing", "character", "color", new_r, new_g, new_b);
338     }
339 
340     public String getShortcut(String key) {
341         return getSCKeyAndModifiers("Shortcuts", key);
342     }
343 
344     public void setShortcut(String key, String new_SCkey, String new_modifiers) {
345         setSCKeyAndModifiers("Shortcuts", key, new_SCkey, new_modifiers);
346     }
347 
348     public void save(String xmlpath) {
349         saveXML(xmlpath, document, e);
350     }
351 
352     private boolean getMode(String node, String parent, String mode) {
353         String value = null;
354         NodeList nl = e.getElementsByTagName(node);
355         Element elem = (Element) nl.item(0);
356         if (parent.equals("")) {
357             value = elem.getAttribute(mode);
358         }
359         else {
360             Element subnode = getElement(node, parent, "");
361             value = subnode.getAttribute(mode);
362         }
363         if (value.equals("true")) {
364             return true;
365         }
366         else {
367             return false;
368         }
369     }
370 
371     private void setMode(String node, String parent, String mode, boolean new_mode) {
372         Element elem = null;
373         if (parent.equals("")) {
374             NodeList nl = e.getElementsByTagName(node);
375             elem = (Element) nl.item(0);
376         }
377         else {
378             elem = getElement(node, parent, "");
379         }
380         elem.removeAttribute(mode);
381         if (new_mode) {
382             elem.setAttribute(mode, "true");
383         }
384         else {
385             elem.setAttribute(mode, "false");
386         }
387     }
388 
389     private String getFirstChildValue(String node, String child) {
390         String value = null;
391         NodeList nl = e.getElementsByTagName(node);
392         Element elem = (Element) nl.item(0);
393         value = elem.getAttribute(child);
394         return value;
395     }
396 
397     private void setFirstChildValue(String node, String child, String new_value) {
398         NodeList nl = e.getElementsByTagName(node);
399         Element elem = (Element) nl.item(0);
400         elem.removeAttribute(child);
401         elem.setAttribute(child, new_value);
402     }
403 
404     private String getSecondChildValue(String node, String parent, String key) {
405         String value = null;
406         Element elem = getElement(node, parent, "");
407         value = elem.getAttribute(key);
408         return value;
409     }
410 
411     private void setSecondChildValue(String node, String parent, String key, String new_value) {
412         Element elem = getElement(node, parent, "");
413         elem.removeAttribute(key);
414         elem.setAttribute(key, new_value);
415     }
416 
417     private Color getColor(String node, String subnode, String type) {
418         int r = 0;
419         int g = 0;
420         int b = 0;
421         Element elem = getElement(node, subnode, type);        
422         r = Integer.valueOf(elem.getAttribute("r")).intValue();
423         g = Integer.valueOf(elem.getAttribute("g")).intValue();
424         b = Integer.valueOf(elem.getAttribute("b")).intValue();
425         
426         return new Color(r, g, b);
427     }
428 
429     private void setColor(String node, String subnode, String type, 
430                                  int new_r, int new_g, int new_b) {
431         Element elem = getElement(node, subnode, type); 
432         elem.removeAttribute("r");
433         elem.setAttribute("r", ""+new_r);
434         elem.removeAttribute("g");
435         elem.setAttribute("g", ""+new_g);
436         elem.removeAttribute("b");
437         elem.setAttribute("b", ""+new_b);
438     }
439 
440     private Font getFont(String node, String subnode, String type) {
441         String name = null;
442         String Str_style = null;
443         int style = 0;
444         int size = 0;
445         Element elem = getElement(node, subnode, type);
446         name = elem.getAttribute("name");
447         Str_style = elem.getAttribute("style");
448             if (Str_style.trim().toLowerCase().equals("bold")) {
449                 style = Font.BOLD;
450             }
451             else if (Str_style.trim().toLowerCase().equals("italic")) {
452                 style = Font.ITALIC;
453             }
454             else if (Str_style.trim().toLowerCase().equals("bold, italic")) {
455                 style = Font.BOLD|Font.ITALIC;
456             }
457             else {
458                 style = Font.PLAIN;
459             }
460         size = Integer.valueOf(elem.getAttribute("size")).intValue();
461 
462         return new Font(name, style, size);
463     }
464 
465     private void setFont(String node, String subnode, String type, 
466                                 String new_name, int new_style, int new_size) {
467         Element elem = getElement(node, subnode, type);
468         elem.removeAttribute("name");
469         elem.setAttribute("name", new_name);
470         elem.removeAttribute("style");
471             if (new_style == Font.BOLD) { 
472                 elem.setAttribute("style", "bold");
473             }
474             else if (new_style == Font.ITALIC) { 
475                 elem.setAttribute("style", "italic");
476             }
477             else if (new_style == Font.PLAIN) {
478                 elem.setAttribute("style", "plain");
479             }
480         elem.removeAttribute("size");
481         elem.setAttribute("size", ""+new_size);
482     }
483 
484     private void setSCKeyAndModifiers(String node, String subnode, String new_value, String new_modifiers) {
485         Element elem = getElement(node, subnode, "");
486         elem.removeAttribute("SCkey");
487         elem.setAttribute("SCkey", new_value);
488         elem.removeAttribute("modifiers");
489         elem.setAttribute("modifiers", new_modifiers);
490     }
491 
492     private String getSCKeyAndModifiers(String node, String subnode) {
493         try {
494             Element elem = getElement(node, subnode, "");
495             String key = elem.getAttribute("SCkey");
496             String modifiers = elem.getAttribute("modifiers");
497             if (key.equals("") || modifiers.equals("")) { 
498                 return "+&+";
499             }
500             else {
501                 return (key + "+&+" + modifiers);
502             }
503         } catch (Exception ex) {
504             return "!";
505         }
506     }
507 
508     private Element getElement(String main, String subnode, String type) {
509         NodeList main_nl = e.getElementsByTagName(main);
510         Element main_elem = (Element) main_nl.item(0);
511         NodeList sub_nl = main_elem.getElementsByTagName(subnode);
512         Element sub_elem = (Element) sub_nl.item(0);
513         if (!type.equals("")) {
514             NodeList attr_nl = sub_elem.getElementsByTagName(type);
515             Element attr_elem = (Element) attr_nl.item(0);
516             return attr_elem;
517         }
518         else {
519             return sub_elem;
520         }
521     }
522 
523     protected static void saveXML(String path, Document doc, Element root) {
524         try {
525             PrintWriter out = new PrintWriter(new OutputStreamWriter (new FileOutputStream(path)));
526             org.apache.xml.serialize.OutputFormat outForm = 
527                 new org.apache.xml.serialize.OutputFormat(doc);
528             org.apache.xml.serialize.XMLSerializer serializer = 
529                 new org.apache.xml.serialize.XMLSerializer(out, outForm);
530             outForm.setIndenting(true);
531             serializer.serialize(root);
532         }
533         catch (Exception exc) {
534             exc.printStackTrace();
535         }
536     }
537 }
538