org.ejen.ext
Class GlobalVariables

java.lang.Object
  |
  +--org.ejen.ext.GlobalVariables

public class GlobalVariables
extends Object

Gobal variables utility (instanciable).

Usage (XSL stylesheet)

  <?xml version="1.0" encoding="iso-8859-1"?>

  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                  ...
                  xmlns:gvs="org.ejen.ext.GlobalVariables"
                  extension-element-prefixes="gvs ..."
                  exclude-result-prefixes="gvs ..."
                  version="1.0">

    <xsl:output method="xml" encoding="iso-8859-1"/>

    <xsl:template match="ejen">

      <xls:variable name="vars1" select="gvs:new()"/>
      <xls:variable name="vars2" select="gvs:new($vars1)"/>
      <gvs:clear/>
      <vars1-count>
        <xsl:value-of select="gvs:size([$vars1])"/>
      </vars1-count>
      <gvs:remove name="myVar" [instance="$vars1"]/>
      <gvs:put name="primary-keys"
              [select="column[@primary-key='true']"]
              [instance="$gvs2"]/>
      or
      <gvs:put name="myVar"
              [attribute="Hello {people[$i]/@name} !"]
              [instance="$gvs2"]/>
      <xsl:value-of select="gvs:get([$vars2,] 'myVar')"/>
      <xsl:for-each select="gvs:elements([$vars2])">
        ...
      </xsl:for-each>
      <xsl:for-each select="gvs:keys([$vars2])">
        ...
      </xsl:for-each>

    </xsl:template>

  </xsl:stylesheet>
 

Version:
1.0
Author:
F. Wolff

Constructor Summary
GlobalVariables()
          Constructs a new GlobalVariables object.
GlobalVariables(GlobalVariables gvs)
          Constructs a new GlobalVariables object with the same mappings as the given GlobalVariables.
 
Method Summary
 void clear(XSLProcessorContext context, ElemExtensionCall elem)
          Throws away all variables.
 NodeSet elements(ExpressionContext context)
          Returns a NodeSet that contains all variable values in this GlobalVariables.
 XObject get(ExpressionContext context, String name)
          Gets the value of a variable.
 XObject get(XSLProcessorContext context, ElemExtensionCall elem)
          Gets the value of a variable.
 NodeSet keys(ExpressionContext context)
          Returns a NodeSet that contains all variable names in this GlobalVariables.
 void put(XSLProcessorContext context, ElemExtensionCall elem)
          Creates (or updates the value of) a variable.
 void remove(XSLProcessorContext context, ElemExtensionCall elem)
          Removes a variable.
 int size(ExpressionContext context)
          Returns variables count.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GlobalVariables

public GlobalVariables()
Constructs a new GlobalVariables object. If this constructor is not explicitly used, the Xalan extension mechanism constructs a default instance.


  <xsl:variable name="gvs1" select="gvs:new()"/>
 


GlobalVariables

public GlobalVariables(GlobalVariables gvs)
Constructs a new GlobalVariables object with the same mappings as the given GlobalVariables.


  <xsl:variable name="gvs2" select="gvs:new($gvs1)"/>
 

XSLT parameters:
[Mandatory] GlobalVariables instance.

Parameters:
gvs - a GlobalVariables object to be copied in this GlobalVariables.
Method Detail

clear

public void clear(XSLProcessorContext context,
                  ElemExtensionCall elem)
Throws away all variables.


  <gvs:clear [instance="$gvs2"]/>
 

XSLT Attributes:
instance [Optional] GlobalVariables instance (if not set, default instance is used).

Parameters:
context - automatically passed by the xalan extension mechanism.
elem - automatically passed by the xalan extension mechanism.
Throws:
WrappedRuntimeException - if the instance attribute is not an instance of GlobalVariables.

size

public int size(ExpressionContext context)
Returns variables count.


  <xsl:value-of select="gvs:size([$gvs2])"/>
 

XSLT parameters:
[Optional] GlobalVariables instance (if not set, default instance is used).

Parameters:
context - automatically passed by the xalan extension mechanism.
Returns:
variables count in this GlobalVariables.

remove

public void remove(XSLProcessorContext context,
                   ElemExtensionCall elem)
Removes a variable.


  <gvs:remove name="myVar"[ instance="$gvs2"]/>
 

XSLT Attributes:
name [Mandatory/AVT] - name of the variable to be removed.
instance [Optional] - GlobalVariables instance.

Parameters:
context - automatically passed by the xalan extension mechanism.
elem - automatically passed by the xalan extension mechanism.
Throws:
WrappedRuntimeException - if the 'name' attribute is missing or if the instance attribute is not an instance of GlobalVariables.

put

public void put(XSLProcessorContext context,
                ElemExtensionCall elem)
Creates (or updates the value of) a variable.


  <gvs:put name="myVar"
          [select="column[@primary-key='true']"]
          [instance="$gvs2"]/>
 or
  <gvs:put name="myVar"
          [attribute="Hello {people[$i]/@name} !"]
          [instance="$gvs2"]/>
 

'select' and 'attribute' cannot be used at the same time. If none of those attributes is used, the default value "(no value)" is bound to the variable name.

XSLT Attributes:
name [Mandatory/AVT] - name of the variable to be removed.
select [Optional/AVT] - XPath expression (nodes set, XSL variable...).
attribute [Optional/AVT] - a String.
instance [Optional] - GlobalVariables instance (if not set, default instance is used).

Parameters:
context - automatically passed by the xalan extension mechanism.
elem - automatically passed by the xalan extension mechanism.
Throws:
WrappedRuntimeException - with a XSL Exception if the 'name' attribute.

get

public XObject get(ExpressionContext context,
                   String name)
Gets the value of a variable.


  <xsl:value-of select="gvs:get([$gvs2, ]'myVar')"/>
 

XSLT parameters:
[Optional] GlobalVariables instance (if not set, default instance is used).
[Mandatory/AVT] name of the variable.

Parameters:
context - automatically passed by the xalan extension mechanism.
name - the name of the variable.
Returns:
the value of the variable or null if there was no such variable.

get

public XObject get(XSLProcessorContext context,
                   ElemExtensionCall elem)
Gets the value of a variable.


  <gvs:get name="myVar"[ instance="$gvs2"]/>
 

XSLT Attributes:
name [Mandatory/AVT] - name of the variable to be removed.
instance [Optional] - GlobalVariables instance (if not set, default instance is used).

Parameters:
context - automatically passed by the xalan extension mechanism.
elem - automatically passed by the xalan extension mechanism.
Returns:
the value of the variable or null if there was no such variable.
Throws:
WrappedRuntimeException - with a XSL Exception if the 'name' attribute is missing.

elements

public NodeSet elements(ExpressionContext context)
Returns a NodeSet that contains all variable values in this GlobalVariables.


  <xsl:for-each select="gvs:elements([$gvs2])">
    ...
  </xsl:for-each>
 

Returned NodeSet is rather an union of the values than a collection of the values: if the value of a variable is already a XNodeSet, returned NodeSet will contain all Nodes of this XNodeSet (and not a Node that contains the XNodeSet); same with XRTreeFrag values; if the value is a XString (or XBoolean, XNumber...), then returned NodeSet will contain a CDATA Node with a String representation of the value.

XSLT parameters:
[Optional] GlobalVariables instance (if not set, default instance is used).
[Mandatory/AVT] name of the variable.

Parameters:
context - automatically passed by the xalan extension mechanism.
Returns:
a NodeSet (may be empty) that contains all variable values in this GlobalVariables.
Throws:
WrappedRuntimeException - DOM Exception...

keys

public NodeSet keys(ExpressionContext context)
Returns a NodeSet that contains all variable names in this GlobalVariables.


  <xsl:for-each select="gvs:keys([$gvs2])">
    ...
  </xsl:for-each>
 

Returned NodeSet is a collection of CDATA Nodes, each of them containing a variable name.

XSLT parameters:
[Optional] GlobalVariables instance (if not set, default instance is used).
[Mandatory/AVT] name of the variable.

Parameters:
context - automatically passed by the xalan extension mechanism.
Returns:
a NodeSet (may be empty) that contains all variable names in this GlobalVariables.
Throws:
WrappedRuntimeException - DOM Exception...