KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * NodeLicList.java
3  *
4  * Created on 3. prosinec 2003, 17:35
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 (or node name patterns) and number of licences.
16  *
17  * @author Ladislav Sobr
18  */

19 public class NodeNameLicList implements Cloneable JavaDoc, Serializable JavaDoc
20 {
21   private List list;
22   
23   public static class NodeLicPair implements Cloneable JavaDoc, Serializable JavaDoc
24   {
25     public String JavaDoc nodeName;
26     public int numberOfLicences; // Licence.ONE_IMPLICIT_LICENCE means "one licence", Licence.ALL_LICENCES means "all remaining licences"
27

28     public NodeLicPair()
29     {
30       numberOfLicences = Licence.ONE_IMPLICIT_LICENCE;
31     }
32     
33     public NodeLicPair(String JavaDoc nodeName, int numberOfLicences)
34     {
35       this.nodeName = nodeName;
36       this.numberOfLicences = numberOfLicences;
37     }
38     
39     public Object JavaDoc clone()
40     {
41       try
42       {
43         return super.clone();
44       }
45       catch (CloneNotSupportedException JavaDoc e)
46       {
47         throw new InternalError JavaDoc();
48       }
49     }
50     
51     public String JavaDoc toString()
52     {
53       if (numberOfLicences == Licence.ONE_IMPLICIT_LICENCE) return nodeName;
54       else if (numberOfLicences == Licence.ALL_LICENCES) return nodeName + " [all licences]";
55       else return nodeName + " [#licences: " + Integer.toString(numberOfLicences) + "]";
56     }
57   }
58
59   
60   /** Creates a new instance of NodeLicList */
61   public NodeNameLicList()
62   {
63     list = Collections.synchronizedList(new LinkedList());
64   }
65
66   public Object JavaDoc clone()
67   {
68     NodeNameLicList clone = null;
69     try
70     {
71       clone = (NodeNameLicList)super.clone();
72     }
73     catch (CloneNotSupportedException JavaDoc e)
74     {
75       throw new InternalError JavaDoc();
76     }
77     clone.list = Collections.synchronizedList(new LinkedList());
78     clone.list.addAll(list);
79     return clone;
80   }
81   
82   public List getList()
83   {
84     return list;
85   }
86   
87   /**
88    * @param number_of_licences == Licence.ALL_LICENCES means "all of remaining licences"
89    */

90   public void add(String JavaDoc nodeName, int number_of_licences)
91   {
92     list.add(new NodeLicPair(nodeName, number_of_licences));
93   }
94
95   public void add(NodeNameLicList nodeNameLicList)
96   {
97     if (nodeNameLicList != null)
98     {
99       List li = nodeNameLicList.getList();
100       synchronized (li)
101       {
102         Iterator it = li.iterator();
103         while (it.hasNext())
104         {
105           NodeLicPair licPair = (NodeLicPair)it.next();
106           list.add(new NodeLicPair(licPair.nodeName, licPair.numberOfLicences));
107         }
108       }
109     }
110   }
111
112   public void add(NodeLicPair nodeLicPair)
113   {
114     list.add(nodeLicPair);
115   }
116   
117   public NodeLicPair find(String JavaDoc nodeName)
118   {
119     synchronized (list)
120     {
121       Iterator it = list.iterator();
122       while (it.hasNext())
123       {
124         NodeLicPair nlp = (NodeLicPair)it.next();
125         if (nlp.nodeName.compareTo(nodeName) == 0) return nlp;
126       }
127     }
128     
129     return null;
130   }
131   
132   /**
133    * Test wheter specified name of node matches any of the patterns in the list.
134    * @param nodeName name of node to test
135    * @return NodeLicPair object representing pair of node name and number of licence copies
136    */

137   public NodeLicPair pass(String JavaDoc nodeName)
138   {
139     NodeInfo nodeInfo = new NodeInfo();
140     try
141     {
142       nodeInfo.setNodeName(nodeName);
143     }
144     catch (NodeInfo.InvalidNodeNameException e)
145     {
146       return null;
147     }
148     
149     return pass(nodeInfo);
150   }
151   
152   /**
153    * Test wheter specified node matches any of the patterns in the list.
154    * @param nodeInfo node to test
155    * @return NodeLicPair object representing pair of node name and number of licence copies
156    */

157   public NodeLicPair pass(NodeInfo nodeInfo)
158   {
159     synchronized (list)
160     {
161       Iterator it = list.iterator();
162       while (it.hasNext())
163       {
164         NodeLicPair nlp = (NodeLicPair)it.next();
165         try
166         {
167           if (nodeInfo.matchPattern(nlp.nodeName)) return nlp;
168         }
169         catch (NodeInfo.InvalidNodeNameException e)
170         {
171         }
172       }
173       return null;
174     }
175   }
176     
177   
178   /**
179    * Loads the NodeNameLicList from the XML DOM tree.
180    * <p>
181    * XML format:
182    * <p>
183    * <pre>
184    * *&lt;node name="string" ?number_of_licences="positive-integer or *"/&gt;
185    * </pre
186    */

187   public void loadFromXML(Element element) throws SAXException JavaDoc
188   {
189     synchronized (list)
190     {
191       NodeList nl = element.getChildNodes();
192       for (int i = 0; i < nl.getLength(); i++)
193       {
194         Node node = nl.item(i);
195         if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().compareTo("node") == 0)
196         {
197           Element el = (Element)node;
198           NodeLicPair nlp = new NodeLicPair();
199           nlp.nodeName = el.getAttribute("name");
200           String JavaDoc s = el.getAttribute("number_of_licences");
201           if (s.compareTo("*") == 0) nlp.numberOfLicences = Licence.ALL_LICENCES;
202           else
203           if (s.length() != 0)
204           {
205             try
206             {
207               int l = Integer.parseInt(s);
208               if (l < 0) throw new NumberFormatException JavaDoc();
209               nlp.numberOfLicences = l;
210             }
211             catch (NumberFormatException JavaDoc e)
212             {
213               throw new SAXException JavaDoc("Invalid 'positive-integer or *' format of 'number_of_licences' attribute of 'node' tag", e);
214             }
215           }
216
217           list.add(nlp);
218         }
219       }
220     }
221   }
222   
223   /**
224    * Saves the NodeNameLicList to XML DOM tree.
225    */

226   public void saveToXML(Element element)
227   {
228     synchronized (list)
229     {
230       Document doc = element.getOwnerDocument();
231
232       Iterator it = list.iterator();
233       while (it.hasNext())
234       {
235         NodeLicPair nlp = (NodeLicPair)it.next();
236
237         Element el = doc.createElement("node");
238         el.setAttribute("name", nlp.nodeName);
239         String JavaDoc s = null;
240         if (nlp.numberOfLicences == Licence.ALL_LICENCES) s = "*";
241         else if (nlp.numberOfLicences > 0) s = Integer.toString(nlp.numberOfLicences);
242         if (s != null) el.setAttribute("number_of_licences", s);
243         element.appendChild(el);
244       }
245     }
246   }
247   
248 }
249
Popular Tags