001: /*
002: ******************************************************************
003: Copyright (c) 2006-2007, Jeff Martin, Tim Bacon
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009:
010: * Redistributions of source code must retain the above copyright
011: notice, this list of conditions and the following disclaimer.
012: * Redistributions in binary form must reproduce the above
013: copyright notice, this list of conditions and the following
014: disclaimer in the documentation and/or other materials provided
015: with the distribution.
016: * Neither the name of the xmlunit.sourceforge.net nor the names
017: of its contributors may be used to endorse or promote products
018: derived from this software without specific prior written
019: permission.
020:
021: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
022: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
023: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
024: FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
025: COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
026: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
027: BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
028: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
029: CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
030: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
031: ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
032: POSSIBILITY OF SUCH DAMAGE.
033:
034: ******************************************************************
035: */
036:
037: package org.custommonkey.xmlunit.jaxp13;
038:
039: import org.custommonkey.xmlunit.NamespaceContext;
040:
041: import java.util.HashMap;
042: import java.util.Iterator;
043: import java.util.Map;
044: import java.util.TreeSet;
045: import javax.xml.XMLConstants;
046:
047: /**
048: * Adapts {@link NamespaceContext XMLUnit's NamespaceContext} to
049: * {@link javax.xml.namespace.NamespaceContext JAXP 1.3's
050: * NamespaceContext}.
051: */
052: public class XMLUnitNamespaceContext2Jaxp13 implements
053: javax.xml.namespace.NamespaceContext {
054:
055: private final Map/*<String, String>*/nsMap;
056:
057: public XMLUnitNamespaceContext2Jaxp13(NamespaceContext ctx) {
058: nsMap = turnIntoMap(ctx);
059: }
060:
061: public String getNamespaceURI(String prefix) {
062: if (prefix == null) {
063: throw new IllegalArgumentException(
064: "prefix must not be null");
065: }
066: String uri = (String) nsMap.get(prefix);
067: if (uri == null) {
068: uri = XMLConstants.NULL_NS_URI;
069: }
070: return uri;
071: }
072:
073: public Iterator getPrefixes(String uri) {
074: if (uri == null) {
075: throw new IllegalArgumentException("uri must not be null");
076: }
077:
078: // ensure that the empty string comes out first when asked for
079: // the default namespace URI's prefix
080: TreeSet/*<String>*/ts = new TreeSet();
081: for (Iterator it = nsMap.entrySet().iterator(); it.hasNext();) {
082: Map.Entry/*<String,String>*/entry = (Map.Entry) it.next();
083: if (uri.equals(entry.getValue())) {
084: ts.add(entry.getKey());
085: }
086: }
087: return ts.iterator();
088: }
089:
090: public String getPrefix(String uri) {
091: Iterator i = getPrefixes(uri);
092: return i.hasNext() ? (String) i.next() : null;
093: }
094:
095: private static Map turnIntoMap(NamespaceContext ctx) {
096: HashMap/*<String, String>*/m = new HashMap();
097: for (Iterator i = ctx.getPrefixes(); i.hasNext();) {
098: String prefix = (String) i.next();
099: String uri = ctx.getNamespaceURI(prefix);
100: // according to the Javadocs only the constants defined in
101: // XMLConstants are allowed as prefixes for the following
102: // two URIs
103: if (!XMLConstants.XML_NS_URI.equals(uri)
104: && !XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(uri)) {
105: m.put(prefix, uri);
106: }
107: }
108: m.put(XMLConstants.XML_NS_PREFIX, XMLConstants.XML_NS_URI);
109: m.put(XMLConstants.XMLNS_ATTRIBUTE,
110: XMLConstants.XMLNS_ATTRIBUTE_NS_URI);
111: return m;
112: }
113: }
|