Source code: org/media/hyperpad/conf/ComboHandler.java
1 /*
2 * $COPYRIGHT$
3 * $Id: ComboHandler.java,v 1.1 2002/09/13 18:47:56 borzy Exp $
4 *
5 * Date Author Changes
6 * Aug 16 2001 Borsos Szabolcs Created
7 */
8 package org.media.hyperpad.conf;
9
10 import java.io.File;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.io.PrintWriter;
14 import java.io.FileInputStream;
15 import java.io.FileOutputStream;
16 import java.io.OutputStreamWriter;
17 import org.w3c.dom.*;
18 import org.xml.sax.InputSource;
19 import org.xml.sax.SAXException;
20 import org.xml.sax.SAXParseException;
21 import javax.xml.parsers.DocumentBuilderFactory;
22 import javax.xml.parsers.FactoryConfigurationError;
23 import javax.xml.parsers.ParserConfigurationException;
24 import javax.xml.parsers.DocumentBuilder;
25 /**
26 * Class meant to handle the ComboBox list for HyperPad.
27 * @author <a href="mailto:borzy@nolimits.ro">Borsos Szabolcs</a>
28 * @version $Revision: 1.1 $ $Date: 2002/09/13 18:47:56 $
29 * @see String
30 */
31 public class ComboHandler {
32 Document document;
33 InputSource is;
34 Element e;
35
36 public ComboHandler() {
37 try {
38 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
39 InputStream ins = getXML(getXMLPath());
40 if (ins != null) {
41 is = new InputSource(ins);
42 DocumentBuilder builder = factory.newDocumentBuilder();
43 document = builder.parse(is);
44 e = document.getDocumentElement();
45 }
46 }
47 catch (Exception ex) {
48 ex.printStackTrace();
49 }
50 }
51
52 private InputStream getXML(String path) throws IOException {
53 try {
54 Class _this = Class.forName("org.media.hyperpad.conf.ComboHandler");
55 InputStream ins = (InputStream)new FileInputStream(path);
56 if (ins != null) {
57 return ins;
58 }
59 else {
60 System.out.println("Wrong path given to ComboHandler. "+path);
61 return null;
62 }
63 }
64 catch (ClassNotFoundException cnfex) {
65 System.out.println("Can't find the combolist XML file!");
66 cnfex.printStackTrace();
67 return null;
68 }
69 }
70
71 public String getListItem(int itemNR) {
72 return getItem("item"+itemNR, "value");
73 }
74
75 public void setListItem(int itemNR, String new_value) {
76 setItem("item"+itemNR, "value", new_value);
77 }
78
79 public void save() {
80 ConfigHandler.saveXML(getXMLPath(), document, e);
81 }
82
83 private String getItem(String node, String element) {
84 String value = null;
85 NodeList nl = e.getElementsByTagName(node);
86 Element elem = (Element) nl.item(0);
87 value = elem.getAttribute(element);
88 return value;
89 }
90
91 private void setItem(String node, String element, String new_value) {
92 NodeList nl = e.getElementsByTagName(node);
93 Element elem = (Element) nl.item(0);
94 elem.removeAttribute(element);
95 elem.setAttribute(element, new_value);
96 }
97
98 private String getXMLPath() {
99 try {
100 Class _this = Class.forName("org.media.hyperpad.conf.ComboHandler");
101 String urlpath = _this.getResource("/conf/combolist.xml").toString();
102 return urlpath.substring(5, urlpath.length());
103 } catch (Exception ex) {
104 ex.printStackTrace();
105 return "";
106 }
107 }
108 }