1 23 package org.ofbiz.webapp.barcode; 24 25 import java.io.IOException ; 26 import java.io.Writer ; 27 import java.util.Map ; 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 43 public class BarcodeTransform implements TemplateTransformModel { 44 45 public static final String module = BarcodeTransform.class.getName(); 46 47 private static String getArg(Map args, String key) { 48 String result = ""; 49 Object 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 getWriter(final Writer out, Map args) { 67 final StringBuffer buf = new StringBuffer (); 68 final String specification = getArg(args, "specification"); 69 70 return new Writer (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 { 77 out.flush(); 78 } 79 80 public void close() throws IOException { 81 if (specification == null) throw new IOException ("Barcode specification not given"); 82 BarcodeGenerator generator = BarcodeFactory.getBarcodeGenerator(specification); 83 if (generator == null) throw new IOException ("Barcode generator creation failed"); 84 85 try { 86 out.write(generator.generateSvgXml(buf.toString())); 87 } catch (BarcodeException e) { 88 throw new IOException (e.toString()); 89 } catch (ConfigurationException e) { 90 throw new IOException (e.toString()); 91 } 92 } 93 }; 94 } 95 } 96 | Popular Tags |