001package ezvcard.io.xml;
002
003import java.util.Collections;
004import java.util.Iterator;
005
006import javax.xml.namespace.NamespaceContext;
007import javax.xml.xpath.XPath;
008
009import ezvcard.VCardVersion;
010
011/**
012 * Used for xCard XPath expressions.
013 * @see XPath#setNamespaceContext(NamespaceContext)
014 * @author Michael Angstadt
015 */
016public class XCardNamespaceContext implements NamespaceContext {
017        private final String ns;
018        private final String prefix;
019
020        /**
021         * @param version the vCard version to use
022         * @param prefix the prefix to use
023         */
024        public XCardNamespaceContext(VCardVersion version, String prefix) {
025                this.ns = version.getXmlNamespace();
026                this.prefix = prefix;
027        }
028
029        /**
030         * Gets the prefix to use in xpath expressions.
031         * @return the xpath prefix
032         */
033        public String getPrefix() {
034                return prefix;
035        }
036
037        //@Override
038        public String getNamespaceURI(String prefix) {
039                if (this.prefix.equals(prefix)) {
040                        return ns;
041                }
042                return null;
043        }
044
045        //@Override
046        public String getPrefix(String ns) {
047                if (this.ns.equals(ns)) {
048                        return prefix;
049                }
050                return null;
051        }
052
053        //@Override
054        public Iterator<String> getPrefixes(String ns) {
055                if (this.ns.equals(ns)) {
056                        return Collections.singletonList(prefix).iterator();
057                }
058                return null;
059        }
060}