KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > Connector > ECG > ConnectorOutputDescriptor


1 /*
2  * ElementOutputDescriptor.java
3  *
4  * Created on 2. duben 2002, 20:56
5  */

6
7 package SOFA.Connector.ECG;
8
9 import org.w3c.dom.Document JavaDoc;
10 import org.w3c.dom.Element JavaDoc;
11 import org.w3c.dom.Node JavaDoc;
12
13 /**
14  *
15  * @author ghort
16  * @version
17  */

18 public class ConnectorOutputDescriptor {
19
20     public String JavaDoc name;
21     public String JavaDoc unit;
22     public String JavaDoc impl;
23
24     public SOFA.Connector.Property[] params;
25
26     public SOFA.Connector.EEG.ElementOutputDescriptor[] elements;
27
28     /** Creates new ElementOutputDescriptor */
29     public ConnectorOutputDescriptor() {
30     }
31
32     public ConnectorOutputDescriptor(Element JavaDoc element) throws ConnectorDescriptorXMLException {
33         fromXML(element);
34     }
35
36     public Element JavaDoc toXML(Document JavaDoc parentDoc) throws ConnectorDescriptorXMLException {
37         Element JavaDoc ret=parentDoc.createElement("sofa_connector");
38         ret.setAttribute("name",name);
39         ret.setAttribute("unit",unit);
40         ret.setAttribute("impl",impl);
41
42         for (int i=0;i<params.length;i++) {
43             Element JavaDoc par=parentDoc.createElement("connector_param");
44             par.setAttribute("name",params[i].name);
45             par.setAttribute("value",params[i].value);
46             ret.appendChild(par);
47         }
48
49         try {
50             for (int i=0;i<elements.length;i++) {
51                 Element JavaDoc el=elements[i].toXML(parentDoc);
52                 ret.appendChild(el);
53             }
54         } catch (SOFA.Connector.EEG.ElementDescriptorXMLException e) {
55             throw new ConnectorDescriptorXMLException("Can't create output descriptor XML.",e);
56         }
57         return ret;
58     }
59
60     public void fromXML(Element JavaDoc element) throws ConnectorDescriptorXMLException {
61         java.util.LinkedList JavaDoc elems=new java.util.LinkedList JavaDoc();
62         java.util.LinkedList JavaDoc pars=new java.util.LinkedList JavaDoc();
63
64         if (!element.hasAttribute("name")) {
65             throw new ConnectorDescriptorXMLException("Element doesn't have 'name' attribute.");
66         }
67         name=element.getAttribute("name");
68
69         if (!element.hasAttribute("unit")) {
70             throw new ConnectorDescriptorXMLException("Element doesn't have 'unit' attribute.");
71         }
72         unit=element.getAttribute("unit");
73
74         if (!element.hasAttribute("impl")) {
75             throw new ConnectorDescriptorXMLException("Element doesn't have 'impl' attribute.");
76         }
77         impl=element.getAttribute("impl");
78         
79         Node JavaDoc node=element.getFirstChild();
80         while (node!=null) {
81             if (node.getNodeType()==Node.ELEMENT_NODE) {
82                 Element JavaDoc el=(Element JavaDoc)node;
83                 if (el.getNodeName().equals("connector_element")) {
84                     try {
85                         elems.add(new SOFA.Connector.EEG.ElementOutputDescriptor(el));
86                     } catch (SOFA.Connector.EEG.ElementDescriptorXMLException e) {
87                         throw new ConnectorDescriptorXMLException("Can't read connector_element.",e);
88                     }
89                 } else if (el.getNodeName().equals("connector_param") && el.hasAttribute("name") && el.hasAttribute("value")) {
90                     pars.add(new SOFA.Connector.Property(el.getAttribute("name"),el.getAttribute("value")));
91                 }
92             }
93             node=node.getNextSibling();
94         }
95         
96         int i;
97         java.util.Iterator JavaDoc iter=elems.iterator();
98         elements=new SOFA.Connector.EEG.ElementOutputDescriptor[elems.size()];
99         for (i=0;iter.hasNext();i++) {
100             elements[i]=(SOFA.Connector.EEG.ElementOutputDescriptor)iter.next();
101         }
102
103         iter=pars.iterator();
104         params=new SOFA.Connector.Property[pars.size()];
105         for (i=0;iter.hasNext();i++) {
106             params[i]=(SOFA.Connector.Property)iter.next();
107         }
108     }
109     
110 }
111
Popular Tags