Subversion Repositories svn.mios

Rev

Rev 709 | Rev 722 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
709 adamjking 1
package org.midibox.midi.xml;
2
 
721 adamjking 3
import java.util.Iterator;
4
import java.util.Vector;
709 adamjking 5
 
721 adamjking 6
import javax.sound.midi.MidiDevice;
7
 
8
import org.midibox.midi.MidiDeviceRouting;
9
import org.midibox.utils.xml.XMLUtils;
10
import org.w3c.dom.Element;
11
import org.w3c.dom.Node;
12
 
13
public class MidiDeviceRoutingXML extends XMLUtils {
14
 
15
    protected MidiDeviceRouting midiDeviceRouting;
16
 
17
    protected String logicalConnectionsTag = "logicalConnections";
18
 
19
    protected String logicalConnectionTag = "logicalConnection";
20
 
21
    protected String sourceMidiDeviceTag = "sourceMidiDevice";
22
 
23
    protected String targetMidiDeviceTag = "targetMidiDevice";
24
 
25
    protected String portsReleasedAttr = "portsReleased";
26
 
27
    public MidiDeviceRoutingXML(MidiDeviceRouting midiDeviceRouting,
28
            String rootElementTag) {
29
 
30
        super(rootElementTag);
31
 
32
        this.midiDeviceRouting = midiDeviceRouting;
33
 
34
        tags.add(logicalConnectionsTag);
35
        tags.add(logicalConnectionTag);
36
    }
37
 
38
    protected void parseElement(Element element) {
39
 
40
        super.parseElement(element);
41
 
42
        String name = element.getNodeName();
43
 
44
        if (name == rootElementTag) {
45
 
46
            midiDeviceRouting.disconnectAll();
47
 
48
        } else if (name == logicalConnectionTag) {
49
 
50
            Element transmittingDeviceElement = (Element) element
51
                    .getFirstChild();
52
 
53
            MidiDevice transmittingDevice = findDeviceHash(midiDeviceRouting
54
                    .getMidiReadDevices(), Integer
55
                    .parseInt(transmittingDeviceElement
56
                            .getAttribute("hashCode")));
57
 
58
            Element receivingDeviceElement = (Element) element.getLastChild();
59
 
60
            MidiDevice receivingDevice = findDeviceHash(midiDeviceRouting
61
                    .getMidiWriteDevices(), Integer
62
                    .parseInt(receivingDeviceElement.getAttribute("hashCode")));
63
            ;
64
 
65
            if (transmittingDevice != null && receivingDevice != null) {
66
 
67
                midiDeviceRouting.connectDevices(transmittingDevice,
68
                    receivingDevice);
69
            }
70
        }
71
    }
72
 
73
    public void saveXML(Node node) {
74
 
75
        super.saveXML(node);
76
 
77
        rootElement.setAttribute(portsReleasedAttr, midiDeviceRouting
78
                .isPortsReleased() ? "true" : "false");
79
 
80
        Element logicalConnectionsElement = document
81
                .createElement(logicalConnectionsTag);
82
 
83
        rootElement.appendChild(logicalConnectionsElement);
84
 
85
        Iterator it = midiDeviceRouting.getLogicalConnections().iterator();
86
 
87
        while (it.hasNext()) {
88
 
89
            MidiDeviceRouting.LogicalConnection logicalConnection = (MidiDeviceRouting.LogicalConnection) it
90
                    .next();
91
 
92
            Element logicalConnectionElement = document
93
                    .createElement(logicalConnectionTag);
94
 
95
            logicalConnectionsElement.appendChild(logicalConnectionElement);
96
 
97
 
98
            MidiDeviceXML sourceMidiDeviceXML = new MidiDeviceXML(
99
                    logicalConnection.getSourceDevice(), sourceMidiDeviceTag);
100
 
101
            sourceMidiDeviceXML.saveXML(logicalConnectionElement);
102
 
103
 
104
            MidiDeviceXML targetMidiDeviceXML = new MidiDeviceXML(
105
                    logicalConnection.getTargetDevice(), targetMidiDeviceTag);
106
 
107
            targetMidiDeviceXML.saveXML(logicalConnectionElement);
108
        }
109
    }
110
 
111
    protected MidiDevice findDeviceHash(Vector midiDevices, int hash) {
112
 
113
        Iterator it = midiDevices.iterator();
114
 
115
        while (it.hasNext()) {
116
 
117
            MidiDevice midiDevice = (MidiDevice) it.next();
118
 
119
            if (midiDevice.getDeviceInfo().toString().hashCode() == hash) {
120
 
121
                return midiDevice;
122
            }
123
        }
124
 
125
        return null;
126
    }
709 adamjking 127
}