alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Lucene example source code file (document-to-html.xsl)

This example Lucene source code file (document-to-html.xsl) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Lucene tags/keywords

apache, asf, fixme, fixme, html, html, license, license, see, this, this, toc, transformer, you

The Lucene document-to-html.xsl source code

<?xml version="1.0"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!--
This stylesheet contains the majority of templates for converting documentv11
to HTML.  It renders XML as HTML in this form:

  <div class="content">
   ...
  </div>

..which site-to-xhtml.xsl then combines with HTML from the index (book-to-menu.xsl)
and tabs (tab-to-menu.xsl) to generate the final HTML.

Section handling
  - <a name/> anchors are added if the id attribute is specified

-->
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:param name="dynamic-page" select="'false'"/>
  <xsl:param name="notoc"/>
  <xsl:param name="path"/>
<!-- 
  <xsl:include href="dotdots.xsl"/>
  <xsl:include href="pathutils.xsl"/>
<!-- Path to site root, eg '../../' -->
  <xsl:variable name="root">
    <xsl:call-template name="dotdots">
      <xsl:with-param name="path" select="$path"/>
    </xsl:call-template>
  </xsl:variable>
  <xsl:variable name="skin-img-dir" select="concat(string($root), 'skin/images')"/>
  <xsl:template match="/">
    <xsl:apply-templates mode="toc"/>
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="document">
    <div class="content">
      <table summary="" class="title">
        <tr>
          <td valign="middle">
            <xsl:if test="normalize-space(header/title)!=''">
              <h1>
                <xsl:value-of select="header/title"/>
              </h1>
            </xsl:if>
          </td>
          <div id="skinconf-printlink"/>
          <xsl:if test="$dynamic-page='false'">
            <div id="skinconf-pdflink"/>
            <div id="skinconf-xmllink"/>
          </xsl:if>
        </tr>
      </table>
      <xsl:if test="normalize-space(header/subtitle)!=''">
        <h3>
          <xsl:value-of select="header/subtitle"/>
        </h3>
      </xsl:if>
      <xsl:apply-templates select="header/type"/>
      <xsl:apply-templates select="header/notice"/>
      <xsl:apply-templates select="header/abstract"/>
      <xsl:apply-templates select="body"/>
      <div class="attribution">
        <xsl:apply-templates select="header/authors"/>
        <xsl:if test="header/authors and header/version">
<xsl:text>; 
        </xsl:if>
        <xsl:apply-templates select="header/version"/>
      </div>
    </div>
  </xsl:template>
  <xsl:template match="body">
    <div id="skinconf-toc-page"/>
    <xsl:apply-templates/>
  </xsl:template>
<!-- Generate a 
  <xsl:template match="@id">
    <xsl:if test="normalize-space(.)!=''">
    </xsl:if>
  </xsl:template>
  <xsl:template match="section">
<!-- count the number of section in the ancestor-or-self axis to compute
         the title element name later on -->
    <xsl:variable name="sectiondepth" select="count(ancestor-or-self::section)"/>
    <xsl:apply-templates select="@id"/>
<!-- generate a title element, level 1 -> h3, level 2 -> h4 and so on... -->
    <xsl:element name="{concat('h',$sectiondepth + 2)}">
      <xsl:value-of select="title"/>
      <xsl:if test="$notoc='true' and $sectiondepth = 3">
        <span style="float: right">^
        </span>
      </xsl:if>
    </xsl:element>
<!-- Indent FAQ entry text 15 pixels -->
    <xsl:variable name="indent">
      <xsl:choose>
        <xsl:when test="$notoc='true' and $sectiondepth = 3">
<xsl:text>15
        </xsl:when>
        <xsl:otherwise>
<xsl:text>0
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <div style="margin-left: {$indent} ; border: 2px">
      <xsl:apply-templates select="*[not(self::title)]"/>
    </div>
  </xsl:template>
  <xsl:template match="note | warning | fixme">
    <xsl:apply-templates select="@id"/>
    <div>
      <xsl:call-template name="add.class">
        <xsl:with-param name="class">
          <xsl:value-of select="local-name()"/>
        </xsl:with-param>
      </xsl:call-template>
      <div class="label">
        <xsl:choose>
<!-- FIXME: i18n Transformer here -->
          <xsl:when test="@label">
            <xsl:value-of select="@label"/>
          </xsl:when>
          <xsl:when test="local-name() = 'note'">Note
          <xsl:when test="local-name() = 'warning'">Warning
          <xsl:otherwise>Fixme ()
        </xsl:choose>
      </div>
      <div class="content">
        <xsl:apply-templates/>
      </div>
    </div>
  </xsl:template>
  <xsl:template match="notice">
    <div class="notice">
<!-- FIXME: i18n Transformer here -->
<xsl:text>Notice: 
      <xsl:apply-templates/>
    </div>
  </xsl:template>
  <xsl:template match="link">
    <xsl:apply-templates select="@id"/>
    <xsl:if test="@class='jump'">
      <xsl:attribute name="target">_top
    </xsl:if>
    <xsl:if test="@class='fork'">
      <xsl:attribute name="target">_blank
    </xsl:if>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="jump">
    <xsl:apply-templates select="@id"/>
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="fork">
    <xsl:apply-templates select="@id"/>
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="p[@xml:space='preserve']">
    <xsl:apply-templates select="@id"/>
    <div class="pre">
      <xsl:copy-of select="@id"/>
      <xsl:apply-templates/>
    </div>
  </xsl:template>
  <xsl:template match="source">
    <xsl:apply-templates select="@id"/>
    <pre class="code">
<!-- Temporarily removed long-line-splitter ... gives out-of-memory problems -->
      <xsl:copy-of select="@id"/>
      <xsl:apply-templates/>
<!--
    <xsl:call-template name="format">
    <xsl:with-param select="." name="txt" />
     <xsl:with-param name="width">80
     </xsl:call-template>
-->
    </pre>
  </xsl:template>
  <xsl:template match="anchor">
    <xsl:copy-of select="@id"/>
  </xsl:template>
  <xsl:template match="icon">
    <xsl:apply-templates select="@id"/>
    <img class="icon">
      <xsl:copy-of select="@height | @width | @src | @alt | @id"/>
    </img>
  </xsl:template>
  <xsl:template match="code">
    <xsl:apply-templates select="@id"/>
    <span>
      <xsl:call-template name="add.class">
        <xsl:with-param name="class">codefrag
      </xsl:call-template>
      <xsl:copy-of select="@id"/>
      <xsl:value-of select="."/>
    </span>
  </xsl:template>
  <xsl:template match="figure">
    <xsl:apply-templates select="@id"/>
    <div align="center">
      <xsl:copy-of select="@id"/>
      <img class="figure">
        <xsl:copy-of select="@height | @width | @src | @alt | @id"/>
      </img>
    </div>
  </xsl:template>
  <xsl:template match="table">
    <xsl:apply-templates select="@id"/>
    <xsl:choose>
<!-- Limit Forrest specific processing to tables without class -->
      <xsl:when test="not(@class) or @class=''">
        <table cellpadding="4" cellspacing="1" class="ForrestTable">
          <xsl:copy-of select="@cellspacing | @cellpadding | @border | @class | @bgcolor |@id"/>
          <xsl:apply-templates/>
        </table>
      </xsl:when>
      <xsl:otherwise>
<!-- Tables with class are passed without change -->
        <xsl:copy>
          <xsl:copy-of select="@*"/>
          <xsl:apply-templates/>
        </xsl:copy>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:if test="@class = ''">
  </xsl:template>
  <xsl:template match="acronym/@title">
    <xsl:attribute name="title">
      <xsl:value-of select="normalize-space(.)"/>
    </xsl:attribute>
  </xsl:template>
  <xsl:template match="header/authors">
    <xsl:for-each select="person">
      <xsl:choose>
        <xsl:when test="position()=1">by
        <xsl:otherwise>,
      </xsl:choose>
<xsl:text> 
      <xsl:value-of select="@name"/>
    </xsl:for-each>
  </xsl:template>
  <xsl:template match="version">
    <span class="version">
      <xsl:apply-templates select="@major"/>
      <xsl:apply-templates select="@minor"/>
      <xsl:apply-templates select="@fix"/>
      <xsl:apply-templates select="@tag"/>
      <xsl:choose>
        <xsl:when test="starts-with(., '$Revision: ')">
          version <xsl:value-of select="substring(., 12, string-length(.) -11-2)"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="."/>
        </xsl:otherwise>
      </xsl:choose>
    </span>
  </xsl:template>
  <xsl:template match="@major">
     v<xsl:value-of select="."/>
  </xsl:template>
  <xsl:template match="@minor | @fix">
    <xsl:value-of select="concat('.',.)"/>
  </xsl:template>
  <xsl:template match="@tag">
    <xsl:value-of select="concat('-',.)"/>
  </xsl:template>
  <xsl:template match="type">
    <p class="type">
<!-- FIXME: i18n Transformer here -->
<xsl:text>Type: 
      <xsl:value-of select="."/>
    </p>
  </xsl:template>
  <xsl:template match="abstract">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  <xsl:template name="email">
    <xsl:attribute name="href">
      <xsl:value-of select="concat('mailto:',@email)"/>
    </xsl:attribute>
    <xsl:value-of select="@name"/>
  </xsl:template>
  <xsl:template name="generate-id">
    <xsl:choose>
      <xsl:when test="@id">
        <xsl:value-of select="@id"/>
      </xsl:when>
      <xsl:when test="@title">
        <xsl:value-of select="@title"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="generate-id(.)"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
<!--  Templates for "toc" mode.  This will generate a complete
        Table of Contents for the document.  This will then be used
        by the site2xhtml to generate a Menu ToC and a Page ToC -->
  <xsl:template match="document" mode="toc">
    <xsl:apply-templates mode="toc"/>
  </xsl:template>
  <xsl:template match="body" mode="toc">
    <tocitems>
      <xsl:if test="../header/meta[@name='forrest.force-toc'] = 'true'">
        <xsl:attribute name="force">true
      </xsl:if>
      <xsl:apply-templates select="section" mode="toc">
        <xsl:with-param name="level" select="1"/>
      </xsl:apply-templates>
    </tocitems>
  </xsl:template>
  <xsl:template match="section" mode="toc">
    <xsl:param name="level"/>
    <tocitem level="{$level}">
      <xsl:attribute name="href">#
      </xsl:attribute>
      <xsl:attribute name="title">
        <xsl:value-of select="title"/>
      </xsl:attribute>
      <xsl:apply-templates mode="toc">
        <xsl:with-param name="level" select="$level+1"/>
      </xsl:apply-templates>
    </tocitem>
  </xsl:template>
  <xsl:template name="add.class">
<!-- use the parameter to set class attribute -->
<!-- if there are already classes set, adds to them -->
    <xsl:param name="class"/>
    <xsl:attribute name="class">
      <xsl:choose>
        <xsl:when test="@class">
          <xsl:value-of select="$class"/>
<xsl:text> 
          <xsl:value-of select="@class"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$class"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:attribute>
  </xsl:template>
  <xsl:template match="node()|@*" mode="toc"/>
<!-- End of "toc" mode templates -->
  <xsl:template match="node()|@*" priority="-1">
<!-- id processing will create its own a-element so processing has to 
         happen outside the copied element 
    -->
    <xsl:apply-templates select="@id"/>
    <xsl:copy>
      <xsl:apply-templates select="@*[name(.) != 'id']"/>
      <xsl:copy-of select="@id"/>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

Other Lucene examples (source code examples)

Here is a short list of links related to this Lucene document-to-html.xsl source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.