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

« back to all changes in this revision

Viewing changes to TuxGuitar/src/org/herac/tuxguitar/gui/help/doc/DocItemReader.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.help.doc;
 
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.w3c.dom.Document;
 
12
import org.w3c.dom.NamedNodeMap;
 
13
import org.w3c.dom.Node;
 
14
import org.w3c.dom.NodeList;
 
15
import org.xml.sax.SAXException;
 
16
 
 
17
public class DocItemReader {
 
18
        private static final String ITEM_TAG = "item";
 
19
        private static final String ATTRIBUTE_NAME = "name";
 
20
        private static final String ATTRIBUTE_URL = "url";
 
21
        
 
22
        public void loadHelpItems(List items,File file){
 
23
                if (file != null && file.exists()){
 
24
                        try {
 
25
                                Document doc = getDocument(file);
 
26
                                loadHelpItems(items,doc.getFirstChild());
 
27
                        } catch (Throwable throwable) {
 
28
                                throwable.printStackTrace();
 
29
                        }
 
30
                }
 
31
        }
 
32
        
 
33
        private void loadHelpItems(List items,Node node){
 
34
                NodeList listNode = node.getChildNodes();
 
35
                for (int i = 0; i < listNode.getLength(); i++) {
 
36
                        Node child = listNode.item(i);
 
37
                        String nameNode = child.getNodeName();
 
38
                        if (nameNode.equals(ITEM_TAG)) {
 
39
                                NamedNodeMap attributes = child.getAttributes();
 
40
                                DocItem item = new DocItem(getAttribute(ATTRIBUTE_NAME,attributes),getAttribute(ATTRIBUTE_URL,attributes));
 
41
                                loadHelpItems(item.getChildren(),child);
 
42
                                items.add(item);
 
43
                        }
 
44
                }
 
45
        }
 
46
        
 
47
        private String getAttribute(String attribute,NamedNodeMap map){
 
48
                Node node = map.getNamedItem(attribute);
 
49
                if(node != null){
 
50
                        return node.getNodeValue();
 
51
                }
 
52
                return null;
 
53
        }
 
54
        
 
55
        private Document getDocument(File file) throws ParserConfigurationException, SAXException, IOException {
 
56
                Document document = null;
 
57
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 
58
                DocumentBuilder builder = factory.newDocumentBuilder();
 
59
                document = builder.parse(file);
 
60
                return document;
 
61
        }
 
62
}