Coverage Report - org.jaxen.XPathFunctionContext

Classes in this File Line Coverage Branch Coverage Complexity
XPathFunctionContext
100% 
100% 
1.167

 1  
 /*
 2  
  * $Header: /home/projects/jaxen/scm/jaxen/src/java/main/org/jaxen/XPathFunctionContext.java,v 1.27 2005/09/24 11:38:54 elharo Exp $
 3  
  * $Revision: 1.27 $
 4  
  * $Date: 2005/09/24 11:38:54 $
 5  
  *
 6  
  * ====================================================================
 7  
  *
 8  
  * Copyright (C) 2000-2002 bob mcwhirter & James Strachan.
 9  
  * All rights reserved.
 10  
  *
 11  
  * Redistribution and use in source and binary forms, with or without
 12  
  * modification, are permitted provided that the following conditions
 13  
  * are met:
 14  
  * 
 15  
  * 1. Redistributions of source code must retain the above copyright
 16  
  *    notice, this list of conditions, and the following disclaimer.
 17  
  *
 18  
  * 2. Redistributions in binary form must reproduce the above copyright
 19  
  *    notice, this list of conditions, and the disclaimer that follows 
 20  
  *    these conditions in the documentation and/or other materials 
 21  
  *    provided with the distribution.
 22  
  *
 23  
  * 3. The name "Jaxen" must not be used to endorse or promote products
 24  
  *    derived from this software without prior written permission.  For
 25  
  *    written permission, please contact license@jaxen.org.
 26  
  * 
 27  
  * 4. Products derived from this software may not be called "Jaxen", nor
 28  
  *    may "Jaxen" appear in their name, without prior written permission
 29  
  *    from the Jaxen Project Management (pm@jaxen.org).
 30  
  * 
 31  
  * In addition, we request (but do not require) that you include in the 
 32  
  * end-user documentation provided with the redistribution and/or in the 
 33  
  * software itself an acknowledgement equivalent to the following:
 34  
  *     "This product includes software developed by the
 35  
  *      Jaxen Project <http://www.jaxen.org/>."
 36  
  * Alternatively, the acknowledgment may be graphical using the logos 
 37  
  * available at http://www.jaxen.org/
 38  
  *
 39  
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 40  
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 41  
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 42  
  * DISCLAIMED.  IN NO EVENT SHALL THE Jaxen AUTHORS OR THE PROJECT
 43  
  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 44  
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 45  
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 46  
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 47  
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 48  
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 49  
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 50  
  * SUCH DAMAGE.
 51  
  *
 52  
  * ====================================================================
 53  
  * This software consists of voluntary contributions made by many 
 54  
  * individuals on behalf of the Jaxen Project and was originally 
 55  
  * created by bob mcwhirter <bob@werken.com> and 
 56  
  * James Strachan <jstrachan@apache.org>.  For more information on the 
 57  
  * Jaxen Project, please see <http://www.jaxen.org/>.
 58  
  * 
 59  
  * $Id: XPathFunctionContext.java,v 1.27 2005/09/24 11:38:54 elharo Exp $
 60  
  */
 61  
 
 62  
 
 63  
 package org.jaxen;
 64  
 
 65  
 import org.jaxen.function.BooleanFunction;
 66  
 import org.jaxen.function.CeilingFunction;
 67  
 import org.jaxen.function.ConcatFunction;
 68  
 import org.jaxen.function.ContainsFunction;
 69  
 import org.jaxen.function.CountFunction;
 70  
 import org.jaxen.function.FalseFunction;
 71  
 import org.jaxen.function.FloorFunction;
 72  
 import org.jaxen.function.IdFunction;
 73  
 import org.jaxen.function.LangFunction;
 74  
 import org.jaxen.function.LastFunction;
 75  
 import org.jaxen.function.LocalNameFunction;
 76  
 import org.jaxen.function.NameFunction;
 77  
 import org.jaxen.function.NamespaceUriFunction;
 78  
 import org.jaxen.function.NormalizeSpaceFunction;
 79  
 import org.jaxen.function.NotFunction;
 80  
 import org.jaxen.function.NumberFunction;
 81  
 import org.jaxen.function.PositionFunction;
 82  
 import org.jaxen.function.RoundFunction;
 83  
 import org.jaxen.function.StartsWithFunction;
 84  
 import org.jaxen.function.StringFunction;
 85  
 import org.jaxen.function.StringLengthFunction;
 86  
 import org.jaxen.function.SubstringAfterFunction;
 87  
 import org.jaxen.function.SubstringBeforeFunction;
 88  
 import org.jaxen.function.SubstringFunction;
 89  
 import org.jaxen.function.SumFunction;
 90  
 import org.jaxen.function.TranslateFunction;
 91  
 import org.jaxen.function.TrueFunction;
 92  
 import org.jaxen.function.ext.EndsWithFunction;
 93  
 import org.jaxen.function.ext.EvaluateFunction;
 94  
 import org.jaxen.function.ext.LowerFunction;
 95  
 import org.jaxen.function.ext.MatrixConcatFunction;
 96  
 import org.jaxen.function.ext.UpperFunction;
 97  
 import org.jaxen.function.xslt.DocumentFunction;
 98  
 
 99  
 /** A <code>FunctionContext</code> implementing the core XPath
 100  
  *  function library, plus Jaxen extensions.
 101  
  *
 102  
  *  <p>
 103  
  *  The core XPath function library is provided through this
 104  
  *  implementation of <code>FunctionContext</code>.  Additionally,
 105  
  *  extension functions have been provided, as enumerated below.
 106  
  *  </p>
 107  
  *
 108  
  *  <p>
 109  
  *  This class is re-entrant and thread-safe.  If using the
 110  
  *  default instance, it is inadvisable to call 
 111  
  *  {@link #registerFunction(String, String, Function)}
 112  
  *  as that will extend the global function context, affecting other
 113  
  *  users.  But that's your call, really, now isn't
 114  
  *  it?  That may be what you really want to do.
 115  
  *  </p>
 116  
  *
 117  
  *  <p>
 118  
  *  Extension functions:
 119  
  *  </p>
 120  
  *
 121  
  *  <ul>
 122  
  *     <li>matrix-concat(..)</li>
 123  
  *     <li>evaluate(..)</li>
 124  
  *     <li>upper-case(..)</li>
 125  
  *     <li>lower-case(..)</li>
 126  
  *     <li>ends-with(..)</li>
 127  
  *  </ul>
 128  
  *
 129  
  *  @see FunctionContext
 130  
  *  @see org.jaxen.function
 131  
  *  @see org.jaxen.function.xslt
 132  
  *  @see org.jaxen.function.ext
 133  
  *
 134  
  *  @author <a href="mailto:bob@werken.com">bob mcwhirter</a>
 135  
  */
 136  
 public class XPathFunctionContext extends SimpleFunctionContext
 137  
 {
 138  288
     private static XPathFunctionContext instance = new XPathFunctionContext();
 139  
 
 140  
     /** Retrieve the default function context
 141  
      *
 142  
      *  @return the default function context
 143  
      */
 144  
     public static FunctionContext getInstance()
 145  
     {
 146  4890
         return instance;
 147  
     }
 148  
 
 149  
     /** Create a new XPath function context.
 150  
      *  All core XPath and Jaxen extension functions are registered.
 151  
      */
 152  
     public XPathFunctionContext()
 153  
     {
 154  288
         this(true);
 155  288
     }
 156  
 
 157  
     /** Create a new XPath function context.
 158  
      *  All core XPath functions are registered.
 159  
      *  
 160  
      * @param includeExtensionFunctions if true extension functions are included;
 161  
      *     if false, they aren't.
 162  
      */
 163  
     public XPathFunctionContext(boolean includeExtensionFunctions)
 164  294
     {
 165  294
         registerXPathFunctions();
 166  294
         if (includeExtensionFunctions) {
 167  288
             registerXSLTFunctions();
 168  288
             registerExtensionFunctions();
 169  
         }
 170  294
     }
 171  
 
 172  
     private void registerXPathFunctions() {
 173  
 
 174  294
         registerFunction( null,  // namespace URI
 175  
                           "boolean",
 176  
                           new BooleanFunction() );
 177  
 
 178  294
         registerFunction( null,  // namespace URI
 179  
                           "ceiling",
 180  
                           new CeilingFunction() );
 181  
 
 182  294
         registerFunction( null,  // namespace URI
 183  
                           "concat",
 184  
                           new ConcatFunction() );
 185  
 
 186  294
         registerFunction( null,  // namespace URI
 187  
                           "contains",
 188  
                           new ContainsFunction() );
 189  
         
 190  294
         registerFunction( null,  // namespace URI
 191  
                           "count",
 192  
                           new CountFunction() );
 193  
 
 194  294
         registerFunction( null,  // namespace URI
 195  
                           "false",
 196  
                           new FalseFunction() );
 197  
 
 198  294
         registerFunction( null,  // namespace URI
 199  
                           "floor",
 200  
                           new FloorFunction() );
 201  
 
 202  294
         registerFunction( null,  // namespace URI
 203  
                           "id",
 204  
                           new IdFunction() );
 205  
 
 206  294
         registerFunction( null,  // namespace URI
 207  
                           "lang",
 208  
                           new LangFunction() );
 209  
 
 210  294
         registerFunction( null,  // namespace URI
 211  
                           "last",
 212  
                           new LastFunction() );
 213  
 
 214  294
         registerFunction( null,  // namespace URI
 215  
                           "local-name",
 216  
                           new LocalNameFunction() );
 217  
 
 218  294
         registerFunction( null,  // namespace URI
 219  
                           "name",
 220  
                           new NameFunction() );
 221  
 
 222  294
         registerFunction( null,  // namespace URI
 223  
                           "namespace-uri",
 224  
                           new NamespaceUriFunction() );
 225  
 
 226  294
         registerFunction( null,  // namespace URI
 227  
                           "normalize-space",
 228  
                           new NormalizeSpaceFunction() );
 229  
 
 230  294
         registerFunction( null,  // namespace URI
 231  
                           "not",
 232  
                           new NotFunction() );
 233  
 
 234  294
         registerFunction( null,  // namespace URI
 235  
                           "number",
 236  
                           new NumberFunction() );
 237  
 
 238  294
         registerFunction( null,  // namespace URI
 239  
                           "position",
 240  
                           new PositionFunction() );
 241  
 
 242  294
         registerFunction( null,  // namespace URI
 243  
                           "round",
 244  
                           new RoundFunction() );
 245  
 
 246  294
         registerFunction( null,  // namespace URI
 247  
                           "starts-with",
 248  
                           new StartsWithFunction() );
 249  
 
 250  294
         registerFunction( null,  // namespace URI
 251  
                           "string",
 252  
                           new StringFunction() );
 253  
 
 254  294
         registerFunction( null,  // namespace URI
 255  
                           "string-length",
 256  
                           new StringLengthFunction() );
 257  
 
 258  294
         registerFunction( null,  // namespace URI
 259  
                           "substring-after",
 260  
                           new SubstringAfterFunction() );
 261  
 
 262  294
         registerFunction( null,  // namespace URI
 263  
                           "substring-before",
 264  
                           new SubstringBeforeFunction() );
 265  
 
 266  294
         registerFunction( null,  // namespace URI
 267  
                           "substring",
 268  
                           new SubstringFunction() );
 269  
 
 270  294
         registerFunction( null,  // namespace URI
 271  
                           "sum",
 272  
                           new SumFunction() );
 273  
 
 274  294
         registerFunction( null,  // namespace URI
 275  
                           "true",
 276  
                           new TrueFunction() );
 277  
         
 278  294
         registerFunction( null,  // namespace URI
 279  
                           "translate",
 280  
                           new TranslateFunction() );
 281  294
     }
 282  
 
 283  
     private void registerXSLTFunctions() {
 284  
 
 285  
         // extension functions defined in XSLT
 286  288
         registerFunction( null,  // namespace URI
 287  
                           "document",
 288  
                           new DocumentFunction() );
 289  288
     }
 290  
 
 291  
     private void registerExtensionFunctions() {
 292  
         // extension functions should go into a namespace, but which one?
 293  
         // for now, keep them in default namespace to not break any code
 294  
 
 295  288
         registerFunction( null,  // namespace URI
 296  
                           "matrix-concat",
 297  
                           new MatrixConcatFunction() );
 298  
 
 299  288
         registerFunction( null,  // namespace URI
 300  
                           "evaluate",
 301  
                           new EvaluateFunction() );
 302  
         
 303  288
         registerFunction( null,  // namespace URI
 304  
                           "lower-case",
 305  
                           new LowerFunction() );
 306  
         
 307  288
         registerFunction( null,  // namespace URI
 308  
                           "upper-case",
 309  
                           new UpperFunction() );
 310  
         
 311  288
         registerFunction( null,  // namespace URI
 312  
                           "ends-with",
 313  
                           new EndsWithFunction() );
 314  288
     }
 315  
 
 316  
     
 317  
 }