use of javax.xml.bind.ValidationEvent in project Payara by payara.
the class Util method handleTypeMismatchError.
/**
* Reports that the type of an object in a property is unexpected.
*/
public static void handleTypeMismatchError(XMLSerializer serializer, Object parentObject, String fieldName, Object childObject) throws AbortSerializationException {
ValidationEvent ve = new ValidationEventImpl(// maybe it should be a fatal error.
ValidationEvent.ERROR, Messages.format(Messages.ERR_TYPE_MISMATCH, getUserFriendlyTypeName(parentObject), fieldName, getUserFriendlyTypeName(childObject)), new ValidationEventLocatorExImpl(parentObject, fieldName));
serializer.reportError(ve);
}
use of javax.xml.bind.ValidationEvent in project sldeditor by robward-scisys.
the class ParseXML method parseFile.
/**
* Parses the xml files, validates against schema and reports any errors.
*
* @param resourceFolder the resource folder
* @param resourceName the resource name
* @param schemaResource the schema resource
* @param classToParse the class to parse
* @return the object
*/
public static Object parseFile(String resourceFolder, String resourceName, String schemaResource, Class<?> classToParse) {
String fullResourceName = resourceFolder + resourceName;
logger.debug("Reading : " + fullResourceName);
InputStream inputStream = ParseXML.class.getResourceAsStream(fullResourceName);
if (inputStream == null) {
File file = new File(fullResourceName);
if (!file.exists()) {
ConsoleManager.getInstance().error(ParseXML.class, Localisation.getField(ParseXML.class, "ParseXML.failedToFindResource") + fullResourceName);
return null;
}
try {
inputStream = new FileInputStream(file);
} catch (FileNotFoundException e) {
ConsoleManager.getInstance().error(ParseXML.class, Localisation.getField(ParseXML.class, "ParseXML.failedToFindResource") + fullResourceName);
return null;
}
}
ValidationEventCollector vec = new ValidationEventCollector();
URL xsdURL = ParseXML.class.getResource(schemaResource);
try {
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(xsdURL);
JAXBContext jaxbContext = JAXBContext.newInstance(classToParse);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
jaxbUnmarshaller.setSchema(schema);
jaxbUnmarshaller.setEventHandler(vec);
return jaxbUnmarshaller.unmarshal(inputStream);
} catch (SAXException e) {
ConsoleManager.getInstance().exception(ParseXML.class, e);
} catch (javax.xml.bind.UnmarshalException ex) {
if (vec != null && vec.hasEvents()) {
for (ValidationEvent ve : vec.getEvents()) {
String msg = ve.getMessage();
ValidationEventLocator vel = ve.getLocator();
String message = String.format("%s %s %s %s %s %d %s %d %s", Localisation.getField(ParseXML.class, "ParseXML.failedToValidate"), fullResourceName, Localisation.getField(ParseXML.class, "ParseXML.usingXSD"), xsdURL.toString(), Localisation.getField(ParseXML.class, "ParseXML.line"), vel.getLineNumber(), Localisation.getField(ParseXML.class, "ParseXML.column"), vel.getColumnNumber(), msg);
ConsoleManager.getInstance().error(ParseXML.class, message);
}
}
} catch (JAXBException e) {
ConsoleManager.getInstance().exception(ParseXML.class, e);
}
return null;
}
use of javax.xml.bind.ValidationEvent in project onebusaway-application-modules by camsys.
the class SiriXmlSerializerV2 method getXml.
public String getXml(Siri siri) throws Exception {
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
marshaller.setEventHandler(new ValidationEventHandler() {
public boolean handleEvent(ValidationEvent event) {
_log.error(event.getMessage(), event.getLinkedException());
throw new RuntimeException(event.getMessage(), event.getLinkedException());
}
});
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
Writer output = new StringWriter();
marshaller.marshal(siri, output);
// FIXME: strip off ns6 namespaces on siri root namespace. super hack, please fix me!
String outputAsString = output.toString();
/*outputAsString = outputAsString.replaceAll("<ns6:", "<");
outputAsString = outputAsString.replaceAll("</ns6:", "</");
outputAsString = outputAsString.replaceAll("xmlns:ns6", "xmlns");
*/
String[] searchList = { "<siriExtensionWrapper>", "</siriExtensionWrapper>", "<siriUpcomingServiceExtension>", "</siriUpcomingServiceExtension>", "<siriPolyLinesExtension>", "</siriPolyLinesExtension>" };
String[] replacementList = { "", "", "", "", "", "" };
outputAsString = StringUtils.replaceEach(outputAsString, searchList, replacementList);
return outputAsString;
}
use of javax.xml.bind.ValidationEvent in project TranskribusCore by Transkribus.
the class JaxbUtils method checkEvents.
private static void checkEvents(ValidationEventCollector vec) {
if (vec.hasEvents()) {
logger.info("Events occured while marshalling xml file: " + vec.getEvents().length);
ValidationEvent[] events = vec.getEvents();
for (ValidationEvent e : events) {
logger.info(e.getMessage());
}
} else {
logger.debug("No events occured during marshalling xml file!");
}
}
use of javax.xml.bind.ValidationEvent in project OpenAM by OpenRock.
the class Util method handlePrintConversionException.
/**
* Reports a print conversion error while marshalling.
*/
public static void handlePrintConversionException(Object caller, Exception e, XMLSerializer serializer) throws SAXException {
if (e instanceof SAXException)
// will be thrown)
throw (SAXException) e;
String message = e.getMessage();
if (message == null) {
message = e.toString();
}
ValidationEvent ve = new PrintConversionEventImpl(ValidationEvent.ERROR, message, new ValidationEventLocatorImpl(caller), e);
serializer.reportError(ve);
}
Aggregations