Loading ...
Sorry, an error occurred while loading the content.
 

a newbie question

Expand Messages
  • longzhao2001@yahoo.com
    I use FOP package from apache and try to conver a smaple XML with XSL to PDF file, but the output file collapse here is the jave code: import java.io.*; import
    Message 1 of 1 , Sep 24, 2001
      I use FOP package from apache
      and try to conver a smaple XML with XSL to PDF file, but the output
      file collapse

      here is the jave code:

      import java.io.*;

      import org.xml.sax.InputSource;
      import org.xml.sax.XMLReader;

      import org.apache.fop.apps.Driver;
      import org.apache.fop.apps.Version;
      import org.apache.fop.apps.FOPException;
      import org.apache.fop.apps.TraxInputHandler;

      public class PrintCRF {
      FileInputStream fis= null;
      FileOutputStream fos= null;

      public static void main(String[] args) throws IOException {

      PrintCRF pcrf = new PrintCRF();
      String xmlPath = "d:\\Development\\source\\xml\\sonnet.xml";
      String xslPath = "d:\\Development\\source\\xml\\sonnet.xsl";
      try {
      File xmlFile = new File(xmlPath);
      File xslFile = new File(xslPath);
      TraxInputHandler xih = new TraxInputHandler(xmlFile, xslFile);
      pcrf.fis = new FileInputStream(tempFile);
      pcrf.fos = new FileOutputStream("output.pdf");
      pcrf.foRenderer(xih.getInputSource(), outPath);
      pcrf.fos.close();
      }
      catch (FileNotFoundException fof) {
      throw fof;
      }
      }
      public void foRenderer(InputSource isrc, String outPath){
      try {
      System.out.println("do it");
      Driver driver = new Driver(isrc, fos);
      driver.setRenderer(driver.RENDER_PDF);
      driver.run();
      }
      catch (FOPException fopex) {}
      catch (IOException ioex){}

      }
      }

      here is the sonnet.xml:

      <?xml version="1.0"?>

      <!DOCTYPE sonnet SYSTEM "sonnet.dtd">
      <sonnet type="Shakespearean">
      <author>
      <last-name>Shakespeare</last-name>
      <first-name>William</first-name>
      <nationality>British</nationality>
      <year-of-birth>1564</year-of-birth>
      <year-of-death>1616</year-of-death>
      </author>
      <title>Sonnet 130</title>
      <lines>
      <line>My mistress' eyes are nothing like the sun,</line>
      <line>Coral is far more red than her lips red.</line>
      <line>If snow be white, why then her breasts are dun,</line>
      <line>If hairs be wires, black wires grow on her head.</line>
      <line>I have seen roses damasked, red and white,</line>
      <line>But no such roses see I in her cheeks.</line>
      <line>And in some perfumes is there more delight</line>
      <line>Than in the breath that from my mistress reeks.</line>
      <line>I love to hear her speak, yet well I know</line>
      <line>That music hath a far more pleasing sound.</line>
      <line>I grant I never saw a goddess go,</line>
      <line>My mistress when she walks, treads on the ground.</line>
      <line>And yet, by Heaven, I think my love as rare</line>
      <line>As any she belied with false compare.</line>
      </lines>
      </sonnet>

      here is the sonnet.xsl :

      <?xml version="1.0"?>
      <xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:fo="http://www.w3.org/1999/XSL/Format">

      <!--
      ================================================================-->
      <!-- Root Element
      Template -->
      <!-- This template specifies what creates the root element of
      the -->
      <!-- result tree. In this case, it tells the XSL processor
      to -->
      <!-- start with the sonnet
      element. -->
      <!--
      ================================================================-->

      <xsl:template match="/">
      <xsl:apply-templates select="sonnet"/>
      </xsl:template>

      <!--
      ================================================================-->
      <!-- sonnet
      template -->
      <!-- This template takes the sonnet element and processes
      its -->
      <!--
      contents. -->
      <!--
      ================================================================-->

      <xsl:template match="sonnet">

      <!--
      ================================================================-->
      <!-- Cocoon processing
      instruction -->
      <!-- This processing instruction lets us serve an XML sonnet as
      a -->
      <!-- PDF. The Cocoon servlet uses this instruction to invoke
      FOP -->
      <!--
      automatically. -->
      <!--
      ================================================================-->

      <xsl:processing-instruction
      name="cocoon-format">type="text/xslfo"</xsl:processing-
      instruction>

      <!--
      ================================================================-->
      <!-- The fo:root element contains the entire
      document. -->
      <!--
      ================================================================-->

      <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">

      <!--
      ================================================================-->
      <!-- The layout-master-set defines a set of page layouts. For
      our -->
      <!-- purposes here, we only need
      one. -->
      <!--
      ================================================================-->

      <fo:layout-master-set>
      <fo:simple-page-master
      page-master-name="main"
      margin-top="75pt"
      margin-bottom="75pt"
      margin-left="75pt"
      margin-right="75pt">

      <!--
      ================================================================-->
      <!-- The region-body element is where all the action is. We'll
      put -->
      <!-- all of our content into this
      space. -->
      <!--
      ================================================================-->

      <fo:region-body margin-bottom="75pt"/>
      </fo:simple-page-master>
      </fo:layout-master-set>

      <!--
      ================================================================-->
      <!-- Our next step is to define the page styles used. We
      can -->
      <!-- different page styles for the first page, odd pages, and
      even -->
      <!-- pages. They're all the same for this simple
      document. -->
      <!--
      ================================================================-->

      <fo:page-sequence>
      <fo:sequence-specification>
      <fo:sequence-specifier-alternating
      page-master-first="main"
      page-master-odd="main"
      page-master-even="main"/>
      </fo:sequence-specification>

      <!--
      ================================================================-->
      <!-- Finally, we'll put everything into a flow
      element. -->
      <!--
      ================================================================-->

      <fo:flow>
      <xsl:apply-templates select="title"/>
      <xsl:apply-templates select="author"/>

      <!--
      ================================================================-->
      <!-- To get the formatting we want, we'll put the lines of
      the -->
      <!-- sonnet into a table. We used this same approach when
      we -->
      <!-- transformed a sonnet into HTML. The rhyme scheme
      indicator -->
      <!-- goes into the first column, and the line of the sonnet goes
      in -->
      <!-- the
      second. -->
      <!--
      ================================================================-->

      <fo:table space-before.optimum="6pt">
      <fo:table-column column-width="30pt"/>
      <fo:table-column column-width="450pt"/>
      <fo:table-body>
      <xsl:apply-templates select="lines/line"/>
      </fo:table-body>
      </fo:table>
      </fo:flow>
      </fo:page-sequence>

      </fo:root>
      </xsl:template>

      <!--
      ================================================================-->
      <!-- Template for the title element. The title of the sonnet
      will -->
      <!-- appear in 24-point type, left aligned (that's what
      the -->
      <!-- text-align-last attribute does). Notice that we've made
      the -->
      <!-- line-height slightly larger than the font-size; this makes
      the -->
      <!-- document much more readable. Without this, the lines of
      text -->
      <!-- in our PDF file are all run
      together. -->
      <!--
      ================================================================-->

      <xsl:template match="title">
      <fo:block
      font-size="24pt" line-height="27pt"
      text-align-last="start"
      space-before.optimum="24pt">
      <xsl:apply-templates/>
      </fo:block>
      </xsl:template>

      <!--
      ================================================================-->
      <!-- Template for the author element. This is more left-
      aligned -->
      <!-- text. We simply pull together the different parts of
      the -->
      <!-- original author element to create this
      subheading. -->
      <!--
      ================================================================-->

      <xsl:template match="author">
      <fo:block font-size="12pt" line-height="15pt"
      text-align-last="start"
      space-before.optimum="12pt">
      <xsl:text>Author: </xsl:text>
      <xsl:apply-templates select="first-name"/>
      <xsl:text> </xsl:text>
      <xsl:apply-templates select="last-name"/>
      <xsl:text> (</xsl:text>

      <!--
      ================================================================-->
      <!-- We want the author's nationality, year of birth, and year
      of -->
      <!-- death to appear in italics. We use the fo:wrapper element
      to -->
      <!-- do
      this. -->
      <!--
      ================================================================-->

      <fo:wrapper font-style="italic">
      <xsl:apply-templates select="nationality"/>
      <xsl:text>, </xsl:text>
      <xsl:apply-templates select="year-of-birth"/>
      <xsl:text>-</xsl:text>
      <xsl:apply-templates select="year-of-death"/>
      </fo:wrapper>
      <xsl:text>)</xsl:text>
      </fo:block>
      </xsl:template>

      <!--
      ================================================================-->
      <!-- Template for the first and third lines of the sonnet. For
      all -->
      <!-- of the lines of the sonnet, we'll create a table-row.
      The -->
      <!-- first cell contains a letter that indicates the rhyme
      scheme, -->
      <!-- while the second cell contains the line of the
      poem. -->
      <!--
      ================================================================-->

      <xsl:template match="line[1]|line[3]">
      <fo:table-row space-before.optimum="3pt">
      <fo:table-cell>
      <fo:block font-size="14pt" color="gray">A</fo:block>
      </fo:table-cell>
      <fo:table-cell>
      <fo:block color="green"><xsl:value-of select="."/></fo:block>
      </fo:table-cell>
      </fo:table-row>
      </xsl:template>

      <!--
      ================================================================-->
      <!-- Template for the second and fourth lines of the
      sonnet. -->
      <!--
      ================================================================-->

      <xsl:template match="line[2]|line[4]">
      <fo:table-row space-before.optimum="3pt">
      <fo:table-cell>
      <fo:block font-size="14pt" color="gray">B</fo:block>
      </fo:table-cell>
      <fo:table-cell>
      <fo:block color="purple"><xsl:value-of select="."/></fo:block>
      </fo:table-cell>
      </fo:table-row>
      </xsl:template>

      <!--
      ================================================================-->
      <!-- Template for the fifth and seventh lines of the
      sonnet. -->
      <!--
      ================================================================-->

      <xsl:template match="line[5]|line[7]">
      <fo:table-row space-before.optimum="3pt">
      <fo:table-cell>
      <fo:block font-size="14pt" color="gray">C</fo:block>
      </fo:table-cell>
      <fo:table-cell>
      <fo:block color="green"><xsl:value-of select="."/></fo:block>
      </fo:table-cell>
      </fo:table-row>
      </xsl:template>

      <!--
      ================================================================-->
      <!-- Template for the sixth and eighth lines of the
      sonnet. -->
      <!--
      ================================================================-->

      <xsl:template match="line[6]|line[8]">
      <fo:table-row space-before.optimum="3pt">
      <fo:table-cell>
      <fo:block font-size="14pt" color="gray">D</fo:block>
      </fo:table-cell>
      <fo:table-cell>
      <fo:block color="purple"><xsl:value-of select="."/></fo:block>
      </fo:table-cell>
      </fo:table-row>
      </xsl:template>

      <!--
      ================================================================-->
      <!-- Template for the ninth and eleventh lines of the
      sonnet. -->
      <!--
      ================================================================-->

      <xsl:template match="line[9]|line[11]">
      <fo:table-row space-before.optimum="3pt">
      <fo:table-cell>
      <fo:block font-size="14pt" color="gray">E</fo:block>
      </fo:table-cell>
      <fo:table-cell>
      <fo:block color="green"><xsl:value-of select="."/></fo:block>
      </fo:table-cell>
      </fo:table-row>
      </xsl:template>

      <!--
      ================================================================-->
      <!-- Template for the tenth and twelfth lines of the
      sonnet. -->
      <!--
      ================================================================-->

      <xsl:template match="line[10]|line[12]">
      <fo:table-row space-before.optimum="3pt">
      <fo:table-cell>
      <fo:block font-size="14pt" color="gray">F</fo:block>
      </fo:table-cell>
      <fo:table-cell>
      <fo:block color="purple"><xsl:value-of select="."/></fo:block>
      </fo:table-cell>
      </fo:table-row>
      </xsl:template>

      <!--
      ================================================================-->
      <!-- Template for the last two lines of the
      sonnet. -->
      <!--
      ================================================================-->

      <xsl:template match="line[13]|line[14]">
      <fo:table-row space-before.optimum="3pt">
      <fo:table-cell>
      <fo:block font-size="14pt" color="gray">G</fo:block>
      </fo:table-cell>
      <fo:table-cell>
      <fo:block color="green"><xsl:value-of select="."/></fo:block>
      </fo:table-cell>
      </fo:table-row>
      </xsl:template>

      </xsl:stylesheet>


      sorry for such a long posting
      thanks

      Long
    Your message has been successfully submitted and would be delivered to recipients shortly.