KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > krysalis > barcode > fop0205 > BarcodeElement


1 /*
2  * $Id: BarcodeElement.java,v 1.1 2003/08/18 19:36:55 jmaerki Exp $
3  * ============================================================================
4  * The Krysalis Patchy Software License, Version 1.1_01
5  * Copyright (c) 2003 Nicola Ken Barozzi. All rights reserved.
6  *
7  * This Licence is compatible with the BSD licence as described and
8  * approved by http://www.opensource.org/, and is based on the
9  * Apache Software Licence Version 1.1.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in
20  * the documentation and/or other materials provided with the
21  * distribution.
22  *
23  * 3. The end-user documentation included with the redistribution,
24  * if any, must include the following acknowledgment:
25  * "This product includes software developed for project
26  * Krysalis (http://www.krysalis.org/)."
27  * Alternately, this acknowledgment may appear in the software itself,
28  * if and wherever such third-party acknowledgments normally appear.
29  *
30  * 4. The names "Krysalis" and "Nicola Ken Barozzi" and
31  * "Krysalis Barcode" must not be used to endorse or promote products
32  * derived from this software without prior written permission. For
33  * written permission, please contact nicolaken@krysalis.org.
34  *
35  * 5. Products derived from this software may not be called "Krysalis",
36  * "Krysalis Barcode", nor may "Krysalis" appear in their name,
37  * without prior written permission of Nicola Ken Barozzi.
38  *
39  * 6. This software may contain voluntary contributions made by many
40  * individuals, who decided to donate the code to this project in
41  * respect of this licence, and was originally created by
42  * Jeremias Maerki <jeremias@maerki.org>.
43  *
44  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
45  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
47  * DISCLAIMED. IN NO EVENT SHALL THE KRYSALIS PROJECT OR
48  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
50  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
51  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
52  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
53  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
54  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  * ====================================================================
57  */

58 package org.krysalis.barcode.fop0205;
59
60 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
61 import javax.xml.parsers.ParserConfigurationException JavaDoc;
62
63 import org.xml.sax.Attributes JavaDoc;
64 import org.w3c.dom.Document JavaDoc;
65 import org.w3c.dom.Element JavaDoc;
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 /**
90  * Class representing bc:barcode pseudo flow object.
91  *
92  * @author Jeremias Maerki
93  */

94 public class BarcodeElement extends BarcodeObj {
95
96     private static final String JavaDoc XMLNS_NAMESPACE_URI =
97             "http://www.w3.org/2000/xmlns/";
98
99
100     /**
101      * inner class for making SVG objects.
102      */

103     public static class Maker extends FObj.Maker {
104
105         /**
106          * make an SVG object.
107          *
108          * @param parent the parent formatting object
109          * @param propertyList the explicit properties of this object
110          *
111          * @return the SVG object
112          */

113         public FObj make(FObj parent, PropertyList propertyList,
114                          String JavaDoc systemId, int line, int column)
115                         throws FOPException {
116             return new BarcodeElement(parent, propertyList,
117                                   systemId, line, column);
118         }
119     }
120
121     /**
122      * returns the maker for this object.
123      *
124      * @return the maker for SVG objects
125      */

126     public static FObj.Maker maker() {
127         return new BarcodeElement.Maker();
128     }
129
130     FontState fs;
131
132     /**
133      * constructs an SVG object (called by Maker).
134      *
135      * @param parent the parent formatting object
136      * @param propertyList the explicit properties of this object
137      */

138     public BarcodeElement(FObj parent, PropertyList propertyList,
139                       String JavaDoc 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 JavaDoc factory = DocumentBuilderFactory.newInstance();
147             factory.setNamespaceAware(true);
148             this.doc = factory.newDocumentBuilder().newDocument();
149         } catch (ParserConfigurationException JavaDoc 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 JavaDoc doc, Element JavaDoc svgRoot) {
160         // build up the info for the top level element
161
if(this.properties instanceof DirectPropertyListBuilder.AttrPropertyList) {
162             Attributes JavaDoc attr = ((DirectPropertyListBuilder.AttrPropertyList)this.properties).getAttributes();
163             for (int count = 0; count < attr.getLength(); count++) {
164                 String JavaDoc rf = attr.getValue(count);
165                 String JavaDoc qname = attr.getQName(count);
166                 if (qname.indexOf(":") == -1) {
167                     element.setAttribute(qname, rf);
168                 } else {
169                     String JavaDoc 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 JavaDoc)ns.get(pref),
179                                                qname, rf);
180                     }
181                 }
182             }
183         } else {
184         }
185     }
186
187     /**
188      * Factory method for creating BarcodeAreas.
189      * @param fontState the font state
190      * @param width the width of the area
191      * @param height the height of the area
192      * @return the newly created BarcodeArea
193      */

194     protected BarcodeArea createArea(FontState fontState, float width, float height) {
195         return new BarcodeArea(fontState, width, height);
196     }
197
198     /**
199      * layout this formatting object.
200      *
201      * @param area the area to layout the object into
202      *
203      * @return the status of the layout
204      */

205     public int layout(final Area area) throws FOPException {
206         if (!(area instanceof ForeignObjectArea)) {
207             // this is an error
208
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         //MessageHandler.logln("Creating barcode area");
218

219         final Element JavaDoc barcodeRoot = element;
220         /* create a barcode area */
221         /* if width and height are zero, get the bounds of the content. */
222         final ForeignObjectArea foa = (ForeignObjectArea)area;
223
224         Element JavaDoc e = this.doc.getDocumentElement();
225
226         //if(!e.hasAttributeNS(XMLSupport.XMLNS_NAMESPACE_URI, "xmlns")) {
227
e.setAttributeNS(XMLNS_NAMESPACE_URI, "xmlns", BarcodeConstants.NAMESPACE);
228         //}
229

230         Configuration cfg = ConfigurationUtil.buildConfiguration(this.doc);
231         try {
232             final String JavaDoc msg = cfg.getAttribute("msg");
233             //MessageHandler.logln("Barcode message: " + msg);
234
final String JavaDoc renderMode = cfg.getAttribute("render-mode", "native");
235             //MessageHandler.logln("Render mode: " + renderMode);
236

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             /* add the SVG area to the containing area */
250             foa.setObject(barcodeArea);
251             foa.setIntrinsicWidth(barcodeArea.getWidth());
252             foa.setIntrinsicHeight(barcodeArea.getHeight());
253             
254             /* return status */
255             return Status.OK;
256         } catch (ConfigurationException JavaDoc 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