View Javadoc

1   /*
2    * Copyright 2006 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.xml;
18  
19  import org.springframework.util.ClassUtils;
20  
21  /**
22   * Helper class used to find the current version of JAXP. We cannot depend on the Java version, since JAXP can be
23   * upgraded independently of the Java version.
24   * <p/>
25   * Only distinguishes between JAXP 1.0, 1.1, 1.3, and 1.4, since JAXP 1.2 was a maintenance release with no new
26   * classes.
27   *
28   * @author Arjen Poutsma
29   * @since 1.0.0
30   */
31  public abstract class JaxpVersion {
32  
33      public static final int JAXP_10 = 0;
34  
35      public static final int JAXP_11 = 1;
36  
37      public static final int JAXP_13 = 3;
38  
39      public static final int JAXP_14 = 4;
40  
41      private static final String JAXP_11_CLASS_NAME = "javax.xml.transform.Transformer";
42  
43      private static final String JAXP_13_CLASS_NAME = "javax.xml.xpath.XPath";
44  
45      private static final String JAXP_14_CLASS_NAME = "javax.xml.transform.stax.StAXSource";
46  
47      private static int jaxpVersion = JAXP_10;
48  
49      static {
50          try {
51              ClassUtils.forName(JAXP_14_CLASS_NAME);
52              jaxpVersion = JAXP_14;
53          }
54          catch (ClassNotFoundException ex1) {
55              try {
56                  ClassUtils.forName(JAXP_13_CLASS_NAME);
57                  jaxpVersion = JAXP_13;
58              }
59              catch (ClassNotFoundException ex2) {
60                  try {
61                      ClassUtils.forName(JAXP_11_CLASS_NAME);
62                      jaxpVersion = JAXP_11;
63                  }
64                  catch (ClassNotFoundException ex3) {
65                      // default to JAXP 1.0
66                  }
67              }
68          }
69      }
70  
71      /**
72       * Gets the JAXP version. This means we can do things like if <code>(getJaxpVersion() < JAXP_13)</code>.
73       *
74       * @return a code comparable to the JAXP_XX codes in this class
75       * @see #JAXP_10
76       * @see #JAXP_11
77       * @see #JAXP_13
78       * @see #JAXP_14
79       */
80      public static int getJaxpVersion() {
81          return jaxpVersion;
82      }
83  
84      /**
85       * Convenience method to determine if the current JAXP version is at least 1.3 (packaged with JDK 1.5).
86       *
87       * @return <code>true</code> if the current JAXP version is at least JAXP 1.3
88       * @see #getJaxpVersion()
89       * @see #JAXP_13
90       */
91      public static boolean isAtLeastJaxp13() {
92          return getJaxpVersion() >= JAXP_13;
93      }
94  
95      /**
96       * Convenience method to determine if the current JAXP version is at least 1.4 (packaged with JDK 1.6).
97       *
98       * @return <code>true</code> if the current JAXP version is at least JAXP 1.4
99       * @see #getJaxpVersion()
100      * @see #JAXP_14
101      */
102     public static boolean isAtLeastJaxp14() {
103         return getJaxpVersion() >= JAXP_14;
104     }
105 
106 }