View Javadoc

1   /*
2      Licensed under the Apache License, Version 2.0 (the "License");
3      you may not use this file except in compliance with the License.
4      You may obtain a copy of the License at
5   
6        http://www.apache.org/licenses/LICENSE-2.0
7   
8      Unless required by applicable law or agreed to in writing, software
9      distributed under the License is distributed on an "AS IS" BASIS,
10     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11     See the License for the specific language governing permissions and
12     limitations under the License.
13  */
14  package uk.nhs.interoperability.util.xml;
15  
16  import java.util.ArrayList;
17  import java.util.HashMap;
18  import java.util.Iterator;
19  import java.util.List;
20  import java.util.Map;
21  
22  import javax.xml.namespace.NamespaceContext;
23  
24  import uk.nhs.interoperability.util.Logger;
25  
26  /**
27   * The Class XMLNamespaceContext.
28   *
29   * @author Michael Odling-Smee
30   * @author Nicholas Jones
31   * @since 0.1
32   */
33  public class XMLNamespaceContext implements NamespaceContext {
34  	
35  	/** The Constant HL7NAMESPACE. */
36  	public static final String HL7NAMESPACE = "urn:hl7-org:v3";
37  	
38  	/** The Constant HL7NAMESPACE_DEFAULY_PREFIX. */
39  	public static final String HL7NAMESPACE_DEFAULY_PREFIX = "hl7";
40      
41      /** The Constant SOAPENVNAMESPACE. */
42      public static final String SOAPENVNAMESPACE = "http://schemas.xmlsoap.org/soap/envelope/";
43      
44      /** The Constant SOAPENVNAMESPACE_DEFAULT_PREFIX. */
45      public static final String SOAPENVNAMESPACE_DEFAULT_PREFIX = "SOAP";
46      
47      /** The Constant HL7V2NAMESPACE. */
48      public static final String HL7V2NAMESPACE = "urn:hl7-org:v2xml";
49      
50      /** The Constant HL7V2NAMESPACE_DEFAULT_PREFIX. */
51      public static final String HL7V2NAMESPACE_DEFAULT_PREFIX = "hl7v2";
52      
53      /** The Constant ITKNAMESPACE. */
54      public static final String ITKNAMESPACE = "urn:nhs-itk:ns:201005";
55      
56      /** The Constant ITKNAMESPACE_DEFAULT_PREFIX. */
57      public static final String ITKNAMESPACE_DEFAULT_PREFIX = "itk";
58      
59      /** The Constant WSANAMESPACE. */
60      public static final String WSANAMESPACE = "http://www.w3.org/2005/08/addressing";
61      
62      /** The Constant WSANAMESPACE_DEFAULT_PREFIX. */
63      public static final String WSANAMESPACE_DEFAULT_PREFIX = "wsa";
64      
65      /** The Constant WSSENAMESPACE. */
66      public static final String WSSENAMESPACE = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
67      
68      /** The Constant WSSENAMESPACE_DEFAULT_PREFIX. */
69      public static final String WSSENAMESPACE_DEFAULT_PREFIX = "wsse";
70      
71      /** The Constant WSUNAMESPACE. */
72      public static final String WSUNAMESPACE = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd";
73      
74      /** The Constant WSUNAMESPACE_DEFAULT_PREFIX. */
75      public static final String WSUNAMESPACE_DEFAULT_PREFIX = "wsu";
76      
77      /** The namespaces. */
78      public static Map<String, String> namespaces = new HashMap<String, String>();
79      
80      static {
81      	namespaces.put(HL7NAMESPACE_DEFAULY_PREFIX, HL7NAMESPACE);
82      	namespaces.put(SOAPENVNAMESPACE_DEFAULT_PREFIX, SOAPENVNAMESPACE);
83      	namespaces.put(HL7V2NAMESPACE_DEFAULT_PREFIX, HL7V2NAMESPACE);
84      	namespaces.put(ITKNAMESPACE_DEFAULT_PREFIX, ITKNAMESPACE);
85      	namespaces.put(WSANAMESPACE_DEFAULT_PREFIX, WSANAMESPACE);
86      	namespaces.put(WSSENAMESPACE_DEFAULT_PREFIX, WSSENAMESPACE);
87      	namespaces.put(WSUNAMESPACE_DEFAULT_PREFIX, WSUNAMESPACE);
88      	
89      }
90  
91  	/* (non-Javadoc)
92  	 * @see javax.xml.namespace.NamespaceContext#getNamespaceURI(java.lang.String)
93  	 */
94  	@Override
95  	public String getNamespaceURI(String prefix) {
96  		return namespaces.get(prefix);
97  	}
98  
99  	/* (non-Javadoc)
100 	 * @see javax.xml.namespace.NamespaceContext#getPrefix(java.lang.String)
101 	 */
102 	@Override
103 	public String getPrefix(String namespaceURI) {
104 		for (Map.Entry<String, String> entry : namespaces.entrySet()) {
105 			if (entry.getValue().equals(namespaceURI)) {
106 				return entry.getKey();
107 			}
108 		}
109 		Logger.warn("No prefix found for namespace: " + namespaceURI);
110 		return null;
111 	}
112 
113 	/* (non-Javadoc)
114 	 * @see javax.xml.namespace.NamespaceContext#getPrefixes(java.lang.String)
115 	 */
116 	@SuppressWarnings("rawtypes")
117 	@Override
118 	public Iterator getPrefixes(String namespaceURI) {
119 		List<String> prefixes = new ArrayList<String>();
120 		for (Map.Entry<String, String> entry : namespaces.entrySet()) {
121 			if (entry.getValue().equals(namespaceURI)) {
122 				prefixes.add(entry.getKey());
123 			}
124 		}
125 		return prefixes.iterator();
126 	}
127 	
128 	
129 
130 }