KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nwalsh > xalan > Params


1 // Params.java - Read stylesheet parameters in Xalan
2

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