Hi there,
I was using Dynamic context and trying to turn on validation. I found some difficulties:
- I need to provide schema to create dynamic context, why is it also needed to provide schema once again to unmarshaller? Schema shouldn't be there by design?
- When i was tryign to setSchema() on unmarshaller and then setValidation(true) it is throwing an expection.
Sample code
DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromXSD(streamSource, null, null, null);
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(streamSource2);
final JAXBUnmarshaller unmarshaller = jaxbContext .createUnmarshaller();
unmarshaller.setSchema(schema);
unmarshaller.setValidating(true);
gives an Exception:
Exception Description: An error occurred resolving the XML Schema.
Internal Exception: java.lang.NullPointerException
Exception [EclipseLink-25012] (Eclipse Persistence Services - 2.6.0.v20150309-bf26070): org.eclipse.persistence.exceptions.XMLMarshalException
Exception Description: An error occurred resolving the XML Schema.
Internal Exception: java.lang.NullPointerException
at org.eclipse.persistence.exceptions.XMLMarshalException.errorResolvingXMLSchema(XMLMarshalException.java:186)
at org.eclipse.persistence.oxm.schema.XMLSchemaClassPathReference.getURL(XMLSchemaClassPathReference.java:48)
at org.eclipse.persistence.oxm.XMLUnmarshaller.initializeSchemas(XMLUnmarshaller.java:211)
at org.eclipse.persistence.oxm.XMLUnmarshaller.setValidationMode(XMLUnmarshaller.java:155)
at org.eclipse.persistence.jaxb.JAXBUnmarshaller.setValidating(JAXBUnmarshaller.java:756)
I found workaround, but it should be handled properly without such hacks.
int numberOfSessions = ((XMLContext) jaxbContext.getXMLContext()).getSessions().size();
for (int x = 0; x < numberOfSessions; x++) {
final Collection values = ((CoreSession) jaxbContext.getXMLContext().getSessions().get(x)).getDescriptors().values();
List<XMLDescriptor> proper = new LinkedList<>();
URL schemaURL;
final Iterator iterator = values.iterator();
while (iterator.hasNext()) {
final XMLDescriptor xmlDescriptor = (XMLDescriptor) iterator.next();
final XMLSchemaReference schemaReference = xmlDescriptor.getSchemaReference();
if (schemaReference.getResource() == null) {
log.info("{}", ToStringBuilder.reflectionToString(schemaReference, ToStringStyle.DEFAULT_STYLE, true));
XMLSchemaFileReference xmlSchemaFileReference = new XMLSchemaFileReference(xsd);
xmlSchemaFileReference.setSchemaContext(schemaReference.getSchemaContext());
xmlSchemaFileReference.setSchemaContextAsQName(schemaReference.getSchemaContextAsQName());
xmlSchemaFileReference.setType(schemaReference.getType());
xmlDescriptor.setSchemaReference(xmlSchemaFileReference);
}
}
}
[Updated on: Fri, 07 August 2015 08:10]
Report message to a moderator