1 6 7 package SOFA.Connector.EEG.EEM; 8 9 import org.w3c.dom.Document ; 10 import org.w3c.dom.Element ; 11 import org.w3c.dom.Node ; 12 13 18 public class ElementDescription { 19 20 public String name; 21 public String type; 22 public String mangled; 23 24 public SOFA.Connector.Property conditions[]; 25 26 public java.util.LinkedList make; 28 29 public ElementDescription() { 30 } 31 32 static public ElementDescription fromXML(org.w3c.dom.Element rootElement) throws ElementDescriptionException { 33 if (!rootElement.getNodeName().equals("element") && !rootElement.getNodeName().equals("module")) { 34 throw new ElementDescriptionException("Root element must be either 'element' or 'module'."); 35 } 36 37 ElementDescription eld=new ElementDescription(); 38 39 eld.make=new java.util.LinkedList (); 40 41 if (!rootElement.hasAttribute("name")) { 42 throw new ElementDescriptionException("Element doesn't have 'name' attribute."); 43 } 44 eld.name=rootElement.getAttribute("name"); 45 46 if (!rootElement.hasAttribute("type") && rootElement.getNodeName().equals("element")) { 47 throw new ElementDescriptionException("Element doesn't have 'type' attribute."); 48 } 49 eld.type=rootElement.getAttribute("type"); 50 51 if (!rootElement.hasAttribute("mangled")) { 52 throw new ElementDescriptionException("Element doesn't have 'mangled' attribute."); 53 } 54 eld.mangled=rootElement.getAttribute("mangled"); 55 56 java.util.LinkedList condProps=new java.util.LinkedList (); 57 Node nodeL1, nodeL2, nodeL3; 58 nodeL1=rootElement.getFirstChild(); 59 while (nodeL1!=null) { 60 if (nodeL1.getNodeType()==Node.ELEMENT_NODE && nodeL1.getNodeName().equals("make")) { 61 nodeL2=nodeL1.getFirstChild(); 62 while (nodeL2!=null) { 63 if (nodeL2.getNodeType()==Node.ELEMENT_NODE && nodeL2.getNodeName().equals("command")) { 64 Element el=(Element )nodeL2; 65 ElementDescriptionCommand cmd=new ElementDescriptionCommand(); 66 if (el.hasAttribute("action")) { 67 cmd.action=el.getAttribute("action"); 68 } else { 69 throw new ElementDescriptionException("Command element must contain attribute 'action'."); 70 } 71 72 java.util.LinkedList cmdParams=new java.util.LinkedList (); 73 nodeL3=nodeL2.getFirstChild(); 74 while (nodeL3!=null) { 75 if (nodeL3.getNodeType()==Node.ELEMENT_NODE && nodeL3.getNodeName().equals("param")) { 76 Element el2=(Element )nodeL3; 77 cmdParams.add(new SOFA.Connector.Property(el2.getAttribute("name"),el2.getAttribute("value"))); 78 } 79 nodeL3=nodeL3.getNextSibling(); 80 } 81 82 cmd.params=new SOFA.Connector.Property[cmdParams.size()]; 83 java.util.Iterator iter=cmdParams.iterator(); 84 for (int i=0;iter.hasNext();i++) { 85 cmd.params[i]=(SOFA.Connector.Property)iter.next(); 86 } 87 eld.make.add(cmd); 88 } else if (nodeL2.getNodeType()==Node.ELEMENT_NODE && nodeL2.getNodeName().equals("build")) { 89 Element el=(Element )nodeL2; 90 ElementDescriptionBuild bld=new ElementDescriptionBuild(); 91 if (el.hasAttribute("element")) { 92 bld.element=el.getAttribute("element"); 93 } else { 94 throw new ElementDescriptionException("Build element must contain attribute 'element'."); 95 } 96 97 if (el.hasAttribute("pathVar")) { 98 bld.pathVar=el.getAttribute("pathVar"); 99 } 100 101 if (el.hasAttribute("packageVar")) { 102 bld.packageVar=el.getAttribute("packageVar"); 103 } 104 105 java.util.LinkedList bldProps=new java.util.LinkedList (); 106 nodeL3=nodeL2.getFirstChild(); 107 while (nodeL3!=null) { 108 if (nodeL3.getNodeType()==Node.ELEMENT_NODE && nodeL3.getNodeName().equals("property")) { 109 Element el2=(Element )nodeL3; 110 bldProps.add(new SOFA.Connector.Property(el2.getAttribute("name"),el2.getAttribute("value"))); 111 } 112 nodeL3=nodeL3.getNextSibling(); 113 } 114 115 bld.props=new SOFA.Connector.Property[bldProps.size()]; 116 java.util.Iterator iter=bldProps.iterator(); 117 for (int i=0;iter.hasNext();i++) { 118 bld.props[i]=(SOFA.Connector.Property)iter.next(); 119 } 120 eld.make.add(bld); 121 } 122 nodeL2=nodeL2.getNextSibling(); 123 } 124 } else if (nodeL1.getNodeType()==Node.ELEMENT_NODE && nodeL1.getNodeName().equals("conditions")) { 125 nodeL2=nodeL1.getFirstChild(); 126 while (nodeL2!=null) { 127 if (nodeL2.getNodeType()==Node.ELEMENT_NODE && nodeL2.getNodeName().equals("property")) { 128 Element el2=(Element )nodeL2; 129 condProps.add(new SOFA.Connector.Property(el2.getAttribute("name"),el2.getAttribute("value"))); 130 } 131 nodeL2=nodeL2.getNextSibling(); 132 } 133 } 134 nodeL1=nodeL1.getNextSibling(); 135 } 136 137 eld.conditions=new SOFA.Connector.Property[condProps.size()]; 138 java.util.Iterator iter=condProps.iterator(); 139 for (int i=0;iter.hasNext();i++) { 140 eld.conditions[i]=(SOFA.Connector.Property)iter.next(); 141 } 142 143 return eld; 144 } 145 146 static public ElementDescription fromXMLFile(String file) throws ElementDescriptionException { 147 try { 148 javax.xml.parsers.DocumentBuilderFactory documentBuilderFactory=javax.xml.parsers.DocumentBuilderFactory.newInstance(); 149 javax.xml.parsers.DocumentBuilder documentBuilder=documentBuilderFactory.newDocumentBuilder(); 150 Document configDoc=documentBuilder.parse(new java.io.File (file)); 151 return fromXML(configDoc.getDocumentElement()); 152 } catch (Exception e) { 153 throw new ElementDescriptionException("Can't load element from file '"+file+"'.",e); 154 } 155 } 156 } 157 | Popular Tags |