View Javadoc

1   /*
2    * Created on Aug 6, 2005. Copyright 1999-2006 Faculty of Mathematics, Physics
3    * and Informatics, Comenius University, Bratislava. This file is protected by
4    * the Mozilla Public License version 1.1 (the "License"); you may not use this
5    * file except in compliance with the License. You may obtain a copy of the
6    * License at http://euromath2.sourceforge.net/license.html Unless required by
7    * applicable law or agreed to in writing, software distributed under the
8    * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
9    * OF ANY KIND, either express or implied. See the License for the specific
10   * language governing permissions and limitations under the License.
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  import org.apache.commons.lang.StringUtils;
18  import sk.baka.ikslibs.ResultEnum;
19  import sk.baka.ikslibs.SourceEnum;
20  /***
21   * Selects preferred renderers.
22   * @author Martin Vysny
23   */
24  public final class RendererSelector {
25  	/***
26  	 * Creates new empty selector, with no preferred renderers.
27  	 */
28  	public RendererSelector() {
29  		super();
30  	}
31  	/***
32  	 * Clones given selector.
33  	 * @param other the selector to clone.
34  	 */
35  	public RendererSelector(final RendererSelector other) {
36  		super();
37          getPreferredRenderers().putAll(other.preferredRenderers);
38  	}
39  	/***
40  	 * Maps namespace URI to a renderer, indended to render the particular
41  	 * namespace. If a namespace key is missing then first suitable renderer is
42  	 * used. Map may be <code>null</code> - in this case the class takes
43  	 * choices by itself, selecting first suitable renderer for all namespaces.
44  	 */
45  	private final Map<String, RendererInfo> preferredRenderers = new HashMap<String, RendererInfo>();
46  	/***
47  	 * Computes and returns information about renderable namespaces.
48  	 * @return map mapping namespace URI to a set of source kinds, in which the
49  	 * namespace is accepted.
50  	 */
51  	public Map<String, EnumSet<SourceEnum>> getRenderable() {
52  		final Map<String, EnumSet<SourceEnum>> accepts = new HashMap<String, EnumSet<SourceEnum>>();
53  		for (final String namespace : EditInstanceProvider.getInstance()
54  				.getRenderableNamespaces()) {
55  			// this enum will contain all kinds of sources that the renderer
56  			// accepts.
57  			final EnumSet<SourceEnum> sources = EnumSet
58  					.noneOf(SourceEnum.class);
59  			if (getPreferredRenderers() != null) {
60  				final RendererInfo ri = getPreferredRenderers().get(namespace);
61  				if (ri != null)
62  					sources.addAll(ri.sourceTypes);
63  			}
64  			final List<RendererInfo> renderers = EditInstanceProvider.getInstance()
65  					.getRenderersForNamespace(namespace,
66  							EnumSet.of(ResultEnum.DOM, ResultEnum.OBJECT));
67  			for (final RendererInfo ri : renderers) {
68  				sources.addAll(ri.sourceTypes);
69  			}
70  			accepts.put(namespace, sources);
71  		}
72  		return accepts;
73  	}
74  	/***
75  	 * Retrieves the renderer that will be used to renderer document with given
76  	 * namespace.
77  	 * @param namespace the nametree namespace.
78  	 * @param products in which kinds of sources the document can be provided.
79  	 * @return rendererinfo instance, never <code>null</code>.
80  	 * @throws EditorException if no renderer is suitable for given namespace.
81  	 */
82  	public RendererInfo getRendererInfo(final String namespace,
83  			final EnumSet<ResultEnum> products) throws EditorException {
84  		if (getPreferredRenderers() != null) {
85  			final RendererInfo result = getPreferredRenderers().get(StringUtils
86  					.defaultString(namespace));
87  			if ((result != null)
88  					&& (ResultEnum.getBestResultSourcePair(products,
89  							result.sourceTypes) != null))
90  				return result;
91  		}
92  		// the renderer is not specified. select first instance.
93  		final List<RendererInfo> renderers = EditInstanceProvider.getInstance()
94  				.getRenderersForNamespace(namespace, products);
95  		if ((renderers == null) || renderers.isEmpty())
96  			throw new EditorException("No suitable renderer for {" + namespace //$NON-NLS-1$
97  					+ "}:" + products); //$NON-NLS-1$
98  		return renderers.get(0);
99  	}
100     /***
101      * @return Returns the preferredRenderers.
102      */
103     protected Map<String, RendererInfo> getPreferredRenderers() {
104         return this.preferredRenderers;
105     }
106 }