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

« back to all changes in this revision

Viewing changes to TuxGuitar/src/org/herac/tuxguitar/gui/tools/browser/xml/TGBrowserReader.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.tools.browser.xml;
 
2
 
 
3
import java.io.File;
 
4
import java.io.IOException;
 
5
 
 
6
import javax.xml.parsers.DocumentBuilder;
 
7
import javax.xml.parsers.DocumentBuilderFactory;
 
8
import javax.xml.parsers.ParserConfigurationException;
 
9
 
 
10
import org.herac.tuxguitar.gui.tools.browser.TGBrowserCollectionInfo;
 
11
import org.herac.tuxguitar.gui.tools.browser.TGBrowserManager;
 
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 TGBrowserReader {
 
19
        private static final String ITEM_TAG = "browser-collection";
 
20
        private static final String ATTRIBUTE_TYPE = "type";
 
21
        private static final String ATTRIBUTE_DATA = "data";
 
22
        
 
23
        public void loadCollections(TGBrowserManager manager,File file){
 
24
                if (file.exists()){
 
25
                        try {
 
26
                                Document doc = getDocument(file);
 
27
                                loadCollections(manager,doc.getFirstChild());
 
28
                        } catch (Throwable throwable) {
 
29
                                throwable.printStackTrace();
 
30
                        }
 
31
                }
 
32
        }
 
33
        
 
34
        private static void loadCollections(TGBrowserManager manager,Node node){
 
35
                NodeList listNode = node.getChildNodes();
 
36
                for (int i = 0; i < listNode.getLength(); i++) {
 
37
                        Node child = listNode.item(i);
 
38
                        String nameNode = child.getNodeName();
 
39
                        if (nameNode.equals(ITEM_TAG)) {
 
40
                                NamedNodeMap params = child.getAttributes();
 
41
                                
 
42
                                String type = params.getNamedItem(ATTRIBUTE_TYPE).getNodeValue();
 
43
                                String data = params.getNamedItem(ATTRIBUTE_DATA).getNodeValue();
 
44
                                if(type != null){
 
45
                                        TGBrowserCollectionInfo info = new TGBrowserCollectionInfo();
 
46
                                        info.setType(type);
 
47
                                        info.setData(data);
 
48
                                        manager.addInfo(info);
 
49
                                }
 
50
                        }
 
51
                }
 
52
        }
 
53
        
 
54
        private static Document getDocument(File file) throws ParserConfigurationException, SAXException, IOException {
 
55
                Document document = null;
 
56
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 
57
                
 
58
                DocumentBuilder builder = factory.newDocumentBuilder();
 
59
                document = builder.parse(file);
 
60
                
 
61
                return document;
 
62
        }
 
63
        
 
64
}