1010import java .io .StringReader ;
1111import java .io .StringWriter ;
1212import java .util .Optional ;
13- import jakarta .xml .bind .JAXBContext ;
14- import jakarta .xml .bind .JAXBElement ;
15- import jakarta .xml .bind .JAXBException ;
16- import jakarta .xml .bind .Marshaller ;
17- import jakarta .xml .bind .Unmarshaller ;
13+ import javax .xml .bind .JAXBContext ;
14+ import javax .xml .bind .JAXBElement ;
15+ import javax .xml .bind .JAXBException ;
16+ import javax .xml .bind .Marshaller ;
17+ import javax .xml .bind .Unmarshaller ;
1818import javax .xml .namespace .QName ;
1919import javax .xml .parsers .DocumentBuilder ;
2020import javax .xml .parsers .DocumentBuilderFactory ;
2121import javax .xml .parsers .ParserConfigurationException ;
2222import org .apache .commons .io .FileUtils ;
23- import org .w3c .dom .Document ;
24- import org .w3c .dom .Node ;
2523import org .apache .logging .log4j .LogManager ;
2624import org .apache .logging .log4j .Logger ;
25+ import org .w3c .dom .Document ;
26+ import org .w3c .dom .Node ;
2727
2828/**
2929 * A singleton to load the very expensive JAXBContext once.
@@ -191,18 +191,23 @@ public String jaxb2XmlFormatter(JAXBElement<?> jaxbElement) throws JAXBException
191191 * @throws JAXBException Could not parse the specified XML document.
192192 */
193193 public <T extends Object > T xml2Jaxb (Class <T > xmlClass , String xml ) throws JAXBException , IllegalArgumentException {
194- Optional <JAXBElement < T >> element ;
194+ Optional <JAXBElement > element = Optional . empty () ;
195195 try (StringReader reader = new StringReader (xml )) {
196- element = Optional .ofNullable ((JAXBElement <T >) unmarshaller ().unmarshal (reader ));
196+ Object unmarshalled = unmarshaller ().unmarshal (reader );
197+ if (unmarshalled instanceof JAXBElement ) {
198+ element = Optional .ofNullable ((JAXBElement ) unmarshalled );
199+ }
197200 }
198201
199202 if (!element .isPresent ()) {
200- throw new IllegalArgumentException ("Unable to convert string to JAXB, class=" + xmlClass .getName () + ", xml=\n " + xml );
203+ throw new IllegalArgumentException ("Unable to convert string to JAXB, class="
204+ + xmlClass .getName () + ", xml=\n " + xml );
201205 } else if (element .get ().getDeclaredType () == xmlClass ) {
202206 return xmlClass .cast (element .get ().getValue ());
203207 }
204208
205- throw new JAXBException ("Expected XML for class " + xmlClass .getCanonicalName () + " but found " + element .get ().getDeclaredType ().getCanonicalName ());
209+ throw new JAXBException ("Expected XML for class " + xmlClass .getCanonicalName () + " but found "
210+ + element .get ().getDeclaredType ().getCanonicalName ());
206211 }
207212
208213 /**
@@ -216,14 +221,20 @@ public <T extends Object> T xml2Jaxb(Class<T> xmlClass, String xml) throws JAXBE
216221 * @throws IOException InputStream could not be read.
217222 */
218223 public <T extends Object > T xml2Jaxb (Class <T > xmlClass , InputStream is ) throws JAXBException , IOException {
219- JAXBElement <T > element = (JAXBElement <T >) unmarshaller ().unmarshal (is );
220- if (element == null ) {
224+ Optional <JAXBElement > element = Optional .empty ();
225+ Object unmarshalled = unmarshaller ().unmarshal (is );
226+ if (unmarshalled instanceof JAXBElement ) {
227+ element = Optional .ofNullable ((JAXBElement ) unmarshalled );
228+ }
229+
230+ if (!element .isPresent ()) {
221231 throw new IllegalArgumentException ("Unable to convert stream to JAXB, class=" + xmlClass .getName ());
222- } else if (element .getDeclaredType () == xmlClass ) {
223- return xmlClass .cast (element .getValue ());
232+ } else if (element .get (). getDeclaredType () == xmlClass ) {
233+ return xmlClass .cast (element .get (). getValue ());
224234 }
225235
226- throw new JAXBException ("Expected XML for class " + xmlClass .getCanonicalName () + " but found " + element .getDeclaredType ().getCanonicalName ());
236+ throw new JAXBException ("Expected XML for class " + xmlClass .getCanonicalName () + " but found "
237+ + element .get ().getDeclaredType ().getCanonicalName ());
227238 }
228239
229240 /**
@@ -237,22 +248,31 @@ public <T extends Object> T xml2Jaxb(Class<T> xmlClass, InputStream is) throws J
237248 * @throws IOException BufferedInputStream could not be read.
238249 */
239250 public <T extends Object > T xml2Jaxb (Class <T > xmlClass , BufferedInputStream is ) throws JAXBException , IOException {
240- JAXBElement <T > element = (JAXBElement <T >) unmarshaller ().unmarshal (is );
241- if (element .getDeclaredType () == xmlClass ) {
242- return xmlClass .cast (element .getValue ());
251+ Optional <JAXBElement > element = Optional .empty ();
252+ Object unmarshalled = unmarshaller ().unmarshal (is );
253+ if (unmarshalled instanceof JAXBElement ) {
254+ element = Optional .ofNullable ((JAXBElement ) unmarshalled );
243255 }
244256
245- throw new JAXBException ("Expected XML for class " + xmlClass .getCanonicalName () + " but found " + element .getDeclaredType ().getCanonicalName ());
257+ if (!element .isPresent ()) {
258+ throw new IllegalArgumentException ("Unable to convert stream to JAXB, class=" + xmlClass .getName ());
259+ } else if (element .get ().getDeclaredType () == xmlClass ) {
260+ return xmlClass .cast (element .get ().getValue ());
261+ }
262+
263+ throw new JAXBException ("Expected XML for class " + xmlClass .getCanonicalName () + " but found "
264+ + element .get ().getDeclaredType ().getCanonicalName ());
246265 }
247266
248267 /**
249268 * Utility method to marshal a JAXB annotated java object to an XML string.
250269 *
270+ * @param <T>
251271 * @param messageClass The class of the object to marshal.
252272 * @param message The object to marshal.
253273 * @return String containing the XML encoded object.
254274 */
255- public static String jaxb2String (Class <? > messageClass , Object message ) {
275+ public static < T extends Object > String jaxb2String (Class <T > messageClass , Object message ) {
256276
257277 // Make sure we are given the correct input.
258278 if (messageClass == null || message == null ) {
@@ -263,14 +283,12 @@ public static String jaxb2String(Class<?> messageClass, Object message) {
263283 StringWriter writer = new StringWriter ();
264284
265285 try {
266- // We will use JAXB to marshal the java objects.
267- final JAXBContext jaxbContext = JAXBContext .newInstance (messageClass );
268-
269286 // We do not have @XmlRootElement annotations on the classes so
270287 // we need to manually create the JAXBElement.
271- JAXBElement <? > element = new JAXBElement (new QName ("uri" , "local" ), messageClass , message );
288+ JAXBElement <T > element = new JAXBElement < T > (new QName ("uri" , "local" ), messageClass , messageClass . cast ( message ) );
272289
273290 // Marshal the object.
291+ final JAXBContext jaxbContext = JAXBContext .newInstance (messageClass );
274292 jaxbContext .createMarshaller ().marshal (element , writer );
275293 } catch (JAXBException e ) {
276294 // Something went wrong so get out of here.
@@ -279,7 +297,6 @@ public static String jaxb2String(Class<?> messageClass, Object message) {
279297 return null ;
280298 }
281299
282- // Return the XML string.
283300 return writer .toString ();
284301 }
285302}
0 commit comments