KickJava   Java API By Example, From Geeks To Geeks.

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


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.ByteArrayInputStream JavaDoc;
26
27 import nu.xom.Node;
28 import nu.xom.Nodes;
29 import nu.xom.converters.DOMConverter;
30
31 import org.apache.avalon.framework.configuration.Configuration;
32 import org.apache.avalon.framework.configuration.ConfigurationException;
33 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
34 import org.krysalis.barcode4j.BarcodeException;
35 import org.krysalis.barcode4j.BarcodeUtil;
36 import org.ofbiz.base.util.Debug;
37 import org.w3c.dom.DocumentFragment JavaDoc;
38
39 /**
40  * Wrapper around Krysalis Barcode for generating barcodes.
41  *
42  * @author Bryce Ewing
43  * @version 0.1
44  */

45 public class BarcodeGenerator {
46
47     public static final String JavaDoc module = BarcodeGenerator.class.getName();
48
49     private Configuration config;
50     // removing this because the new version of Barcode4J doesn't have a parameter for it: private Logger log;
51
private BarcodeUtil barcodeUtil;
52
53     public BarcodeGenerator(String JavaDoc barcodeFormat) {
54         DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
55         try {
56             byte [] configData = barcodeFormat.getBytes();
57             config = builder.build(new ByteArrayInputStream JavaDoc(configData));
58         }
59         catch (Exception JavaDoc e) {
60             Debug.logError(e, "Couldn't create config for Barcode Generator", module);
61         }
62
63         //log = new Log4JLogger(Debug.getLogger(module));
64

65         barcodeUtil = BarcodeUtil.getInstance();
66     }
67
68     public String JavaDoc generateSvgXml(String JavaDoc message) throws BarcodeException, ConfigurationException {
69         if (config != null) {
70             DocumentFragment JavaDoc fragment = barcodeUtil.generateSVGBarcode(config, message);
71
72             Nodes nodes = DOMConverter.convert(fragment);
73
74             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
75             for (int i = 0; i < nodes.size(); i++) {
76                 Node node = nodes.get(i);
77                 buffer.append(node.toXML());
78             }
79
80             return buffer.toString();
81         } else {
82             return "";
83         }
84     }
85 }
86
Popular Tags