3232public final class XmlUtility {
3333 private static Logger logger = LogManager .getLogger (XmlUtility .class );
3434 private static final String XmlHeader = "<?xml version=\" 1.0\" encoding=\" UTF-8\" standalone=\" yes\" ?>" ;
35- private static JAXBContext request_ctx = null ;
36- private static JAXBContext response_ctx = null ;
37- private static HashMap <String , JAXBContext > jaxbContext = new HashMap <String , JAXBContext >();
35+ private static JAXBContext request_ctx = null ;
36+ private static JAXBContext response_ctx = null ;
37+ private static HashMap <String , JAXBContext > jaxbContext = new HashMap <String , JAXBContext >();
3838
3939
4040 /**
41- * Default C'tor, cannot be instantiated
42- */
41+ * Default C'tor, cannot be instantiated
42+ */
4343 private XmlUtility () {
4444 }
4545
@@ -53,32 +53,32 @@ private XmlUtility() {
5353 */
5454 public static synchronized <T extends Serializable > String getXml (T entity ) throws IOException , JAXBException
5555 {
56- StringWriter sw = new StringWriter ();
56+ StringWriter sw = new StringWriter ();
5757
58- if ( null != entity )
58+ if ( null != entity )
5959 {
60- if (!jaxbContext .containsKey (entity .getClass ().toString ()))
61- {
62- request_ctx = JAXBContext .newInstance (entity .getClass ());
63- jaxbContext .put (entity .getClass ().toString (), request_ctx );
64- }
65- else
66- {
67- request_ctx = jaxbContext .get (entity .getClass ().toString ());
68- }
60+ if (!jaxbContext .containsKey (entity .getClass ().toString ()))
61+ {
62+ request_ctx = JAXBContext .newInstance (entity .getClass ());
63+ jaxbContext .put (entity .getClass ().toString (), request_ctx );
64+ }
65+ else
66+ {
67+ request_ctx = jaxbContext .get (entity .getClass ().toString ());
68+ }
6969
70- if (request_ctx != null )
71- {
72- Marshaller m = request_ctx .createMarshaller ();
73- m .setProperty (Marshaller .JAXB_FORMATTED_OUTPUT , true );
74-
75- m .marshal (entity , sw );
76- }
70+ if (request_ctx != null )
71+ {
72+ Marshaller m = request_ctx .createMarshaller ();
73+ m .setProperty (Marshaller .JAXB_FORMATTED_OUTPUT , true );
74+
75+ m .marshal (entity , sw );
76+ }
7777 }
78- sw .flush ();
79- sw .close ();
78+ sw .flush ();
79+ sw .close ();
8080
81- return sw .toString ();
81+ return sw .toString ();
8282 }
8383
8484 /**
@@ -99,7 +99,7 @@ public static synchronized <T extends Serializable> T create(String xml, Class<T
9999 //Disable XXE
100100 SAXParserFactory spf = SAXParserFactory .newInstance ();
101101 spf .setNamespaceAware (true );
102- spf .setValidating (true );
102+ spf .setValidating (true );
103103 spf .setFeature ("http://xml.org/sax/features/external-general-entities" , false );
104104 spf .setFeature ("http://xml.org/sax/features/external-parameter-entities" , false );
105105 spf .setFeature ("http://apache.org/xml/features/nonvalidating/load-external-dtd" , false );
@@ -110,44 +110,44 @@ public static synchronized <T extends Serializable> T create(String xml, Class<T
110110 //make sure we have not null and not-empty string to de-serialize
111111 if ( null != xml && !xml .trim ().isEmpty ())
112112 {
113- if (!jaxbContext .containsKey (classType .toString ()))
114- {
115- response_ctx = JAXBContext .newInstance (classType );
116- jaxbContext .put (classType .toString (), response_ctx );
117- }
118- else
119- {
120- response_ctx = jaxbContext .get (classType .toString ());
121- }
122-
123- if (response_ctx != null )
124- {
125- Unmarshaller um = response_ctx .createUnmarshaller ();
126- try {
127- Object unmarshaled = um .unmarshal (xmlSource );
128- if ( null != unmarshaled )
129- {
130- try {
131- entity = classType .cast (unmarshaled );
132- } catch (ClassCastException cce ) {
133- if (unmarshaled instanceof JAXBElement ) {
134- @ SuppressWarnings ("rawtypes" )
135- JAXBElement element = (JAXBElement ) unmarshaled ;
136- if ( null != element .getValue () && element .getValue ().getClass ()==classType ) {
137- entity = (T ) element .getValue ();
138- }
139- }
140- }
141- }
142- } catch (JAXBException jaxbe ) {
143- LogHelper .info (logger , "Exception - while deserializing text:'%s' " , xml );
144- LogHelper .warn (logger , "Exception Details-> Code:'%s', Message:'%s'" , jaxbe .getErrorCode (), jaxbe .getMessage ());
145- throw jaxbe ;
146- }
147- }
113+ if (!jaxbContext .containsKey (classType .toString ()))
114+ {
115+ response_ctx = JAXBContext .newInstance (classType );
116+ jaxbContext .put (classType .toString (), response_ctx );
117+ }
118+ else
119+ {
120+ response_ctx = jaxbContext .get (classType .toString ());
121+ }
122+
123+ if (response_ctx != null )
124+ {
125+ Unmarshaller um = response_ctx .createUnmarshaller ();
126+ try {
127+ Object unmarshaled = um .unmarshal (xmlSource );
128+ if ( null != unmarshaled )
129+ {
130+ try {
131+ entity = classType .cast (unmarshaled );
132+ } catch (ClassCastException cce ) {
133+ if (unmarshaled instanceof JAXBElement ) {
134+ @ SuppressWarnings ("rawtypes" )
135+ JAXBElement element = (JAXBElement ) unmarshaled ;
136+ if ( null != element .getValue () && element .getValue ().getClass ()==classType ) {
137+ entity = (T ) element .getValue ();
138+ }
139+ }
140+ }
141+ }
142+ } catch (JAXBException jaxbe ) {
143+ LogHelper .info (logger , "Exception - while deserializing text:'%s' " , xml );
144+ LogHelper .warn (logger , "Exception Details-> Code:'%s', Message:'%s'" , jaxbe .getErrorCode (), jaxbe .getMessage ());
145+ throw jaxbe ;
146+ }
147+ }
148148 }
149149
150- return entity ;
150+ return entity ;
151151 }
152152
153153 /**
@@ -233,7 +233,7 @@ public static <T extends Serializable> String getRootElementXml(T entity) {
233233 * @return String root element xml without prologue
234234 */
235235 public static String getRootElementXml (String xmlString ) {
236- return xmlString .replace (XmlHeader , "" );
236+ return xmlString .replace (XmlHeader , "" );
237237 }
238238
239239 @ XmlRootElement
0 commit comments