001/*-------------------------------------------------------------------------+
002|                                                                          |
003| Copyright 2005-2011 The ConQAT Project                                   |
004|                                                                          |
005| Licensed under the Apache License, Version 2.0 (the "License");          |
006| you may not use this file except in compliance with the License.         |
007| You may obtain a copy of the License at                                  |
008|                                                                          |
009|    http://www.apache.org/licenses/LICENSE-2.0                            |
010|                                                                          |
011| Unless required by applicable law or agreed to in writing, software      |
012| distributed under the License is distributed on an "AS IS" BASIS,        |
013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
014| See the License for the specific language governing permissions and      |
015| limitations under the License.                                           |
016+-------------------------------------------------------------------------*/
017package org.conqat.lib.commons.xml;
018
019import java.util.Iterator;
020
021import javax.xml.namespace.NamespaceContext;
022import javax.xml.xpath.XPath;
023
024/**
025 * A minimalistic implementation of {@link NamespaceContext} to be used with
026 * {@link XPath}. Method {@link #getNamespaceURI(String)} always returns the
027 * string provided to the constructor. All other methods throw
028 * {@link UnsupportedOperationException}s. These methods are not needed for
029 * XPath resolution.
030 * <p>
031 * Implementation is inspired by snippet on
032 * <a href="http://www.ibm.com/developerworks/library/x-javaxpathapi.html"
033 * >http://www.ibm.com/developerworks/library/x-javaxpathapi.html</a>.
034 * 
035 * @author deissenb
036 */
037public class ConstantNamespaceContext implements NamespaceContext {
038
039        /** The URI */
040        private final String namesspaceURI;
041
042        /** Create new context. */
043        public ConstantNamespaceContext(String namesspaceURI) {
044                this.namesspaceURI = namesspaceURI;
045        }
046
047        /**
048         * Always returns the string provided to the constructor.
049         */
050        @Override
051        public String getNamespaceURI(String prefix) {
052                return namesspaceURI;
053        }
054
055        /**
056         * Throws {@link UnsupportedOperationException}. This method isn't necessary
057         * for XPath processing.
058         */
059        @Override
060        public String getPrefix(String uri) {
061                throw new UnsupportedOperationException();
062        }
063
064        /**
065         * Throws {@link UnsupportedOperationException}. This method isn't necessary
066         * for XPath processing.
067         */
068        @Override
069        @SuppressWarnings("rawtypes")
070        public Iterator getPrefixes(String uri) {
071                throw new UnsupportedOperationException();
072        }
073
074}