1
2
3
4
5
6
7
8
9
10
11
12 package sk.uniba.euromath.editor;
13 import java.util.EnumSet;
14 import java.util.HashMap;
15 import java.util.List;
16 import java.util.Map;
17
18 import org.apache.commons.lang.StringUtils;
19
20 import sk.baka.ikslibs.ResultEnum;
21 import sk.baka.ikslibs.SourceEnum;
22 /***
23 * Selects preferred renderers.
24 * @author Martin Vysny
25 */
26 public final class RendererSelector {
27 /***
28 * Creates new empty selector, with no preferred renderers.
29 */
30 public RendererSelector() {
31 super();
32 }
33 /***
34 * Clones given selector.
35 * @param other the selector to clone.
36 */
37 public RendererSelector(final RendererSelector other) {
38 super();
39 getPreferredRenderers().putAll(other.preferredRenderers);
40 }
41 /***
42 * Maps namespace URI to a renderer, indended to render the particular
43 * namespace. If a namespace key is missing then first suitable renderer is
44 * used. Map may be <code>null</code> - in this case the class takes
45 * choices by itself, selecting first suitable renderer for all namespaces.
46 */
47 private final Map<String, RendererInfo> preferredRenderers = new HashMap<String, RendererInfo>();
48 /***
49 * Computes and returns information about renderable namespaces.
50 * @return map mapping namespace URI to a set of source kinds, in which the
51 * namespace is accepted.
52 */
53 public Map<String, EnumSet<SourceEnum>> getRenderable() {
54 final Map<String, EnumSet<SourceEnum>> accepts = new HashMap<String, EnumSet<SourceEnum>>();
55 for (final String namespace : EditInstanceProvider.getInstance()
56 .getRenderableNamespaces()) {
57
58
59 final EnumSet<SourceEnum> sources = EnumSet
60 .noneOf(SourceEnum.class);
61 if (getPreferredRenderers() != null) {
62 final RendererInfo ri = getPreferredRenderers().get(namespace);
63 if (ri != null)
64 sources.addAll(ri.sourceTypes);
65 }
66 final List<RendererInfo> renderers = EditInstanceProvider.getInstance()
67 .getRenderersForNamespace(namespace,
68 EnumSet.of(ResultEnum.DOM, ResultEnum.OBJECT));
69 for (final RendererInfo ri : renderers) {
70 sources.addAll(ri.sourceTypes);
71 }
72 accepts.put(namespace, sources);
73 }
74 return accepts;
75 }
76 /***
77 * Retrieves the renderer that will be used to renderer document with given
78 * namespace.
79 * @param namespace the nametree namespace.
80 * @param products in which kinds of sources the document can be provided.
81 * @return rendererinfo instance, never <code>null</code>.
82 * @throws EditorException if no renderer is suitable for given namespace.
83 */
84 public RendererInfo getRendererInfo(final String namespace,
85 final EnumSet<ResultEnum> products) throws EditorException {
86 if (getPreferredRenderers() != null) {
87 final RendererInfo result = getPreferredRenderers().get(StringUtils
88 .defaultString(namespace));
89 if ((result != null)
90 && (ResultEnum.getBestResultSourcePair(products,
91 result.sourceTypes) != null))
92 return result;
93 }
94
95 final List<RendererInfo> renderers = EditInstanceProvider.getInstance()
96 .getRenderersForNamespace(namespace, products);
97 if ((renderers == null) || renderers.isEmpty())
98 throw new EditorException("No suitable renderer for {" + namespace
99 + "}:" + products);
100 return renderers.get(0);
101 }
102 /***
103 * @return Returns the preferredRenderers.
104 */
105 protected Map<String, RendererInfo> getPreferredRenderers() {
106 return this.preferredRenderers;
107 }
108 }