01: /**
02: * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
03: * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
04: */package com.sun.xml.stream.xerces.util;
05:
06: import java.util.Enumeration;
07: import java.util.Vector;
08: import javax.xml.namespace.NamespaceContext;
09:
10: /**
11: * Writing a wrapper to re-use most of the namespace functionality
12: * already provided by NamespaceSupport, which implements NamespaceContext
13: * from XNI. It would be good if we can change the XNI NamespaceContext
14: * interface to implement the JAXP NamespaceContext interface.
15: *
16: * Note that NamespaceSupport assumes the use of symbols. Since this class
17: * can be exposed to the application, we must intern all Strings before
18: * calling NamespaceSupport methods.
19: *
20: * @author Neeraj Bajaj, Sun Microsystems, inc.
21: * @author Santiago.PericasGeertsen@sun.com
22: *
23: */
24: public class NamespaceContextWrapper implements NamespaceContext {
25:
26: private com.sun.xml.stream.xerces.xni.NamespaceContext fNamespaceContext;
27:
28: public NamespaceContextWrapper(
29: com.sun.xml.stream.xerces.xni.NamespaceContext namespaceContext) {
30: fNamespaceContext = namespaceContext;
31: }
32:
33: public String getNamespaceURI(String prefix) {
34: if (prefix == null) {
35: throw new IllegalArgumentException("Prefix can't be null");
36: }
37: return fNamespaceContext.getURI(prefix.intern());
38: }
39:
40: public String getPrefix(String namespaceURI) {
41: if (namespaceURI == null || namespaceURI.trim().length() == 0) {
42: throw new IllegalArgumentException(
43: "URI can't be null or empty String");
44: }
45: return fNamespaceContext.getPrefix(namespaceURI.intern());
46: }
47:
48: /**
49: * TODO: Namespace doesn't give information giving multiple prefixes for
50: * the same namespaceURI.
51: */
52: public java.util.Iterator getPrefixes(String namespaceURI) {
53: if (namespaceURI == null || namespaceURI.trim().length() == 0) {
54: throw new IllegalArgumentException(
55: "URI can't be null or empty String");
56: } else {
57: Vector vector = ((NamespaceSupport) fNamespaceContext)
58: .getPrefixes(namespaceURI.intern());
59: return vector.iterator();
60: }
61: }
62:
63: /**
64: * This method supports all functions in the NamespaceContext utility class
65: */
66: public com.sun.xml.stream.xerces.xni.NamespaceContext getNamespaceContext() {
67: return fNamespaceContext;
68: }
69: }
|