KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

17 public class ConnectorInputDescriptor {
18
19     public String JavaDoc instanceName;
20     
21     public String JavaDoc unit;
22
23     public String JavaDoc implementation;
24     
25     public SOFA.Connector.Property[] props;
26     
27     public SOFA.Connector.Property[] techProps; // mozna do props
28

29     public UnitInterface provides[];
30     public UnitInterface requires[];
31
32     public SOFA.Connector.EEG.ElementInputDescriptor[] elements;
33     
34     /** Creates new ElementInputDescriptor */
35     public ConnectorInputDescriptor() {
36     }
37
38     static public ConnectorInputDescriptor fromXML(Element JavaDoc rootElement) throws ConnectorDescriptorXMLException {
39         ConnectorInputDescriptor cid=new ConnectorInputDescriptor();
40         java.util.LinkedList JavaDoc descrProps=new java.util.LinkedList JavaDoc();
41         java.util.LinkedList JavaDoc provIfaces=new java.util.LinkedList JavaDoc();
42         java.util.LinkedList JavaDoc reqIfaces=new java.util.LinkedList JavaDoc();
43         java.util.LinkedList JavaDoc elems=new java.util.LinkedList JavaDoc();
44
45         if (!rootElement.hasAttribute("name")) {
46             throw new ConnectorDescriptorXMLException("Element doesn't have 'name' attribute.");
47         }
48         cid.instanceName=rootElement.getAttribute("name");
49
50         if (!rootElement.hasAttribute("unit")) {
51             throw new ConnectorDescriptorXMLException("Element doesn't have 'unit' attribute.");
52         }
53         cid.unit=rootElement.getAttribute("unit");
54
55         if (!rootElement.hasAttribute("impl")) {
56             throw new ConnectorDescriptorXMLException("Element doesn't have 'impl' attribute.");
57         }
58         cid.implementation=rootElement.getAttribute("impl");
59         
60         if (rootElement.hasAttribute("provides")) {
61             provIfaces.add(new UnitInterface(rootElement.getAttribute("provides"),cid.unit));
62         }
63         
64         if (rootElement.hasAttribute("requires")) {
65             reqIfaces.add(new UnitInterface(rootElement.getAttribute("requires"),cid.unit));
66         }
67         
68         Node JavaDoc node=rootElement.getFirstChild();
69         while (node!=null) {
70             if (node.getNodeType()==Node.ELEMENT_NODE) {
71                 Element JavaDoc el=(Element JavaDoc)node;
72                 if (((Element JavaDoc)el).getNodeName().equals("property") && el.hasAttribute("name") && el.hasAttribute("value")) {
73                     descrProps.add(new SOFA.Connector.Property(el.getAttribute("name"),el.getAttribute("value")));
74                 }
75                 else if (el.getNodeName().equals("otherUnit") && el.hasAttribute("name")) {
76                     if (el.hasAttribute("provides")) {
77                         provIfaces.add(new UnitInterface(el.getAttribute("provides"),el.getAttribute("name")));
78                     }
79                     if (rootElement.hasAttribute("requires")) {
80                         reqIfaces.add(new UnitInterface(rootElement.getAttribute("requires"),el.getAttribute("name")));
81                     }
82                 }
83                 else if (el.getNodeName().equals("element") && el.hasAttribute("name") && el.hasAttribute("impl")) {
84                     try {
85                         elems.add(SOFA.Connector.EEG.ElementInputDescriptor.fromXML(el));
86                     } catch (Exception JavaDoc e) {
87                         throw new ConnectorDescriptorXMLException("Can't parse connector element.",e);
88                     }
89                 }
90             }
91             node=node.getNextSibling();
92         }
93         
94         int i;
95         Object JavaDoc[] arr;
96         
97         cid.techProps=new SOFA.Connector.Property[0]; // FIXME: mela by se nacitat take z XML
98

99         cid.props=new SOFA.Connector.Property[(arr=descrProps.toArray()).length];
100         for (i=0;i<arr.length;i++) {
101             cid.props[i]=(SOFA.Connector.Property)arr[i];
102         }
103
104         cid.elements=new SOFA.Connector.EEG.ElementInputDescriptor[(arr=elems.toArray()).length];
105         for (i=0;i<arr.length;i++) {
106             cid.elements[i]=(SOFA.Connector.EEG.ElementInputDescriptor)arr[i];
107         }
108
109         cid.provides=new UnitInterface[(arr=provIfaces.toArray()).length];
110         for (i=0;i<arr.length;i++) {
111             cid.provides[i]=(UnitInterface)arr[i];
112         }
113
114         cid.requires=new UnitInterface[(arr=reqIfaces.toArray()).length];
115         for (i=0;i<arr.length;i++) {
116             cid.requires[i]=(UnitInterface)arr[i];
117         }
118         
119         return cid;
120     }
121 }
122
Popular Tags