KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
26 import java.io.Writer JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import org.apache.avalon.framework.configuration.ConfigurationException;
30 import org.krysalis.barcode4j.BarcodeException;
31 import org.ofbiz.base.util.Debug;
32
33 import freemarker.template.TemplateModelException;
34 import freemarker.template.TemplateScalarModel;
35 import freemarker.template.TemplateTransformModel;
36
37 /**
38  * BarcodeTransform - Freemarker Transform for Barcodes
39  *
40  * @author Bryce Ewing
41  * @version 0.1
42  */

43 public class BarcodeTransform implements TemplateTransformModel {
44
45     public static final String JavaDoc module = BarcodeTransform.class.getName();
46
47     private static String JavaDoc getArg(Map JavaDoc args, String JavaDoc key) {
48         String JavaDoc result = "";
49         Object JavaDoc o = args.get(key);
50         if (o != null) {
51             if (Debug.verboseOn()) Debug.logVerbose("Arg Object : " + o.getClass().getName(), module);
52             if (o instanceof TemplateScalarModel) {
53                 TemplateScalarModel s = (TemplateScalarModel) o;
54                 try {
55                     result = s.getAsString();
56                 } catch (TemplateModelException e) {
57                     Debug.logError(e, "Template Exception", module);
58                 }
59             } else {
60               result = o.toString();
61             }
62         }
63         return result;
64     }
65
66     public Writer JavaDoc getWriter(final Writer JavaDoc out, Map JavaDoc args) {
67         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
68         final String JavaDoc specification = getArg(args, "specification");
69
70         return new Writer JavaDoc(out) {
71
72             public void write(char cbuf[], int off, int len) {
73                 buf.append(cbuf, off, len);
74             }
75
76             public void flush() throws IOException JavaDoc {
77                 out.flush();
78             }
79
80             public void close() throws IOException JavaDoc {
81                 if (specification == null) throw new IOException JavaDoc("Barcode specification not given");
82                 BarcodeGenerator generator = BarcodeFactory.getBarcodeGenerator(specification);
83                 if (generator == null) throw new IOException JavaDoc("Barcode generator creation failed");
84
85                 try {
86                     out.write(generator.generateSvgXml(buf.toString()));
87                 } catch (BarcodeException e) {
88                     throw new IOException JavaDoc(e.toString());
89                 } catch (ConfigurationException e) {
90                     throw new IOException JavaDoc(e.toString());
91                 }
92             }
93         };
94     }
95 }
96
Popular Tags