1 6 7 package SOFA.SOFAnet.Repository; 8 9 import java.io.*; 10 import java.util.*; 11 import java.text.*; 12 import SOFA.Util.XML; 13 import org.w3c.dom.*; 14 import org.xml.sax.SAXException ; 15 16 31 public class Licence extends XMLStorageItem implements StorageItem, Cloneable , Serializable 32 { 33 final static public int TYPE_PHYSICAL = 0; 34 final static public int TYPE_ACTIVE = 1; 35 final static public int ALL_LICENCES = -2; 36 final static public int ONE_IMPLICIT_LICENCE = -1; 37 final static public int WITHOUT_COPIES = -3; 38 39 private int type; 40 private int numberOfCopies; private Date validFrom; private Date validTo; private String text; 44 45 46 public Licence(String name, File file) 47 { 48 super(name, file, "licence"); 49 reset(); 50 } 51 52 public Licence() 53 { 54 super("", null, "licence"); 55 reset(); 56 } 57 58 public Object clone() 59 { 60 Licence clone = null; 61 try 62 { 63 clone = (Licence)super.clone(); 64 } 65 catch (CloneNotSupportedException e) 66 { 67 throw new InternalError (); 68 } 69 if (validFrom != null) clone.validFrom = (Date)validFrom.clone(); 70 if (validTo != null) clone.validTo = (Date)validTo.clone(); 71 return clone; 72 } 73 74 77 protected void reset() 78 { 79 type = TYPE_PHYSICAL; 80 numberOfCopies = WITHOUT_COPIES; 81 validFrom = null; 82 validTo = null; 83 text = ""; 84 } 85 86 107 public void loadFromXML(Element element) throws SAXException 108 { 109 reset(); 110 NodeList nl = element.getChildNodes(); 111 for (int i = 0; i < nl.getLength(); i++) 112 { 113 Node node = nl.item(i); 114 if (node.getNodeType() == Node.ELEMENT_NODE) 115 { 116 Element el = (Element)node; 117 String nodeName = node.getNodeName(); 118 119 if (nodeName.compareTo("validity") == 0) 120 { 121 String from = el.getAttribute("from"); 122 String to = el.getAttribute("to"); 123 DateFormat dateFormat = DateFormat.getInstance(); 124 if (from.length() > 0) 125 { 126 try 127 { 128 validFrom = dateFormat.parse(from); 129 } 130 catch (ParseException e) 131 { 132 throw new SAXException ("Cannot parse date/time format of 'from' attribute of 'validity' tag", e); 133 } 134 } 135 136 if (to.length() > 0) 137 { 138 try 139 { 140 validTo = dateFormat.parse(to); 141 } 142 catch (ParseException e) 143 { 144 throw new SAXException ("Cannot parse date/time format of 'to' attribute of 'validity' tag", e); 145 } 146 } 147 } 148 else 149 if (nodeName.compareTo("type") == 0) 150 { 151 String t = XML.getTextContent(el).trim().toLowerCase(); 152 if (t.compareTo("physical_copy") == 0) type = TYPE_PHYSICAL; 153 else if (t.compareTo("active_copy") == 0) type = TYPE_ACTIVE; 154 else throw new SAXException ("Invalid type of licence ('type' tag)"); 155 } 156 else 157 if (nodeName.compareTo("number_of_copies") == 0) 158 { 159 try 160 { 161 int l = Integer.parseInt(XML.getTextContent(el)); 162 if (l < 0) throw new NumberFormatException (); 163 numberOfCopies = l; 164 } 165 catch (NumberFormatException e) 166 { 167 throw new SAXException ("Invalid 'positive-integer' format of content of 'number_of_copies' tag", e); 168 } 169 } 170 else 171 if (nodeName.compareTo("text") == 0) 172 { 173 text = XML.getTextContent(el); 174 } 175 } 176 } 177 } 178 179 182 public void saveToXML(Element element) 183 { 184 Document doc = element.getOwnerDocument(); 185 Element el; 186 187 if (validFrom != null || validTo != null) 188 { 189 DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM); 190 el = doc.createElement("validity"); 191 if (validFrom != null) el.setAttribute("from", dateFormat.format(validFrom)); 192 if (validTo != null) el.setAttribute("to", dateFormat.format(validTo)); 193 element.appendChild(el); 194 } 195 196 String typeText = "physical_copy"; 197 switch (type) 198 { 199 case TYPE_ACTIVE: typeText = "active_copy"; break; 200 default: 201 case TYPE_PHYSICAL: typeText = "physical_copy"; break; 202 } 203 204 XML.newTextElement(element, "type", typeText); 205 206 if (numberOfCopies >= 0) 207 { 208 XML.newTextElement(element, "number_of_copies", Integer.toString(numberOfCopies)); 209 } 210 211 if (text.length() > 0) 212 { 213 XML.newTextElement(element, "text", text); 214 } 215 } 216 217 public int getType() 218 { 219 return type; 220 } 221 222 public Date getValidFrom() { 224 return validFrom; 225 } 226 227 public Date getValidTo() { 229 return validTo; 230 } 231 232 public boolean isValid() 233 { 234 Date now = Calendar.getInstance().getTime(); 235 return (validFrom == null || !validFrom.after(now)) && (validTo == null || !validTo.before(now)); 236 } 237 238 public int getNumberOfCopies() 239 { 240 return numberOfCopies; 241 } 242 243 public boolean withCopies() 244 { 245 return numberOfCopies != WITHOUT_COPIES; 246 } 247 248 public String getText() 249 { 250 return text; 251 } 252 253 254 public void setType(int type) 255 { 256 if (type != TYPE_PHYSICAL && type != TYPE_ACTIVE) type = TYPE_PHYSICAL; 257 this.type = type; 258 } 259 260 public void setValidFrom(Date validFrom) { 262 this.validFrom = validFrom; 263 } 264 265 public void setValidTo(Date validTo) { 267 this.validTo = validTo; 268 } 269 270 public void setNumberOfCopies(int numberOfCopies) 271 { 272 if (numberOfCopies < 0) numberOfCopies = WITHOUT_COPIES; 273 this.numberOfCopies = numberOfCopies; 274 } 275 276 public void increaseNumberOfCopies(int increment) 277 { 278 if (numberOfCopies != WITHOUT_COPIES && increment >= 0) 279 { 280 numberOfCopies += increment; 281 } 282 } 283 284 public void decreaseNumberOfCopies(int decrement) 285 { 286 if (numberOfCopies != WITHOUT_COPIES && decrement >= 0) 287 { 288 if (numberOfCopies > decrement) numberOfCopies -= decrement; 289 else numberOfCopies = 0; 290 } 291 } 292 293 public void setText(String text) 294 { 295 this.text = text; 296 } 297 298 public boolean isEmpty() 299 { 300 return type == TYPE_PHYSICAL && numberOfCopies == WITHOUT_COPIES && 301 validFrom == null && validTo == null && text.length() == 0; 302 } 303 } 304 | Popular Tags |