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  
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  			// this enum will contain all kinds of sources that the renderer
58  			// accepts.
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  		// the renderer is not specified. select first instance.
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 //$NON-NLS-1$
99  					+ "}:" + products); //$NON-NLS-1$
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 }