KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > krysalis > barcode > output > bitmap > BitmapEncoderRegistry


1 /*
2  * $Id: BitmapEncoderRegistry.java,v 1.7 2003/10/31 07:42:03 jmaerki Exp $
3  * ============================================================================
4  * The Krysalis Patchy Software License, Version 1.1_01
5  * Copyright (c) 2002-2003 Nicola Ken Barozzi. All rights reserved.
6  *
7  * This Licence is compatible with the BSD licence as described and
8  * approved by http://www.opensource.org/, and is based on the
9  * Apache Software Licence Version 1.1.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in
20  * the documentation and/or other materials provided with the
21  * distribution.
22  *
23  * 3. The end-user documentation included with the redistribution,
24  * if any, must include the following acknowledgment:
25  * "This product includes software developed for project
26  * Krysalis (http://www.krysalis.org/)."
27  * Alternately, this acknowledgment may appear in the software itself,
28  * if and wherever such third-party acknowledgments normally appear.
29  *
30  * 4. The names "Krysalis" and "Nicola Ken Barozzi" and
31  * "Krysalis Barcode" must not be used to endorse or promote products
32  * derived from this software without prior written permission. For
33  * written permission, please contact nicolaken@krysalis.org.
34  *
35  * 5. Products derived from this software may not be called "Krysalis",
36  * "Krysalis Barcode", nor may "Krysalis" appear in their name,
37  * without prior written permission of Nicola Ken Barozzi.
38  *
39  * 6. This software may contain voluntary contributions made by many
40  * individuals, who decided to donate the code to this project in
41  * respect of this licence, and was originally created by
42  * Jeremias Maerki <jeremias@maerki.org>.
43  *
44  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
45  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
47  * DISCLAIMED. IN NO EVENT SHALL THE KRYSALIS PROJECT OR
48  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
50  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
51  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
52  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
53  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
54  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  * ====================================================================
57  */

58 package org.krysalis.barcode.output.bitmap;
59
60 import java.util.Iterator JavaDoc;
61 import java.util.Set JavaDoc;
62
63 /**
64  * Registry class for BitmapEncoders.
65  *
66  * @author Jeremias Maerki
67  */

68 public class BitmapEncoderRegistry {
69
70     private static Set JavaDoc encoders = new java.util.TreeSet JavaDoc();
71
72     static {
73         register("org.krysalis.barcode.output.bitmap.SunJPEGBitmapEncoder", 0, false);
74         register("org.krysalis.barcode.output.bitmap.ImageIOBitmapEncoder", 50, false);
75     }
76
77     /**
78      * Utility class: Constructor prevents instantiating when subclassed.
79      */

80     protected BitmapEncoderRegistry() {
81         throw new UnsupportedOperationException JavaDoc();
82     }
83     
84     private static class Entry implements Comparable JavaDoc {
85         private BitmapEncoder encoder;
86         private int priority;
87         
88         public Entry(BitmapEncoder encoder, int priority) {
89             this.encoder = encoder;
90             this.priority = priority;
91         }
92         
93         /**
94          * @see java.lang.Comparable#compareTo(java.lang.Object)
95          */

96         public int compareTo(Object JavaDoc o) {
97             Entry e = (Entry)o;
98             return e.priority - this.priority; //highest priority first
99
}
100     }
101     
102     private static void register(String JavaDoc classname, int priority, boolean complain) {
103         boolean failed = false;
104         try {
105             Class JavaDoc clazz = Class.forName(classname);
106             BitmapEncoder encoder = (BitmapEncoder)clazz.newInstance();
107             encoders.add(new Entry(encoder, priority));
108         } catch (Exception JavaDoc e) {
109             failed = true;
110         } catch (LinkageError JavaDoc le) {
111             failed = true; //NoClassDefFoundError for example
112
}
113         if (failed) {
114             if (complain) {
115                 throw new IllegalArgumentException JavaDoc(
116                     "The implementation being registered is unavailable or "
117                     + "cannot be instantiated: " + classname);
118             } else {
119                 return;
120             }
121         }
122     }
123
124     /**
125      * Register a new BitmapEncoder implementation.
126      * @param classname fully qualified classname of the BitmapEncoder
127      * implementation
128      * @param priority lets you define a priority for an encoder. If you want
129      * to give an encoder a high priority, assign a value of 100 or higher.
130      */

131     public static void register(String JavaDoc classname, int priority) {
132         register(classname, priority, true);
133     }
134
135     /**
136      * Indicates whether a specific BitmapEncoder implementation supports a
137      * particular MIME type.
138      * @param encoder BitmapEncoder to inspect
139      * @param mime MIME type to check
140      * @return true if the MIME type is supported
141      */

142     public static boolean supports(BitmapEncoder encoder, String JavaDoc mime) {
143         String JavaDoc[] mimes = encoder.getSupportedMIMETypes();
144         for (int i = 0; i < mimes.length; i++) {
145             if (mimes[i].equals(mime)) {
146                 return true;
147             }
148         }
149         return false;
150     }
151
152     /**
153      * Indicates whether a particular MIME type is supported by one of the
154      * registered BitmapEncoder implementations.
155      * @param mime MIME type to check
156      * @return true if the MIME type is supported
157      */

158     public static boolean supports(String JavaDoc mime) {
159         Iterator JavaDoc i = encoders.iterator();
160         while (i.hasNext()) {
161             Entry entry = (Entry)i.next();
162             BitmapEncoder encoder = entry.encoder;
163             if (supports(encoder, mime)) {
164                 return true;
165             }
166         }
167         return false;
168     }
169
170     /**
171      * Returns a BitmapEncoder instance for a particular MIME type.
172      * @param mime desired MIME type
173      * @return a BitmapEncoder instance (throws an UnsupportedOperationException
174      * if no suitable BitmapEncoder is available)
175      */

176     public static BitmapEncoder getInstance(String JavaDoc mime) {
177         Iterator JavaDoc i = encoders.iterator();
178         while (i.hasNext()) {
179             Entry entry = (Entry)i.next();
180             BitmapEncoder encoder = entry.encoder;
181             if (supports(encoder, mime)) {
182                 return encoder;
183             }
184         }
185         throw new UnsupportedOperationException JavaDoc(
186             "No BitmapEncoder available for " + mime);
187     }
188
189     /**
190      * Returns a Set of Strings with all the supported MIME types from all
191      * registered BitmapEncoders.
192      * @return a Set of Strings (MIME types)
193      */

194     public static Set JavaDoc getSupportedMIMETypes() {
195         Set JavaDoc mimes = new java.util.HashSet JavaDoc();
196         Iterator JavaDoc i = encoders.iterator();
197         while (i.hasNext()) {
198             Entry entry = (Entry)i.next();
199             BitmapEncoder encoder = entry.encoder;
200             String JavaDoc[] s = encoder.getSupportedMIMETypes();
201             for (int j = 0; j < s.length; j++) {
202                 mimes.add(s[j]);
203             }
204         }
205         return mimes;
206     }
207
208 }
209
Popular Tags