| 1 | |
package net.sourceforge.jeuclid.elements.support; |
| 2 | |
|
| 3 | |
import java.util.Collections; |
| 4 | |
import java.util.Iterator; |
| 5 | |
|
| 6 | |
import javax.annotation.Nonnull; |
| 7 | |
import javax.annotation.Nullable; |
| 8 | |
import javax.xml.XMLConstants; |
| 9 | |
import javax.xml.namespace.NamespaceContext; |
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
public class NamespaceContextAdder implements NamespaceContext { |
| 17 | |
|
| 18 | |
private final String namespacePrefix; |
| 19 | |
private final String namespaceURI; |
| 20 | |
private final NamespaceContext delegateContext; |
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
public NamespaceContextAdder(@Nonnull final String ns, |
| 35 | |
@Nonnull final String nsuri, |
| 36 | 16 | @Nullable final NamespaceContext delegate) { |
| 37 | 16 | this.namespacePrefix = ns; |
| 38 | 16 | this.namespaceURI = nsuri; |
| 39 | 16 | this.delegateContext = delegate; |
| 40 | 16 | } |
| 41 | |
|
| 42 | |
|
| 43 | |
public String getNamespaceURI(final String prefix) { |
| 44 | |
String retVal; |
| 45 | 144 | if (this.namespacePrefix.equals(prefix)) { |
| 46 | 144 | retVal = this.namespaceURI; |
| 47 | 0 | } else if (this.delegateContext == null) { |
| 48 | 0 | retVal = XMLConstants.NULL_NS_URI; |
| 49 | |
} else { |
| 50 | 0 | retVal = this.delegateContext.getNamespaceURI(prefix); |
| 51 | |
} |
| 52 | 144 | return retVal; |
| 53 | |
} |
| 54 | |
|
| 55 | |
|
| 56 | |
public String getPrefix(final String uri) { |
| 57 | |
String retVal; |
| 58 | 0 | if (this.namespaceURI.equals(uri)) { |
| 59 | 0 | retVal = this.namespacePrefix; |
| 60 | 0 | } else if (this.delegateContext == null) { |
| 61 | 0 | retVal = ""; |
| 62 | |
} else { |
| 63 | 0 | retVal = this.delegateContext.getPrefix(uri); |
| 64 | |
} |
| 65 | 0 | return retVal; |
| 66 | |
} |
| 67 | |
|
| 68 | |
|
| 69 | |
@SuppressWarnings("unchecked") |
| 70 | |
public Iterator<String> getPrefixes(final String uri) { |
| 71 | |
Iterator<String> retVal; |
| 72 | 0 | if (this.namespaceURI.equals(uri)) { |
| 73 | 0 | retVal = Collections.singleton(this.namespacePrefix).iterator(); |
| 74 | 0 | } else if (this.delegateContext == null) { |
| 75 | 0 | retVal = Collections.EMPTY_LIST.iterator(); |
| 76 | |
} else { |
| 77 | 0 | retVal = this.delegateContext.getPrefixes(uri); |
| 78 | |
} |
| 79 | 0 | return retVal; |
| 80 | |
} |
| 81 | |
|
| 82 | |
} |