1 58 package org.krysalis.barcode.xalan; 59 60 import javax.xml.transform.TransformerException ; 61 62 import org.apache.avalon.framework.configuration.Configuration; 63 import org.apache.avalon.framework.logger.Logger; 64 import org.apache.xalan.extensions.XSLProcessorContext; 65 import org.apache.xalan.templates.ElemExtensionCall; 66 import org.krysalis.barcode.BarcodeGenerator; 67 import org.krysalis.barcode.BarcodeUtil; 68 import org.krysalis.barcode.output.svg.SVGCanvasProvider; 69 import org.krysalis.barcode.tools.ConfigurationUtil; 70 import org.w3c.dom.DocumentFragment ; 71 import org.w3c.dom.NodeList ; 72 73 import org.xml.sax.SAXException ; 74 75 80 public class BarcodeExt { 81 82 private Logger log = new org.apache.avalon.framework.logger.ConsoleLogger(); 83 84 91 public DocumentFragment generate(NodeList nl, String message) 92 throws SAXException { 93 try { 94 95 final Configuration cfg = ConfigurationUtil.buildConfiguration(nl.item(0)); 97 98 final BarcodeGenerator gen = 100 BarcodeUtil.getInstance().createBarcodeGenerator(cfg, log); 101 102 final SVGCanvasProvider svg; 104 if (cfg.getAttributeAsBoolean("useNamespace", true)) { 105 svg = new SVGCanvasProvider(cfg.getAttribute("prefix", "svg")); 106 } else { 107 svg = new SVGCanvasProvider(false); 108 } 109 gen.generateBarcode(svg, message); 111 112 return svg.getDOMFragment(); 113 } catch (Exception e) { 114 e.printStackTrace(); 115 throw new SAXException (e); 116 } 117 } 118 119 127 public DocumentFragment barcode(XSLProcessorContext context, ElemExtensionCall elem) 128 throws javax.xml.transform.TransformerException { 129 try { 130 String message = elem.getAttribute("message", 131 context.getContextNode(), context.getTransformer()); 132 133 final Configuration cfg = new ElemWrappingConfiguration(elem); 135 136 final BarcodeGenerator gen = 138 BarcodeUtil.getInstance().createBarcodeGenerator(cfg, log); 139 140 final SVGCanvasProvider svg; 142 if (cfg.getAttributeAsBoolean("useNamespace", true)) { 143 svg = new SVGCanvasProvider(cfg.getAttribute("prefix", "svg")); 144 } else { 145 svg = new SVGCanvasProvider(false); 146 } 147 gen.generateBarcode(svg, message); 149 150 return svg.getDOMFragment(); 151 } catch (Exception e) { 152 e.printStackTrace(); 153 throw new TransformerException (e); 154 } 155 156 } 157 158 159 } 160 | Popular Tags |