1 /*
2 * Copyright 2007 - 2007 JEuclid, http://jeuclid.sf.net
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 /* $Id: ConverterRegistry.java,v 5a7becda9147 2009/10/23 10:57:54 max $ */
18
19 package net.sourceforge.jeuclid.converter;
20
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.util.Iterator;
24 import java.util.Locale;
25 import java.util.Map;
26 import java.util.Set;
27
28 import org.apache.xmlgraphics.util.Service;
29
30 /**
31 * A registry for image converters.
32 *
33 * @version $Revision: 5a7becda9147 $
34 */
35 public final class ConverterRegistry {
36
37 private static final class SingletonHolder {
38 private static final ConverterRegistry INSTANCE = new ConverterRegistry();
39
40 private SingletonHolder() {
41 }
42 }
43
44 private final Map<String, ConverterPlugin> mimetype2converter = new HashMap<String, ConverterPlugin>();
45
46 private final Map<String, String> mimetype2suffix = new HashMap<String, String>();
47
48 private final Map<String, String> suffix2mimetype = new HashMap<String, String>();
49
50 /**
51 * Default constructor.
52 */
53 @SuppressWarnings("unchecked")
54 protected ConverterRegistry() {
55 final Iterator<ConverterDetector> it = Service
56 .providers(ConverterDetector.class);
57 while (it.hasNext()) {
58 final ConverterDetector det = it.next();
59 det.detectConversionPlugins(this);
60 }
61 }
62
63 /**
64 * Retrieve the default registry instance.
65 *
66 * @return the ConverterRegistry.
67 */
68 public static ConverterRegistry getInstance() {
69 return ConverterRegistry.SingletonHolder.INSTANCE;
70 }
71
72 /**
73 * use {@link #getInstance()} instead.
74 *
75 * @return see {@link #getInstance()}
76 * @deprecated use {@link #getInstance()} instead.
77 */
78 @Deprecated
79 public static ConverterRegistry getRegisty() {
80 return ConverterRegistry.getInstance();
81 }
82
83 /**
84 * Retrieve a list of available mime types for conversion.
85 *
86 * @return a Set<String> containing all valid mime-types.
87 */
88 public Set<String> getAvailableOutfileTypes() {
89 return this.mimetype2converter.keySet();
90 }
91
92 /**
93 * Retrieve a list of all available extensions.
94 *
95 * @return a list of available extensions.
96 */
97 public Set<String> getAvailableExtensions() {
98 final Set<String> extensions = new HashSet<String>();
99 for (final Map.Entry<String, String> e : this.suffix2mimetype
100 .entrySet()) {
101 if (this.mimetype2converter.containsKey(e.getValue())) {
102 extensions.add(e.getKey());
103 }
104 }
105 return extensions;
106 }
107
108 /**
109 * Returns the file suffix suitable for the given mime type.
110 * <p>
111 * This function is not fully implemented yet
112 *
113 * @param mimeType
114 * a mimetype, as returned by {@link #getAvailableOutfileTypes()}
115 * , or null if unknown.
116 * @return the three letter suffix common for this type.
117 */
118 public String getSuffixForMimeType(final String mimeType) {
119 return this.mimetype2suffix.get(mimeType.toLowerCase(Locale.ENGLISH));
120 }
121
122 /**
123 * Returns the MimeType for a given suffix.
124 *
125 * @param suffix
126 * the suffix, e.g. png, or null if unknown.
127 * @return the mime-type
128 */
129 public String getMimeTypeForSuffix(final String suffix) {
130 return this.suffix2mimetype.get(suffix.toLowerCase(Locale.ENGLISH));
131 }
132
133 /**
134 * Registers a new MimeType and it's suffix.
135 *
136 * @param mimeType
137 * the Mime-Type
138 * @param suffix
139 * The Suffix
140 * @param primary
141 * If true, old mappings will be overwritten. If false and a
142 * mapping already exists, it will stay the same.
143 */
144 public void registerMimeTypeAndSuffix(final String mimeType,
145 final String suffix, final boolean primary) {
146
147 final String lMimeType = mimeType.toLowerCase(Locale.ENGLISH);
148 final String lSuffix = suffix.toLowerCase(Locale.ENGLISH);
149
150 if (primary || !this.suffix2mimetype.containsKey(lSuffix)) {
151 this.suffix2mimetype.put(lSuffix, lMimeType);
152 }
153 if (primary || !this.mimetype2suffix.containsKey(lMimeType)) {
154 this.mimetype2suffix.put(lMimeType, lSuffix);
155 }
156 }
157
158 /**
159 * Registers a converter for the given mime type.
160 *
161 * @param mimeType
162 * The mime type.
163 * @param converter
164 * The converter to register.
165 * @param primary
166 * Converter for this type. If true, old mappings will be
167 * overwritten. If false and a mapping already exists, it will
168 * stay the same.
169 */
170 public void registerConverter(final String mimeType,
171 final ConverterPlugin converter, final boolean primary) {
172 if (primary || !this.mimetype2converter.containsKey(mimeType)) {
173 this.mimetype2converter.put(mimeType, converter);
174 }
175 }
176
177 /**
178 * Retrieve the converter for a given mime-type.
179 *
180 * @param mimeType
181 * the Mime-Type.
182 * @return a Converter instance
183 */
184 public ConverterPlugin getConverter(final String mimeType) {
185 return this.mimetype2converter
186 .get(mimeType.toLowerCase(Locale.ENGLISH));
187 }
188 }