01: // Params.java - Read stylesheet parameters in Xalan
02:
03: package com.nwalsh.xalan;
04:
05: import org.apache.xpath.objects.XObject;
06: import org.apache.xpath.XPathContext;
07: import org.apache.xalan.extensions.ExpressionContext;
08: import org.apache.xml.utils.QName;
09:
10: import javax.xml.transform.TransformerException;
11:
12: public class Params {
13:
14: public static String getString(ExpressionContext context,
15: String varName) {
16: try {
17: XObject var = context
18: .getVariableOrParam(new QName(varName));
19: if (var != null) {
20: return var.toString();
21: } else {
22: System.out.println("$" + varName
23: + " is not a defined parameter.");
24: return "";
25: }
26: } catch (TransformerException te) {
27: // Nevermind the warning
28: // System.out.println("Transformer exception getting value of $" + varName);
29: return "";
30: }
31: }
32:
33: public static int getInt(ExpressionContext context, String varName) {
34: String stringValue = getString(context, varName);
35: if (stringValue != null) {
36: try {
37: int value = Integer.parseInt(stringValue);
38: return value;
39: } catch (NumberFormatException e) {
40: System.out.println("$" + varName
41: + " is not an integer.");
42: }
43: }
44: return 0;
45: }
46:
47: public static boolean getBoolean(ExpressionContext context,
48: String varName) {
49: String stringValue = getString(context, varName);
50: if (stringValue != null) {
51: if (stringValue.equals("0") || stringValue.equals("")) {
52: return false;
53: } else {
54: return true;
55: }
56: } else {
57: return false;
58: }
59: }
60: }
|