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

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/gui/scale/xml/ScaleReader.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.scale.xml;
2
 
 
3
 
import java.io.File;
4
 
import java.io.IOException;
5
 
import java.util.List;
6
 
 
7
 
import javax.xml.parsers.DocumentBuilder;
8
 
import javax.xml.parsers.DocumentBuilderFactory;
9
 
import javax.xml.parsers.ParserConfigurationException;
10
 
 
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;
17
 
 
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";
22
 
        
23
 
    public void loadScales(List scales,String fileName) throws ParserConfigurationException, SAXException, IOException {
24
 
        try{            
25
 
                File file = new File(fileName);        
26
 
                if (file.exists()){
27
 
                        Document doc = getDocument(file);
28
 
                        loadScales(scales,doc.getFirstChild());
29
 
                }   
30
 
        }catch(Exception e){
31
 
                e.printStackTrace();
32
 
        }        
33
 
    }
34
 
    
35
 
    private static Document getDocument(File file) throws ParserConfigurationException, SAXException, IOException {
36
 
        Document document = null;
37
 
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
38
 
        
39
 
        DocumentBuilder builder = factory.newDocumentBuilder();
40
 
        document = builder.parse(file);
41
 
        
42
 
        
43
 
        return document;
44
 
    }
45
 
 
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();
51
 
 
52
 
            if (nodeName.equals(SCALE_TAG)) {
53
 
                NamedNodeMap params = child.getAttributes();
54
 
 
55
 
                String name = params.getNamedItem(NAME_ATTRIBUTE).getNodeValue();
56
 
                String keys = params.getNamedItem(KEYS_ATTRIBUTE).getNodeValue();
57
 
                
58
 
                
59
 
                if (name == null || keys == null || name.trim().equals("") || keys.trim().equals("")){
60
 
                        throw new RuntimeException("Invalid Scale file format.");
61
 
                }
62
 
                
63
 
                scales.add(new ScaleInfo(name,keys));
64
 
            }
65
 
        }
66
 
    }
67
 
}