KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Licence.java
3  *
4  * Created on 14. prosinec 2003, 17:39
5  */

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 JavaDoc;
15
16 /**
17  * Representation of the licence.
18  * The class is used either as one item of persistent storage (called licence files)
19  * or as the represenation of licence in other data structures.
20  * <p>
21  * The licence contains:
22  * <ul>
23  * <li>type of licence: active copies (only presence of "software" in memory is limited) or physical copies (physical presence of the "software" is limited
24  * <li>time validity of the licence
25  * <li>number of licence copies or flag that this number is not limited (without copies)
26  * <li>text of licence
27  * </ul>
28  *
29  * @author Ladislav Sobr
30  */

31 public class Licence extends XMLStorageItem implements StorageItem, Cloneable JavaDoc, 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; //WITHOUT_COPIES or integer >= 0
41
private Date validFrom; //can be null
42
private Date validTo; //can be null
43
private String JavaDoc text;
44
45   /** Creates a new instance of Licence */
46   public Licence(String JavaDoc 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 JavaDoc clone()
59   {
60     Licence clone = null;
61     try
62     {
63       clone = (Licence)super.clone();
64     }
65     catch (CloneNotSupportedException JavaDoc e)
66     {
67       throw new InternalError JavaDoc();
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   /**
75    * Resets content of the item.
76    */

77   protected void reset()
78   {
79     type = TYPE_PHYSICAL;
80     numberOfCopies = WITHOUT_COPIES;
81     validFrom = null;
82     validTo = null;
83     text = "";
84   }
85
86   /**
87    * Loads the item from the XML DOM tree.
88    * <p>
89    * XML format of storage item:
90    * <p>
91    * <pre>
92    * &lt;licence&gt;
93    * Licence XML format
94    * &lt;/licence&gt;
95    * </pre>
96    * <p>
97    * XML format:
98    * <p>
99    * <pre>
100    * ?&lt;validity ?from="date" ?to="date"/&gt;
101    * ?&lt;type&gt;PHYSICAL_COPY or ACTIVE_COPY&lt;/type&gt;
102    * ?&lt;number_of_copies&gt;non-negative-integer&lt;/number_of_copies&gt;
103    * ?&lt;text&gt;string&lt;/text&gt;
104    * </pre>
105    *
106    */

107   public void loadFromXML(Element element) throws SAXException JavaDoc
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 JavaDoc nodeName = node.getNodeName();
118         
119         if (nodeName.compareTo("validity") == 0)
120         {
121           String JavaDoc from = el.getAttribute("from");
122           String JavaDoc 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 JavaDoc("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 JavaDoc("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 JavaDoc 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 JavaDoc("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 JavaDoc();
163             numberOfCopies = l;
164           }
165           catch (NumberFormatException JavaDoc e)
166           {
167             throw new SAXException JavaDoc("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   /**
180    * Saves the item to XML DOM tree.
181    */

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 JavaDoc 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() //can be null
223
{
224     return validFrom;
225   }
226   
227   public Date getValidTo() //can be null
228
{
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 JavaDoc 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) //can be null
261
{
262     this.validFrom = validFrom;
263   }
264   
265   public void setValidTo(Date validTo) //can be null
266
{
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 JavaDoc 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