W3C home > Mailing lists > Public > www-xsl-fo@w3.org > November 2002

HOWTO indent java code in PDF (with example)

From: Manrico_antonio Sessa <sessam@tin.it>
Date: Mon, 18 Nov 2002 12:02:54 +0100
Message-ID: <3DD8C8DE.1583187A@tin.it>
To: www-xsl-fo@w3.org
CC: sessa@cli.di.unipi.it

Hi, I'm new in XSL:FO.

I'm programming a tool that tackes a java code file and translates it in XML.

This is my input java code, CiaoMondo.java:

class CiaoMondo extends Object {
    public static void main (String[] args) {
        System.out.println("Hello, World!!");
    }
}

and this is my result in XML, CiaoMondo.xml:

<?xml version="1.0" ?>
<source color="#FFFFFF" size="2" family="Courier New, Courier">
<br/><word color='#00FF00'>class</word> CiaoMondo <word color='#00FF00'>extends</word> Object {
<br/><ht num="1"/><word color='#00FF00'>public</word> <word color='#00FF00'>static</word> <base color='#0000FF'>void</base> main (String[] args) {
<br/><ht num="2"/>System.out.println(&quot;Hello, World!!&quot;);
<br/><ht num="1"/>}
<br/>}
</source>

where

the tag <ht num=.../> counts tabs to indent the code

and the other tags for sysntax colouring.

The templates that translate XML in HTML, are this:

<xsl:template match="source" mode="build-source">
  <table border="1">
    <tr><td>
      <font>
        <xsl:attribute name="color"><xsl:value-of select="@color" /></xsl:attribute>
        <xsl:attribute name="size"><xsl:value-of select="@size" /></xsl:attribute>
        <xsl:attribute name="face"><xsl:value-of select="@family" /></xsl:attribute>
        <xsl:apply-templates select="*|text()" mode="build-source" />
      </font>
    </td></tr>
  </table>
</xsl:template>

<xsl:template match="//word" mode="build-source">
  <font>
    <xsl:attribute name="color">
          <xsl:value-of select="@color" />
    </xsl:attribute>
    <xsl:apply-templates select="*|@*|comment()|processing-instruction()|text()" mode="build-source" />
  </font>
</xsl:template>

<xsl:template match="//base" mode="build-source">
  <font>
    <xsl:attribute name="color">
          <xsl:value-of select="@color" />
    </xsl:attribute>
    <xsl:apply-templates select="*|@*|comment()|processing-instruction()|text()" mode="build-source" />
  </font>
</xsl:template>

<xsl:template match="//specialw" mode="build-source">
  <font>
    <xsl:attribute name="color">
          <xsl:value-of select="@color" />
    </xsl:attribute>
    <xsl:apply-templates select="*|@*|comment()|processing-instruction()|text()" mode="build-source" />
  </font>
</xsl:template>

<xsl:template match="//ht" mode="build-source"><xsl:call-template name="htloop" /></xsl:template>

<xsl:template name="htloop" mode="build-source">
<xsl:param name="count" select="1" />
  <xsl:param name="numTabs" select="@num" />
  <xsl:if test="$count &lt;= $numTabs">
    <xsl:text disable-output-escaping="yes">&amp;#xa0;&amp;#xa0;&amp;#xa0;&amp;#xa0;</xsl:text><xsl:call-template name="htloop">
      <xsl:with-param name="count" select="$count+1" />
    </xsl:call-template>
  </xsl:if>
</xsl:template>

This work fine for me!

But, the problem is now to have the same page in PDF.

Any help would be extremly appriciated!!

Thanks,

            MANRICO SESSA
Received on Monday, 18 November 2002 06:03:52 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Wednesday, 3 October 2007 16:06:10 GMT