1
package org.herac.tuxguitar.gui.scale.xml;
4
import java.io.IOException;
7
import javax.xml.parsers.DocumentBuilder;
8
import javax.xml.parsers.DocumentBuilderFactory;
9
import javax.xml.parsers.ParserConfigurationException;
11
import org.herac.tuxguitar.gui.scale.ScaleInfo;
12
import org.w3c.dom.Document;
13
import org.w3c.dom.NamedNodeMap;
14
import org.w3c.dom.Node;
15
import org.w3c.dom.NodeList;
16
import org.xml.sax.SAXException;
18
public class ScaleReader {
19
private static final String SCALE_TAG = "scale";
20
private static final String NAME_ATTRIBUTE = "name";
21
private static final String KEYS_ATTRIBUTE = "keys";
23
public void loadScales(List scales,String fileName) throws ParserConfigurationException, SAXException, IOException {
25
File file = new File(fileName);
27
Document doc = getDocument(file);
28
loadScales(scales,doc.getFirstChild());
35
private static Document getDocument(File file) throws ParserConfigurationException, SAXException, IOException {
36
Document document = null;
37
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
39
DocumentBuilder builder = factory.newDocumentBuilder();
40
document = builder.parse(file);
46
private static void loadScales(List scales,Node node){
47
NodeList nodeList = node.getChildNodes();
48
for (int i = 0; i < nodeList.getLength(); i++) {
49
Node child = nodeList.item(i);
50
String nodeName = child.getNodeName();
52
if (nodeName.equals(SCALE_TAG)) {
53
NamedNodeMap params = child.getAttributes();
55
String name = params.getNamedItem(NAME_ATTRIBUTE).getNodeValue();
56
String keys = params.getNamedItem(KEYS_ATTRIBUTE).getNodeValue();
59
if (name == null || keys == null || name.trim().equals("") || keys.trim().equals("")){
60
throw new RuntimeException("Invalid Scale file format.");
63
scales.add(new ScaleInfo(name,keys));