1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.mevenide.grammar;
18
19 import java.util.HashMap;
20 import java.util.Map;
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.mevenide.grammar.impl.EmptyAttributeCompletionImpl;
24 import org.mevenide.grammar.impl.EmptyTagLibImpl;
25
26 /***
27 * Manager of TagLib instances. Works as a cache lazyily grabbing taglibs from
28 * the TagLibProvider.
29 * @author Milos Kleint (ca206216@tiscali.cz)
30 */
31 public final class TagLibManager {
32
33 private static Log logger = LogFactory.getLog(TagLibManager.class);
34
35 private TagLibProvider provider;
36 private AttrCompletionProvider attrComplProvider;
37 private Map libcache;
38 private Map attrCompletionCache;
39
40
41 /*** Creates a new instance of TagLibManager */
42 public TagLibManager() {
43 libcache = new HashMap();
44 attrCompletionCache = new HashMap();
45 }
46
47 /***
48 * sets a TagLibProvider instance that will be used to populate the cache.
49 */
50 public void setProvider(TagLibProvider prov) {
51 provider = prov;
52 }
53
54 /***
55 * sets the AttrCompletionProvider instance that will populate the cache.
56 */
57 public void setAttrCompletionProvider(AttrCompletionProvider prov) {
58 attrComplProvider = prov;
59 }
60
61 /***
62 * get available taglibnames. Returns only String[] so that they don't need to be fully
63 * initialized. At this point the provider has to be set.
64 */
65 public String[] getAvailableTagLibs() {
66 assertHasProvider();
67 return provider.getAvailableTags();
68 }
69
70 /***
71 * Loads the tagLib instance by name. Consults the cache first, if not found, gets it from the
72 * provider. If not found there, it's not supported somehow, puts an instance of EmptyTagLibImpl in place.
73 * Should never return null. At this point the provider has to be set.
74 */
75 public TagLib getTagLibrary(String name) {
76 assertHasProvider();
77 TagLib lib = (TagLib)libcache.get(name);
78 if (lib == null) {
79 lib = provider.retrieveTagLib(name);
80 if (lib == null) {
81 logger.error("No such taglibrary defined by provider:" + name);
82
83 lib = new EmptyTagLibImpl(name);
84 }
85 libcache.put(name, lib);
86 }
87 return lib;
88 }
89
90 public AttributeCompletion getAttributeCompletion(String name) {
91 assertHasProvider();
92 AttributeCompletion compl = (AttributeCompletion)attrCompletionCache.get(name);
93 if (compl == null) {
94 compl = attrComplProvider.retrieveAttributeCompletion(name);
95 if (compl == null) {
96 logger.error("No such attribute completion defined by provider:" + name);
97
98 compl = new EmptyAttributeCompletionImpl(name);
99 }
100 attrCompletionCache.put(name, compl);
101 }
102 return compl;
103 }
104
105 private void assertHasProvider() {
106 if (provider == null) {
107 logger.fatal("No taglib provider assigned.");
108 throw new RuntimeException("No taglib provider assigned.");
109 }
110 }
111 }