KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnet > Repository > BundleContent


1 /*
2  * BundleContent.java
3  *
4  * Created on 23. listopad 2003, 16:18
5  */

6
7 package SOFA.SOFAnet.Repository;
8
9 import java.io.*;
10 import java.util.*;
11 import SOFA.SOFAnode.TR.Impl.ComponentInfoImpl;
12 import SOFA.SOFAnode.TR.ComponentInfo;
13 import SOFA.Util.XML;
14 import org.w3c.dom.*;
15 import org.xml.sax.SAXException JavaDoc;
16
17 /**
18  * Representation of the bundle content.
19  * One item of persistent storage.
20  * <p>
21  * The bundle contant contains:
22  * <ul>
23  * <li>list of components contained in the bundle
24  * </ul>
25  *
26  * @author Ladislav Sobr
27  */

28 public class BundleContent extends XMLStorageItem implements StorageItem, Serializable
29 {
30   private List components;
31   
32   /** Creates a new instance of BundleContent */
33   public BundleContent(String JavaDoc name, File file)
34   {
35     super(name, file, "bundle_content");
36     components = Collections.synchronizedList(new LinkedList());
37   }
38
39   /**
40    * Resets content of the item.
41    */

42   protected void reset()
43   {
44     components.clear();
45   }
46   
47   /**
48    * Loads the item from the XML DOM tree.
49    * <p>
50    * XML format of storage item:
51    * <p>
52    * <pre>
53    * &lt;bundle_content&gt;
54    * BundleContent XML format
55    * &lt;/bundle_content&gt;
56    * </pre>
57    * <p>
58    * XML format:
59    * <p>
60    * <pre>
61    * *&lt;component name="string" implementation_version="string"/&gt;
62    * </pre>
63    */

64   public void loadFromXML(Element element) throws SAXException JavaDoc
65   {
66     synchronized (components)
67     {
68       components.clear();
69       NodeList nl = element.getChildNodes();
70       for (int i = 0; i < nl.getLength(); i++)
71       {
72         Node node = nl.item(i);
73         if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().compareTo("component") == 0)
74         {
75           Element e = (Element)node;
76           String JavaDoc name = e.getAttribute("name");
77           String JavaDoc version = e.getAttribute("implementation_version");
78           if (name.length() == 0) throw new SAXException JavaDoc("Missing 'name' attribute in 'component' tag");
79           if (version.length() == 0) throw new SAXException JavaDoc("Missing 'implementation_version' attribute in 'component' tag");
80           components.add(new ComponentInfoImpl(name, version));
81         }
82       }
83     }
84   }
85   
86   /**
87    * Saves the item to XML DOM tree.
88    */

89   public void saveToXML(Element element)
90   {
91     synchronized (components)
92     {
93       Document doc = element.getOwnerDocument();
94       Iterator it = components.iterator();
95       while (it.hasNext())
96       {
97         ComponentInfo ci = (ComponentInfo)it.next();
98         Element el = doc.createElement("component");
99         el.setAttribute("name", ci.getName());
100         el.setAttribute("implementation_version", ci.getImplementationVersion());
101         element.appendChild(el);
102       }
103     }
104   }
105   
106   
107   public List getComponents()
108   {
109     return components;
110   }
111
112 }
113
Popular Tags