javax.xml.bind.ValidationEvent Java Examples

The following examples show how to use javax.xml.bind.ValidationEvent. 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: RuntimeBuiltinLeafInfoImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public String print(XMLGregorianCalendar cal) {
    XMLSerializer xs = XMLSerializer.getInstance();

    QName type = xs.getSchemaType();
    if (type != null) {
        try {
            checkXmlGregorianCalendarFieldRef(type, cal);
            String format = xmlGregorianCalendarFormatString.get(type);
            if (format != null) {
                return format(format, cal);
            }
        } catch (javax.xml.bind.MarshalException e) {
            // see issue 649
            xs.handleEvent(new ValidationEventImpl(ValidationEvent.WARNING, e.getMessage(),
                xs.getCurrentLocation(null) ));
            return "";
        }
    }
    return cal.toXMLFormat();
}
 
Example #2
Source File: UnmarshallingContext.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Reports an error to the user, and asks if s/he wants
 * to recover. If the canRecover flag is false, regardless
 * of the client instruction, an exception will be thrown.
 *
 * Only if the flag is true and the user wants to recover from an error,
 * the method returns normally.
 *
 * The thrown exception will be catched by the unmarshaller.
 */
public void handleEvent(ValidationEvent event, boolean canRecover ) throws SAXException {
    ValidationEventHandler eventHandler = parent.getEventHandler();

    boolean recover = eventHandler.handleEvent(event);

    // if the handler says "abort", we will not return the object
    // from the unmarshaller.getResult()
    if(!recover)    aborted = true;

    if( !canRecover || !recover )
        throw new SAXParseException2( event.getMessage(), locator,
            new UnmarshalException(
                event.getMessage(),
                event.getLinkedException() ) );
}
 
Example #3
Source File: ValidationEventCollector.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
public boolean handleEvent( ValidationEvent event ) {
    events.add(event);

    boolean retVal = true;
    switch( event.getSeverity() ) {
        case ValidationEvent.WARNING:
            retVal = true; // continue validation
            break;
        case ValidationEvent.ERROR:
            retVal = true; // continue validation
            break;
        case ValidationEvent.FATAL_ERROR:
            retVal = false; // halt validation
            break;
        default:
            _assert( false,
                     Messages.format( Messages.UNRECOGNIZED_SEVERITY,
                             event.getSeverity() ) );
            break;
    }

    return retVal;
}
 
Example #4
Source File: XMLSerializer.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
void reconcileID() throws SAXException {
    // find objects that were not a part of the object graph
    idReferencedObjects.removeAll(objectsWithId);

    for( Object idObj : idReferencedObjects ) {
        try {
            String id = getIdFromObject(idObj);
            reportError( new NotIdentifiableEventImpl(
                ValidationEvent.ERROR,
                Messages.DANGLING_IDREF.format(id),
                new ValidationEventLocatorImpl(idObj) ) );
        } catch (JAXBException e) {
            // this error should have been reported already. just ignore here.
        }
    }

    // clear the garbage
    idReferencedObjects.clear();
    objectsWithId.clear();
}
 
Example #5
Source File: UnmarshallingContext.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Reports an error to the user, and asks if s/he wants
 * to recover. If the canRecover flag is false, regardless
 * of the client instruction, an exception will be thrown.
 *
 * Only if the flag is true and the user wants to recover from an error,
 * the method returns normally.
 *
 * The thrown exception will be catched by the unmarshaller.
 */
public void handleEvent(ValidationEvent event, boolean canRecover ) throws SAXException {
    ValidationEventHandler eventHandler = parent.getEventHandler();

    boolean recover = eventHandler.handleEvent(event);

    // if the handler says "abort", we will not return the object
    // from the unmarshaller.getResult()
    if(!recover)    aborted = true;

    if( !canRecover || !recover )
        throw new SAXParseException2( event.getMessage(), locator,
            new UnmarshalException(
                event.getMessage(),
                event.getLinkedException() ) );
}
 
Example #6
Source File: ClassBeanInfoImpl.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public void serializeRoot(BeanT bean, XMLSerializer target) throws SAXException, IOException, XMLStreamException {
    if(tagName==null) {
        Class beanClass = bean.getClass();
        String message;
        if (beanClass.isAnnotationPresent(XmlRootElement.class)) {
            message = Messages.UNABLE_TO_MARSHAL_UNBOUND_CLASS.format(beanClass.getName());
        } else {
            message = Messages.UNABLE_TO_MARSHAL_NON_ELEMENT.format(beanClass.getName());
        }
        target.reportError(new ValidationEventImpl(ValidationEvent.ERROR,message,null, null));
    } else {
        target.startElement(tagName,bean);
        target.childAsSoleContent(bean,null);
        target.endElement();
        if (retainPropertyInfo) {
            target.currentProperty.remove();
        }
    }
}
 
Example #7
Source File: XMLSerializer.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public String onIDREF( Object obj ) throws SAXException {
    String id;
    try {
        id = getIdFromObject(obj);
    } catch (JAXBException e) {
        reportError(null,e);
        return null; // recover by returning null
    }
    idReferencedObjects.add(obj);
    if(id==null) {
        reportError( new NotIdentifiableEventImpl(
            ValidationEvent.ERROR,
            Messages.NOT_IDENTIFIABLE.format(),
            new ValidationEventLocatorImpl(obj) ) );
    }
    return id;
}
 
Example #8
Source File: RuntimeBuiltinLeafInfoImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public String print(XMLGregorianCalendar cal) {
    XMLSerializer xs = XMLSerializer.getInstance();

    QName type = xs.getSchemaType();
    if (type != null) {
        try {
            checkXmlGregorianCalendarFieldRef(type, cal);
            String format = xmlGregorianCalendarFormatString.get(type);
            if (format != null) {
                return format(format, cal);
            }
        } catch (javax.xml.bind.MarshalException e) {
            // see issue 649
            xs.handleEvent(new ValidationEventImpl(ValidationEvent.WARNING, e.getMessage(),
                xs.getCurrentLocation(null) ));
            return "";
        }
    }
    return cal.toXMLFormat();
}
 
Example #9
Source File: ValidationEventCollector.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public boolean handleEvent( ValidationEvent event ) {
    events.add(event);

    boolean retVal = true;
    switch( event.getSeverity() ) {
        case ValidationEvent.WARNING:
            retVal = true; // continue validation
            break;
        case ValidationEvent.ERROR:
            retVal = true; // continue validation
            break;
        case ValidationEvent.FATAL_ERROR:
            retVal = false; // halt validation
            break;
        default:
            _assert( false,
                     Messages.format( Messages.UNRECOGNIZED_SEVERITY,
                             event.getSeverity() ) );
            break;
    }

    return retVal;
}
 
Example #10
Source File: UnmarshallingContext.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Reports an error to the user, and asks if s/he wants
 * to recover. If the canRecover flag is false, regardless
 * of the client instruction, an exception will be thrown.
 *
 * Only if the flag is true and the user wants to recover from an error,
 * the method returns normally.
 *
 * The thrown exception will be catched by the unmarshaller.
 */
public void handleEvent(ValidationEvent event, boolean canRecover ) throws SAXException {
    ValidationEventHandler eventHandler = parent.getEventHandler();

    boolean recover = eventHandler.handleEvent(event);

    // if the handler says "abort", we will not return the object
    // from the unmarshaller.getResult()
    if(!recover)    aborted = true;

    if( !canRecover || !recover )
        throw new SAXParseException2( event.getMessage(), locator,
            new UnmarshalException(
                event.getMessage(),
                event.getLinkedException() ) );
}
 
Example #11
Source File: XMLSerializer.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public String onIDREF( Object obj ) throws SAXException {
    String id;
    try {
        id = getIdFromObject(obj);
    } catch (JAXBException e) {
        reportError(null,e);
        return null; // recover by returning null
    }
    idReferencedObjects.add(obj);
    if(id==null) {
        reportError( new NotIdentifiableEventImpl(
            ValidationEvent.ERROR,
            Messages.NOT_IDENTIFIABLE.format(),
            new ValidationEventLocatorImpl(obj) ) );
    }
    return id;
}
 
Example #12
Source File: ValidationErrorHandler.java    From importer-exporter with Apache License 2.0 6 votes vote down vote up
@Override
public boolean handleEvent(ValidationEvent event) {
    StringBuilder msg = new StringBuilder();
    LogLevel type;

    switch (event.getSeverity()) {
        case ValidationEvent.FATAL_ERROR:
        case ValidationEvent.ERROR:
            msg.append("Invalid content");
            type = LogLevel.ERROR;
            break;
        case ValidationEvent.WARNING:
            msg.append("Warning");
            type = LogLevel.WARN;
            break;
        default:
            return reportAllErrors;
    }

    msg.append(": ").append(event.getMessage());
    log.log(type, msg.toString());

    validationErrors++;
    return reportAllErrors;
}
 
Example #13
Source File: XMLSerializer.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private void reportMissingObjectError(String fieldName) throws SAXException {
    reportError(new ValidationEventImpl(
        ValidationEvent.ERROR,
        Messages.MISSING_OBJECT.format(fieldName),
            getCurrentLocation(fieldName),
        new NullPointerException() ));
}
 
Example #14
Source File: ValueListBeanInfoImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public final void serializeRoot(Object array, XMLSerializer target) throws SAXException {
    target.reportError(
            new ValidationEventImpl(
                    ValidationEvent.ERROR,
                    Messages.UNABLE_TO_MARSHAL_NON_ELEMENT.format(array.getClass().getName()),
                    null,
                    null));
}
 
Example #15
Source File: ArrayBeanInfoImpl.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public final void serializeRoot(Object array, XMLSerializer target) throws SAXException, IOException, XMLStreamException {
    target.reportError(
            new ValidationEventImpl(
                    ValidationEvent.ERROR,
                    Messages.UNABLE_TO_MARSHAL_NON_ELEMENT.format(array.getClass().getName()),
                    null,
                    null));
}
 
Example #16
Source File: DefaultValidationEventHandler.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public boolean handleEvent( ValidationEvent event ) {

        if( event == null ) {
            throw new IllegalArgumentException();
        }

        // calculate the severity prefix and return value
        String severity = null;
        boolean retVal = false;
        switch ( event.getSeverity() ) {
            case ValidationEvent.WARNING:
                severity = Messages.format( Messages.WARNING );
                retVal = true; // continue after warnings
                break;
            case ValidationEvent.ERROR:
                severity = Messages.format( Messages.ERROR );
                retVal = false; // terminate after errors
                break;
            case ValidationEvent.FATAL_ERROR:
                severity = Messages.format( Messages.FATAL_ERROR );
                retVal = false; // terminate after fatal errors
                break;
            default:
                assert false :
                    Messages.format( Messages.UNRECOGNIZED_SEVERITY,
                            event.getSeverity() );
        }

        // calculate the location message
        String location = getLocation( event );

        System.out.println(
            Messages.format( Messages.SEVERITY_MESSAGE,
                             severity,
                             event.getMessage(),
                             location ) );

        // fail on the first error or fatal error
        return retVal;
    }
 
Example #17
Source File: Openscoring.java    From openscoring with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public boolean handleEvent(ValidationEvent event){
	int severity = event.getSeverity();

	switch(severity){
		case ValidationEvent.ERROR:
		case ValidationEvent.FATAL_ERROR:
			return false;
		default:
			return true;
	}
}
 
Example #18
Source File: CompositeStructureBeanInfo.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public void serializeRoot(CompositeStructure o, XMLSerializer target) throws SAXException, IOException, XMLStreamException {
    target.reportError(
            new ValidationEventImpl(
                    ValidationEvent.ERROR,
                    Messages.UNABLE_TO_MARSHAL_NON_ELEMENT.format(o.getClass().getName()),
                    null,
                    null));
}
 
Example #19
Source File: UnmarshallingContext.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean handleEvent(ValidationEvent event) {
    try {
        // if the handler says "abort", we will not return the object.
        boolean recover = parent.getEventHandler().handleEvent(event);
        if(!recover)    aborted = true;
        return recover;
    } catch( RuntimeException re ) {
        // if client event handler causes a runtime exception, then we
        // have to return false.
        return false;
    }
}
 
Example #20
Source File: ValidationEventImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Set the severity field of this event.
 *
 * @param _severity Must be one of ValidationEvent.WARNING,
 * ValidationEvent.ERROR, or ValidationEvent.FATAL_ERROR.
 * @throws IllegalArgumentException if an illegal severity field is supplied
 */
public void setSeverity( int _severity ) {

    if( _severity != ValidationEvent.WARNING &&
        _severity != ValidationEvent.ERROR &&
        _severity != ValidationEvent.FATAL_ERROR ) {
            throw new IllegalArgumentException(
                Messages.format( Messages.ILLEGAL_SEVERITY ) );
    }

    this.severity = _severity;
}
 
Example #21
Source File: LeafBeanInfoImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public final void serializeRoot(BeanT bean, XMLSerializer target) throws SAXException, IOException, XMLStreamException {
    if(tagName==null) {
        target.reportError(
            new ValidationEventImpl(
                ValidationEvent.ERROR,
                Messages.UNABLE_TO_MARSHAL_NON_ELEMENT.format(bean.getClass().getName()),
                null,
                null));
    }
    else {
        target.startElement(tagName,bean);
        target.childAsSoleContent(bean,null);
        target.endElement();
    }
}
 
Example #22
Source File: UnmarshallingContext.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Based on current {@link Logger} {@link Level} and errorCounter value determines if error should be reported.
 *
 * If the method called and return true it is expected that error will be reported. And that's why
 * errorCounter is automatically decremented during the check.
 *
 * NOT THREAD SAFE!!! In case of heave concurrency access several additional errors could be reported. It's not expected to be the
 * problem. Otherwise add synchronization here.
 *
 * @return true in case if {@link Level#FINEST} is set OR we haven't exceed errors reporting limit.
 */
public boolean shouldErrorBeReported() throws SAXException {
    if (logger.isLoggable(Level.FINEST))
        return true;

    if (errorsCounter >= 0) {
        --errorsCounter;
        if (errorsCounter == 0) // it's possible to miss this because of concurrency. If required add synchronization here
            handleEvent(new ValidationEventImpl(ValidationEvent.WARNING, Messages.ERRORS_LIMIT_EXCEEDED.format(),
                    getLocator().getLocation(), null), true);
    }
    return errorsCounter >= 0;
}
 
Example #23
Source File: AnyTypeBeanInfo.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public void serializeRoot(Object element, XMLSerializer target) throws SAXException {
    target.reportError(
            new ValidationEventImpl(
                    ValidationEvent.ERROR,
                    Messages.UNABLE_TO_MARSHAL_NON_ELEMENT.format(element.getClass().getName()),
                    null,
                    null));
}
 
Example #24
Source File: RuntimeBuiltinLeafInfoImpl.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public Base64Data print(Image v) {
    ByteArrayOutputStreamEx imageData = new ByteArrayOutputStreamEx();
    XMLSerializer xs = XMLSerializer.getInstance();

    String mimeType = xs.getXMIMEContentType();
    if(mimeType==null || mimeType.startsWith("image/*"))
        // because PNG is lossless, it's a good default
        //
        // mime type can be a range, in which case we can't just pass that
        // to ImageIO.getImageWritersByMIMEType, so here I'm just assuming
        // the default of PNG. Not sure if this is complete.
        mimeType = "image/png";

    try {
        Iterator<ImageWriter> itr = ImageIO.getImageWritersByMIMEType(mimeType);
        if(itr.hasNext()) {
            ImageWriter w = itr.next();
            ImageOutputStream os = ImageIO.createImageOutputStream(imageData);
            w.setOutput(os);
            w.write(convertToBufferedImage(v));
            os.close();
            w.dispose();
        } else {
            // no encoder
            xs.handleEvent(new ValidationEventImpl(
                ValidationEvent.ERROR,
                Messages.NO_IMAGE_WRITER.format(mimeType),
                xs.getCurrentLocation(null) ));
            // TODO: proper error reporting
            throw new RuntimeException("no encoder for MIME type "+mimeType);
        }
    } catch (IOException e) {
        xs.handleError(e);
        // TODO: proper error reporting
        throw new RuntimeException(e);
    }
    Base64Data bd = new Base64Data();
    imageData.set(bd,mimeType);
    return bd;
}
 
Example #25
Source File: Loader.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public static void reportError(String msg, Exception nested, boolean canRecover) throws SAXException {
    UnmarshallingContext context = UnmarshallingContext.getInstance();
    context.handleEvent( new ValidationEventImpl(
        canRecover? ValidationEvent.ERROR : ValidationEvent.FATAL_ERROR,
        msg,
        context.getLocator().getLocation(),
        nested ), canRecover );
}
 
Example #26
Source File: DefaultValidationEventHandler.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public boolean handleEvent( ValidationEvent event ) {

        if( event == null ) {
            throw new IllegalArgumentException();
        }

        // calculate the severity prefix and return value
        String severity = null;
        boolean retVal = false;
        switch ( event.getSeverity() ) {
            case ValidationEvent.WARNING:
                severity = Messages.format( Messages.WARNING );
                retVal = true; // continue after warnings
                break;
            case ValidationEvent.ERROR:
                severity = Messages.format( Messages.ERROR );
                retVal = false; // terminate after errors
                break;
            case ValidationEvent.FATAL_ERROR:
                severity = Messages.format( Messages.FATAL_ERROR );
                retVal = false; // terminate after fatal errors
                break;
            default:
                assert false :
                    Messages.format( Messages.UNRECOGNIZED_SEVERITY,
                            event.getSeverity() );
        }

        // calculate the location message
        String location = getLocation( event );

        System.out.println(
            Messages.format( Messages.SEVERITY_MESSAGE,
                             severity,
                             event.getMessage(),
                             location ) );

        // fail on the first error or fatal error
        return retVal;
    }
 
Example #27
Source File: CollectingErrorEventHandler.java    From validator with Apache License 2.0 5 votes vote down vote up
private static XMLSyntaxErrorSeverity translateSeverity(final int severity) {
    switch (severity) {
        case ValidationEvent.WARNING:
            return XMLSyntaxErrorSeverity.SEVERITY_WARNING;
        case ValidationEvent.ERROR:
            return XMLSyntaxErrorSeverity.SEVERITY_ERROR;
        case ValidationEvent.FATAL_ERROR:
            return XMLSyntaxErrorSeverity.SEVERITY_FATAL_ERROR;
        default:
            throw new IllegalArgumentException("Unknown severity level " + severity);
    }
}
 
Example #28
Source File: XMLSerializer.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public boolean handleEvent(ValidationEvent event) {
    try {
        return marshaller.getEventHandler().handleEvent(event);
    } catch (JAXBException e) {
        // impossible
        throw new Error(e);
    }
}
 
Example #29
Source File: ArrayBeanInfoImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public final void serializeRoot(Object array, XMLSerializer target) throws SAXException, IOException, XMLStreamException {
    target.reportError(
            new ValidationEventImpl(
                    ValidationEvent.ERROR,
                    Messages.UNABLE_TO_MARSHAL_NON_ELEMENT.format(array.getClass().getName()),
                    null,
                    null));
}
 
Example #30
Source File: XMLSerializer.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public boolean handleError(Exception e,Object source,String fieldName) {
    return handleEvent(
        new ValidationEventImpl(
            ValidationEvent.ERROR,
            e.getMessage(),
            new ValidationEventLocatorExImpl(source,fieldName),
                e));
}