KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > krysalis > barcode > BarcodeUtil


1 /*
2  * $Id: BarcodeUtil.java,v 1.8 2003/10/29 07:50:25 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;
59
60 import org.apache.avalon.framework.activity.Initializable;
61 import org.apache.avalon.framework.configuration.Configurable;
62 import org.apache.avalon.framework.configuration.Configuration;
63 import org.apache.avalon.framework.logger.LogEnabled;
64 import org.apache.avalon.framework.logger.Logger;
65 import org.krysalis.barcode.output.svg.SVGCanvasProvider;
66 import org.w3c.dom.DocumentFragment JavaDoc;
67
68 /**
69  * This is a convenience class to generate barcodes. It is implemented as
70  * Singleton to cache the BarcodeClassResolver. However, the class also
71  * contains a set of static methods which you can use of you manage your own
72  * BarcodeClassResolver.
73  *
74  * @author Jeremias Maerki
75  */

76 public class BarcodeUtil {
77     
78     private static BarcodeUtil instance = null;
79     
80     private BarcodeClassResolver classResolver = new DefaultBarcodeClassResolver();
81     
82     
83     /**
84      * Creates a new BarcodeUtil object. This constructor is protected because
85      * this class is designed as a singleton.
86      */

87     protected BarcodeUtil() {
88         //nop
89
}
90     
91     /**
92      * Returns the default instance of this class.
93      * @return the singleton
94      */

95     public static BarcodeUtil getInstance() {
96         if (instance == null) {
97             instance = new BarcodeUtil();
98         }
99         return instance;
100     }
101     
102     /**
103      * Creates a BarcoderGenerator.
104      * @param cfg Configuration object that specifies the barcode to produce.
105      * @param logger The logger to use
106      * @param classResolver The BarcodeClassResolver to use for lookup of
107      * barcode implementations.
108      * @return the newly instantiated BarcodeGenerator
109      * @throws BarcodeException if setting up a BarcodeGenerator fails
110      */

111     public static BarcodeGenerator createBarcodeGenerator(Configuration cfg,
112                                     Logger logger,
113                                     BarcodeClassResolver classResolver)
114             throws BarcodeException {
115         try {
116             Class JavaDoc cl = null;
117             
118             //First, check Configuration directly
119
String JavaDoc type = cfg.getName();
120             try {
121                 cl = classResolver.resolve(type);
122             } catch (ClassNotFoundException JavaDoc cnfe) {
123                 cl = null;
124             }
125             Configuration child = null;
126             if (cl == null) {
127                 //Second, check children
128
Configuration[] children = cfg.getChildren();
129                 if (children.length == 0) {
130                     throw new BarcodeException("Barcode configuration element expected");
131                 }
132
133                 //Find barcode config element
134
for (int i = 0; i < children.length; i++) {
135                     child = children[i];
136                     type = child.getName();
137                     try {
138                         cl = classResolver.resolve(type);
139                         break;
140                     } catch (ClassNotFoundException JavaDoc cnfe) {
141                         cl = null;
142                     }
143                 }
144             }
145
146             if (cl == null) {
147                 throw new BarcodeException(
148                     "No barcode configuration element not found");
149             }
150
151             //Instantiate the BarcodeGenerator
152
BarcodeGenerator gen = (BarcodeGenerator)cl.newInstance();
153             if (gen instanceof LogEnabled) {
154                 ((LogEnabled)gen).enableLogging(logger);
155             }
156             if (gen instanceof Configurable) {
157                 ((Configurable)gen).configure((child != null ? child : cfg));
158             }
159             if (gen instanceof Initializable) {
160                 ((Initializable)gen).initialize();
161             }
162             return gen;
163         } catch (Exception JavaDoc e) {
164             throw new BarcodeException("Error instantiating a barcode generator", e);
165         }
166     }
167             
168     /**
169      * Creates a BarcoderGenerator.
170      * @param cfg Configuration object that specifies the barcode to produce.
171      * @param logger The logger to use
172      * @return the newly instantiated BarcodeGenerator
173      * @throws BarcodeException if setting up a BarcodeGenerator fails
174      */

175     public BarcodeGenerator createBarcodeGenerator(Configuration cfg,
176                                                    Logger logger)
177             throws BarcodeException {
178         return createBarcodeGenerator(cfg, logger, this.classResolver);
179     }
180     
181     /**
182      * Convenience method to create an SVG barocde as a DOM fragment.
183      * @param cfg Configuration object that specifies the barcode to produce.
184      * @param logger The logger to use
185      * @param msg message to encode.
186      * @return the requested barcode as an DOM fragment (SVG format)
187      * @throws BarcodeException if an error occurs during barcode generation
188      */

189     public DocumentFragment JavaDoc generateBarcode(Configuration cfg,
190                                             Logger logger,
191                                             String JavaDoc msg)
192                     throws BarcodeException {
193         BarcodeGenerator gen = createBarcodeGenerator(cfg, logger);
194         try {
195             SVGCanvasProvider svg = new SVGCanvasProvider(false);
196
197             //Create Barcode and render it to SVG
198
gen.generateBarcode(svg, msg);
199     
200             return svg.getDOMFragment();
201         } catch (Exception JavaDoc e) {
202             throw new BarcodeException("Error while generating barcode", e);
203         }
204     }
205
206 }
207
Popular Tags