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

« back to all changes in this revision

Viewing changes to TuxGuitar/src/org/herac/tuxguitar/gui/editors/chord/xml/ChordXMLWriter.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.editors.chord.xml;
 
2
 
 
3
import java.io.File;
 
4
import java.io.FileNotFoundException;
 
5
import java.io.FileOutputStream;
 
6
import java.util.Iterator;
 
7
import java.util.List;
 
8
 
 
9
import javax.xml.parsers.DocumentBuilder;
 
10
import javax.xml.parsers.DocumentBuilderFactory;
 
11
import javax.xml.parsers.FactoryConfigurationError;
 
12
import javax.xml.parsers.ParserConfigurationException;
 
13
import javax.xml.transform.OutputKeys;
 
14
import javax.xml.transform.Result;
 
15
import javax.xml.transform.Source;
 
16
import javax.xml.transform.Transformer;
 
17
import javax.xml.transform.TransformerConfigurationException;
 
18
import javax.xml.transform.TransformerException;
 
19
import javax.xml.transform.TransformerFactory;
 
20
import javax.xml.transform.dom.DOMSource;
 
21
import javax.xml.transform.stream.StreamResult;
 
22
 
 
23
import org.herac.tuxguitar.song.models.TGChord;
 
24
import org.w3c.dom.Attr;
 
25
import org.w3c.dom.Document;
 
26
import org.w3c.dom.Node;
 
27
 
 
28
public class ChordXMLWriter {
 
29
        
 
30
        public static void setChords(List chords,String fileName) {
 
31
                File file = new File(fileName);
 
32
                
 
33
                Document doc = createDocument();
 
34
                setChords(chords,doc);
 
35
                saveDocument(doc,file);
 
36
        }
 
37
        
 
38
        public static Document createDocument() {
 
39
                Document document = null;
 
40
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 
41
                try {
 
42
                        DocumentBuilder builder = factory.newDocumentBuilder();
 
43
                        document = builder.newDocument();
 
44
                }catch (ParserConfigurationException pce) {
 
45
                        pce.printStackTrace();
 
46
                }
 
47
                return document;
 
48
        }
 
49
        
 
50
        public static void saveDocument(Document document,File file) {
 
51
                try {
 
52
                        FileOutputStream fs = new FileOutputStream(file);
 
53
                        
 
54
                        // Write it out again
 
55
                        TransformerFactory xformFactory = TransformerFactory.newInstance();
 
56
                        Transformer idTransform = xformFactory.newTransformer();
 
57
                        Source input = new DOMSource(document);
 
58
                        Result output = new StreamResult(fs);
 
59
                        idTransform.setOutputProperty(OutputKeys.INDENT, "yes");
 
60
                        idTransform.transform(input, output);
 
61
                } catch (FactoryConfigurationError e) {
 
62
                        e.printStackTrace();
 
63
                } catch (TransformerConfigurationException e) {
 
64
                        e.printStackTrace();
 
65
                } catch (TransformerException e) {
 
66
                        e.printStackTrace();
 
67
                } catch (FileNotFoundException e) {
 
68
                        e.printStackTrace();
 
69
                }
 
70
        }
 
71
        
 
72
        /**
 
73
         * Write chords to xml file
 
74
         */
 
75
        private static void setChords(List chords,Document document){
 
76
                //chords tag
 
77
                Node chordsNode = document.createElement(ChordXML.CHORD_LIST_TAG);
 
78
                
 
79
                Iterator it = chords.iterator();
 
80
                while(it.hasNext()){
 
81
                        TGChord chord = (TGChord)it.next();
 
82
                        
 
83
                        //chord tag
 
84
                        Node chordNode = document.createElement(ChordXML.CHORD_TAG);
 
85
                        chordsNode.appendChild(chordNode);
 
86
                        
 
87
                        //name attribute
 
88
                        Attr nameAttr = document.createAttribute(ChordXML.CHORD_NAME_ATTRIBUTE);
 
89
                        nameAttr.setNodeValue( chord.getName());
 
90
                        chordNode.getAttributes().setNamedItem(nameAttr);
 
91
                        
 
92
                        //strings attribute
 
93
                        Attr stringsAttr = document.createAttribute(ChordXML.CHORD_STRINGS_ATTRIBUTE);
 
94
                        stringsAttr.setNodeValue(Integer.toString(chord.getStrings().length));
 
95
                        chordNode.getAttributes().setNamedItem(stringsAttr);
 
96
                        
 
97
                        //first fret attribute
 
98
                        Attr firstFretAttr = document.createAttribute(ChordXML.CHORD_FIRST_FRET_ATTRIBUTE);
 
99
                        firstFretAttr.setNodeValue(Integer.toString(chord.getFirstFret()));
 
100
                        chordNode.getAttributes().setNamedItem(firstFretAttr);
 
101
                        
 
102
                        for(int i = 0;i < chord.getStrings().length; i++){
 
103
                                //string tag
 
104
                                Node stringNode = document.createElement(ChordXML.STRING_TAG);
 
105
                                chordNode.appendChild(stringNode);
 
106
                                
 
107
                                //number attribute
 
108
                                Attr numberAttr = document.createAttribute(ChordXML.STRING_NUMBER_ATTRIBUTE);
 
109
                                numberAttr.setNodeValue(Integer.toString(i));
 
110
                                stringNode.getAttributes().setNamedItem(numberAttr);
 
111
                                
 
112
                                //fret attribute
 
113
                                Attr fretAttr = document.createAttribute(ChordXML.STRING_FRET_ATTRIBUTE);
 
114
                                fretAttr.setNodeValue(Integer.toString(chord.getFretValue(i)));
 
115
                                stringNode.getAttributes().setNamedItem(fretAttr);
 
116
                        }
 
117
                }
 
118
                
 
119
                document.appendChild(chordsNode);
 
120
        }
 
121
}