33 */
44package org .csuc .deserialize ;
55
6+ import isbn ._1_931666_22_9 .Ead ;
7+ import nl .memorix_maior .api .rest ._3 .Memorix ;
8+ import nl .mindbus .a2a .A2AType ;
69import org .apache .logging .log4j .LogManager ;
710import org .apache .logging .log4j .Logger ;
11+ import org .openarchives .oai ._2 .OAIPMHtype ;
12+ import org .openarchives .oai ._2_0 .oai_dc .OaiDcType ;
813import org .w3c .dom .Node ;
914import org .xml .sax .SAXException ;
1015import org .xml .sax .SAXNotRecognizedException ;
1116
17+ import javax .xml .XMLConstants ;
1218import javax .xml .bind .JAXBContext ;
1319import javax .xml .bind .JAXBElement ;
1420import javax .xml .bind .JAXBException ;
2329import javax .xml .transform .dom .DOMSource ;
2430import javax .xml .transform .sax .SAXSource ;
2531import javax .xml .transform .stream .StreamSource ;
32+ import javax .xml .validation .Schema ;
33+ import javax .xml .validation .SchemaFactory ;
2634import java .io .File ;
2735import java .io .IOException ;
2836import java .io .InputStream ;
2937import java .io .StringReader ;
3038import java .net .URL ;
39+ import java .util .ArrayList ;
40+ import java .util .List ;
41+ import java .util .Objects ;
42+ import java .util .stream .Stream ;
3143
3244
3345/**
@@ -48,6 +60,7 @@ public JaxbUnmarshal(File file, Class[] classType) {
4860 JAXBContext jc = JAXBContext .newInstance (classType );
4961 Unmarshaller u = jc .createUnmarshaller ();
5062 u .setEventHandler (validationEvent );
63+ u .setSchema (addSchema (classType ));
5164
5265 Object obj = u .unmarshal (file );
5366
@@ -56,24 +69,25 @@ public JaxbUnmarshal(File file, Class[] classType) {
5669 else data = obj ;
5770
5871 logger .debug (String .format ("isValidating %s" , validationEvent .isValidating () ));
59- } catch (JAXBException e ) {
72+ } catch (JAXBException | SAXException e ) {
6073 logger .error (String .format ("JaxbUnmarshal file %s" , e ));
61- }
74+ }
6275 }
6376
6477 public JaxbUnmarshal (InputStream inputStream , Class [] classType ) {
6578 try {
6679 JAXBContext jc = JAXBContext .newInstance (classType );
6780 Unmarshaller u = jc .createUnmarshaller ();
6881 u .setEventHandler (validationEvent );
82+ u .setSchema (addSchema (classType ));
6983
7084 Object obj = u .unmarshal (inputStream );
7185 if (obj instanceof JAXBElement )
7286 data = ((JAXBElement <Object >) obj ).getValue ();
7387 else data = obj ;
7488
7589 logger .debug (String .format ("isValidating %s" , validationEvent .isValidating () ));
76- } catch (JAXBException e ) {
90+ } catch (JAXBException | SAXException e ) {
7791 logger .error (String .format ("JaxbUnmarshal InputStream %s" , e ));
7892 }
7993 }
@@ -85,6 +99,7 @@ public JaxbUnmarshal(URL url, Class[] classType) {
8599 JAXBContext jc = JAXBContext .newInstance (classType );
86100 Unmarshaller u = jc .createUnmarshaller ();
87101 u .setEventHandler (validationEvent );
102+ u .setSchema (addSchema (classType ));
88103
89104 Object obj = u .unmarshal (url );
90105
@@ -93,7 +108,7 @@ public JaxbUnmarshal(URL url, Class[] classType) {
93108 else data = obj ;
94109
95110 logger .debug (String .format ("isValidating %s" , validationEvent .isValidating () ));
96- } catch (JAXBException e ) {
111+ } catch (JAXBException | SAXException e ) {
97112 logger .error (String .format ("JaxbUnmarshal URL %s" , e ));
98113 }
99114 }
@@ -104,6 +119,7 @@ public JaxbUnmarshal(StringBuffer stringbuffer, Class[] classType) {
104119 JAXBContext jc = JAXBContext .newInstance (classType );
105120 Unmarshaller u = jc .createUnmarshaller ();
106121 u .setEventHandler (validationEvent );
122+ u .setSchema (addSchema (classType ));
107123
108124 Object obj = u .unmarshal (new StreamSource (new StringReader (stringbuffer .toString ())));
109125
@@ -112,17 +128,18 @@ public JaxbUnmarshal(StringBuffer stringbuffer, Class[] classType) {
112128 else data = obj ;
113129
114130 logger .debug (String .format ("isValidating %s" , validationEvent .isValidating () ));
115- } catch (JAXBException e ) {
131+ } catch (JAXBException | SAXException e ) {
116132 logger .error (String .format ("JaxbUnmarshal StringBuffer %s" , e ));
117133 }
118134 }
119135
120- public JaxbUnmarshal (Node node , Class <?> classType ) throws IOException {
136+ public JaxbUnmarshal (Node node , Class [] classType ) throws IOException {
121137 logger .debug (String .format ("read Node %s" , node ));
122138 try {
123139 Source xmlSource = new DOMSource (node );
124140 Unmarshaller u = JAXBContext .newInstance (classType ).createUnmarshaller ();
125141 u .setEventHandler (validationEvent );
142+ u .setSchema (addSchema (classType ));
126143
127144 Object obj = u .unmarshal (xmlSource );
128145
@@ -131,7 +148,7 @@ public JaxbUnmarshal(Node node, Class<?> classType) throws IOException {
131148 else data = obj ;
132149
133150 logger .debug (String .format ("isValidating %s" , validationEvent .isValidating () ));
134- }catch (JAXBException e ) {
151+ }catch (JAXBException | SAXException e ) {
135152 logger .error (String .format ("JaxbUnmarshal Node %s" , e ));
136153 }
137154 }
@@ -172,6 +189,7 @@ public JaxbUnmarshal(SAXSource saxSource, Class[] classType) {
172189 Unmarshaller u = jc .createUnmarshaller ();
173190 ValidationEventCollector vec = new ValidationEventCollector ();
174191 u .setEventHandler ( vec );
192+ u .setSchema (addSchema (classType ));
175193
176194 // turn off the JAXB provider's default validation mechanism to
177195 // avoid duplicate validation
@@ -201,6 +219,7 @@ public JaxbUnmarshal(XMLStreamReader xmlStreamReader, Class[] classType) {
201219 try {
202220 JAXBContext jc = JAXBContext .newInstance (classType );
203221 Unmarshaller u = jc .createUnmarshaller ();
222+ u .setSchema (addSchema (classType ));
204223
205224 Object obj = u .unmarshal (xmlStreamReader );
206225
@@ -209,7 +228,7 @@ public JaxbUnmarshal(XMLStreamReader xmlStreamReader, Class[] classType) {
209228 else data = obj ;
210229
211230 logger .debug (String .format ("isValidating %s" , validationEvent .isValidating () ));
212- } catch (JAXBException e ) {
231+ } catch (JAXBException | SAXException e ) {
213232 logger .error (String .format ("JaxbUnmarshal XMLStreamReader %s" , e ));
214233 }
215234 }
@@ -219,6 +238,7 @@ public JaxbUnmarshal(XMLEventReader xmlEventReader, Class[] classType) {
219238 try {
220239 JAXBContext jc = JAXBContext .newInstance (classType );
221240 Unmarshaller u = jc .createUnmarshaller ();
241+ u .setSchema (addSchema (classType ));
222242
223243 Object obj = u .unmarshal (xmlEventReader );
224244
@@ -227,7 +247,7 @@ public JaxbUnmarshal(XMLEventReader xmlEventReader, Class[] classType) {
227247 else data = obj ;
228248
229249 logger .debug (String .format ("isValidating %s" , validationEvent .isValidating () ));
230- }catch (JAXBException e ) {
250+ }catch (JAXBException | SAXException e ) {
231251 logger .error (String .format ("JaxbUnmarshal XMLEventReader %s" , e ));
232252 }
233253 }
@@ -239,4 +259,20 @@ public Object getObject() {
239259 public boolean isValidating () {
240260 return validationEvent .isValidating ();
241261 }
262+
263+ private Schema addSchema (Class [] classType ) throws SAXException {
264+ List <Source > list = new ArrayList <>();
265+
266+ Stream .of (classType ).forEach (type ->{
267+ if (Objects .equals (type , A2AType .class )) list .add (new StreamSource (A2AType .class .getClassLoader ().getResource ("A2AAllInOne_v.1.7.xsd" ).toExternalForm ()));
268+ if (Objects .equals (type , OaiDcType .class )) list .add (new StreamSource (OaiDcType .class .getClassLoader ().getResource ("oai_dc.xsd" ).toExternalForm ()));
269+ if (Objects .equals (type , Ead .class )) list .add (new StreamSource (Ead .class .getClassLoader ().getResource ("apeEAD.xsd" ).toExternalForm ()));
270+ if (Objects .equals (type , Memorix .class )) list .add (new StreamSource (Memorix .class .getClassLoader ().getResource ("MRX-API-ANY.xsd" ).toExternalForm ()));
271+ if (Objects .equals (type , OAIPMHtype .class )) list .add (new StreamSource (OAIPMHtype .class .getClassLoader ().getResource ("OAI-PMH.xsd" ).toExternalForm ()));
272+ });
273+
274+ Source [] schemaFiles = list .toArray (new Source [list .size ()]);
275+ SchemaFactory sf = SchemaFactory .newInstance ( XMLConstants .W3C_XML_SCHEMA_NS_URI );
276+ return sf .newSchema ( schemaFiles );
277+ }
242278}
0 commit comments