KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > uitags > js > Suites


1 package net.sf.uitags.js;
2
3 import java.io.IOException JavaDoc;
4 import java.util.Arrays JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.LinkedHashMap JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Map JavaDoc;
9
10 import net.sf.uitags.util.ArrayUtils;
11 import net.sf.uitags.util.PropertiesLoadingException;
12
13 import org.apache.commons.collections.ExtendedProperties;
14
15 /**
16  * Represents uitags suites whose JS files are to be served to the browser.
17  *
18  * @author jonni
19  */

20 abstract class Suites {
21   /////////////////////////////
22
////////// Constants //////////
23
///////////////////////////////
24

25   /**
26    * Name of the properties file containing a map of suites to JS file names.
27    */

28   private static final String JavaDoc SUITE_PROPS = "file-names.properties";
29
30   /**
31    * The keyword to use in place of suite names in web.xml in order to
32    * include JS files in all suites.
33    */

34   static final String JavaDoc KEYWORD_ALL = "all";
35
36
37
38   ////////////////////////////
39
////////// Fields //////////
40
////////////////////////////
41

42   /**
43    * Map of <code>String</code>s to <code>List</code>s.
44    */

45   protected Map JavaDoc mapping = new LinkedHashMap JavaDoc();
46
47
48
49   //////////////////////////////////
50
////////// Construction //////////
51
//////////////////////////////////
52

53   /**
54    * Provided only to allow subclassing. To create an instance, use
55    * {@link #getInstance()}.
56    */

57   protected Suites() {
58     initSuiteNameToFileNamesMapping();
59   }
60
61   static Suites getInstance(String JavaDoc suites) {
62     if (suites == null || isKeywordAll(suites)) {
63       return new AllSuites();
64     }
65
66     // Make sure the keyword "all" doesn't appear amongst other suite names.
67
String JavaDoc[] suitesAsArray = ArrayUtils.toArrayOfTrimmed(suites);
68     for (int i = 0; i < suitesAsArray.length; i++) {
69       if (isKeywordAll(suitesAsArray[i])) {
70         throw new IllegalArgumentException JavaDoc(
71             "The keyword 'all' can only be used by itself.");
72       }
73     }
74
75     return new NamedSuites(suitesAsArray);
76   }
77
78
79
80   ////////////////////////////
81
////////// Methods //////////
82
/////////////////////////////
83

84   private static boolean isKeywordAll(String JavaDoc suitesAsString) {
85     return suitesAsString.trim().equalsIgnoreCase(KEYWORD_ALL);
86   }
87
88   private void initSuiteNameToFileNamesMapping() {
89     ExtendedProperties props = loadMappingFromProperties();
90
91     for (Iterator JavaDoc suites = props.getKeys(); suites.hasNext(); ) {
92       String JavaDoc suiteName = (String JavaDoc) suites.next();
93       String JavaDoc[] fileNames = props.getStringArray(suiteName);
94
95       for (int i = 0; i < fileNames.length; i++) {
96         fileNames[i] = fileNames[i];
97       }
98
99       mapping.put(suiteName, Arrays.asList(fileNames));
100     }
101   }
102
103   private ExtendedProperties loadMappingFromProperties() {
104     ExtendedProperties props = new ExtendedProperties();
105     try {
106       props.load(this.getClass().getResourceAsStream(SUITE_PROPS));
107     }
108     catch (IOException JavaDoc e) {
109       throw new PropertiesLoadingException(e, SUITE_PROPS);
110     }
111
112     return props;
113   }
114
115   protected abstract List JavaDoc getFileNames();
116 }
117
Popular Tags