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