KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * NodeList.java
3  *
4  * Created on 3. prosinec 2003, 17:53
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  * Implementation of list of node names.
16  *
17  * @author Ladislav Sobr
18  */

19 public class NodeNameList implements Cloneable JavaDoc, Serializable JavaDoc
20 {
21   private List list;
22   
23   /** Creates a new instance of NodeList */
24   public NodeNameList()
25   {
26     list = Collections.synchronizedList(new LinkedList());
27   }
28
29   public Object JavaDoc clone()
30   {
31     NodeNameList clone = null;
32     try
33     {
34       clone = (NodeNameList)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   public List getList()
46   {
47     return list;
48   }
49
50   public void add(String JavaDoc nodeName)
51   {
52     list.add(nodeName);
53   }
54
55   public void add(NodeNameList nodeNameList)
56   {
57     if (nodeNameList != null)
58     {
59       List li = nodeNameList.getList();
60       synchronized (li)
61       {
62         Iterator it = li.iterator();
63         while (it.hasNext())
64         {
65           String JavaDoc nodeName = (String JavaDoc)it.next();
66           list.add(nodeName);
67         }
68       }
69     }
70   }
71   
72   public boolean contains(String JavaDoc nodeName)
73   {
74     return list.contains(nodeName);
75   }
76   
77   public void remove(String JavaDoc nodeName)
78   {
79     list.remove(nodeName);
80   }
81   
82   /**
83    * XML format:
84    * <p>
85    * <pre>
86    * *&lt;node name="string"/&gt;
87    * </pre
88    */

89   public void loadFromXML(Element element) throws SAXException JavaDoc
90   {
91     synchronized (list)
92     {
93       NodeList nl = element.getChildNodes();
94       for (int i = 0; i < nl.getLength(); i++)
95       {
96         Node node = nl.item(i);
97         if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().compareTo("node") == 0)
98         {
99           Element el = (Element)node;
100           String JavaDoc name = el.getAttribute("name");
101           list.add(name);
102         }
103       }
104     }
105   }
106   
107   public void saveToXML(Element element)
108   {
109     synchronized (list)
110     {
111       Document doc = element.getOwnerDocument();
112
113       Iterator it = list.iterator();
114       while (it.hasNext())
115       {
116         String JavaDoc name = (String JavaDoc)it.next();
117
118         Element el = doc.createElement("node");
119         el.setAttribute("name", name);
120         element.appendChild(el);
121       }
122     }
123   }
124   
125 }
126
Popular Tags