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

org.navimatrix.jaxen.function.BooleanFunction Maven / Gradle / Ivy

There is a newer version: 1.0.5
Show newest version
/*
 * $Header: /cvs/jaxen/jaxen/src/java/main/org/jaxen/function/BooleanFunction.java,v 1.17 2006/02/05 21:47:41 elharo Exp $
 * $Revision: 1.17 $
 * $Date: 2006/02/05 21:47:41 $
 *
 * ====================================================================
 *
 * 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: BooleanFunction.java,v 1.17 2006/02/05 21:47:41 elharo Exp $
 */


package org.navimatrix.jaxen.function;

import java.util.List;

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

/**
 * 

* 4.3 boolean boolean(object) *

* *
*

* The boolean * function converts its argument to a boolean as follows: *

* *
    * *
  • *

    * a number is true if and only if it is neither positive or negative * zero nor NaN *

    *
  • * *
  • *

    * a node-set is true if and only if it is non-empty *

    *
  • * *
  • *

    * a string is true if and only if its length is non-zero *

    *
  • * *
  • * *

    * an object of a type other than the four basic types is converted to a * boolean in a way that is dependent on that type *

*
* * @author bob mcwhirter (bob @ werken.com) * @see Section 4.3 of the XPath Specification */ public class BooleanFunction implements Function { /** * Create a new BooleanFunction object. */ public BooleanFunction() {} /** Convert the argument to a Boolean * * @param context the context at the point in the * expression when the function is called * @param args a list with exactly one item which will be converted to a * Boolean * * @return the result of evaluating the function; * Boolean.TRUE or Boolean.FALSE * * @throws FunctionCallException if args has more or less than one item */ public Object call(Context context, List args) throws FunctionCallException { if ( args.size() == 1 ) { return evaluate( args.get(0), context.getNavigator() ); } throw new FunctionCallException("boolean() requires one argument"); } /** *

Convert the argument obj to a Boolean * according to the following rules:

* *
    *
  • Lists are false if they're empty; true if they're not.
  • *
  • Booleans are false if they're false; true if they're true.
  • *
  • Strings are false if they're empty; true if they're not.
  • *
  • Numbers are false if they're 0 or NaN; true if they're not.
  • *
  • All other objects are true.
  • *
* * @param obj the object to convert to a boolean * @param nav ignored * * @return Boolean.TRUE or Boolean.FALSE */ public static Boolean evaluate(Object obj, Navigator nav) { if ( obj instanceof List ) { List list = (List) obj; // if it's an empty list, then we have a null node-set -> false if (list.size() == 0) { return Boolean.FALSE; } // otherwise, unwrap the list and check the primitive obj = list.get(0); } // now check for primitive types // otherwise a non-empty node-set is true // if it's a Boolean, let it decide if ( obj instanceof Boolean ) { return (Boolean) obj; } // if it's a Number, != 0 -> true else if ( obj instanceof Number ) { double d = ((Number) obj).doubleValue(); if ( d == 0 || Double.isNaN(d) ) { return Boolean.FALSE; } return Boolean.TRUE; } // if it's a String, "" -> false else if ( obj instanceof String ) { return ( ((String)obj).length() > 0 ? Boolean.TRUE : Boolean.FALSE ); } else { // assume it's a node so that this node-set is non-empty // and so it's true return ( obj != null ) ? Boolean.TRUE : Boolean.FALSE; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy