| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
| SimpleFunctionContext |
|
| 2.0;2 |
| 1 | /* |
|
| 2 | * $Header: /home/projects/jaxen/scm/jaxen/src/java/main/org/jaxen/SimpleFunctionContext.java,v 1.12 2005/09/25 11:39:21 elharo Exp $ |
|
| 3 | * $Revision: 1.12 $ |
|
| 4 | * $Date: 2005/09/25 11:39:21 $ |
|
| 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: SimpleFunctionContext.java,v 1.12 2005/09/25 11:39:21 elharo Exp $ |
|
| 60 | */ |
|
| 61 | ||
| 62 | ||
| 63 | package org.jaxen; |
|
| 64 | ||
| 65 | import java.util.HashMap; |
|
| 66 | ||
| 67 | /** Simple default implementation of <code>FunctionContext</code>. |
|
| 68 | * |
|
| 69 | * <p> |
|
| 70 | * This is a simple table-based key-lookup implementation |
|
| 71 | * for <code>FunctionContext</code> which can be programmatically |
|
| 72 | * extended by registering additional functions. |
|
| 73 | * </p> |
|
| 74 | * |
|
| 75 | * @see XPathFunctionContext |
|
| 76 | * |
|
| 77 | * @author <a href="mailto:bob@werken.com">bob mcwhirter</a> |
|
| 78 | */ |
|
| 79 | public class SimpleFunctionContext implements FunctionContext |
|
| 80 | { |
|
| 81 | /** Table of functions. */ |
|
| 82 | private HashMap functions; |
|
| 83 | ||
| 84 | /** |
|
| 85 | * <p> |
|
| 86 | * Construct an empty function context. |
|
| 87 | * </p> |
|
| 88 | */ |
|
| 89 | public SimpleFunctionContext() |
|
| 90 | 294 | { |
| 91 | 294 | this.functions = new HashMap(); |
| 92 | 294 | } |
| 93 | ||
| 94 | /** Register a new function. |
|
| 95 | * |
|
| 96 | * <p> |
|
| 97 | * By registering a new function, any XPath expression |
|
| 98 | * that utilizes this <code>FunctionContext</code> may |
|
| 99 | * refer to and use the new function. |
|
| 100 | * </p> |
|
| 101 | * |
|
| 102 | * <p> |
|
| 103 | * Functions may exist either in a namespace or not. |
|
| 104 | * Namespace prefix-to-URI resolution is the responsibility |
|
| 105 | * of a {@link NamespaceContext}. Within this <code>FunctionContext</code> |
|
| 106 | * functions are only referenced using the URI, <strong>not</strong> |
|
| 107 | * the prefix. |
|
| 108 | * </p> |
|
| 109 | * |
|
| 110 | * <p> |
|
| 111 | * The namespace URI of a function may be <code>null</code> |
|
| 112 | * to indicate that it exists without a namespace. |
|
| 113 | * </p> |
|
| 114 | * |
|
| 115 | * @param namespaceURI the namespace URI of the function to |
|
| 116 | * be registered with this context |
|
| 117 | * @param localName the non-prefixed local portion of the |
|
| 118 | * function to be registered with this context |
|
| 119 | * @param function a {@link Function} implementation object |
|
| 120 | * to be used when evaluating the function |
|
| 121 | */ |
|
| 122 | public void registerFunction(String namespaceURI, |
|
| 123 | String localName, |
|
| 124 | Function function ) |
|
| 125 | { |
|
| 126 | 9672 | this.functions.put( new QualifiedName(namespaceURI, null, localName), |
| 127 | function ); |
|
| 128 | 9672 | } |
| 129 | ||
| 130 | public Function getFunction(String namespaceURI, |
|
| 131 | String prefix, |
|
| 132 | String localName ) |
|
| 133 | throws UnresolvableException |
|
| 134 | { |
|
| 135 | 12594 | QualifiedName key = new QualifiedName( namespaceURI, prefix, localName ); |
| 136 | ||
| 137 | 12594 | if ( this.functions.containsKey(key) ) { |
| 138 | 12576 | return (Function) this.functions.get( key ); |
| 139 | } |
|
| 140 | else { |
|
| 141 | 18 | throw new UnresolvableException( "No Such Function " + key.getClarkForm() ); |
| 142 | } |
|
| 143 | } |
|
| 144 | } |