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

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/gui/editors/chord/xml/ChordReader.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.editors.chord.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.song.models.Chord;
13
 
import org.w3c.dom.Document;
14
 
import org.w3c.dom.NamedNodeMap;
15
 
import org.w3c.dom.Node;
16
 
import org.w3c.dom.NodeList;
17
 
import org.xml.sax.SAXException;
18
 
 
19
 
public class ChordReader {
20
 
        private static final String ACTION_ATTRIBUTE = "action";
21
 
        private static final String KEYS_ATTRIBUTE = "keys";
22
 
        
23
 
        private static final String CHORD_LIST_TAG = "chords";
24
 
        private static final String CHORD_TAG = "chord";
25
 
        private static final String CHORD_NAME_ATTRIBUTE = "name";
26
 
        private static final String CHORD_STRINGS_ATTRIBUTE = "strings";
27
 
        private static final String STRING_TAG = "string";
28
 
        private static final String STRING_NUMBER_ATTRIBUTE = "number";
29
 
        private static final String STRING_FRET_ATTRIBUTE = "fret";
30
 
        
31
 
    public static List getChords(String fileName) {
32
 
        List chords = new ArrayList();
33
 
        try{
34
 
                File file = new File(fileName);        
35
 
                if (file.exists()){
36
 
                        Document doc = getDocument(file);
37
 
                        loadChords(doc.getFirstChild(),chords);
38
 
                }
39
 
        }catch(Exception e){
40
 
                e.printStackTrace();
41
 
        }        
42
 
        return chords;
43
 
    }
44
 
    
45
 
    private static Document getDocument(File file) {
46
 
        Document document = null;
47
 
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
48
 
        try {
49
 
            DocumentBuilder builder = factory.newDocumentBuilder();
50
 
            document = builder.parse(file);
51
 
        } catch (SAXException sxe) {
52
 
            sxe.printStackTrace();
53
 
        } catch (ParserConfigurationException pce) {
54
 
            pce.printStackTrace();
55
 
        } catch (IOException ioe) {
56
 
            ioe.printStackTrace();
57
 
        }
58
 
        return document;
59
 
    }
60
 
 
61
 
    /**
62
 
     * Read shortcuts from xml file
63
 
     * 
64
 
     * @param shortcutsNode
65
 
     * @return
66
 
     */
67
 
    private static void loadChords(Node chordsNode,List chords){
68
 
        try{
69
 
                NodeList chordList = chordsNode.getChildNodes();
70
 
                for (int i = 0; i < chordList.getLength(); i++) {
71
 
                        Node chordItem = chordList.item(i);
72
 
                        if (chordItem.getNodeName().equals(CHORD_TAG)) {
73
 
                                NamedNodeMap chordAttributes = chordItem.getAttributes();
74
 
 
75
 
                                String name = chordAttributes.getNamedItem(CHORD_NAME_ATTRIBUTE).getNodeValue();
76
 
                                String strings = chordAttributes.getNamedItem(CHORD_STRINGS_ATTRIBUTE).getNodeValue();
77
 
                
78
 
                                Chord chord = new Chord(Integer.parseInt(strings));
79
 
                                chord.setName(name);
80
 
                
81
 
                                NodeList stringList = chordItem.getChildNodes();
82
 
                                for (int j = 0; j < stringList.getLength(); j++) {
83
 
                                        Node stringItem = stringList.item(j);                
84
 
                                        if (stringItem.getNodeName().equals(STRING_TAG)) {
85
 
                                                NamedNodeMap stringAttributes = stringItem.getAttributes();
86
 
                                
87
 
                                                String number = stringAttributes.getNamedItem(STRING_NUMBER_ATTRIBUTE).getNodeValue();
88
 
                                                String fret = stringAttributes.getNamedItem(STRING_FRET_ATTRIBUTE).getNodeValue();
89
 
                                
90
 
                                                chord.addFretValue(Integer.parseInt(number),Integer.parseInt(fret));
91
 
                                        }       
92
 
                                }
93
 
                                chords.add(chord);
94
 
                        }
95
 
 
96
 
                }
97
 
        }catch(Exception e){
98
 
                chords.clear();
99
 
                e.printStackTrace();
100
 
        }
101
 
    }
102
 
}