Coverage Report - org.apache.camel.builder.xml.DefaultNamespaceContext
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultNamespaceContext
44% 
36% 
2.75
 
 1  
 /**
 2  
  *
 3  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 4  
  * contributor license agreements.  See the NOTICE file distributed with
 5  
  * this work for additional information regarding copyright ownership.
 6  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 7  
  * (the "License"); you may not use this file except in compliance with
 8  
  * the License.  You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 package org.apache.camel.builder.xml;
 19  
 
 20  
 import org.w3c.dom.Element;
 21  
 import org.w3c.dom.NamedNodeMap;
 22  
 import org.w3c.dom.Node;
 23  
 import org.w3c.dom.Attr;
 24  
 
 25  
 import javax.xml.namespace.NamespaceContext;
 26  
 import javax.xml.xpath.XPathFactory;
 27  
 import java.util.HashMap;
 28  
 import java.util.HashSet;
 29  
 import java.util.Iterator;
 30  
 import java.util.Map;
 31  
 import java.util.Set;
 32  
 
 33  
 /**
 34  
  * An implementation of {@link NamespaceContext} which uses a simple Map where
 35  
  * the keys are the prefixes and the values are the URIs
 36  
  *
 37  
  * @version $Revision: $
 38  
  */
 39  
 public class DefaultNamespaceContext implements NamespaceContext {
 40  
 
 41  
     private final Map map;
 42  
     private final NamespaceContext parent;
 43  
 
 44  
     public DefaultNamespaceContext() {
 45  0
         this(XPathFactory.newInstance());
 46  0
     }
 47  
 
 48  16
     public DefaultNamespaceContext(XPathFactory factory) {
 49  16
         this.parent = factory.newXPath().getNamespaceContext();
 50  16
         this.map = new HashMap();
 51  16
     }
 52  
 
 53  0
     public DefaultNamespaceContext(NamespaceContext parent, Map map) {
 54  0
         this.parent = parent;
 55  0
         this.map = map;
 56  0
     }
 57  
 
 58  
     /**
 59  
      * A helper method to make it easy to create newly populated instances
 60  
      */
 61  
     public DefaultNamespaceContext add(String prefix, String uri) {
 62  9
         map.put(prefix, uri);
 63  9
         return this;
 64  
     }
 65  
 
 66  
     public String getNamespaceURI(String prefix) {
 67  6
         String answer = (String) map.get(prefix);
 68  6
         if (answer == null && parent != null) {
 69  0
             return parent.getNamespaceURI(prefix);
 70  
         }
 71  6
         return answer;
 72  
     }
 73  
 
 74  
     public String getPrefix(String namespaceURI) {
 75  0
         for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
 76  0
             Map.Entry entry = (Map.Entry) iter.next();
 77  0
             if (namespaceURI.equals(entry.getValue())) {
 78  0
                 return (String) entry.getKey();
 79  
             }
 80  0
         }
 81  0
         if (parent != null) {
 82  0
             return parent.getPrefix(namespaceURI);
 83  
         }
 84  0
         return null;
 85  
     }
 86  
 
 87  
     public Iterator getPrefixes(String namespaceURI) {
 88  0
         Set set = new HashSet();
 89  0
         for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
 90  0
             Map.Entry entry = (Map.Entry) iter.next();
 91  0
             if (namespaceURI.equals(entry.getValue())) {
 92  0
                 set.add(entry.getKey());
 93  
             }
 94  0
         }
 95  0
         if (parent != null) {
 96  0
             Iterator iter = parent.getPrefixes(namespaceURI);
 97  0
             while (iter.hasNext()) {
 98  0
                 set.add(iter.next());
 99  0
             }
 100  
         }
 101  0
         return set.iterator();
 102  
     }
 103  
 
 104  
     public void setNamespacesFromDom(Element element) {
 105  
         // lets set the parent first in case we overload a prefix here
 106  2
         Node parentNode = element.getParentNode();
 107  2
         if (parentNode instanceof Element) {
 108  1
             setNamespacesFromDom((Element) parentNode);
 109  
         }
 110  2
         NamedNodeMap attributes = element.getAttributes();
 111  6
         for (int i = 0, size = attributes.getLength(); i < size; i++) {
 112  4
             Attr node = (Attr) attributes.item(i);
 113  4
             String name = node.getName();
 114  4
             if (name.startsWith("xmlns:")) {
 115  3
                 String prefix = name.substring("xmlns:".length());
 116  3
                 String uri = node.getValue();
 117  3
                 add(prefix, uri);
 118  
             }
 119  
         }
 120  2
     }
 121  
 }