Java Code Examples for javax.xml.bind.util.ValidationEventCollector#getEvents()

The following examples show how to use javax.xml.bind.util.ValidationEventCollector#getEvents() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JaxbUtils.java    From TranskribusCore with GNU General Public License v3.0 6 votes vote down vote up
/**
	 * Marshal object to stream.
	 * 
	 * FIXME
	 * This method support JSON, but the representation does not match the one produced and accepted by Jersey! See {@link ExportParameters}Test
	 * 
	 * @param object
	 * @param out
	 * @param doFormatting
	 * @param type
	 * @param nestedClasses
	 * @return
	 * @throws JAXBException
	 */
	private static <T> ValidationEvent[] marshalToStream(T object, OutputStream out, 
			boolean doFormatting, MarshallerType type, Class<?>... nestedClasses) throws JAXBException {
		ValidationEventCollector vec = new ValidationEventCollector();

		if(type == null) {
			type = MarshallerType.XML;
		}
		final Marshaller marshaller;
		switch(type) {
		case JSON:
			marshaller = createJsonMarshaller(object, doFormatting, nestedClasses);
			break;
		default:
			marshaller = createXmlMarshaller(object, doFormatting, nestedClasses);
			break;
		}
//		marshaller = createXmlMarshaller(object, doFormatting, nestedClasses);
		marshaller.setEventHandler(vec);
		marshaller.marshal(object, out);
		
		checkEvents(vec);
		
		return vec.getEvents();
	}
 
Example 2
Source File: PcGtsTypeMessageBodyReaderTest.java    From TranskribusCore with GNU General Public License v3.0 6 votes vote down vote up
public void testDamagedXml() throws FileNotFoundException, IOException, JAXBException {
		File f = new File("/media/daten/Dokumente/Bentham_batch2_test/page/116_630_001.xml");
		
//		PcGtsTypeMessageBodyReader reader = new PcGtsTypeMessageBodyReader();
//		
//		reader.readFrom(type, genericType, annotations, mediaType, httpHeaders, entityStream)
		try(FileInputStream stream = new FileInputStream(f)) {
			
			ValidationEventCollector vec = new ValidationEventCollector();
			PcGtsType pc =  PageXmlUtils.unmarshal(stream, vec);
			
			for(ValidationEvent e : vec.getEvents()) {
				logger.info("Has validation event with severity: " + e.getSeverity());
			}
			
//			List<TextLineType> lines = PageXmlUtils.getLines(pc);
//			logger.info("has lines: " + lines.size());
//			for(TextLineType l : lines) {
//				logger.debug(l.getId());
//			}
		}
		
		
	}
 
Example 3
Source File: ParseXML.java    From sldeditor with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Output parse errors.
 *
 * @param fullResourceName the full resource name
 * @param vec the vec
 * @param xsdURL the xsd URL
 */
private static void outputParseErrors(
        String fullResourceName, ValidationEventCollector vec, URL xsdURL) {
    if (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);
        }
    }
}
 
Example 4
Source File: PageXmlUtils.java    From TranskribusCore with GNU General Public License v3.0 5 votes vote down vote up
private static String buildMsg(ValidationEventCollector vec, PcGtsType page) {
	String imgFn = "";
	if (page.getPage()!=null) {
		imgFn = page.getPage().getImageFilename();
	}
	
	String msg;
	if (vec.hasEvents()) {
		msg="Events occured while marshalling xml file: " + vec.getEvents().length;
	} else {
		msg=NO_EVENTS_MSG;
	}
	if (!imgFn.isEmpty()) msg += " (img: "+imgFn+")";
	return msg;
}
 
Example 5
Source File: JaxbUtils.java    From TranskribusCore with GNU General Public License v3.0 5 votes vote down vote up
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!");
	}
}
 
Example 6
Source File: WorkflowProfiles.java    From proarc with GNU General Public License v3.0 5 votes vote down vote up
private void read() throws JAXBException {
    long currentTime = file.lastModified();
    if (currentTime == lastModified) {
        return ;
    }
    Unmarshaller unmarshaller = getUnmarshaller();
    ValidationEventCollector errors = (ValidationEventCollector) unmarshaller.getEventHandler();
    WorkflowDefinition fetchedWf = null;
    try {
        WorkflowDefinition wf = (WorkflowDefinition) unmarshaller.unmarshal(file);
        if (!errors.hasEvents()) {
            readCaches(wf);
            fetchedWf = wf;
        }
    } catch (UnmarshalException ex) {
        if (!errors.hasEvents()) {
            throw ex;
        }
    } finally {
        setProfiles(fetchedWf, currentTime);
    }
    if (errors.hasEvents()) {
        StringBuilder err = new StringBuilder();
        for (ValidationEvent event : errors.getEvents()) {
            err.append(event).append('\n');
        }
        throw new JAXBException(err.toString());
    }
}
 
Example 7
Source File: XMLUnMarshaller.java    From cougar with Apache License 2.0 5 votes vote down vote up
private void validate(ValidationEventCollector handler)
		throws ValidationException {
	if(handler.hasEvents()) {
		ValidationEvent[] events = handler.getEvents();
		StringBuilder sb = new StringBuilder();
		for(ValidationEvent event : events) {
			sb.append(event.getMessage());
		}
		throw new ValidationException(sb.toString());
	}
}