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

« back to all changes in this revision

Viewing changes to TuxGuitar/src/org/herac/tuxguitar/gui/items/xml/ToolBarsWriter.java

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2008-06-19 00:30:30 UTC
  • mfrom: (1.1.1 upstream) (2.1.3 hardy)
  • Revision ID: james.westby@ubuntu.com-20080619003030-agens2gvd5m4dacu
New upstream release (Closes: #481728) also (LP: #176979, #212207)

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.FileNotFoundException;
 
5
import java.io.FileOutputStream;
 
6
 
 
7
import javax.xml.parsers.DocumentBuilder;
 
8
import javax.xml.parsers.DocumentBuilderFactory;
 
9
import javax.xml.parsers.ParserConfigurationException;
 
10
import javax.xml.transform.OutputKeys;
 
11
import javax.xml.transform.Result;
 
12
import javax.xml.transform.Source;
 
13
import javax.xml.transform.Transformer;
 
14
import javax.xml.transform.TransformerException;
 
15
import javax.xml.transform.TransformerFactory;
 
16
import javax.xml.transform.dom.DOMSource;
 
17
import javax.xml.transform.stream.StreamResult;
 
18
 
 
19
import org.herac.tuxguitar.gui.items.ToolItems;
 
20
import org.w3c.dom.Attr;
 
21
import org.w3c.dom.Document;
 
22
import org.w3c.dom.Node;
 
23
 
 
24
public class ToolBarsWriter {
 
25
        
 
26
        private static final String ITEM_LIST_TAG = "toolbars";
 
27
        private static final String ITEM_TAG = "toolbar";
 
28
        private static final String ATTR_NAME = "name";
 
29
        private static final String ATTR_ENABLED = "enabled";
 
30
        
 
31
        public static void saveToolBars(ToolItems[] items,File file) {
 
32
                try {
 
33
                        Document doc = createDocument();
 
34
                        setToolBars(items,doc);
 
35
                        saveDocument(doc,file);
 
36
                } catch (Throwable throwable) {
 
37
                        throwable.printStackTrace();
 
38
                }
 
39
        }
 
40
        
 
41
        public static Document createDocument() throws ParserConfigurationException {
 
42
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 
43
                DocumentBuilder builder = factory.newDocumentBuilder();
 
44
                Document document = builder.newDocument();
 
45
                return document;
 
46
        }
 
47
        
 
48
        public static void saveDocument(Document document,File file) throws FileNotFoundException, TransformerException {
 
49
                FileOutputStream fs = new FileOutputStream(file);
 
50
                
 
51
                // Write it out again
 
52
                TransformerFactory xformFactory = TransformerFactory.newInstance();
 
53
                Transformer idTransform = xformFactory.newTransformer();
 
54
                Source input = new DOMSource(document);
 
55
                Result output = new StreamResult(fs);
 
56
                idTransform.setOutputProperty(OutputKeys.INDENT, "yes");
 
57
                idTransform.transform(input, output);
 
58
        }
 
59
        
 
60
        private static void setToolBars(ToolItems[] items,Document document){
 
61
                //chords tag
 
62
                Node listNode = document.createElement(ITEM_LIST_TAG);
 
63
                
 
64
                for(int i = 0; i < items.length ; i ++){
 
65
                        
 
66
                        //chord tag
 
67
                        Node node = document.createElement(ITEM_TAG);
 
68
                        listNode.appendChild(node);
 
69
                        
 
70
                        //name attribute
 
71
                        Attr attribute = document.createAttribute(ATTR_NAME);
 
72
                        attribute.setNodeValue( items[i].getName());
 
73
                        node.getAttributes().setNamedItem(attribute);
 
74
                        
 
75
                        //enabled attribute
 
76
                        attribute = document.createAttribute(ATTR_ENABLED);
 
77
                        attribute.setNodeValue( Boolean.toString(items[i].isEnabled()));
 
78
                        node.getAttributes().setNamedItem(attribute);
 
79
                }
 
80
                
 
81
                document.appendChild(listNode);
 
82
        }
 
83
}