1 58 package org.krysalis.barcode.fop0205; 59 60 import javax.xml.parsers.DocumentBuilderFactory ; 61 import javax.xml.parsers.ParserConfigurationException ; 62 63 import org.xml.sax.Attributes ; 64 import org.w3c.dom.Document ; 65 import org.w3c.dom.Element ; 66 67 import org.apache.avalon.framework.CascadingRuntimeException; 68 import org.apache.avalon.framework.configuration.Configuration; 69 import org.apache.avalon.framework.configuration.ConfigurationException; 70 import org.apache.avalon.framework.logger.NullLogger; 71 import org.apache.fop.apps.FOPException; 72 import org.apache.fop.fo.FObj; 73 import org.apache.fop.fo.PropertyList; 74 import org.apache.fop.fo.Status; 75 import org.apache.fop.fo.DirectPropertyListBuilder; 76 import org.apache.fop.layout.Area; 77 import org.apache.fop.layout.FontState; 78 import org.apache.fop.layout.inline.ForeignObjectArea; 79 import org.apache.fop.messaging.MessageHandler; 80 81 import org.krysalis.barcode.BarcodeConstants; 82 import org.krysalis.barcode.BarcodeDimension; 83 import org.krysalis.barcode.BarcodeException; 84 import org.krysalis.barcode.BarcodeGenerator; 85 import org.krysalis.barcode.BarcodeUtil; 86 import org.krysalis.barcode.tools.ConfigurationUtil; 87 import org.krysalis.barcode.tools.UnitConv; 88 89 94 public class BarcodeElement extends BarcodeObj { 95 96 private static final String XMLNS_NAMESPACE_URI = 97 "http://www.w3.org/2000/xmlns/"; 98 99 100 103 public static class Maker extends FObj.Maker { 104 105 113 public FObj make(FObj parent, PropertyList propertyList, 114 String systemId, int line, int column) 115 throws FOPException { 116 return new BarcodeElement(parent, propertyList, 117 systemId, line, column); 118 } 119 } 120 121 126 public static FObj.Maker maker() { 127 return new BarcodeElement.Maker(); 128 } 129 130 FontState fs; 131 132 138 public BarcodeElement(FObj parent, PropertyList propertyList, 139 String systemId, int line, int column) { 140 super(parent, propertyList, "barcode", systemId, line, column); 141 init(); 142 } 143 144 protected void init() { 145 try { 146 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 147 factory.setNamespaceAware(true); 148 this.doc = factory.newDocumentBuilder().newDocument(); 149 } catch (ParserConfigurationException pce) { 150 throw new CascadingRuntimeException("Error creating DOM", pce); 151 } 152 this.doc.appendChild(this.doc.createElementNS(BarcodeConstants.NAMESPACE, "barcode")); 153 154 this.element = doc.getDocumentElement(); 155 156 buildTopLevel(this.doc, this.element); 157 } 158 159 public void buildTopLevel(Document doc, Element svgRoot) { 160 if(this.properties instanceof DirectPropertyListBuilder.AttrPropertyList) { 162 Attributes attr = ((DirectPropertyListBuilder.AttrPropertyList)this.properties).getAttributes(); 163 for (int count = 0; count < attr.getLength(); count++) { 164 String rf = attr.getValue(count); 165 String qname = attr.getQName(count); 166 if (qname.indexOf(":") == -1) { 167 element.setAttribute(qname, rf); 168 } else { 169 String pref = 170 qname.substring(0, qname.indexOf(":")); 171 ns.put("xlink", "http://www.w3.org/1999/xlink"); 172 if (pref.equals("xmlns")) { 173 ns.put(qname.substring(qname.indexOf(":") 174 + 1), rf); 175 element.setAttributeNS(XMLNS_NAMESPACE_URI, 176 qname, rf); 177 } else { 178 element.setAttributeNS((String )ns.get(pref), 179 qname, rf); 180 } 181 } 182 } 183 } else { 184 } 185 } 186 187 194 protected BarcodeArea createArea(FontState fontState, float width, float height) { 195 return new BarcodeArea(fontState, width, height); 196 } 197 198 205 public int layout(final Area area) throws FOPException { 206 if (!(area instanceof ForeignObjectArea)) { 207 throw new FOPException("Barcode not in fo:instream-foreign-object"); 209 } 210 211 if (this.marker == START) { 212 this.fs = area.getFontState(); 213 214 this.marker = 0; 215 } 216 217 219 final Element barcodeRoot = element; 220 221 222 final ForeignObjectArea foa = (ForeignObjectArea)area; 223 224 Element e = this.doc.getDocumentElement(); 225 226 e.setAttributeNS(XMLNS_NAMESPACE_URI, "xmlns", BarcodeConstants.NAMESPACE); 228 230 Configuration cfg = ConfigurationUtil.buildConfiguration(this.doc); 231 try { 232 final String msg = cfg.getAttribute("msg"); 233 final String renderMode = cfg.getAttribute("render-mode", "native"); 235 237 BarcodeGenerator bargen = BarcodeUtil.getInstance(). 238 createBarcodeGenerator(cfg, new NullLogger()); 239 BarcodeDimension bardim = bargen.calcDimensions(msg); 240 final float w = (float)UnitConv.mm2pt(bardim.getWidthPlusQuiet()) * 1000; 241 final float h = (float)UnitConv.mm2pt(bardim.getHeightPlusQuiet()) * 1000; 242 243 244 BarcodeArea barcodeArea = createArea(fs, w, h); 245 barcodeArea.setBarcode(bargen, msg, renderMode); 246 barcodeArea.start(); 247 barcodeArea.end(); 248 249 250 foa.setObject(barcodeArea); 251 foa.setIntrinsicWidth(barcodeArea.getWidth()); 252 foa.setIntrinsicHeight(barcodeArea.getHeight()); 253 254 255 return Status.OK; 256 } catch (ConfigurationException ce) { 257 MessageHandler.errorln("Error in barcode XML: " + ce.getMessage()); 258 throw new FOPException("Error in barcode XML", ce); 259 } catch (BarcodeException be) { 260 MessageHandler.errorln("Error generating barcode: " + be.getMessage()); 261 throw new FOPException("Error generating barcode", be); 262 } 263 } 264 265 266 } 267 | Popular Tags |