~ubuntu-branches/ubuntu/oneiric/tuxguitar/oneiric

« back to all changes in this revision

Viewing changes to TuxGuitar/src/org/herac/tuxguitar/gui/system/keybindings/xml/KeyBindingReader.java

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2008-06-19 00:30:30 UTC
  • mto: (5.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20080619003030-h719szrhsngou7c6
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.herac.tuxguitar.gui.system.keybindings.xml;
 
2
 
 
3
import java.io.File;
 
4
import java.io.IOException;
 
5
import java.util.ArrayList;
 
6
import java.util.List;
 
7
 
 
8
import javax.xml.parsers.DocumentBuilder;
 
9
import javax.xml.parsers.DocumentBuilderFactory;
 
10
import javax.xml.parsers.ParserConfigurationException;
 
11
 
 
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;
 
19
 
 
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";
 
24
        
 
25
        public static List getKeyBindings(String fileName) {
 
26
                try{
 
27
                        File file = new File(fileName);
 
28
                        if (file.exists()){
 
29
                                return getBindings(getDocument(file).getFirstChild());
 
30
                        }
 
31
                }catch(Throwable throwable){
 
32
                        throwable.printStackTrace();
 
33
                }
 
34
                return null;
 
35
        }
 
36
        
 
37
        private static Document getDocument(File file) {
 
38
                Document document = null;
 
39
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 
40
                try {
 
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();
 
49
                }
 
50
                return document;
 
51
        }
 
52
        
 
53
        /**
 
54
         * Read shortcuts from xml file
 
55
         * 
 
56
         * @param shortcutsNode
 
57
         * @return
 
58
         */
 
59
        private static List getBindings(Node shortcutsNode){
 
60
                List list = new ArrayList();
 
61
                
 
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();
 
66
                        
 
67
                        if (nodeName.equals(SHORTCUT_TAG)) {
 
68
                                NamedNodeMap params = child.getAttributes();
 
69
                                
 
70
                                String key = params.getNamedItem(KEYS_ATTRIBUTE).getNodeValue();
 
71
                                String action = params.getNamedItem(ACTION_ATTRIBUTE).getNodeValue();
 
72
                                
 
73
                                if (key == null || action == null || key.trim().equals("") || action.trim().equals("")){
 
74
                                        throw new RuntimeException("Invalid KeyBinding file format.");
 
75
                                }
 
76
                                
 
77
                                list.add(new KeyBindingAction(action, KeyBinding.parse(key)));
 
78
                        }
 
79
                }
 
80
                return list;
 
81
        }
 
82
}