KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > Connector > EEG > EEM > ElementDescription


1 /*
2  * ElementDescription.java
3  *
4  * Created on 2. duben 2002, 23:08
5  */

6
7 package SOFA.Connector.EEG.EEM;
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 ElementDescription {
19
20     public String JavaDoc name;
21     public String JavaDoc type;
22     public String JavaDoc mangled;
23
24     public SOFA.Connector.Property conditions[];
25
26     public java.util.LinkedList JavaDoc make; // of ElementDescriptionCommand or ElementDescriptionBuild
27

28     /** Creates new ElementDescription */
29     public ElementDescription() {
30     }
31
32     static public ElementDescription fromXML(org.w3c.dom.Element JavaDoc 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 JavaDoc();
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 JavaDoc condProps=new java.util.LinkedList JavaDoc();
57         Node JavaDoc 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 JavaDoc el=(Element JavaDoc)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 JavaDoc cmdParams=new java.util.LinkedList JavaDoc();
73                         nodeL3=nodeL2.getFirstChild();
74                         while (nodeL3!=null) {
75                             if (nodeL3.getNodeType()==Node.ELEMENT_NODE && nodeL3.getNodeName().equals("param")) {
76                                 Element JavaDoc el2=(Element JavaDoc)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 JavaDoc 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 JavaDoc el=(Element JavaDoc)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 JavaDoc bldProps=new java.util.LinkedList JavaDoc();
106                         nodeL3=nodeL2.getFirstChild();
107                         while (nodeL3!=null) {
108                             if (nodeL3.getNodeType()==Node.ELEMENT_NODE && nodeL3.getNodeName().equals("property")) {
109                                 Element JavaDoc el2=(Element JavaDoc)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 JavaDoc 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 JavaDoc el2=(Element JavaDoc)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 JavaDoc 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 JavaDoc file) throws ElementDescriptionException {
147         try {
148             javax.xml.parsers.DocumentBuilderFactory JavaDoc documentBuilderFactory=javax.xml.parsers.DocumentBuilderFactory.newInstance();
149             javax.xml.parsers.DocumentBuilder JavaDoc documentBuilder=documentBuilderFactory.newDocumentBuilder();
150             Document JavaDoc configDoc=documentBuilder.parse(new java.io.File JavaDoc(file));
151             return fromXML(configDoc.getDocumentElement());
152         } catch (Exception JavaDoc e) {
153             throw new ElementDescriptionException("Can't load element from file '"+file+"'.",e);
154         }
155     }
156 }
157
Popular Tags