You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xalan.apache.org by dl...@locus.apache.org on 2000/08/03 21:07:47 UTC
cvs commit: xml-xalan/java/samples/extensions 1-redir.xml 1-redir.xsl 2-basicJscript.xml 2-basicJscript.xsl 3-java-namespace.xml 3-java-namespace.xsl 4-numlistJava.xml 4-numlistJava.xsl 5-numlistJscript.xml 5-numlistJscript.xsl IntDate.java MyCounter.java readme.html SimpleRedirect.java
dleslie 00/08/03 12:07:46
Added: java/samples/extensions 1-redir.xml 1-redir.xsl
2-basicJscript.xml 2-basicJscript.xsl
3-java-namespace.xml 3-java-namespace.xsl
4-numlistJava.xml 4-numlistJava.xsl
5-numlistJscript.xml 5-numlistJscript.xsl
IntDate.java MyCounter.java readme.html
SimpleRedirect.java
Log:
Xalan-j 2.0 sample extensions. not all working yet.
Revision Changes Path
1.1 xml-xalan/java/samples/extensions/1-redir.xml
Index: 1-redir.xml
===================================================================
<?xml version="1.0"?>
<doc>
<foo file="1-redir.out">
Testing Redirect extension:
<bar>A foo subelement text node</bar>
</foo>
<main>
Everything else
</main>
</doc>
1.1 xml-xalan/java/samples/extensions/1-redir.xsl
Index: 1-redir.xsl
===================================================================
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:lxslt="http://xml.apache.org/xslt"
xmlns:redirect="org.apache.xalan.lib.Redirect"
extension-element-prefixes="redirect">
<xsl:template match="/">
<standard-out>
Standard output:
<xsl:apply-templates/>
</standard-out>
</xsl:template>
<xsl:template match="main">
<main>
<xsl:apply-templates/>
</main>
</xsl:template>
<xsl:template match="/doc/foo">
<redirect:write select="@file">
<foo-out>
<xsl:apply-templates/>
</foo-out>
</redirect:write>
</xsl:template>
<xsl:template match="bar">
<foobar-out>
<xsl:apply-templates/>
</foobar-out>
</xsl:template>
</xsl:stylesheet>
1.1 xml-xalan/java/samples/extensions/2-basicJscript.xml
Index: 2-basicJscript.xml
===================================================================
<?xml version="1.0"?>
<doc>
<deadline numdays="2"/>
</doc>
1.1 xml-xalan/java/samples/extensions/2-basicJscript.xsl
Index: 2-basicJscript.xsl
===================================================================
<?xml version="1.0"?>
<!--Namespaces are global if you set them in the stylesheet element-->
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:lxslt="http://xml.apache.org/xslt"
xmlns:my-ext="ext1"
extension-element-prefixes="my-ext">
<!--The component and its script are in the lxslt namespace and define the implementation-->
<lxslt:component prefix="my-ext" elements="timelapse" functions="getdate">
<lxslt:script lang="javascript">
var multiplier=1;
// Extension element implementations always take two arguments. The first
// argument is the XSL Processor context; the second argument is the element.
function timelapse(xslProcessorContext, elem)
{
multiplier=parseInt(elem.getAttribute("multiplier"));
// The element return value is placed in the result tree.
// If you do not want a return value, return null.
return null;
}
function getdate(numdays)
{
var d = new Date();
d.setDate(d.getDate() + parseInt(numdays*multiplier));
return d.toLocaleString();
}
</lxslt:script>
</lxslt:component>
<xsl:template match="deadline">
<p><my-ext:timelapse multiplier="2"/>We have received your enquiry and will
respond by <xsl:value-of select="my-ext:getdate(string(@numdays))"/></p>
</xsl:template>
</xsl:stylesheet>
1.1 xml-xalan/java/samples/extensions/3-java-namespace.xml
Index: 3-java-namespace.xml
===================================================================
<?xml version="1.0"?>
<doc>
<date
year="2000" month="4" day="27"
format="EEEE, MMM dd, yyyy"/>
</doc>
1.1 xml-xalan/java/samples/extensions/3-java-namespace.xsl
Index: 3-java-namespace.xsl
===================================================================
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:java="http://xml.apache.org/xslt/java"
version="1.0">
<xsl:template match="date">
<xsl:variable name="year" select="string(./@year)"/>
<xsl:variable name="month" select="string(./@month)"/>
<xsl:variable name="day" select="string(./@day)"/>
<xsl:variable name="format" select="string(./@format)"/>
<xsl:variable name="formatter"
select="java:java.text.SimpleDateFormat.new($format)"/>
<xsl:variable name="date"
select="java:IntDate.getDate($year,$month,$day)"/>
<p>Date: <xsl:value-of select="java:format($formatter, $date)"/></p>
</xsl:template>
</xsl:stylesheet>
1.1 xml-xalan/java/samples/extensions/4-numlistJava.xml
Index: 4-numlistJava.xml
===================================================================
<?xml version="1.0"?>
<doc>
<name first="Sanjiva" last="Weerawarana"/>
<name first="Joseph" last="Kesselman"/>
<name first="Stephen" last="Auriemma"/>
<name first="Igor" last="Belakovskiy"/>
<name first="David" last="Marston"/>
<name first="David" last="Bertoni"/>
<name first="Donald" last="Leslie"/>
<name first="Emily" last="Farmer"/>
<name first="Myriam" last="Midy"/>
<name first="Paul" last="Dick"/>
<name first="Scott" last="Boag"/>
<name first="Shane" last="Curcuru"/>
</doc>
1.1 xml-xalan/java/samples/extensions/4-numlistJava.xsl
Index: 4-numlistJava.xsl
===================================================================
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:lxslt="http://xml.apache.org/xslt"
xmlns:counter="MyCounter"
extension-element-prefixes="counter"
version="1.0">
<lxslt:component prefix="counter"
elements="init incr" functions="read">
<lxslt:script lang="javaclass" src="MyCounter"/>
</lxslt:component>
<xsl:template match="/">
<HTML>
<H1>Java Example</H1>
<counter:init name="index" value="1"/>
<p>Here are the names in alphabetical order by last name:</p>
<xsl:for-each select="doc/name">
<xsl:sort select="@last"/>
<xsl:sort select="@first"/>
<p>
<xsl:text>[</xsl:text>
<xsl:value-of select="counter:read('index')"/>
<xsl:text>]. </xsl:text>
<xsl:value-of select="@last"/>
<xsl:text>, </xsl:text>
<xsl:value-of select="@first"/>
</p>
<counter:incr name="index"/>
</xsl:for-each>
</HTML>
</xsl:template>
</xsl:stylesheet>
1.1 xml-xalan/java/samples/extensions/5-numlistJscript.xml
Index: 5-numlistJscript.xml
===================================================================
<?xml version="1.0"?>
<doc>
<name first="David" last="Marston"/>
<name first="David" last="Bertoni"/>
<name first="Donald" last="Leslie"/>
<name first="Emily" last="Farmer"/>
<name first="Jack" last="Donohue"/>
<name first="Myriam" last="Midy"/>
<name first="Paul" last="Dick"/>
<name first="Robert" last="Weir"/>
<name first="Scott" last="Boag"/>
<name first="Shane" last="Curcuru"/>
</doc>
1.1 xml-xalan/java/samples/extensions/5-numlistJscript.xsl
Index: 5-numlistJscript.xsl
===================================================================
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:lxslt="http://xml.apache.org/xslt"
xmlns:counter="MyCounter"
extension-element-prefixes="counter"
version="1.0">
<lxslt:component prefix="counter"
elements="init incr" functions="read">
<lxslt:script lang="javascript">
var counters = new Array();
function init (xslproc, elem) {
name = elem.getAttribute ("name");
value = parseInt(elem.getAttribute ("value"));
counters[name] = value;
return null;
}
function read (name) {
return "" + (counters[name]);
}
function incr (xslproc, elem)
{
name = elem.getAttribute ("name");
counters[name]++;
return null;
}
</lxslt:script>
</lxslt:component>
<xsl:template match="/">
<HTML>
<H1>JavaScript Example.</H1>
<counter:init name="index" value="1"/>
<p>Here are the names in alphabetical order by last name:</p>
<xsl:for-each select="doc/name">
<xsl:sort select="@last"/>
<xsl:sort select="@first"/>
<p>
<xsl:text>[</xsl:text>
<xsl:value-of select="counter:read('index')"/>
<xsl:text>]. </xsl:text>
<xsl:value-of select="@last"/>
<xsl:text>, </xsl:text>
<xsl:value-of select="@first"/>
</p>
<counter:incr name="index"/>
</xsl:for-each>
</HTML>
</xsl:template>
</xsl:stylesheet>
1.1 xml-xalan/java/samples/extensions/IntDate.java
Index: IntDate.java
===================================================================
import java.util.Date;
import java.util.Calendar;
public class IntDate
{
public static Date getDate(String year, String month, String day)
{
// Date(int, int, int) has been deprecated, so use Calendar to
// set the year, month, and day.
Calendar c = Calendar.getInstance();
// Convert each argument to int.
c.set(Integer.parseInt(year),Integer.parseInt(month),Integer.parseInt(day));
return c.getTime();
}
}
1.1 xml-xalan/java/samples/extensions/MyCounter.java
Index: MyCounter.java
===================================================================
import java.util.*;
public class MyCounter {
Hashtable counters = new Hashtable ();
public MyCounter ()
{}
public void init(org.apache.xalan.extensions.XSLProcessorContext context,
org.apache.xalan.templates.ElemExtensionCall extElem)
{
String name = extElem.getAttribute("name");
String value = extElem.getAttribute("value");
int val;
try
{
val = Integer.parseInt (value);
}
catch (NumberFormatException e)
{
e.printStackTrace ();
val = 0;
}
counters.put (name, new Integer (val));
}
public int read(String name)
{
Integer cval = (Integer) counters.get (name);
return (cval == null) ? 0 : cval.intValue ();
}
public void incr(org.apache.xalan.extensions.XSLProcessorContext context,
org.apache.xalan.templates.ElemExtensionCall extElem) {
String name = extElem.getAttribute("name");
Integer cval = (Integer) counters.get(name);
int nval = (cval == null) ? 0 : (cval.intValue () + 1);
counters.put (name, new Integer (nval));
}
}
1.1 xml-xalan/java/samples/extensions/readme.html
Index: readme.html
===================================================================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Xalan Samples</title>
</head>
<body>
<h2>Xalan Samples</h2>
<p>For information about the samples (what they illustrate and how to run them), see <a href="../../docs/samples.html">Samples</a>.</p>
</body>
</html>
1.1 xml-xalan/java/samples/extensions/SimpleRedirect.java
Index: SimpleRedirect.java
===================================================================
/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Xalan" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation and was
* originally based on software copyright (c) 1999, Lotus
* Development Corporation., http://www.lotus.com. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
// Imported TraX classes
import trax.Processor;
import trax.Templates;
import trax.Transformer;
import trax.Result;
import trax.ProcessorException;
import trax.ProcessorFactoryException;
import trax.TransformException;
// Imported SAX classes
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
// Imported java.io classes
import java.io.FileWriter;
import java.io.IOException;
/**
* Use the TraX interface to perform a transformation in the simplest manner possible
* (4 statements).
*/
public class SimpleRedirect
{
public static void main(String[] args)
throws ProcessorException, ProcessorFactoryException,
TransformException, SAXException, IOException
{
// Instantiate a stylesheet processor.
Processor processor = Processor.newInstance("xslt");
// Use the stylesheet processor to process the stylesheet (foo.xsl) and
// return a Templates object.
Templates templates = processor.process(new InputSource("1-redir.xsl"));
// Use the Templates object to generate a Transformer object.
Transformer transformer = templates.newTransformer();
// Use the transformer to apply the Templates object to an XML document
// (foo.xml). The output that is not redirected by the stylesheet is
// written to foo.out. The redirected output is written to the file
// designated in the stylesheet.
transformer.transform(new InputSource("1-redir.xml"), new Result(new FileWriter("1-nonredir.out")));
System.out.println("");
System.out.println("***** The results are in 1-nonredir.out and the ****");
System.out.println("**** 'redirect' file designated in 1-redir.xsl. ****");
}
}