1 6 7 package SOFA.SOFAnet.Repository; 8 9 import java.util.*; 10 import org.w3c.dom.*; 11 import org.xml.sax.SAXException ; 12 import java.io.Serializable ; 13 14 19 public class NodeNameLicList implements Cloneable , Serializable 20 { 21 private List list; 22 23 public static class NodeLicPair implements Cloneable , Serializable 24 { 25 public String nodeName; 26 public int numberOfLicences; 28 public NodeLicPair() 29 { 30 numberOfLicences = Licence.ONE_IMPLICIT_LICENCE; 31 } 32 33 public NodeLicPair(String nodeName, int numberOfLicences) 34 { 35 this.nodeName = nodeName; 36 this.numberOfLicences = numberOfLicences; 37 } 38 39 public Object clone() 40 { 41 try 42 { 43 return super.clone(); 44 } 45 catch (CloneNotSupportedException e) 46 { 47 throw new InternalError (); 48 } 49 } 50 51 public String 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 61 public NodeNameLicList() 62 { 63 list = Collections.synchronizedList(new LinkedList()); 64 } 65 66 public Object clone() 67 { 68 NodeNameLicList clone = null; 69 try 70 { 71 clone = (NodeNameLicList)super.clone(); 72 } 73 catch (CloneNotSupportedException e) 74 { 75 throw new InternalError (); 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 90 public void add(String 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 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 137 public NodeLicPair pass(String 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 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 187 public void loadFromXML(Element element) throws SAXException 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 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 (); 209 nlp.numberOfLicences = l; 210 } 211 catch (NumberFormatException e) 212 { 213 throw new SAXException ("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 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 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 |