|
16 | 16 |
|
17 | 17 | package org.springframework.batch.item.xml;
|
18 | 18 |
|
19 |
| -import java.lang.reflect.Constructor; |
20 |
| -import java.lang.reflect.Method; |
21 | 19 | import javax.xml.stream.XMLEventReader;
|
22 | 20 | import javax.xml.stream.XMLEventWriter;
|
| 21 | +import javax.xml.stream.XMLStreamException; |
23 | 22 | import javax.xml.transform.Result;
|
24 | 23 | import javax.xml.transform.Source;
|
| 24 | +import javax.xml.transform.stax.StAXResult; |
| 25 | +import javax.xml.transform.stax.StAXSource; |
25 | 26 |
|
26 |
| -import org.apache.commons.logging.Log; |
27 |
| -import org.apache.commons.logging.LogFactory; |
28 |
| - |
29 |
| -import org.springframework.util.Assert; |
30 |
| -import org.springframework.util.ClassUtils; |
31 | 27 |
|
32 | 28 | /**
|
33 |
| - * This class provides a little bit of indirection to avoid ugly conditional object creation. It is unfortunately |
34 |
| - * a bit redundant assuming a Spring 3.0 environment, but is necessary to work with Spring WS 1.5.x. |
35 |
| - * <br> |
36 |
| - * The returned object determines whether the environment has Spring OXM as included in the Spring 3.x series of relies |
37 |
| - * or whether it has Spring OXM from Spring WS 1.5x and factories a StaxSource instance appropriately. |
| 29 | + * StAX utility methods. |
38 | 30 | * <br>
|
39 |
| - * As the only class state maintained is to cache java reflection metadata, which is thread-safe, this class is thread-safe. |
| 31 | + * This class is thread-safe. |
40 | 32 | *
|
41 | 33 | * @author Josh Long
|
42 | 34 | *
|
43 | 35 | */
|
44 | 36 | public abstract class StaxUtils {
|
45 | 37 |
|
46 |
| - private static final Log logger = LogFactory.getLog(StaxUtils.class); |
47 |
| - |
48 |
| - private static ClassLoader defaultClassLoader = ClassUtils.getDefaultClassLoader(); |
49 |
| - |
50 |
| - // regular object. |
51 |
| - private static String staxSourceClassNameOnSpringWs15 = "org.springframework.xml.transform.StaxSource"; |
52 |
| - private static String staxResultClassNameOnSpringOxm15 = "org.springframework.xml.transform.StaxResult"; |
53 |
| - |
54 |
| - // in Spring 3, StaxUtils is package private, so use static utility StaxUtils#createStaxSource / StaxUtils#createStaxResult |
55 |
| - private static String staxSourceClassNameOnSpringOxm30 = "org.springframework.util.xml.StaxUtils"; |
56 |
| - |
57 |
| - private static boolean hasSpringWs15StaxSupport = ClassUtils.isPresent(staxSourceClassNameOnSpringWs15, defaultClassLoader); |
58 |
| - |
59 |
| - private static boolean hasSpring30StaxSupport = ClassUtils.isPresent(staxSourceClassNameOnSpringOxm30, defaultClassLoader); |
60 |
| - |
61 |
| - private static Method staxUtilsSourceMethodOnSpring30, staxUtilsResultMethodOnSpring30; |
62 |
| - |
63 |
| - private static Constructor<?> staxSourceClassCtorOnSpringWs15, staxResultClassCtorOnSpringWs15; |
64 |
| - |
65 |
| - static { |
66 |
| - try { |
67 |
| - |
68 |
| - // cache the factory method / constructor so that we spend as little time in reflection as possible |
69 |
| - if (hasSpring30StaxSupport) { |
70 |
| - Class<?> clzz = ClassUtils.forName(staxSourceClassNameOnSpringOxm30, defaultClassLoader); |
71 |
| - |
72 |
| - // javax.xml.transform.Source |
73 |
| - staxUtilsSourceMethodOnSpring30 = ClassUtils.getStaticMethod(clzz, "createStaxSource", XMLEventReader.class); |
74 |
| - |
75 |
| - // javax.xml.transform.Result |
76 |
| - staxUtilsResultMethodOnSpring30 = ClassUtils.getStaticMethod(clzz, "createStaxResult", XMLEventWriter.class); |
77 |
| - } else if (hasSpringWs15StaxSupport) { |
78 |
| - |
79 |
| - // javax.xml.transform.Source |
80 |
| - Class<?> staxSourceClassOnSpringWs15 = ClassUtils.forName(staxSourceClassNameOnSpringWs15, defaultClassLoader); |
81 |
| - staxSourceClassCtorOnSpringWs15 = staxSourceClassOnSpringWs15.getConstructor(XMLEventReader.class); |
82 |
| - |
83 |
| - // javax.xml.transform.Result |
84 |
| - Class<?> staxResultClassOnSpringWs15 = ClassUtils.forName(staxResultClassNameOnSpringOxm15, defaultClassLoader); |
85 |
| - staxResultClassCtorOnSpringWs15 = staxResultClassOnSpringWs15.getConstructor(XMLEventWriter.class); |
86 |
| - } else { |
87 |
| - |
88 |
| - if (logger.isDebugEnabled()) { |
89 |
| - logger.debug("'StaxSource' was not detected in Spring 3.0's OXM support or Spring WS 1.5's OXM support. " + |
90 |
| - "This is a problem if you intend to use the " +StaxEventItemWriter.class.getName() + " or " + |
91 |
| - StaxEventItemReader.class.getName()+". Please add the appropriate dependencies."); |
92 |
| - } |
93 |
| - |
94 |
| - } |
95 |
| - } catch (Exception ex) { |
96 |
| - logger.error("Could not precache required class and method metadata in " + StaxUtils.class.getName()); |
97 |
| - } |
98 |
| - } |
99 |
| - |
100 |
| - public static Source getSource(XMLEventReader r) throws Exception { |
101 |
| - if (hasSpring30StaxSupport) { |
102 |
| - // org.springframework.util.xml.StaxUtils.createStaxSource(r) |
103 |
| - Object result = staxUtilsSourceMethodOnSpring30.invoke(null,r); |
104 |
| - Assert.isInstanceOf(Source.class, result, "the result should be assignable to " + Source.class.getName()); |
105 |
| - return (Source) result; |
106 |
| - } else if (hasSpringWs15StaxSupport) { |
107 |
| - Object result = staxSourceClassCtorOnSpringWs15.newInstance(r); |
108 |
| - Assert.isInstanceOf(Source.class, result, "the result should be assignable to " + Source.class.getName()); |
109 |
| - return (Source) result; |
110 |
| - } |
111 |
| - // maybe you don't have either environment? |
112 |
| - return null; |
| 38 | + public static Source getSource(XMLEventReader r) throws XMLStreamException { |
| 39 | + return new StAXSource(r); |
113 | 40 | }
|
114 | 41 |
|
115 |
| - public static Result getResult(XMLEventWriter w) throws Exception { |
116 |
| - if (hasSpring30StaxSupport) { |
117 |
| - Object result = staxUtilsResultMethodOnSpring30.invoke(null,w); |
118 |
| - Assert.isInstanceOf(Result.class, result, "the result should be assignable to " + Result.class.getName()); |
119 |
| - return (Result) result; |
120 |
| - } else if (hasSpringWs15StaxSupport) { |
121 |
| - Object result = staxResultClassCtorOnSpringWs15.newInstance(w); |
122 |
| - Assert.isInstanceOf(Result.class, result, "the result should be assignable to " + Result.class.getName()); |
123 |
| - return (Result) result; |
124 |
| - } |
125 |
| - // maybe you don't have either environment? |
126 |
| - return null; |
| 42 | + public static Result getResult(XMLEventWriter w) { |
| 43 | + return new StAXResult(w); |
127 | 44 | }
|
128 |
| - |
129 |
| - public static XMLEventWriter getXmlEventWriter(Result r) throws Exception { |
130 |
| - Method m = r.getClass().getDeclaredMethod("getXMLEventWriter"); |
131 |
| - boolean accessible = m.isAccessible(); |
132 |
| - m.setAccessible(true); |
133 |
| - Object result = m.invoke(r); |
134 |
| - m.setAccessible(accessible); |
135 |
| - return (XMLEventWriter) result; |
136 |
| - } |
137 |
| - |
138 |
| - public static XMLEventReader getXmlEventReader(Source s) throws Exception { |
139 |
| - Method m = s.getClass().getDeclaredMethod("getXMLEventReader"); |
140 |
| - boolean accessible = m.isAccessible(); |
141 |
| - m.setAccessible(true); |
142 |
| - Object result = m.invoke(s); |
143 |
| - m.setAccessible(accessible); |
144 |
| - return (XMLEventReader) result; |
145 |
| - } |
146 | 45 | }
|
0 commit comments