View Javadoc

1   /*
2    * Copyright 2005 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  package org.springframework.oxm.jaxb;
17  
18  import javax.xml.bind.JAXBException;
19  import javax.xml.bind.MarshalException;
20  import javax.xml.bind.UnmarshalException;
21  import javax.xml.bind.ValidationException;
22  
23  import org.springframework.oxm.XmlMappingException;
24  import org.springframework.util.ClassUtils;
25  
26  /**
27   * Generic utility methods for working with JAXB. Mainly for internal use within the framework.
28   *
29   * @author Arjen Poutsma
30   * @since 1.0.0
31   */
32  public abstract class JaxbUtils {
33  
34      public static final int JAXB_1 = 0;
35  
36      public static final int JAXB_2 = 1;
37  
38      private static final String JAXB_2_CLASS_NAME = "javax.xml.bind.Binder";
39  
40      private static int jaxbVersion = JAXB_1;
41  
42      static {
43          try {
44              ClassUtils.forName(JAXB_2_CLASS_NAME);
45              jaxbVersion = JAXB_2;
46          }
47          catch (ClassNotFoundException ex) {
48              // leave JAXB 1 as default
49          }
50      }
51  
52      /**
53       * Gets the major JAXB version. This means we can do things like if <code>(getJaxbVersion() &lt;= JAXB_2)</code>.
54       *
55       * @return a code comparable to the JAXP_XX codes in this class
56       * @see #JAXB_1
57       * @see #JAXB_2
58       */
59      public static int getJaxbVersion() {
60          return jaxbVersion;
61      }
62  
63      /**
64       * Gets the major JAXB version. This means we can do things like if <code>(getJaxbVersion() &lt;= JAXB_2)</code>.
65       *
66       * @return a code comparable to the JAXP_XX codes in this class
67       * @see #JAXB_1
68       * @see #JAXB_2
69       */
70      public static int getJaxbVersion(ClassLoader classLoader) {
71          try {
72              ClassUtils.forName(JAXB_2_CLASS_NAME, classLoader);
73              return JAXB_2;
74          }
75          catch (ClassNotFoundException ex) {
76              return JAXB_1;
77          }
78      }
79  
80      /**
81       * Converts the given <code>JAXBException</code> to an appropriate exception from the
82       * <code>org.springframework.oxm</code> hierarchy.
83       *
84       * @param ex <code>JAXBException</code> that occured
85       * @return the corresponding <code>XmlMappingException</code>
86       */
87      public static XmlMappingException convertJaxbException(JAXBException ex) {
88          if (ex instanceof MarshalException) {
89              return new JaxbMarshallingFailureException((MarshalException) ex);
90          }
91          else if (ex instanceof UnmarshalException) {
92              return new JaxbUnmarshallingFailureException((UnmarshalException) ex);
93          }
94          else if (ex instanceof ValidationException) {
95              return new JaxbValidationFailureException((ValidationException) ex);
96          }
97          // fallback
98          return new JaxbSystemException(ex);
99      }
100 
101 }