KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > doclet > JobsAndTypes


1 /*
2  * (c) Rob Gordon 2005
3  */

4 package org.oddjob.doclet;
5
6 import java.io.IOException JavaDoc;
7 import java.io.InputStream JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.Collections JavaDoc;
10 import java.util.HashMap JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.LinkedHashMap JavaDoc;
13 import java.util.List JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.Properties JavaDoc;
16
17 /**
18  * Collects together the jobs and types the reference will reference.
19  *
20  * @author Rob Gordon.
21  */

22 public class JobsAndTypes {
23
24     /** Map of fqcn to tag for jobs. */
25     private Map JavaDoc jobs = new LinkedHashMap JavaDoc();
26     
27     /** Map of fqcn to tag for types. */
28     private Map JavaDoc types = new LinkedHashMap JavaDoc();
29
30     /** Other won't appear in index but can
31      * be linked to.
32      */

33     private Map JavaDoc other = new HashMap JavaDoc();
34     
35     public JobsAndTypes() {
36         loadProps("/org/oddjob/oddjob.properties", jobs);
37         loadProps("/org/oddjob/values/types.properties", types);
38         loadProps("/org/oddjob/schedules/schedules.properties", other);
39         loadProps("/org/oddjob/scheduling/scheduling.properties", other);
40         loadProps("/org/oddjob/quartz/scheduling.properties", other);
41     }
42
43     void loadProps(String JavaDoc resource, Map JavaDoc into) {
44         Properties JavaDoc props = new Properties JavaDoc();
45         try {
46             InputStream JavaDoc in = this.getClass().getResourceAsStream(resource);
47             if (in == null) {
48                 throw new IllegalStateException JavaDoc("[" + resource + " does not exsit.");
49             }
50             props.load(in);
51             in.close();
52         }
53         catch (IOException JavaDoc e) {
54             e.printStackTrace();
55         }
56         List JavaDoc sorted = new ArrayList JavaDoc(props.keySet());
57         Collections.sort(sorted);
58         for (Iterator JavaDoc it = sorted.iterator(); it.hasNext(); ) {
59             String JavaDoc tag = (String JavaDoc) it.next();
60             into.put(props.get(tag), tag);
61         }
62     }
63     
64     public String JavaDoc nameFor(String JavaDoc fqcn) {
65         String JavaDoc name = (String JavaDoc) jobs.get(fqcn);
66         if (name == null) {
67             name = (String JavaDoc) types.get(fqcn);
68         }
69         if (name == null) {
70             name = (String JavaDoc) other.get(fqcn);
71         }
72         return name;
73     }
74     
75     public boolean isJob(String JavaDoc fqcn) {
76         return !((String JavaDoc) jobs.get(fqcn) == null);
77         
78     }
79     
80     public boolean isType(String JavaDoc fqcn) {
81         return !((String JavaDoc) types.get(fqcn) == null);
82         
83     }
84     
85     public boolean contains(String JavaDoc fqcn) {
86         if (jobs.containsKey(fqcn)) {
87             return true;
88         }
89         if (types.containsKey(fqcn)) {
90             return true;
91         }
92         if (other.containsKey(fqcn)) {
93             return true;
94         }
95         return false;
96     }
97     
98     /**
99      * Return an array of short (xml tag) names for types.
100      *
101      * @return The short names.
102      */

103     public String JavaDoc[] types(){
104         return (String JavaDoc[]) types.values().toArray(new String JavaDoc[0]);
105     }
106     
107     /**
108      * Return an array of short (xml tag) names for jobs.
109      *
110      * @return The short names.
111      */

112     public String JavaDoc[] jobs(){
113         return (String JavaDoc[]) jobs.values().toArray(new String JavaDoc[0]);
114     }
115     
116 }
117
Popular Tags