
com.cloudseal.client.saml2.NamespaceContextMap Maven / Gradle / Ivy
/* Copyright 2012 Cloudseal Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.cloudseal.client.saml2;
import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;
import java.util.*;
public class NamespaceContextMap implements NamespaceContext {
private final Map prefixMap;
private final Map> nsMap;
public NamespaceContextMap(
Map prefixMappings) {
prefixMap = createPrefixMap(prefixMappings);
nsMap = createNamespaceMap(prefixMap);
}
/**
* Convenience constructor.
*
* @param mappingPairs
* pairs of prefix-namespaceURI values
*/
public NamespaceContextMap(String... mappingPairs) {
this(toMap(mappingPairs));
}
private static Map toMap(
String... mappingPairs) {
Map prefixMappings = new HashMap(
mappingPairs.length / 2);
for (int i = 0; i < mappingPairs.length; i++) {
prefixMappings
.put(mappingPairs[i], mappingPairs[++i]);
}
return prefixMappings;
}
private Map createPrefixMap(
Map prefixMappings) {
Map prefixMap = new HashMap(
prefixMappings);
addConstant(prefixMap, XMLConstants.XML_NS_PREFIX,
XMLConstants.XML_NS_URI);
addConstant(prefixMap, XMLConstants.XMLNS_ATTRIBUTE,
XMLConstants.XMLNS_ATTRIBUTE_NS_URI);
return Collections.unmodifiableMap(prefixMap);
}
private void addConstant(Map prefixMap,
String prefix, String nsURI) {
String previous = prefixMap.put(prefix, nsURI);
if (previous != null && !previous.equals(nsURI)) {
throw new IllegalArgumentException(prefix + " -> "
+ previous + "; see NamespaceContext contract");
}
}
private Map> createNamespaceMap(
Map prefixMap) {
Map> nsMap = new HashMap>();
for (Map.Entry entry : prefixMap
.entrySet()) {
String nsURI = entry.getValue();
Set prefixes = nsMap.get(nsURI);
if (prefixes == null) {
prefixes = new HashSet();
nsMap.put(nsURI, prefixes);
}
prefixes.add(entry.getKey());
}
for (Map.Entry> entry : nsMap
.entrySet()) {
Set readOnly = Collections
.unmodifiableSet(entry.getValue());
entry.setValue(readOnly);
}
return nsMap;
}
@Override
public String getNamespaceURI(String prefix) {
checkNotNull(prefix);
String nsURI = prefixMap.get(prefix);
return nsURI == null ? XMLConstants.NULL_NS_URI : nsURI;
}
@Override
public String getPrefix(String namespaceURI) {
checkNotNull(namespaceURI);
Set set = nsMap.get(namespaceURI);
return set == null ? null : set.iterator().next();
}
@Override
public Iterator getPrefixes(String namespaceURI) {
checkNotNull(namespaceURI);
Set set = nsMap.get(namespaceURI);
return set.iterator();
}
private void checkNotNull(String value) {
if (value == null) {
throw new IllegalArgumentException("null");
}
}
/**
* @return an unmodifiable map of the mappings in the form prefix-namespaceURI
*/
public Map getMap() {
return prefixMap;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy