KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xalan > internal > lib > ExsltBase


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: ExsltBase.java,v 1.5 2004/02/11 17:56:36 minchau Exp $
18  */

19 package com.sun.org.apache.xalan.internal.lib;
20
21 import com.sun.org.apache.xml.internal.dtm.ref.DTMNodeProxy;
22
23 import org.w3c.dom.Node JavaDoc;
24 import org.w3c.dom.NodeList JavaDoc;
25
26 /**
27  * The base class for some EXSLT extension classes.
28  * It contains common utility methods to be used by the sub-classes.
29  */

30 public abstract class ExsltBase
31 {
32   /**
33    * Return the string value of a Node
34    *
35    * @param n The Node.
36    * @return The string value of the Node
37    */

38   protected static String JavaDoc toString(Node JavaDoc n)
39   {
40     if (n instanceof DTMNodeProxy)
41      return ((DTMNodeProxy)n).getStringValue();
42     else
43     {
44       String JavaDoc value = n.getNodeValue();
45       if (value == null)
46       {
47         NodeList JavaDoc nodelist = n.getChildNodes();
48         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
49         for (int i = 0; i < nodelist.getLength(); i++)
50         {
51           Node JavaDoc childNode = nodelist.item(i);
52           buf.append(toString(childNode));
53         }
54         return buf.toString();
55       }
56       else
57         return value;
58     }
59   }
60   
61   /**
62    * Convert the string value of a Node to a number.
63    * Return NaN if the string is not a valid number.
64    *
65    * @param n The Node.
66    * @return The number value of the Node
67    */

68   protected static double toNumber(Node JavaDoc n)
69   {
70     double d = 0.0;
71     String JavaDoc str = toString(n);
72     try
73     {
74       d = Double.valueOf(str).doubleValue();
75     }
76     catch (NumberFormatException JavaDoc e)
77     {
78       d= Double.NaN;
79     }
80     return d;
81   }
82 }
83
Popular Tags