1
package org.herac.tuxguitar.gui.system.keybindings.xml;
4
import java.io.IOException;
5
import java.util.ArrayList;
8
import javax.xml.parsers.DocumentBuilder;
9
import javax.xml.parsers.DocumentBuilderFactory;
10
import javax.xml.parsers.ParserConfigurationException;
12
import org.herac.tuxguitar.gui.system.keybindings.KeyBindingAction;
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 List getKeyBindings(String fileName) {
27
File file = new File(fileName);
29
return getBindings(getDocument(file).getFirstChild());
31
}catch(Throwable throwable){
32
throwable.printStackTrace();
37
private static Document getDocument(File file) {
38
Document document = null;
39
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
41
DocumentBuilder builder = factory.newDocumentBuilder();
42
document = builder.parse(file);
43
} catch (SAXException sxe) {
44
sxe.printStackTrace();
45
} catch (ParserConfigurationException pce) {
46
pce.printStackTrace();
47
} catch (IOException ioe) {
48
ioe.printStackTrace();
54
* Read shortcuts from xml file
56
* @param shortcutsNode
59
private static List getBindings(Node shortcutsNode){
60
List list = new ArrayList();
62
NodeList nodeList = shortcutsNode.getChildNodes();
63
for (int i = 0; i < nodeList.getLength(); i++) {
64
Node child = nodeList.item(i);
65
String nodeName = child.getNodeName();
67
if (nodeName.equals(SHORTCUT_TAG)) {
68
NamedNodeMap params = child.getAttributes();
70
String key = params.getNamedItem(KEYS_ATTRIBUTE).getNodeValue();
71
String action = params.getNamedItem(ACTION_ATTRIBUTE).getNodeValue();
73
if (key == null || action == null || key.trim().equals("") || action.trim().equals("")){
74
throw new RuntimeException("Invalid KeyBinding file format.");
77
list.add(new KeyBindingAction(action, KeyBinding.parse(key)));