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

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/gui/items/xml/ToolBarsReader.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.items.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.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 ToolBarsReader {
19
 
        private static final String ITEM_LIST_TAG = "toolbars";
20
 
        private static final String ITEM_TAG = "toolbar";
21
 
        private static final String NAME_ATTRIBUTE = "name";
22
 
        
23
 
    public List loadToolBars(File file){
24
 
        List items = new ArrayList();             
25
 
        if (file.exists()){
26
 
                        try {
27
 
                                Document doc = getDocument(file);
28
 
                                loadToolBars(items,doc.getFirstChild());
29
 
                        } catch (ParserConfigurationException e) {
30
 
                                e.printStackTrace();
31
 
                        } catch (SAXException e) {
32
 
                                e.printStackTrace();
33
 
                        } catch (IOException e) {
34
 
                                e.printStackTrace();
35
 
                        } catch (Exception e) {
36
 
                    e.printStackTrace();
37
 
                } catch (Error e) {
38
 
                        e.printStackTrace();
39
 
                }       
40
 
        }      
41
 
        
42
 
        return items;
43
 
    }
44
 
    
45
 
    private static Document getDocument(File file) throws ParserConfigurationException, SAXException, IOException {
46
 
        Document document = null;
47
 
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
48
 
        
49
 
        DocumentBuilder builder = factory.newDocumentBuilder();
50
 
        document = builder.parse(file);
51
 
        
52
 
        
53
 
        return document;
54
 
    }
55
 
 
56
 
    private static void loadToolBars(List items,Node node){
57
 
        NodeList listNode = node.getChildNodes();
58
 
        for (int i = 0; i < listNode.getLength(); i++) {
59
 
            Node child = listNode.item(i);
60
 
            String nameNode = child.getNodeName();
61
 
 
62
 
            if (nameNode.equals(ITEM_TAG)) {
63
 
                NamedNodeMap params = child.getAttributes();
64
 
 
65
 
                String name = params.getNamedItem(NAME_ATTRIBUTE).getNodeValue();                                               
66
 
                if (name == null || name.trim().length() == 0){
67
 
                        System.err.println("Invalid ToolBar name.");
68
 
                }               
69
 
                items.add(name);
70
 
            }
71
 
        }
72
 
    }
73
 
}