KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > webapp > barcode > BarcodeFactory


1 /*
2  * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
19  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
20  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */

23 package org.ofbiz.webapp.barcode;
24
25 import java.io.BufferedReader JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStreamReader JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import org.ofbiz.base.util.Debug;
33 import org.ofbiz.base.util.UtilURL;
34
35 /**
36  * Factory for the creation of barcode generators. The barcode generator is based
37  * on the barcode type given. This selects which barcode specification file to use.
38  * The barcode specification file is found in the barcode directory on the classpath.
39  * The mapping between barcodeType and the specification file is, e.g:
40  * invoiceShippingId -> barcode/invoiceShippingId.xml
41  *
42  * The barcode specification file must be formatted based on the formats given at:
43  * http://krysalis.org/barcode/barcode-xml.html
44  *
45  * @author Bryce Ewing
46  * @version 0.1
47  */

48 public class BarcodeFactory {
49
50     public static final String JavaDoc module = BarcodeFactory.class.getName();
51
52     private static Map JavaDoc barcodeGeneratorMap = new HashMap JavaDoc();
53
54     /**
55      * Get a barcode generator for the given barcode type.
56      *
57      * @param barcodeType the barcode type of the barcode generator.
58      * @return the given barcode generator.
59      */

60     public static BarcodeGenerator getBarcodeGenerator(String JavaDoc barcodeType) {
61         synchronized (barcodeGeneratorMap) {
62             if (barcodeGeneratorMap.containsKey(barcodeType)) {
63                 return (BarcodeGenerator) barcodeGeneratorMap.get(barcodeType);
64             }
65             else {
66                 String JavaDoc specificationFile = "barcode/" + barcodeType + ".xml";
67
68                 URL JavaDoc specificationUrl = UtilURL.fromResource(specificationFile);
69                 if (specificationUrl == null) {
70                     Debug.logError("Problem getting the specification URL: " + specificationFile + " not found", module);
71                     return null;
72                 }
73
74                 StringBuffer JavaDoc format = new StringBuffer JavaDoc();
75                 BufferedReader JavaDoc specificationReader = null;
76                 try {
77                     specificationReader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(specificationUrl.openStream()));
78                     String JavaDoc line = null;
79                     while ((line = specificationReader.readLine()) != null) {
80                         format.append(line);
81                     }
82                 }
83                 catch (java.io.IOException JavaDoc e) {
84                     Debug.logError(e, "Error reading the specification URL: " + specificationFile, module);
85                     return null;
86                 }
87                 finally {
88                     if (specificationReader != null) {
89                         try {
90                             specificationReader.close();
91                         }
92                         catch (IOException JavaDoc e) {
93                             Debug.logError(e, "Error closing the specification URL: " + specificationFile, module);
94                             return null;
95                         }
96                     }
97                 }
98
99                 BarcodeGenerator generator = new BarcodeGenerator(format.toString());
100                 barcodeGeneratorMap.put(barcodeType, generator);
101                 return generator;
102             }
103         }
104     }
105
106 }
107
Popular Tags