View Javadoc

1   /*
2    * Copyright 2005-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.ws.server.endpoint.support;
18  
19  import java.lang.reflect.AnnotatedElement;
20  import java.lang.reflect.Method;
21  import javax.xml.namespace.NamespaceContext;
22  
23  import org.springframework.util.Assert;
24  import org.springframework.ws.server.endpoint.annotation.Namespace;
25  import org.springframework.ws.server.endpoint.annotation.Namespaces;
26  import org.springframework.xml.namespace.SimpleNamespaceContext;
27  
28  /**
29   * Helper class for handling [email protected] Namespace @Namespace} annotations.
30   *
31   * @author Arjen Poutsma
32   * @since 2.0
33   */
34  public abstract class NamespaceUtils {
35  
36      private NamespaceUtils() {
37      }
38  
39      /**
40       * Creates a [email protected] NamespaceContext} for the specified method, based on [email protected] Namespaces @Namespaces} and [email protected]
41       * Namespace @Namespace} annotations.
42       * <p/>
43       * This method will search for [email protected] Namespaces @Namespaces} and [email protected] Namespace @Namespace} annotation in the
44       * given method, its class, and its package, in reverse order. That is: package-level annotations are overridden by
45       * class-level annotations, which again are overridden by method-level annotations.
46       *
47       * @param method the method to create the namespace context for
48       * @return the namespace context
49       */
50      public static NamespaceContext getNamespaceContext(Method method) {
51          Assert.notNull(method, "'method' must not be null");
52          SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext();
53          Class<?> endpointClass = method.getDeclaringClass();
54          Package endpointPackage = endpointClass.getPackage();
55          if (endpointPackage != null) {
56              addNamespaceAnnotations(endpointPackage, namespaceContext);
57          }
58          addNamespaceAnnotations(endpointClass, namespaceContext);
59          addNamespaceAnnotations(method, namespaceContext);
60          return namespaceContext;
61      }
62  
63      private static void addNamespaceAnnotations(AnnotatedElement annotatedElement,
64                                                  SimpleNamespaceContext namespaceContext) {
65          if (annotatedElement.isAnnotationPresent(Namespaces.class)) {
66              Namespaces namespacesAnn = annotatedElement.getAnnotation(Namespaces.class);
67              for (Namespace namespaceAnn : namespacesAnn.value()) {
68                  namespaceContext.bindNamespaceUri(namespaceAnn.prefix(), namespaceAnn.uri());
69              }
70          }
71          if (annotatedElement.isAnnotationPresent(Namespace.class)) {
72              Namespace namespaceAnn = annotatedElement.getAnnotation(Namespace.class);
73              namespaceContext.bindNamespaceUri(namespaceAnn.prefix(), namespaceAnn.uri());
74          }
75      }
76  
77  }