Now I get it. Thanks. See my comments inline.
On 06.12.2004 17:48:21 f.dalan wrote:
> Thank you Jeremias for your reply.
> I use the FOP extension actually. Excuse me, but maybe I didn't understad.
>=20
> My code is the following:
<snip/>
>=20
> public class KrysalisTest {
>=20
> public KrysalisTest() throws Exception {
> Driver driver =3D new Driver();
> FileOutputStream pdfStream =3D null;
>=20
> //Setup logger
> Logger logger =3D new ConsoleLogger(ConsoleLogger.LEVEL_INFO);
> driver.setLogger(logger);
> MessageHandler.setScreenLogger(logger);
>=20
> //Setup Renderer (output format)
> driver.setRenderer(Driver.RENDER_PDF);
> try {
> pdfStream =3D new FileOutputStream("./report/krysalisTest.=
pdf"
> );
Free bonus tip: Buffer the output stream. In production environments this
can really make a difference in performance.
> driver.setOutputStream(pdfStream);
>=20
> File xslFile =3D new File("./report/krysalisTest.xsl");
> if (!xslFile.isFile())
> throw new Exception("Cant' find xsl file:
> krysalisTest.xsl");
>=20
> //Setup XSLT
> TransformerFactory factory =3D TransformerFactory.newInsta=
nce
> ();
> Transformer transformer =3D
> factory.newTransformer(new StreamSource(xslFile));
>=20
> StringBuffer dummy =3D new StringBuffer();
> dummy.append("<bar1>\n");
> dummy.append("<msg>")
> .append(new String("10L1" + Code128LogicImpl.FNC_1 +
> "370010"))
> .append("</msg>\n");
> dummy.append("<code128>\n");
> dummy.append("<module-width>0.75mm</module-width>\n");
> dummy.append("<module-height>25.00mm</module-height>\n");
> dummy.append("</code128>\n");
> dummy.append("</bar1>\n");
>=20
> String xmlString =3D new StringBuffer("<?xml version=3D\"1.=
0\"
> encoding=3D\"US-ASCII\"?>\n")
If you really want to concat your XML file this way, I'd use the
following:
encoding=3D\"UTF-8\"?>\n)
> .append("<xmlDocument>\n")
> .append(dummy.toString())
> .append("\n</xmlDocument>\n")
> .toString();
>=20
> System.out.print(xmlString);
>=20
> //Setup input for XSLT transformation
> Source src =3D new StreamSource(
> new ByteArrayInputStream(xmlString.getBytes()));
and...
new ByteArrayInputStream(xmlString.getBytes("UTF-8"))=
);
=2E...then you're problem should be solved, because the FNC_1 character is
then properly encoded in the XML. With UTF-8 you won't have any problems
with missing characters. Remember that Java uses Unicode internally with
Strings.
Personally, for this approach I would have generated SAX events by hand
instead of concatenating the XML as a String. That allows me to remove
the encoding and subsequent decoding (by the XML parser). Less buffering,
no encoding problems but may be a bit more complicated but only until
you've done that one time. I wrote an example for this:
http://xml.apache.org/fop/embedding.html#ExampleObj2XML
Obviously, this is a bit of overkill for this here, but you wouldn't
have had these problems. :-)
>=20
> //Resulting SAX events (the generated FO) must be piped
> through to FOP
> Result res =3D new SAXResult(driver.getContentHandler());
>=20
> //Start XSLT transformation and FOP processing
> transformer.transform(src, res);
>=20
> } finally {
> if (pdfStream !=3D null)
> pdfStream.close();
> }
> }
>=20
> public static void main(String[] args) {
>=20
> try {
> new KrysalisTest();
>=20
> } catch (Exception excp) {
> excp.printStackTrace();
> }
>=20
> }
> }
>=20
> The result is the following:
> java.lang.IllegalArgumentException: Illegal character: ?
> at
> org.krysalis.barcode.impl.DefaultCode128Encoder.encodeAorB(DefaultCode128=
Encoder.java:175)
> at
> org.krysalis.barcode.impl.DefaultCode128Encoder.encode(DefaultCode128Enco=
der.java:289)
> at
> org.krysalis.barcode.impl.Code128LogicImpl.createEncodedMessage(Code128Lo=
gicImpl.java:315)
> at org.krysalis.barcode.impl.Code128.calcDimensions(Code128.java:105
> ...
>=20
> If I substitute the Code128LogicImpl.FNC_1 constant with the &#F1; string=
,
> with "UTF-8" encoding, I obtain the following result
My bad, I'm sorry! It should have been ñ
> ; Line#: 4; Column#: 12
> javax.xml.transform.TransformerException: The character reference must en=
d
> with the ';' delimiter.
> at
> org.apache.xalan.transformer.TransformerImpl.fatalError(TransformerImpl.j=
ava:744)
> at
> org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.ja=
va:720)
> at
> org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.ja=
va:1192)
> at
> org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.ja=
va:1170)
> at KrysalisTest.<init>(KrysalisTest.java:74)
> at KrysalisTest.main(KrysalisTest.java:85)
>=20
> Where am I going wrong?
I hope this helps.
Jeremias Maerki
|