1
package org.herac.tuxguitar.gui.editors.chord.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.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;
19
public class ChordReader {
20
private static final String ACTION_ATTRIBUTE = "action";
21
private static final String KEYS_ATTRIBUTE = "keys";
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";
31
public static List getChords(String fileName) {
32
List chords = new ArrayList();
34
File file = new File(fileName);
36
Document doc = getDocument(file);
37
loadChords(doc.getFirstChild(),chords);
45
private static Document getDocument(File file) {
46
Document document = null;
47
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
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();
62
* Read shortcuts from xml file
64
* @param shortcutsNode
67
private static void loadChords(Node chordsNode,List chords){
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();
75
String name = chordAttributes.getNamedItem(CHORD_NAME_ATTRIBUTE).getNodeValue();
76
String strings = chordAttributes.getNamedItem(CHORD_STRINGS_ATTRIBUTE).getNodeValue();
78
Chord chord = new Chord(Integer.parseInt(strings));
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();
87
String number = stringAttributes.getNamedItem(STRING_NUMBER_ATTRIBUTE).getNodeValue();
88
String fret = stringAttributes.getNamedItem(STRING_FRET_ATTRIBUTE).getNodeValue();
90
chord.addFretValue(Integer.parseInt(number),Integer.parseInt(fret));