All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jaxen.function.StringFunction Maven / Gradle / Ivy

/*
 * $Header$
 * $Revision$
 * $Date$
 *
 * ====================================================================
 *
 * Copyright 2000-2002 bob mcwhirter & James Strachan.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 * 
 *   * Neither the name of the Jaxen Project nor the names of its
 *     contributors may be used to endorse or promote products derived 
 *     from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * ====================================================================
 * This software consists of voluntary contributions made by many 
 * individuals on behalf of the Jaxen Project and was originally 
 * created by bob mcwhirter  and 
 * James Strachan .  For more information on the 
 * Jaxen Project, please see .
 * 
 * $Id$
 */


package org.jaxen.function;

import org.jaxen.Context;
import org.jaxen.Function; 
import org.jaxen.Navigator; 

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.DecimalFormatSymbols;
import java.util.List;
import java.util.Iterator;
import java.util.Locale;
 
/**
 * 

* 4.2 string string(object) *

* * *
*

* The string function converts * an object to a string as follows: *

* *
    * *
  • *

    * A node-set is converted to a string by returning the string-value of the node in the node-set * that is first in document order. If * the node-set is empty, an empty string is returned. *

    *
  • * *
  • *

    * A number is converted to a string as follows *

    * *
      * *
    • *

      * NaN is converted to the string NaN *

      *
    • * *
    • *

      * positive zero is converted to the string 0 *

      *
    • * *
    • * *

      * negative zero is converted to the string 0 *

      *
    • * *
    • *

      * positive infinity is converted to the string Infinity *

      *
    • * *
    • *

      * negative infinity is converted to the string -Infinity * *

      *
    • * *
    • *

      * if the number is an integer, the number is represented in decimal * form as a Number with no decimal point and * no leading zeros, preceded by a minus sign (-) if * the number is negative *

      *
    • * *
    • *

      * otherwise, the number is represented in decimal form as a Number including a decimal point with at least * one digit before the decimal point and at least one digit after the * decimal point, preceded by a minus sign (-) if the * number is negative; there must be no leading zeros before the decimal * point apart possibly from the one required digit immediately before * the decimal point; beyond the one required digit after the decimal * point there must be as many, but only as many, more digits as are * needed to uniquely distinguish the number from all other IEEE 754 * numeric values. *

      * *
    • * *
    * *
  • * *
  • *

    * The boolean false value is converted to the string false. * The boolean true value is converted to the string true. *

    *
  • * *
  • *

    * An object of a type other than the four basic types is converted to a * string in a way that is dependent on that type. *

    * *
  • * *
* *

* If the argument is omitted, it defaults to a node-set with the * context node as its only member. *

* *
* * @author bob mcwhirter (bob @ werken.com) * @see Section 4.2 of the XPath Specification */ @SuppressWarnings({"rawtypes"}) public class StringFunction implements Function { private static DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(Locale.ENGLISH); static { DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.ENGLISH); symbols.setNaN("NaN"); symbols.setInfinity("Infinity"); format.setGroupingUsed(false); format.setMaximumFractionDigits(32); format.setDecimalFormatSymbols(symbols); } /** * Create a new StringFunction object. */ public StringFunction() {} /** * Returns the string-value of args.get(0) * or of the context node if args is empty. * * @param context the context at the point in the * expression where the function is called * @param args list with zero or one element * * @return a String * * @ if args has more than one item */ public Object call(Context context, List args) { int size = args.size(); if ( size == 0 ) { return evaluate( context.getNodeSet(), context.getNavigator() ); } else if ( size == 1 ) { return evaluate( args.get(0), context.getNavigator() ); } throw new RuntimeException( "string() takes at most argument." ); } /** * Returns the XPath string-value of obj. * This operation is only defined if obj is a node, node-set, * String, Number, or * Boolean. For other types this function * returns the empty string. * * @param obj the node, node-set, string, number, or boolean * whose string-value is calculated * @param nav the navigator used to calculate the string-value * * @return a String. May be empty but is never null. */ public static String evaluate(Object obj, Navigator nav) { try { // Workaround because XOM uses lists for Text nodes // so we need to check for that first if (nav != null && nav.isText(obj)) { return nav.getTextStringValue(obj); } if (obj instanceof List) { List list = (List) obj; if (list.isEmpty()) { return ""; } // do not recurse: only first list should unwrap obj = list.get(0); } if (nav != null) { // This stack of instanceof really suggests there's // a failure to take advantage of polymorphism here if (nav.isElement(obj)) { return nav.getElementStringValue(obj); } else if (nav.isAttribute(obj)) { return nav.getAttributeStringValue(obj); } else if (nav.isDocument(obj)) { Iterator childAxisIterator = nav.getChildAxisIterator(obj); while (childAxisIterator.hasNext()) { Object descendant = childAxisIterator.next(); if (nav.isElement(descendant)) { return nav.getElementStringValue(descendant); } } } else if (nav.isProcessingInstruction(obj)) { return nav.getProcessingInstructionData(obj); } else if (nav.isComment(obj)) { return nav.getCommentStringValue(obj); } else if (nav.isText(obj)) { return nav.getTextStringValue(obj); } else if (nav.isNamespace(obj)) { return nav.getNamespaceStringValue(obj); } } if (obj instanceof String) { return (String) obj; } else if (obj instanceof Boolean) { return stringValue(((Boolean) obj).booleanValue()); } else if (obj instanceof Number) { return stringValue(((Number) obj).doubleValue()); } } catch (Exception e) { throw new RuntimeException(e); } return ""; } private static String stringValue(double value) { // DecimalFormat formats negative zero as "-0". // Therefore we need to test for zero explicitly here. if (value == 0) return "0"; // need to synchronize object for thread-safety String result = null; synchronized (format) { result = format.format(value); } return result; } private static String stringValue(boolean value) { return value ? "true" : "false"; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy