1 6 7 package SOFA.Connector.ECG; 8 9 import org.w3c.dom.Document ; 10 import org.w3c.dom.Element ; 11 import org.w3c.dom.Node ; 12 13 18 public class ConnectorOutputDescriptor { 19 20 public String name; 21 public String unit; 22 public String impl; 23 24 public SOFA.Connector.Property[] params; 25 26 public SOFA.Connector.EEG.ElementOutputDescriptor[] elements; 27 28 29 public ConnectorOutputDescriptor() { 30 } 31 32 public ConnectorOutputDescriptor(Element element) throws ConnectorDescriptorXMLException { 33 fromXML(element); 34 } 35 36 public Element toXML(Document parentDoc) throws ConnectorDescriptorXMLException { 37 Element 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 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 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 element) throws ConnectorDescriptorXMLException { 61 java.util.LinkedList elems=new java.util.LinkedList (); 62 java.util.LinkedList pars=new java.util.LinkedList (); 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 node=element.getFirstChild(); 80 while (node!=null) { 81 if (node.getNodeType()==Node.ELEMENT_NODE) { 82 Element el=(Element )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 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 |