KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > TR > Impl > AssemblyDescriptorImpl


1 /* $Id: AssemblyDescriptorImpl.java,v 1.2 2004/05/20 14:23:52 bures Exp $ */
2 package SOFA.SOFAnode.TR.Impl;
3
4 import org.w3c.dom.*;
5
6 import javax.xml.parsers.*;
7
8 import org.xml.sax.*;
9
10 import java.util.*;
11 import java.io.IOException JavaDoc;
12 import java.io.File JavaDoc;
13
14 /** The assembly descriptor. Greatly inspired by the implementation of
15  * {@link SOFA.SOFAnode.Run.Deployment.DeploymentDescriptorImpl}. It contains assembly information
16  * about one SOFA component. This information is always stored in an XML file of the name
17  * <em>index.dc</em>.
18  *
19  * @author Petr Panuska
20  */

21 class AssemblyDescriptorImpl {
22
23   /**
24    * The root element of the document.
25    */

26   private Element element;
27
28   /**
29    * The document.
30    */

31   private Document document;
32
33   /**
34    * The component implemenatation version.
35    */

36   private String JavaDoc version;
37
38   /**
39    * Creates the assembly descriptor from the given file.
40    * @param file the file containing the assembly descriptor.
41    * @throws IOException if some I/O operation fails.
42    * @throws ParserConfigurationException if the component assembly file is corrupted or missing.
43    * @throws SAXException if the component assembly file is corrupted or missing.
44    *
45    */

46   public AssemblyDescriptorImpl (File JavaDoc file) throws IOException JavaDoc, ParserConfigurationException, SAXException {
47     if (!file.exists()) {
48       throw new IOException JavaDoc("File \"" + file.getName() + "\" doesn't exist.");
49     }
50
51     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
52     DocumentBuilder builder = factory.newDocumentBuilder();
53     document = builder.parse(file);
54     element = document.getDocumentElement();
55     element.normalize();
56     initialize();
57   }
58
59   /**
60    * Initializes this descriptor.
61    */

62   private void initialize () {
63     NodeList nl = element.getChildNodes();
64     for (int i = 0; i < nl.getLength(); i++) {
65       Node n = nl.item(i);
66       if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().compareTo("version") == 0)) {
67         version = n.getFirstChild().getNodeValue();
68         break;
69       }
70     }
71   }
72
73   /**
74    * Gets the component implementation version.
75    * @return the component implementation version.
76    */

77   public String JavaDoc getImplementationVersion () {
78     return version;
79   }
80
81   /**
82    * Gets a map of all interface types (structures, etc.) the component needs. Every key in the map
83    * is the full Java name of the class implementing this type, the associated value is the full
84    * interface name (::provider::name?specificationVersion).
85    * @return a map containing all interfaces this component needs (types used in method parameters or
86    * return values).
87    */

88   public Map getCDLEntities () {
89     NodeList nl = element.getChildNodes();
90     for (int i = 0; i < nl.getLength(); i++) {
91       Node n = nl.item(i);
92       if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().compareTo("cdl_entities") == 0)) {
93         NodeList nl1 = n.getChildNodes();
94         HashMap ret = new HashMap();
95         for (int j = 0; j < nl1.getLength(); j++) {
96           Node n1 = nl1.item(j);
97           if ((n1.getNodeType() == Node.ELEMENT_NODE) && (n1.getNodeName().compareTo("entity") == 0)) {
98             ret.put(((Element) n1).getAttribute("javaname"), ((Element) n1).getAttribute("cdlname"));
99           }
100         }
101         return ret;
102       }
103     }
104     return null;
105   }
106
107   /**
108    * Gets a set of all sub-components of this component. Every value in this set is a fullname
109    * of a component.
110    * @return a set of all sub-components.
111    */

112   public Set getSubComponents () {
113     Set set = new HashSet();
114     NodeList nl = document.getElementsByTagName("component_ref");
115     int length = nl.getLength();
116     for (int i = 0; i < length; i++) {
117       assert(!(nl.item(i) instanceof Element));
118       Element el = (Element) nl.item(i);
119       set.add(el.getAttribute("arch") + "[" + el.getAttribute("version") + "]");
120     }
121     return set;
122   }
123
124 }
125
Popular Tags