You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by Morten Grue Nielsen <mg...@bording.dk> on 2001/12/18 13:42:57 UTC

XALAN 2 - Using Java Extension Functions

Hi

I have made a Translate class, in which a replace method is found. This
method replace words, with other in a string. I have a problem trying to get
to this method by using xslt extension functions with xalan j_2_2_D14.
Instead of using the replace method in my Translate class, it uses the
replace method in the java.lang.String class. Can you help me ??

---------- The xslt script : -----------

xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:lxslt="http://xml.apache.org/xslt"
 xmlns:bdf="dk.bording.axt.tc.xsl.Translate"
 extension-element-prefixes="bdf"
 version="1.0">

 <lxslt:component prefix="bdf" functions="replace">
  <lxslt:script lang="javaclass"
src="xalan://dk.bording.axt.tc.xsl.Translate"/>
 </lxslt:component>

 <xsl:output indent="yes" method="xml" encoding="iso-8859-1"
cdata-section-elements="VALUE NAME KEY SOURCE pling"/>

 <xsl:template match="/">
  <xsl:apply-templates select="text()"/>
  <document>
   <pling>
    <xsl:for-each select="function/test">
     <xsl:value-of select="bdf:replace(string(text()),'asd','abc')"/>
    </xsl:for-each>
   </pling>
  </document>
 </xsl:template>
</xsl:stylesheet>


------- Java file --------

package dk.bording.axt.tc.xsl;

import org.w3c.dom.*;
import org.apache.xalan.extensions.*;
import org.w3c.dom.traversal.*;
import org.apache.xalan.templates.*;

public class Translate
{
	static StringBuffer sb;
	static int i;
	static String s1;

	public static String replace(XSLProcessorContext processorContext,
ElemExtensionCall extElem) {
	return "hest";
	}

	public static String replace(NodeList nl, String s2, String s3) {
		for (int i = nl.getLength() - 1; i >= 0; i--) {
			s1 = (String)nl.item(i).getNodeValue();
			s1 = (String)rep(s1, "%pl%", "'");
			s2 = (String)rep(s2, "%pl%", "'");
			s3 = (String)rep(s3, "%pl%", "'");
		}
		return rep(s1,s2,s3);
	}

	public static String replace(ExpressionContext ec, String s2, String
s3) {
		NodeIterator ni = ec.getContextNodes();
		boolean more = true;
		while (more) {
			Node n = ni.nextNode();
			if (n != null) {
				more = true;
			} else {
				more = false;
			}
			s1 = (String)n.getNodeValue();
			s1 = (String)rep(s1, "%pl%", "'");
			s2 = (String)rep(s2, "%pl%", "'");
			s3 = (String)rep(s3, "%pl%", "'");
		}
		return rep(s1,s2,s3);
	}

	public static String replace2(String s1, String s2, String s3) {
		s1 = (String)rep(s1, "%pl%", "'");
		s2 = (String)rep(s2, "%pl%", "'");
		s3 = (String)rep(s3, "%pl%", "'");
		return rep(s1,s2,s3);
	}

	private static String rep(String s1, String s2, String s3) {
		int l = s1.length();
		for (int i2=0;i2<l;i2++) {
			char rp = s2.charAt(0);
			char fc = s1.charAt(i2);
			if (rp == fc) {
				String s = s1.substring(i2, i2 +
s2.length());
				if (s.equals(s2)) {
					StringBuffer sb = new
StringBuffer(s1);
					sb.delete(i2, i2 + s2.length());
					l = l - s2.length();
					sb.insert(i2, s3);
					l = l + s3.length();
					s1 = sb.toString();
					for (int i3=0;i3<s3.length();i3++) {
						i2++;
					}
				}
			}
		}
		return s1;
	}
}


Re: XALAN 2 - Using Java Extension Functions

Posted by "Kevin A. Burton" <bu...@openprivacy.org>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Morten Grue Nielsen <mg...@bording.dk> writes:

> Hi
> 
> I have made a Translate class, in which a replace method is found. This
> method replace words, with other in a string. I have a problem trying to get
> to this method by using xslt extension functions with xalan j_2_2_D14.
> Instead of using the replace method in my Translate class, it uses the
> replace method in the java.lang.String class. Can you help me ??

Hm... it kind of looks like your lxslt component is screwed up.

Either way you should be using the abbreviated syntax.  It is much cooler.
Just do:

xmlns:bdf="xalan://dk.bording.axt.tc.xsl.Translate"
extension-element-prefixes="bdf"

You don't event need to declar the lxslt namespace.  Just snip it.

<snip/>

I think it should work now.

- -- 
Kevin A. Burton ( burton@apache.org, burton@openprivacy.org, burtonator@acm.org )
             Location - San Francisco, CA, Cell - 415.595.9965
        Jabber - burtonator@jabber.org,  Web - http://relativity.yi.org/

To fight and conquer in all your battles is not supreme excellence; supreme
excellence consists in breaking the enemy's resistance without fighting.
    - Sun Tzu, 300 B.C.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Get my public key at: http://relativity.yi.org/pgpkey.txt

iD8DBQE8IS8aAwM6xb2dfE0RAq06AJwMXPqkNCWmEVEcrooCqz0u8zGdXgCgiuCE
3iGjZxY/LBzPZovnrhZiCo8=
=R9g8
-----END PGP SIGNATURE-----

RE: XALAN 2 - Using Java Extension Functions

Posted by Gary L Peskin <ga...@firstech.com>.
Hi, Morten --

Sorry it's taken me so long to respond.  The short answer is that it is
most likely that your Translate class is not on your classpath.  I tried
your example and, after changing the "replace" call in the stylesheet to
"replace2", it worked just fine.  Be sure that you know what your
classpath is.  Are you executing this from the command line or from a
servlet or EJB or something like that?

If you have any questions regarding this, look at Shane's
org.apache.xalan.xslt.EnvironmentCheck class and follow the directions
in the javadoc there.

You didn't include a stack trace so I can't be sure but if you see the
class org.apache.xalan.extensions.ExtensionHandlerJavaPackage near the
top of the trace, this indicates that java couldn't find your
dk.bording.axt.tc.xsl.Translate class and is instead trying to treat it
as a package.

HTH,
Gary

> -----Original Message-----
> From: Morten Grue Nielsen [mailto:mgn@bording.dk] 
> Sent: Tuesday, December 18, 2001 4:43 AM
> To: 'xalan-j-users@xml.apache.org'
> Subject: XALAN 2 - Using Java Extension Functions
> 
> 
> Hi
> 
> I have made a Translate class, in which a replace method is 
> found. This method replace words, with other in a string. I 
> have a problem trying to get to this method by using xslt 
> extension functions with xalan j_2_2_D14. Instead of using 
> the replace method in my Translate class, it uses the replace 
> method in the java.lang.String class. Can you help me ??
> 
> ---------- The xslt script : -----------
> 
> xml version="1.0" encoding="ISO-8859-1"?>
> <xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>  xmlns:lxslt="http://xml.apache.org/xslt"
>  xmlns:bdf="dk.bording.axt.tc.xsl.Translate"
>  extension-element-prefixes="bdf"
>  version="1.0">
> 
>  <lxslt:component prefix="bdf" functions="replace">
>   <lxslt:script lang="javaclass" 
> src="xalan://dk.bording.axt.tc.xsl.Translate"/>
>  </lxslt:component>
> 
>  <xsl:output indent="yes" method="xml" encoding="iso-8859-1" 
> cdata-section-elements="VALUE NAME KEY SOURCE pling"/>
> 
>  <xsl:template match="/">
>   <xsl:apply-templates select="text()"/>
>   <document>
>    <pling>
>     <xsl:for-each select="function/test">
>      <xsl:value-of select="bdf:replace(string(text()),'asd','abc')"/>
>     </xsl:for-each>
>    </pling>
>   </document>
>  </xsl:template>
> </xsl:stylesheet>
> 
> 
> ------- Java file --------
> 
> package dk.bording.axt.tc.xsl;
> 
> import org.w3c.dom.*;
> import org.apache.xalan.extensions.*;
> import org.w3c.dom.traversal.*;
> import org.apache.xalan.templates.*;
> 
> public class Translate
> {
> 	static StringBuffer sb;
> 	static int i;
> 	static String s1;
> 
> 	public static String replace(XSLProcessorContext 
> processorContext, ElemExtensionCall extElem) {
> 	return "hest";
> 	}
> 
> 	public static String replace(NodeList nl, String s2, 
> String s3) {
> 		for (int i = nl.getLength() - 1; i >= 0; i--) {
> 			s1 = (String)nl.item(i).getNodeValue();
> 			s1 = (String)rep(s1, "%pl%", "'");
> 			s2 = (String)rep(s2, "%pl%", "'");
> 			s3 = (String)rep(s3, "%pl%", "'");
> 		}
> 		return rep(s1,s2,s3);
> 	}
> 
> 	public static String replace(ExpressionContext ec, 
> String s2, String
> s3) {
> 		NodeIterator ni = ec.getContextNodes();
> 		boolean more = true;
> 		while (more) {
> 			Node n = ni.nextNode();
> 			if (n != null) {
> 				more = true;
> 			} else {
> 				more = false;
> 			}
> 			s1 = (String)n.getNodeValue();
> 			s1 = (String)rep(s1, "%pl%", "'");
> 			s2 = (String)rep(s2, "%pl%", "'");
> 			s3 = (String)rep(s3, "%pl%", "'");
> 		}
> 		return rep(s1,s2,s3);
> 	}
> 
> 	public static String replace2(String s1, String s2, String s3) {
> 		s1 = (String)rep(s1, "%pl%", "'");
> 		s2 = (String)rep(s2, "%pl%", "'");
> 		s3 = (String)rep(s3, "%pl%", "'");
> 		return rep(s1,s2,s3);
> 	}
> 
> 	private static String rep(String s1, String s2, String s3) {
> 		int l = s1.length();
> 		for (int i2=0;i2<l;i2++) {
> 			char rp = s2.charAt(0);
> 			char fc = s1.charAt(i2);
> 			if (rp == fc) {
> 				String s = s1.substring(i2, i2 +
> s2.length());
> 				if (s.equals(s2)) {
> 					StringBuffer sb = new
> StringBuffer(s1);
> 					sb.delete(i2, i2 + s2.length());
> 					l = l - s2.length();
> 					sb.insert(i2, s3);
> 					l = l + s3.length();
> 					s1 = sb.toString();
> 					for (int 
> i3=0;i3<s3.length();i3++) {
> 						i2++;
> 					}
> 				}
> 			}
> 		}
> 		return s1;
> 	}
> }
>