Coverage Report - org.jaxen.SimpleNamespaceContext
 
Classes in this File Line Coverage Branch Coverage Complexity
SimpleNamespaceContext
96%
25/26
100%
5/5
2.4
 
 1  
 /*
 2  
  * $Header: /home/projects/jaxen/scm/jaxen/src/java/main/org/jaxen/SimpleNamespaceContext.java,v 1.18 2006/06/03 20:19:26 elharo Exp $
 3  
  * $Revision: 1.18 $
 4  
  * $Date: 2006/06/03 20:19:26 $
 5  
  *
 6  
  * ====================================================================
 7  
  *
 8  
  * Copyright 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 are
 13  
  * met:
 14  
  * 
 15  
  *   * Redistributions of source code must retain the above copyright
 16  
  *     notice, this list of conditions and the following disclaimer.
 17  
  * 
 18  
  *   * Redistributions in binary form must reproduce the above copyright
 19  
  *     notice, this list of conditions and the following disclaimer in the
 20  
  *     documentation and/or other materials provided with the distribution.
 21  
  * 
 22  
  *   * Neither the name of the Jaxen Project nor the names of its
 23  
  *     contributors may be used to endorse or promote products derived 
 24  
  *     from this software without specific prior written permission.
 25  
  * 
 26  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 27  
  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 28  
  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 29  
  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
 30  
  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 31  
  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 32  
  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 33  
  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 34  
  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 35  
  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 36  
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 37  
  *
 38  
  * ====================================================================
 39  
  * This software consists of voluntary contributions made by many 
 40  
  * individuals on behalf of the Jaxen Project and was originally 
 41  
  * created by bob mcwhirter <bob@werken.com> and 
 42  
  * James Strachan <jstrachan@apache.org>.  For more information on the 
 43  
  * Jaxen Project, please see <http://www.jaxen.org/>.
 44  
  * 
 45  
  * $Id: SimpleNamespaceContext.java,v 1.18 2006/06/03 20:19:26 elharo Exp $
 46  
  */
 47  
 
 48  
 
 49  
 package org.jaxen;
 50  
 
 51  
 import java.io.Serializable;
 52  
 import java.util.HashMap;
 53  
 import java.util.Iterator;
 54  
 import java.util.Map;
 55  
 
 56  
 /**
 57  
  *  Provides mappings from namespace prefix to namespace URI to the XPath
 58  
  *  engine.
 59  
  */
 60  
 public class SimpleNamespaceContext implements NamespaceContext, Serializable
 61  
 {
 62  
     
 63  
     /**
 64  
      * 
 65  
      */
 66  
     private static final long serialVersionUID = -808928409643497762L;
 67  
     // XXX should this prebind the xml prefix?
 68  
     private Map namespaces;
 69  
 
 70  
     /**
 71  
      * Creates a new empty namespace context.
 72  
      */
 73  
     public SimpleNamespaceContext()
 74  1746
     {
 75  1746
         this.namespaces = new HashMap();
 76  1746
     }
 77  
 
 78  
     /**
 79  
      * Creates a new namespace context pre-populated with the specified bindings. 
 80  
      * 
 81  
      * @param namespaces the initial namespace bindings in scope. The keys in this
 82  
      *     must be strings containing the prefixes and the values are strings
 83  
      *     containing the namespace URIs.
 84  
      *     
 85  
      * @throws NullPointerException if the argument is null   
 86  
      * @throws ClassCastException if any keys or values in the map are not strings   
 87  
      */
 88  
     public SimpleNamespaceContext(Map namespaces)
 89  6
     {
 90  6
         Iterator entries = namespaces.entrySet().iterator();
 91  6
         while (entries.hasNext()) {
 92  4
             Map.Entry entry = (Map.Entry) entries.next();
 93  4
             if (! (entry.getKey() instanceof String)
 94  
               || ! (entry.getValue() instanceof String)) {
 95  4
                 throw new ClassCastException("Non-string namespace binding");
 96  
             }
 97  0
         }
 98  2
         this.namespaces = new HashMap(namespaces);
 99  2
     }
 100  
 
 101  
     /**
 102  
      *  Adds all the namespace declarations that are in scope on the given
 103  
      *  element. In the case of an XSLT stylesheet, this would be the element
 104  
      *  that has the XPath expression in one of its attributes; e.g.
 105  
      *  <code>&lt;xsl:if test="condition/xpath/expression"&gt;</code>.
 106  
      *
 107  
      *  @param nav  the navigator for use in conjunction with
 108  
      *              <code>element</code>
 109  
      *  @param element the element to copy the namespaces from
 110  
      *  @throws UnsupportedAxisException if the navigator does not support the 
 111  
      *     namespace axis
 112  
      */
 113  
     public void addElementNamespaces( Navigator nav, Object element )
 114  
         throws UnsupportedAxisException
 115  
     {
 116  4
         Iterator namespaceAxis = nav.getNamespaceAxisIterator( element );
 117  
 
 118  8
         while ( namespaceAxis.hasNext() ) {
 119  4
             Object namespace = namespaceAxis.next();
 120  4
             String prefix = nav.getNamespacePrefix( namespace );
 121  4
             String uri = nav.getNamespaceStringValue( namespace );
 122  4
             if ( translateNamespacePrefixToUri(prefix) == null ) {
 123  4
                 addNamespace( prefix, uri );
 124  
             }
 125  4
         }
 126  4
     }    
 127  
 
 128  
     // ???? What if prefix or URI is null, or both?
 129  
     /**
 130  
      * Binds a prefix to a URI in this context.
 131  
      * 
 132  
      * @param prefix the namespace prefix
 133  
      * @param URI    the namespace URI
 134  
      */
 135  
     public void addNamespace(String prefix, String URI)
 136  
     {
 137  94
         this.namespaces.put( prefix, URI );
 138  94
     }
 139  
 
 140  
     public String translateNamespacePrefixToUri(String prefix)
 141  
     {
 142  4494
         if ( this.namespaces.containsKey( prefix ) )
 143  
         {
 144  220
             return (String) this.namespaces.get( prefix );
 145  
         }
 146  
 
 147  4274
         return null;
 148  
     }
 149  
 }