jaxp/src/com/sun/org/apache/xalan/internal/lib/ExsltBase.java
author duke
Wed, 05 Jul 2017 19:04:46 +0200
changeset 18896 98b0babd0565
parent 12457 c348e06f0e82
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     6
 * Copyright 1999-2004 The Apache Software Foundation.
7f561c08de6b Initial load
duke
parents:
diff changeset
     7
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
     8
 * Licensed under the Apache License, Version 2.0 (the "License");
7f561c08de6b Initial load
duke
parents:
diff changeset
     9
 * you may not use this file except in compliance with the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    10
 * You may obtain a copy of the License at
7f561c08de6b Initial load
duke
parents:
diff changeset
    11
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *     http://www.apache.org/licenses/LICENSE-2.0
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 * Unless required by applicable law or agreed to in writing, software
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * distributed under the License is distributed on an "AS IS" BASIS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * See the License for the specific language governing permissions and
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * limitations under the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
 * $Id: ExsltBase.java,v 1.1.2.1 2005/08/01 02:08:51 jeffsuttor Exp $
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
package com.sun.org.apache.xalan.internal.lib;
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
import com.sun.org.apache.xml.internal.dtm.ref.DTMNodeProxy;
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
import org.w3c.dom.Node;
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import org.w3c.dom.NodeList;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
 * The base class for some EXSLT extension classes.
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
 * It contains common utility methods to be used by the sub-classes.
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
public abstract class ExsltBase
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
{
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
   * Return the string value of a Node
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
   * @param n The Node.
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
   * @return The string value of the Node
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
  protected static String toString(Node n)
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
    if (n instanceof DTMNodeProxy)
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
         return ((DTMNodeProxy)n).getStringValue();
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
    else
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
      String value = n.getNodeValue();
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
      if (value == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
        NodeList nodelist = n.getChildNodes();
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
        StringBuffer buf = new StringBuffer();
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
        for (int i = 0; i < nodelist.getLength(); i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
          Node childNode = nodelist.item(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
          buf.append(toString(childNode));
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
        return buf.toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
      else
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
        return value;
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
   * Convert the string value of a Node to a number.
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
   * Return NaN if the string is not a valid number.
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
   * @param n The Node.
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
   * @return The number value of the Node
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
  protected static double toNumber(Node n)
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
    double d = 0.0;
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
    String str = toString(n);
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
    try
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
      d = Double.valueOf(str).doubleValue();
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
    catch (NumberFormatException e)
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
      d= Double.NaN;
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
    return d;
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
}