001/*
002Copyright (c) 2011+, HL7, Inc
003All rights reserved.
004
005Redistribution and use in source and binary forms, with or without modification, 
006are permitted provided that the following conditions are met:
007
008 * Redistributions of source code must retain the above copyright notice, this 
009   list of conditions and the following disclaimer.
010 * Redistributions in binary form must reproduce the above copyright notice, 
011   this list of conditions and the following disclaimer in the documentation 
012   and/or other materials provided with the distribution.
013 * Neither the name of HL7 nor the names of its contributors may be used to 
014   endorse or promote products derived from this software without specific 
015   prior written permission.
016
017THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
018ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
019WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
020IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
021INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
022NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
023PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
024WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
025ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
026POSSIBILITY OF SUCH DAMAGE.
027
028*/
029package org.hl7.fhir.utilities.xml;
030
031/*-
032 * #%L
033 * org.hl7.fhir.utilities
034 * %%
035 * Copyright (C) 2014 - 2019 Health Level 7
036 * %%
037 * Licensed under the Apache License, Version 2.0 (the "License");
038 * you may not use this file except in compliance with the License.
039 * You may obtain a copy of the License at
040 * 
041 *      http://www.apache.org/licenses/LICENSE-2.0
042 * 
043 * Unless required by applicable law or agreed to in writing, software
044 * distributed under the License is distributed on an "AS IS" BASIS,
045 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
046 * See the License for the specific language governing permissions and
047 * limitations under the License.
048 * #L%
049 */
050
051
052// see http://illegalargumentexception.blogspot.com.au/2009/05/java-using-xpath-with-namespaces-and.html
053
054import java.util.Collections;
055import java.util.HashMap;
056import java.util.HashSet;
057import java.util.Iterator;
058import java.util.Map;
059import java.util.Set;
060
061import javax.xml.XMLConstants;
062import javax.xml.namespace.NamespaceContext;
063
064/**
065* An implementation of <a
066* href="http://java.sun.com/javase/6/docs/api/javax/xml/namespace/NamespaceContext.html">
067* NamespaceContext </a>. Instances are immutable.
068* 
069* @author McDowell
070*/
071public final class NamespaceContextMap implements NamespaceContext {
072
073 private final Map<String, String> prefixMap;
074 private final Map<String, Set<String>> nsMap;
075
076 /**
077  * Constructor that takes a map of XML prefix-namespaceURI values. A defensive
078  * copy is made of the map. An IllegalArgumentException will be thrown if the
079  * map attempts to remap the standard prefixes defined in the NamespaceContext
080  * contract.
081  * 
082  * @param prefixMappings
083  *          a map of prefix:namespaceURI values
084  */
085 public NamespaceContextMap(
086     Map<String, String> prefixMappings) {
087   prefixMap = createPrefixMap(prefixMappings);
088   nsMap = createNamespaceMap(prefixMap);
089 }
090
091 /**
092  * Convenience constructor.
093  * 
094  * @param mappingPairs
095  *          pairs of prefix-namespaceURI values
096  */
097 public NamespaceContextMap(String... mappingPairs) {
098   this(toMap(mappingPairs));
099 }
100
101 private static Map<String, String> toMap(
102     String... mappingPairs) {
103   Map<String, String> prefixMappings = new HashMap<String, String>(
104       mappingPairs.length / 2);
105   for (int i = 0; i < mappingPairs.length; i++) {
106     prefixMappings
107         .put(mappingPairs[i], mappingPairs[++i]);
108   }
109   return prefixMappings;
110 }
111
112 private Map<String, String> createPrefixMap(
113     Map<String, String> prefixMappings) {
114   Map<String, String> prefixMap = new HashMap<String, String>(
115       prefixMappings);
116   addConstant(prefixMap, XMLConstants.XML_NS_PREFIX,
117       XMLConstants.XML_NS_URI);
118   addConstant(prefixMap, XMLConstants.XMLNS_ATTRIBUTE,
119       XMLConstants.XMLNS_ATTRIBUTE_NS_URI);
120   return Collections.unmodifiableMap(prefixMap);
121 }
122
123 private void addConstant(Map<String, String> prefixMap,
124     String prefix, String nsURI) {
125   String previous = prefixMap.put(prefix, nsURI);
126   if (previous != null && !previous.equals(nsURI)) {
127     throw new IllegalArgumentException(prefix + " -> "
128         + previous + "; see NamespaceContext contract");
129   }
130 }
131
132 private Map<String, Set<String>> createNamespaceMap(
133     Map<String, String> prefixMap) {
134   Map<String, Set<String>> nsMap = new HashMap<String, Set<String>>();
135   for (Map.Entry<String, String> entry : prefixMap
136       .entrySet()) {
137     String nsURI = entry.getValue();
138     Set<String> prefixes = nsMap.get(nsURI);
139     if (prefixes == null) {
140       prefixes = new HashSet<String>();
141       nsMap.put(nsURI, prefixes);
142     }
143     prefixes.add(entry.getKey());
144   }
145   for (Map.Entry<String, Set<String>> entry : nsMap
146       .entrySet()) {
147     Set<String> readOnly = Collections
148         .unmodifiableSet(entry.getValue());
149     entry.setValue(readOnly);
150   }
151   return nsMap;
152 }
153
154 @Override
155 public String getNamespaceURI(String prefix) {
156   checkNotNull(prefix);
157   String nsURI = prefixMap.get(prefix);
158   return nsURI == null ? XMLConstants.NULL_NS_URI : nsURI;
159 }
160
161 @Override
162 public String getPrefix(String namespaceURI) {
163   checkNotNull(namespaceURI);
164   Set<String> set = nsMap.get(namespaceURI);
165   return set == null ? null : set.iterator().next();
166 }
167
168 @Override
169 public Iterator<String> getPrefixes(String namespaceURI) {
170   checkNotNull(namespaceURI);
171   Set<String> set = nsMap.get(namespaceURI);
172   return set.iterator();
173 }
174
175 private void checkNotNull(String value) {
176   if (value == null) {
177     throw new IllegalArgumentException("null");
178   }
179 }
180
181 /**
182  * @return an unmodifiable map of the mappings in the form prefix-namespaceURI
183  */
184 public Map<String, String> getMap() {
185   return prefixMap;
186 }
187
188}