Clover coverage report -
Coverage timestamp: Sun Nov 1 2009 23:08:24 UTC
file stats: LOC: 136   Methods: 13
NCLOC: 76   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
NamespaceMap.java 92.9% 71.4% 61.5% 74.2%
coverage coverage
 1    /*
 2    * Licensed to the Apache Software Foundation (ASF) under one or more
 3    * contributor license agreements. See the NOTICE file distributed with
 4    * this work for additional information regarding copyright ownership.
 5    * The ASF licenses this file to You under the Apache License, Version 2.0
 6    * (the "License"); you may not use this file except in compliance with
 7    * the License. You may obtain a copy of the License at
 8    *
 9    * http://www.apache.org/licenses/LICENSE-2.0
 10    *
 11    * Unless required by applicable law or agreed to in writing, software
 12    * distributed under the License is distributed on an "AS IS" BASIS,
 13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14    * See the License for the specific language governing permissions and
 15    * limitations under the License.
 16    *
 17    * $Id: NamespaceMap.java 730491 2009-01-01 00:05:24Z natalia $
 18    */
 19   
 20    package org.apache.xindice.xml;
 21   
 22    import org.apache.xindice.xml.dom.DocumentImpl;
 23   
 24    import org.w3c.dom.Document;
 25    import org.w3c.dom.Element;
 26    import org.w3c.dom.Node;
 27   
 28    import java.util.*;
 29   
 30    /**
 31    * NamespaceMap is just a HashMap extension that provides some useful
 32    * Namespace related functionality.
 33    *
 34    * <p>
 35    * FIXME:
 36    * XUpdate library has a bug that stores default namespace with null prefix
 37    * istead of empty string. As a workaround, storing values with null key will
 38    * store them with empty string key instead.
 39    * If you are using Map interface methods on a NamespaceMap object, be aware
 40    * of this.
 41    * </p>
 42    * @version $Revision: 730491 $, $Date: 2009-01-01 00:05:24 +0000 (Thu, 01 Jan 2009) $
 43    */
 44    public final class NamespaceMap extends HashMap {
 45   
 46    private Element elem;
 47   
 48   
 49  13 public NamespaceMap() {
 50    }
 51   
 52  9 public NamespaceMap(Map namespaces) {
 53  9 putAll(namespaces);
 54    }
 55   
 56  42 public Node getContextNode() {
 57  42 if (elem == null) {
 58  21 Document d = new DocumentImpl();
 59  21 elem = d.createElement("nsmap");
 60  21 d.appendChild(elem);
 61  21 Iterator i = entrySet().iterator();
 62  21 while (i.hasNext()) {
 63  24 Map.Entry entry = (Map.Entry) i.next();
 64  24 String pfx = (String) entry.getKey();
 65  24 String uri = (String) entry.getValue();
 66  24 if ("".equals(pfx)) {
 67  3 elem.setAttribute("xmlns", uri);
 68    } else {
 69  21 elem.setAttribute("xmlns:" + pfx, uri);
 70    }
 71    }
 72    }
 73  42 return elem;
 74    }
 75   
 76  0 public void clearNamespaces() {
 77  0 clear();
 78  0 elem = null;
 79    }
 80   
 81  0 public String getDefaultNamespaceURI() {
 82  0 return (String) get("");
 83    }
 84   
 85  33 public String getNamespaceURI(String prefix) {
 86  33 return (String) get(prefix);
 87    }
 88   
 89  0 public void setDefaultNamespace(String uri) {
 90  0 put("", uri);
 91  0 elem = null;
 92    }
 93   
 94  1 public void setNamespace(String prefix, String uri) {
 95  1 put(prefix, uri);
 96  1 elem = null;
 97    }
 98   
 99  0 public void removeDefaultNamespace() {
 100  0 remove("");
 101  0 elem = null;
 102    }
 103   
 104  0 public void removeNamespace(String prefix) {
 105  0 remove(prefix);
 106  0 elem = null;
 107    }
 108   
 109  12 public void includeNamespaces(Map nsMap, boolean override) {
 110  12 Iterator newEntries = nsMap.entrySet().iterator();
 111  12 while (newEntries.hasNext()) {
 112  15 Map.Entry entry = (Map.Entry) newEntries.next();
 113  15 if (!override && containsKey(entry.getKey())) {
 114  0 continue;
 115    }
 116  15 put(entry.getKey(), entry.getValue());
 117    }
 118  12 elem = null;
 119    }
 120   
 121    /**
 122    * This is a workaround.
 123    * @see org.apache.xindice.xml.NamespaceMap
 124    */
 125  25 public Object put(Object o, Object o1) {
 126  25 return o == null ? super.put("", o1) : super.put(o, o1);
 127    }
 128   
 129    /**
 130    * This is a workaround.
 131    * @see org.apache.xindice.xml.NamespaceMap
 132    */
 133  15 public boolean containsKey(Object o) {
 134  15 return o == null ? super.containsKey(""): super.containsKey(o);
 135    }
 136    }