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.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;
20
public class ChordXMLReader {
22
public static List getChords(String fileName) {
23
List chords = new ArrayList();
25
File file = new File(fileName);
27
Document doc = getDocument(file);
28
loadChords(doc.getFirstChild(),chords);
36
private static Document getDocument(File file) {
37
Document document = null;
38
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
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();
53
* Read shortcuts from xml file
55
* @param shortcutsNode
58
private static void loadChords(Node chordsNode,List chords){
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();
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();
70
TGChord chord = TuxGuitar.instance().getSongManager().getFactory().newChord(Integer.parseInt(strings));
72
chord.setFirstFret(Integer.parseInt(firstFret));
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();
80
String number = stringAttributes.getNamedItem(ChordXML.STRING_NUMBER_ATTRIBUTE).getNodeValue();
81
String fret = stringAttributes.getNamedItem(ChordXML.STRING_FRET_ATTRIBUTE).getNodeValue();
83
chord.addFretValue(Integer.parseInt(number),Integer.parseInt(fret));