KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > boot > servlets > Util


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.boot.servlets;
8
9 import javax.xml.transform.TransformerException JavaDoc;
10
11 import org.apache.xalan.extensions.ExpressionContext;
12 import org.apache.xml.utils.QName;
13 import org.apache.xpath.objects.XObject;
14
15 import gnu.regexp.RE;
16 import gnu.regexp.REException;
17 import gnu.regexp.REMatch;
18
19 /** A utility class that implements a xalan XSLT extension function used by
20 the default.xsl transformation document.
21
22  *
23  * @author Scott.Stark@jboss.org
24  * @version $revision:$
25  */

26 public class Util
27 {
28    static RE variableRE;
29
30    public static void setVariableRE(RE variableRE)
31    {
32       Util.variableRE = variableRE;
33    }
34
35    /** This function replaces all occurrences of variable references ${...} with
36     the corresponding XSL variable. If no such variable is defined the variable
37     is replaced with an empty string.
38    */

39    public static String JavaDoc replaceVariables(ExpressionContext ctx, String JavaDoc text)
40    {
41       String JavaDoc value = text;
42       try
43       {
44          REMatch[] matches = variableRE.getAllMatches(text);
45          if( matches.length > 0 )
46          {
47             StringBuffer JavaDoc tmp = new StringBuffer JavaDoc();
48             for(int m = 0; m < matches.length; m ++)
49             {
50                String JavaDoc prefix = matches[m].toString(1);
51                String JavaDoc name = matches[m].toString(2);
52                String JavaDoc suffix = matches[m].toString(3);
53                QName varName = new QName(name);
54                XObject var = ctx.getVariableOrParam(varName);
55                tmp.append(prefix);
56                if( var != null )
57                   tmp.append(var.toString());
58                tmp.append(suffix);
59             }
60             value = tmp.toString();
61          }
62       }
63       catch(TransformerException JavaDoc e)
64       {
65          e.printStackTrace();
66       }
67       return value;
68    }
69
70 }
71
Popular Tags