1 /*
2  * JasperReports - Free Java Reporting Library.
3  * Copyright (C) 2001 - 2019 TIBCO Software Inc. All rights reserved.
4  * http://www.jaspersoft.com
5  *
6  * Unless you have purchased a commercial license agreement from Jaspersoft,
7  * the following license terms apply:
8  *
9  * This program is part of JasperReports.
10  *
11  * JasperReports is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * JasperReports is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
23  */

24 package net.sf.jasperreports.components.barbecue;
25
26 import java.util.HashMap;
27 import java.util.Map;
28
29 import net.sf.jasperreports.engine.JRRuntimeException;
30 import net.sourceforge.barbecue.Barcode;
31 import net.sourceforge.barbecue.BarcodeException;
32 import net.sourceforge.barbecue.BarcodeFactory;
33 import net.sourceforge.barbecue.linear.ean.UCCEAN128Barcode;
34
35 /**
36  * 
37  * @author Lucian Chirita (lucianc@users.sourceforge.net)
38  */

39 public final class BarcodeProviders
40 {
41     public static final String EXCEPTION_MESSAGE_KEY_BARCODE_PROVIDER_NOT_FOUND = "components.barbecue.barcode.provider.not.found";
42     public static final String EXCEPTION_MESSAGE_KEY_ERROR_CREATING_BARCODE = "components.barbecue.error.creating.barcode";
43     
44     private static Map<String, BarcodeProvider> providers;
45
46     private static synchronized void initProviders()
47     {
48         if (providers != null)
49         {
50             return;
51         }
52
53         providers = new HashMap<String, BarcodeProvider>();
54         providers.put("2of7"new Barcode2of7Provider());
55         providers.put("3of9"new Barcode3of9Provider());
56         providers.put("Bookland"new BooklandProvider());
57         providers.put("Codabar"new CodabarProvider());
58         providers.put("Code128"new Code128Provider());
59         providers.put("Code128A"new Code128AProvider());
60         providers.put("Code128B"new Code128Provider());
61         providers.put("Code128C"new Code128CProvider());
62         providers.put("Code39"new Code39Provider());
63         providers.put("Code39 (Extended)"
64                 new Code39ExtendedProvider());
65         providers.put("EAN128"new EAN128Provider());
66         providers.put("EAN13"new EAN13Provider());
67         providers.put("GlobalTradeItemNumber"
68                 new GlobalTradeItemNumberProvider());
69         providers.put("Int2of5"new Int2of5Provider());
70         providers.put("Monarch"new MonarchProvider());
71         providers.put("NW7"new NW7Provider());
72         providers.put("PDF417"new PDF417Provider());
73         providers.put("PostNet"new PostNetProvider());
74         providers.put("RandomWeightUPCA"new RandomWeightUPCAProvider());
75         providers.put("SCC14ShippingCode"
76                 new SCC14ShippingCodeProvider());
77         providers.put("ShipmentIdentificationNumber"
78                 new ShipmentIdentificationNumberProvider());
79         providers.put("SSCC18"new SSCC18Provider());
80         providers.put("Std2of5"new Std2of5Provider());
81         providers.put("UCC128"new UCC128Provider());
82         providers.put("UPCA"new UPCAProvider());
83         providers.put("USD3"new USD3Provider());
84         providers.put("USD4"new USD4Provider());
85         providers.put("USPS"new USPSProvider());
86     }
87     
88     public static boolean isTypeSupported(String type)
89     {
90         initProviders();
91         
92         return providers.containsKey(type);
93     }
94     
95     public static Barcode createBarcode(BarcodeInfo barcodeInfo)
96     {
97         initProviders();
98         
99         BarcodeProvider provider = providers.get(
100                 barcodeInfo.getType());
101         if (provider == null)
102         {
103             throw 
104                 new JRRuntimeException(
105                     EXCEPTION_MESSAGE_KEY_BARCODE_PROVIDER_NOT_FOUND,
106                     new Object[]{barcodeInfo.getType()});
107         }
108         try
109         {
110             return provider.createBarcode(barcodeInfo);
111         }
112         catch (BarcodeException e)
113         {
114             throw 
115                 new JRRuntimeException(
116                     EXCEPTION_MESSAGE_KEY_ERROR_CREATING_BARCODE,
117                     (Object[])null,
118                     e);
119         }
120     }
121
122     public static class Barcode2of7Provider extends BaseBarcodeProvider
123     {
124         @Override
125         protected Barcode createBaseBarcode(BarcodeInfo barcodeInfo)
126                 throws BarcodeException
127         {
128             return BarcodeFactory.create2of7(barcodeInfo.getCode());
129         }
130     }
131
132     public static class Barcode3of9Provider extends BaseBarcodeProvider
133     {
134         @Override
135         protected Barcode createBaseBarcode(BarcodeInfo barcodeInfo)
136                 throws BarcodeException
137         {
138             return BarcodeFactory.create3of9(barcodeInfo.getCode(), 
139                     barcodeInfo.getRequiresChecksum());
140         }
141     }
142
143     public static class BooklandProvider extends BaseBarcodeProvider
144     {
145         @Override
146         protected Barcode createBaseBarcode(BarcodeInfo barcodeInfo)
147                 throws BarcodeException
148         {
149             return BarcodeFactory.createBookland(barcodeInfo.getCode());
150         }
151     }
152
153     public static class CodabarProvider extends BaseBarcodeProvider
154     {
155         @Override
156         protected Barcode createBaseBarcode(BarcodeInfo barcodeInfo)
157                 throws BarcodeException
158         {
159             return BarcodeFactory.createCodabar(barcodeInfo.getCode());
160         }
161     }
162
163     public static class Code128Provider extends BaseBarcodeProvider
164     {
165         @Override
166         protected Barcode createBaseBarcode(BarcodeInfo barcodeInfo)
167                 throws BarcodeException
168         {
169             return BarcodeFactory.createCode128(barcodeInfo.getCode());
170         }
171     }
172
173     public static class Code128AProvider extends BaseBarcodeProvider
174     {
175         @Override
176         protected Barcode createBaseBarcode(BarcodeInfo barcodeInfo)
177                 throws BarcodeException
178         {
179             return BarcodeFactory.createCode128A(barcodeInfo.getCode());
180         }
181     }
182
183     public static class Code128BProvider extends BaseBarcodeProvider
184     {
185         @Override
186         protected Barcode createBaseBarcode(BarcodeInfo barcodeInfo)
187                 throws BarcodeException
188         {
189             return BarcodeFactory.createCode128B(barcodeInfo.getCode());
190         }
191     }
192
193     public static class Code128CProvider extends BaseBarcodeProvider
194     {
195         @Override
196         protected Barcode createBaseBarcode(BarcodeInfo barcodeInfo)
197                 throws BarcodeException
198         {
199             return BarcodeFactory.createCode128C(barcodeInfo.getCode());
200         }
201     }
202
203     public static class Code39Provider extends BaseBarcodeProvider
204     {
205         @Override
206         protected Barcode createBaseBarcode(BarcodeInfo barcodeInfo)
207                 throws BarcodeException
208         {
209             return BarcodeFactory.createCode39(barcodeInfo.getCode(), 
210                     barcodeInfo.getRequiresChecksum());
211         }
212     }
213
214     public static class Code39ExtendedProvider extends BaseBarcodeProvider
215     {
216         @Override
217         protected Barcode createBaseBarcode(BarcodeInfo barcodeInfo)
218                 throws BarcodeException
219         {
220             return new net.sourceforge.barbecue.linear.code39.Code39Barcode(
221                     barcodeInfo.getCode(), barcodeInfo.getRequiresChecksum(), true);
222         }
223     }
224
225     public static class EAN128Provider extends BaseBarcodeProvider
226     {
227         @Override
228         protected Barcode createBaseBarcode(BarcodeInfo barcodeInfo)
229                 throws BarcodeException
230         {
231             return new UCCEAN128Barcode(UCCEAN128Barcode.EAN128_AI, 
232                     barcodeInfo.getCode(), barcodeInfo.getRequiresChecksum());
233         }
234     }
235
236     public static class EAN13Provider extends BaseBarcodeProvider
237     {
238         @Override
239         protected Barcode createBaseBarcode(BarcodeInfo barcodeInfo)
240                 throws BarcodeException
241         {
242             return BarcodeFactory.createEAN13(barcodeInfo.getCode());
243         }
244     }
245
246     public static class GlobalTradeItemNumberProvider extends BaseBarcodeProvider
247     {
248         @Override
249         protected Barcode createBaseBarcode(BarcodeInfo barcodeInfo)
250                 throws BarcodeException
251         {
252             return BarcodeFactory.createGlobalTradeItemNumber(barcodeInfo.getCode());
253         }
254     }
255
256     public static class Int2of5Provider extends BaseBarcodeProvider
257     {
258         @Override
259         protected Barcode createBaseBarcode(BarcodeInfo barcodeInfo)
260                 throws BarcodeException
261         {
262             return BarcodeFactory.createInt2of5(
263                     barcodeInfo.getCode(), barcodeInfo.getRequiresChecksum());
264         }
265     }
266
267     public static class MonarchProvider extends BaseBarcodeProvider
268     {
269         @Override
270         protected Barcode createBaseBarcode(BarcodeInfo barcodeInfo)
271                 throws BarcodeException
272         {
273             return BarcodeFactory.createMonarch(barcodeInfo.getCode());
274         }
275     }
276
277     public static class NW7Provider extends BaseBarcodeProvider
278     {
279         @Override
280         protected Barcode createBaseBarcode(BarcodeInfo barcodeInfo)
281                 throws BarcodeException
282         {
283             return BarcodeFactory.createNW7(barcodeInfo.getCode());
284         }
285     }
286
287     public static class PDF417Provider extends BaseBarcodeProvider
288     {
289         @Override
290         protected Barcode createBaseBarcode(BarcodeInfo barcodeInfo)
291                 throws BarcodeException
292         {
293             return BarcodeFactory.createPDF417(barcodeInfo.getCode());
294         }
295     }
296
297     public static class PostNetProvider extends BaseBarcodeProvider
298     {
299         @Override
300         protected Barcode createBaseBarcode(BarcodeInfo barcodeInfo)
301                 throws BarcodeException
302         {
303             return BarcodeFactory.createPostNet(barcodeInfo.getCode());
304         }
305     }
306
307     public static class RandomWeightUPCAProvider extends BaseBarcodeProvider
308     {
309         @Override
310         protected Barcode createBaseBarcode(BarcodeInfo barcodeInfo)
311                 throws BarcodeException
312         {
313             return BarcodeFactory.createRandomWeightUPCA(barcodeInfo.getCode());
314         }
315     }
316
317     public static class SCC14ShippingCodeProvider extends BaseBarcodeProvider
318     {
319         @Override
320         protected Barcode createBaseBarcode(BarcodeInfo barcodeInfo)
321                 throws BarcodeException
322         {
323             return BarcodeFactory.createSCC14ShippingCode(barcodeInfo.getCode());
324         }
325     }
326
327     public static class ShipmentIdentificationNumberProvider extends BaseBarcodeProvider
328     {
329         @Override
330         protected Barcode createBaseBarcode(BarcodeInfo barcodeInfo)
331                 throws BarcodeException
332         {
333             return BarcodeFactory.createShipmentIdentificationNumber(barcodeInfo.getCode());
334         }
335     }
336
337     public static class SSCC18Provider extends BaseBarcodeProvider
338     {
339         @Override
340         protected Barcode createBaseBarcode(BarcodeInfo barcodeInfo)
341                 throws BarcodeException
342         {
343             return new UCCEAN128Barcode(UCCEAN128Barcode.SSCC_18_AI, 
344                     barcodeInfo.getCode(),
345                     barcodeInfo.getRequiresChecksum());
346         }
347     }
348
349     public static class Std2of5Provider extends BaseBarcodeProvider
350     {
351         @Override
352         protected Barcode createBaseBarcode(BarcodeInfo barcodeInfo)
353                 throws BarcodeException
354         {
355             return BarcodeFactory.createStd2of5(barcodeInfo.getCode(), 
356                     barcodeInfo.getRequiresChecksum());
357         }
358     }
359
360     public static class UCC128Provider extends BaseBarcodeProvider
361     {
362         @Override
363         protected Barcode createBaseBarcode(BarcodeInfo barcodeInfo)
364                 throws BarcodeException
365         {
366             return new UCCEAN128Barcode(barcodeInfo.getApplicationIdentifier(), 
367                     barcodeInfo.getCode(), barcodeInfo.getRequiresChecksum());
368         }
369     }
370
371     public static class UPCAProvider extends BaseBarcodeProvider
372     {
373         @Override
374         protected Barcode createBaseBarcode(BarcodeInfo barcodeInfo)
375                 throws BarcodeException
376         {
377             return BarcodeFactory.createUPCA(barcodeInfo.getCode());
378         }
379     }
380
381     public static class USD3Provider extends BaseBarcodeProvider
382     {
383         @Override
384         protected Barcode createBaseBarcode(BarcodeInfo barcodeInfo)
385                 throws BarcodeException
386         {
387             return BarcodeFactory.createUSD3(barcodeInfo.getCode(),
388                     barcodeInfo.getRequiresChecksum());
389         }
390     }
391
392     public static class USD4Provider extends BaseBarcodeProvider
393     {
394         @Override
395         protected Barcode createBaseBarcode(BarcodeInfo barcodeInfo)
396                 throws BarcodeException
397         {
398             return BarcodeFactory.createUSD4(barcodeInfo.getCode());
399         }
400     }
401
402     public static class USPSProvider extends BaseBarcodeProvider
403     {
404         @Override
405         protected Barcode createBaseBarcode(BarcodeInfo barcodeInfo)
406                 throws BarcodeException
407         {
408             return BarcodeFactory.createUSPS(barcodeInfo.getCode());
409         }
410     }
411     
412     private BarcodeProviders()
413     {
414     }
415 }
416