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

« back to all changes in this revision

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