1
package org.herac.tuxguitar.gui.system.keybindings.xml;
4
import java.io.IOException;
5
import java.util.HashMap;
8
import javax.xml.parsers.DocumentBuilder;
9
import javax.xml.parsers.DocumentBuilderFactory;
10
import javax.xml.parsers.ParserConfigurationException;
12
import org.herac.tuxguitar.gui.TuxGuitar;
13
import org.herac.tuxguitar.gui.system.keybindings.KeyBinding;
14
import org.w3c.dom.Document;
15
import org.w3c.dom.NamedNodeMap;
16
import org.w3c.dom.Node;
17
import org.w3c.dom.NodeList;
18
import org.xml.sax.SAXException;
20
public class KeyBindingReader {
21
private static final String SHORTCUT_TAG = "shortcut";
22
private static final String ACTION_ATTRIBUTE = "action";
23
private static final String KEYS_ATTRIBUTE = "keys";
25
public static Map getKeyBindings(String fileName) {
28
File file = new File(fileName);
31
Document doc = getDocument(file);
32
bindings = getBindings(doc.getFirstChild());
40
private static Document getDocument(File file) {
41
Document document = null;
42
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
44
DocumentBuilder builder = factory.newDocumentBuilder();
45
document = builder.parse(file);
46
} catch (SAXException sxe) {
47
sxe.printStackTrace();
48
} catch (ParserConfigurationException pce) {
49
pce.printStackTrace();
50
} catch (IOException ioe) {
51
ioe.printStackTrace();
57
* Read shortcuts from xml file
59
* @param shortcutsNode
62
private static Map getBindings(Node shortcutsNode){
63
Map bindings = new HashMap();
65
NodeList nodeList = shortcutsNode.getChildNodes();
66
for (int i = 0; i < nodeList.getLength(); i++) {
67
Node child = nodeList.item(i);
68
String nodeName = child.getNodeName();
70
if (nodeName.equals(SHORTCUT_TAG)) {
71
NamedNodeMap params = child.getAttributes();
73
String key = params.getNamedItem(KEYS_ATTRIBUTE).getNodeValue();
74
String action = params.getNamedItem(ACTION_ATTRIBUTE).getNodeValue();
76
if (key == null || action == null || key.trim().equals("") || action.trim().equals("")){
77
throw new RuntimeException("Invalid KeyBinding file format.");
80
bindings.put(KeyBinding.parse(key),TuxGuitar.instance().getAction(action));