KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ShareGroups.java
3  *
4  * Created on 18. bøezen 2004, 23:45
5  */

6
7 package SOFA.SOFAnet.Repository;
8
9 import java.util.*;
10 import org.w3c.dom.*;
11 import org.xml.sax.SAXException JavaDoc;
12 import java.io.Serializable JavaDoc;
13
14 /**
15  * Implemention of list of share groups (strings).
16  *
17  * @author Ladislav Sobr
18  */

19 public class ShareGroups implements Cloneable JavaDoc, Serializable JavaDoc
20 {
21   private List list; // empty list means "all groups"
22

23   /** Creates a new instance of ShareGroups */
24   public ShareGroups()
25   {
26     list = Collections.synchronizedList(new LinkedList());
27   }
28   
29   public Object JavaDoc clone()
30   {
31     ShareGroups clone = null;
32     try
33     {
34       clone = (ShareGroups)super.clone();
35     }
36     catch (CloneNotSupportedException JavaDoc e)
37     {
38       throw new InternalError JavaDoc();
39     }
40     clone.list = Collections.synchronizedList(new LinkedList());
41     clone.list.addAll(list);
42     return clone;
43   }
44   
45
46   public List getList()
47   {
48     return list;
49   }
50
51   public void add(String JavaDoc group)
52   {
53     list.add(group);
54   }
55
56   public void add(ShareGroups shareGroups)
57   {
58     if (shareGroups != null)
59     {
60       List li = shareGroups.getList();
61       synchronized (li)
62       {
63         Iterator it = li.iterator();
64         while (it.hasNext())
65         {
66           String JavaDoc group = (String JavaDoc)it.next();
67           list.add(group);
68         }
69       }
70     }
71   }
72   
73   public boolean contains(String JavaDoc shareGroup)
74   {
75     return list.contains(shareGroup);
76   }
77       
78   /**
79    * Loads the ShareGroups from the XML DOM tree.
80    * <p>
81    * XML format:
82    * <p>
83    * <pre>
84    * *&lt;share_group name="string"/&gt;
85    * </pre>
86    */

87   public void loadFromXML(Element element) throws SAXException JavaDoc
88   {
89     synchronized (list)
90     {
91       NodeList nl = element.getChildNodes();
92       for (int i = 0; i < nl.getLength(); i++)
93       {
94         Node node = nl.item(i);
95         if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().compareTo("share_group") == 0)
96         {
97           Element el = (Element)node;
98           String JavaDoc name = el.getAttribute("name");
99           list.add(name);
100         }
101       }
102     }
103   }
104   
105   /**
106    * Saves the ShareGroups to XML DOM tree.
107    */

108   public void saveToXML(Element element)
109   {
110     synchronized (list)
111     {
112       Document doc = element.getOwnerDocument();
113
114       Iterator it = list.iterator();
115       while (it.hasNext())
116       {
117         String JavaDoc name = (String JavaDoc)it.next();
118
119         Element el = doc.createElement("share_group");
120         el.setAttribute("name", name);
121         element.appendChild(el);
122       }
123     }
124   }
125   
126 }
127
Popular Tags