KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * BundleInfo.java
3  *
4  * Created on 22. listopad 2003, 20:56
5  */

6
7 package SOFA.SOFAnet.Repository;
8
9 import java.io.*;
10
11 /**
12  * Representation of bundle name and/or file with binary version of bundle
13  *
14  * @author Ladislav Sobr
15  */

16 public class BundleInfo implements StorageItem
17 {
18   private File file;
19   private String JavaDoc shortName; // name without version
20
private String JavaDoc version;
21   private String JavaDoc fullName; // == shortName + "[" + version + "]";
22
private String JavaDoc fileName;
23   private String JavaDoc [] nameParts;
24   private boolean component;
25   private boolean completeComponent;
26   
27   public static class InvalidBundleNameException extends SOFA.SOFAnode.SOFAException
28   {
29     public InvalidBundleNameException(String JavaDoc message)
30     {
31       super(message);
32     }
33
34     public InvalidBundleNameException(String JavaDoc message, Throwable JavaDoc cause)
35     {
36       super(message, cause);
37     }
38     
39     public InvalidBundleNameException(Throwable JavaDoc cause)
40     {
41       super(cause);
42     }
43   }
44   
45   /** Creates a new instance of BundleInfo */
46   public BundleInfo()
47   {
48   }
49   
50
51   public void setFile(File file)
52   {
53     this.file = file;
54   }
55   
56   public File getFile()
57   {
58     return file;
59   }
60
61   public String JavaDoc getShortName()
62   {
63     return shortName;
64   }
65   
66   public String JavaDoc getVersion()
67   {
68     return version;
69   }
70   
71   public String JavaDoc getFullName()
72   {
73     return fullName;
74   }
75   
76   public String JavaDoc getFileName()
77   {
78     return fileName;
79   }
80   
81   public String JavaDoc [] getNameParts()
82   {
83     return nameParts;
84   }
85   
86   public boolean isComponent()
87   {
88     return component;
89   }
90   
91   public boolean isCompleteComponent()
92   {
93     return completeComponent;
94   }
95   
96   public boolean isSingleComponent()
97   {
98     return component && !completeComponent;
99   }
100   
101   /**
102    * @param name Name of bundle in filesystem without ".jar" extension.
103    */

104   public void fromFilename(String JavaDoc name) throws InvalidBundleNameException
105   {
106     if (name.length() <= 0) throw new InvalidBundleNameException("Empty bundle name");
107     
108     fileName = name;
109     
110     if (name.charAt(0) == '-') //name begins with "-"
111
{
112       component = true;
113       name = name.substring(1);
114       if (name.length() > 0 && name.charAt(name.length() - 1) == '$') //name ends with "-$"
115
{
116         completeComponent = true;
117         name = name.substring(0, name.length() - 2);
118       }
119       else completeComponent = false;
120     }
121     else
122     {
123       component = false;
124       completeComponent = false;
125     }
126     
127     int index = name.lastIndexOf('-');
128     
129     if (index == -1) throw new InvalidBundleNameException("Missing bundle version in " + fileName);
130     
131     version = name.substring(index + 1);
132     name = name.substring(0, index);
133     
134     if (name.length() <= 0) throw new InvalidBundleNameException("Missing bundle short name in " + fileName);
135     if (component) shortName = "::" + name.replaceAll("-", "::");
136     else shortName = name;
137     
138     nameParts = name.split("-");
139     
140     fullName = shortName + "[" + version + "]";
141     if (completeComponent) fullName += "$";
142   }
143   
144   public void fromBundleName(String JavaDoc name) throws InvalidBundleNameException
145   {
146     if (name.length() <= 0) throw new InvalidBundleNameException("Empty bundle name");
147   
148     fullName = name;
149     
150     if (name.startsWith("::"))
151     {
152       component = true;
153       name = name.substring(2);
154       if (name.length() <= 0) throw new InvalidBundleNameException("Invalid bundle name: " + fullName);
155       if (name.charAt(name.length() - 1) == '$')
156       {
157         name = name.substring(0, name.length() - 1);
158         completeComponent = true;
159       }
160       else completeComponent = false;
161     }
162     else
163     {
164       component = false;
165       completeComponent = false;
166     }
167
168     if (name.charAt(name.length() - 1) != ']') throw new InvalidBundleNameException("Invalid version in bundle name: " + fullName);
169     name = name.substring(0, name.length() - 1);
170
171     int index = name.lastIndexOf('[');
172     if (index == -1 || index == name.length() - 1) throw new InvalidBundleNameException("Missing version in bundle name: " + fullName);
173     version = name.substring(index + 1);
174
175     name = name.substring(0, index);
176     
177     if (component)
178     {
179       shortName = "::" + name;
180       name = name.replaceAll("::", "-");
181     }
182     else
183     {
184       shortName = "";
185     }
186     
187     nameParts = name.split("-");
188       
189     if (component)
190     {
191       fileName = "-" + name + "-" + version;
192       if (completeComponent) fileName += "-$";
193     }
194     else
195     {
196       fileName = name + "-" + version;
197     }
198   }
199   
200   /**
201    * Returns true when specified pattern match bundle name stored inside this BundleInfo.
202    * <p>
203    * <ul>
204    * <li>Pattern structure:
205    * <ul>
206    * <li>short_name[version]complete_component
207    * <li>* &nbsp;&nbsp;&nbsp; matches any bundle
208    * </ul>
209    *
210    * <li>short_name:
211    * <ul>
212    * <li>::str1::str2::...::strN for components
213    * <li>str1-str2-...-strN for other bundles
214    * <li>where:
215    * <ul>
216    * <li>strX can be
217    * <ul>
218    * <li>a) text that doesn't contain "characters" ?, ::, -, *
219    * <ul>
220    * <li>exact match of "pattern text" and "real name text" is required
221    * </ul>
222    * <li>b) character ?
223    * <ul>
224    * <li>"real name text" can be arbitrary
225    * </ul>
226    * </ul>
227    * <li>short_name can end by character * at any place
228    * <ul>
229    * <li>from this point up to the end od short name "real name text" can be arbitrary
230    * <li>examples:
231    * <ul>
232    * <li>::CUNI::*
233    * <li>::CUNI::SO* (::CUNI::SOFA::xxx matches this pattern)
234    * </ul>
235    * </ul>
236    * </ul>
237    * </ul>
238    *
239    *
240    * <li>version:
241    * <ul>
242    * <li>a) &gt;pattern_text
243    * <ul>
244    * <li>"real version text" must be lexicographicaly greater than pattern_text
245    * </ul>
246    * <li>b) &gt;=pattern_text
247    * <ul>
248    * <li>"real version text" must be lexicographicaly greater or equal than pattern_text
249    * </ul>
250    * <li>c) &lt;pattern_text
251    * <ul>
252    * <li>"real version text" must be lexicographicaly less than pattern_text
253    * </ul>
254    * <li>d) &lt;=pattern_text
255    * <ul>
256    * <li>"real version text" must be lexicographicaly less or equal than pattern_text
257    * </ul>
258    * <li>e) * or ?
259    * <ul>
260    * <li>"real version text" can be arbitrary
261    * </ul>
262    * <li>f) pattern_text which doesnt match any of a) - e)
263    * <ul>
264    * <li>exact match of pattern_text and "real version text" is required
265    * </ul>
266    * </ul>
267    *
268    * <li>complete_component
269    * <ul>
270    * <li>a) "empty"
271    * <ul>
272    * <li>bundle name must not represent complete-component bundle
273    * </ul>
274    * <li>b) $
275    * <ul>
276    * <li>bundle name must represent complete-component bundle
277    * </ul>
278    * <li>c) ? or *
279    * <ul>
280    * <li>bundle name can represent any "kind" of bundle
281    * </ul>
282    * </ul>
283    * </ul>
284    * <p>
285    *
286    * Remark: Don't use this function and exception InvalidBundleNameException to
287    * test correctness of the pattern format. The function can return result
288    * "false" even for badly formatted pattern!
289    *
290    * @param bundleNamePattern Pattern of bundle name.
291    */

292   public boolean matchPattern(String JavaDoc bundleNamePattern) throws InvalidBundleNameException
293   {
294     String JavaDoc name = bundleNamePattern.trim();
295     if (name.compareTo("*") == 0) return true;
296     
297     //find split pattern into short name, version and after-version
298
int index1 = name.lastIndexOf('[');
299     int index2 = name.lastIndexOf(']');
300     if (index1 == -1 || index2 == -1 || index2 < index1) throw new InvalidBundleNameException("Cannot find version in bundle name pattern: " + bundleNamePattern);
301
302     int len;
303     char ch, ch2;
304
305     String JavaDoc shortNamePattern = name.substring(0, index1);
306     
307     //check short name
308
if (shortNamePattern.startsWith("::"))
309     {
310       if (!component) return false;
311       shortNamePattern = shortNamePattern.substring(2);
312       shortNamePattern = shortNamePattern.replaceAll("::", "-");
313     }
314     else
315     {
316       if (component && shortNamePattern.compareTo("*") != 0) return false;
317     }
318     
319     String JavaDoc [] patternParts = shortNamePattern.split("-");
320     
321     boolean asterisk = false;
322     
323     for (int i = 0; i < patternParts.length; i++)
324     {
325       len = patternParts[i].length();
326       if (len == 0) return false;
327       if (nameParts.length <= i) return false;
328       ch = patternParts[i].charAt(0);
329       if (ch == '?')
330       {
331         if (len == 2 && patternParts[i].charAt(1) == '*')
332         {
333           asterisk = true;
334           break;
335         }
336         if (len > 1) throw new InvalidBundleNameException("Error in bundle name pattern \"" + bundleNamePattern + "\" at short-name part");
337         else continue;
338       }
339       int index = patternParts[i].indexOf('*');
340       if (index != -1 && index != patternParts[i].length() - 1 ||
341           patternParts[i].indexOf('?') != -1) throw new InvalidBundleNameException("Error in bundle name pattern \"" + bundleNamePattern + "\" at short-name part");
342       if (index == -1)
343       {
344         if (nameParts[i].compareTo(patternParts[i]) != 0) return false;
345       }
346       else
347       {
348         if (nameParts[i].length() < index) return false;
349         if (nameParts[i].substring(0, index).compareTo(patternParts[i].substring(0, index)) != 0) return false;
350         asterisk = true;
351         break;
352       }
353     }
354     
355     if (!asterisk && patternParts.length != nameParts.length) return false;
356     
357     
358     //check version
359
String JavaDoc versionPattern = name.substring(index1 + 1, index2);
360     len = versionPattern.length();
361     ch = len > 0 ? versionPattern.charAt(0) : 'x';
362     ch2 = len > 1 ? versionPattern.charAt(1) : 'x';
363     if (len == 0 ||
364         len == 1 && (ch == '<' || ch == '>') ||
365         len == 2 && (ch == '<' || ch == '>') && ch2 == '=' ||
366         len > 1 && (ch == '?' || ch == '*'))
367         throw new InvalidBundleNameException("Error in bundle name pattern \"" + bundleNamePattern + "\" at version part");
368     
369     if (len == 1)
370     {
371       if (ch != '?' && ch != '*' && version.compareTo(versionPattern) != 0) return false;
372     }
373     else //len > 1
374
{
375       if (ch == '<' && ch2 != '=')
376       {
377         if (version.compareTo(versionPattern.substring(1)) >= 0) return false;
378       }
379       else
380       if (ch == '<' && ch2 == '=')
381       {
382         if (version.compareTo(versionPattern.substring(2)) > 0) return false;
383       }
384       else
385       if (ch == '>' && ch2 != '=')
386       {
387         if (version.compareTo(versionPattern.substring(1)) <= 0) return false;
388       }
389       else
390       if (ch == '>' && ch2 == '=')
391       {
392         if (version.compareTo(versionPattern.substring(2)) < 0) return false;
393       }
394       else
395       {
396         if (version.compareTo(versionPattern) != 0) return false;
397       }
398     }
399
400     //check after-version
401
String JavaDoc afterVersionPattern = name.substring(index2 + 1);
402     len = afterVersionPattern.length();
403     ch = len > 0 ? afterVersionPattern.charAt(0) : 'x';
404     if (len > 1 || len == 1 && ch != '$' && ch != '*' && ch != '?') throw new InvalidBundleNameException("Error in bundle name pattern \"" + bundleNamePattern + "\" at complete-component part");
405     
406     if (len == 0 && completeComponent) return false;
407     if (len == 1 && ch == '$' && (!component || !completeComponent)) return false;
408     
409     return true;
410   }
411   
412   public boolean isComponentBundleOf(String JavaDoc componentFullName)
413   {
414     return component && componentFullName.compareTo(shortName + "[" + version + "]") == 0;
415   }
416   
417   public boolean isLoaded()
418   {
419     return false;
420   }
421   public void setLoaded(boolean loaded)
422   {
423   }
424   
425   public boolean toBeDeleted()
426   {
427     return true;
428   }
429
430   public void loadFromStorage()
431   {
432   }
433   
434   public void saveToStorage()
435   {
436   }
437
438   public String JavaDoc getName()
439   {
440     return fullName;
441   }
442
443   public void deleteFromStorage()
444   {
445   }
446   
447   public static String JavaDoc fileNameToBundleName(String JavaDoc fileName)
448   {
449     BundleInfo bundleInfo = new BundleInfo();
450     try
451     {
452       bundleInfo.fromFilename(fileName);
453     }
454     catch (BundleInfo.InvalidBundleNameException e)
455     {
456       return null;
457     }
458     return bundleInfo.getFullName();
459   }
460   
461   public static String JavaDoc bundleNameToFileName(String JavaDoc bundleName)
462   {
463     BundleInfo bundleInfo = new BundleInfo();
464     try
465     {
466       bundleInfo.fromBundleName(bundleName);
467     }
468     catch (BundleInfo.InvalidBundleNameException e)
469     {
470       return null;
471     }
472     return bundleInfo.getFileName();
473   }
474   
475   public static String JavaDoc makeComponentBundleName(String JavaDoc nameOfComponent, String JavaDoc version, boolean isCompleteComponent)
476   {
477     if (isCompleteComponent) return nameOfComponent + "[" + version + "]$";
478     else return nameOfComponent + "[" + version + "]";
479   }
480   
481   public static String JavaDoc makeUserBundleName(String JavaDoc name, String JavaDoc version)
482   {
483     return name + "[" + version + "]";
484   }
485
486   public static boolean matchPattern(String JavaDoc bundleName, String JavaDoc bundleNamePattern) throws InvalidBundleNameException
487   {
488     BundleInfo bundleInfo = new BundleInfo();
489     bundleInfo.fromBundleName(bundleName);
490     return bundleInfo.matchPattern(bundleNamePattern);
491   }
492   
493 }
494
Popular Tags