001//
002// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v1.0.6-01/24/2006 06:15 PM(kohsuke)-fcs 
003// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
004// Any modifications to this file will be lost upon recompilation of the source schema. 
005// Generated on: 2012.10.03 at 04:27:47 AM CEST 
006//
007
008package org.jdtaus.mojo.resource.model.impl.runtime;
009
010import javax.xml.bind.ValidationEvent;
011import javax.xml.bind.helpers.PrintConversionEventImpl;
012import javax.xml.bind.helpers.ValidationEventImpl;
013import javax.xml.bind.helpers.ValidationEventLocatorImpl;
014
015import org.xml.sax.SAXException;
016
017import com.sun.xml.bind.Messages;
018import com.sun.xml.bind.serializer.AbortSerializationException;
019import com.sun.xml.bind.util.ValidationEventLocatorExImpl;
020
021/**
022 * 
023 * @author
024 *     Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
025 */
026public class Util {
027    /**
028     * Reports a print conversion error while marshalling.
029     */
030    public static void handlePrintConversionException(
031        Object caller, Exception e, XMLSerializer serializer ) throws SAXException {
032        
033        if( e instanceof SAXException )
034            // assume this exception is not from application.
035            // (e.g., when a marshaller aborts the processing, this exception
036            //        will be thrown) 
037            throw (SAXException)e;
038        
039        String message = e.getMessage();
040        if(message==null) {
041            message = e.toString();
042        }
043        
044        ValidationEvent ve = new PrintConversionEventImpl(
045            ValidationEvent.ERROR, message,
046            new ValidationEventLocatorImpl(caller), e );
047        serializer.reportError(ve);
048    }
049    
050    /**
051     * Reports that the type of an object in a property is unexpected.  
052     */
053    public static void handleTypeMismatchError( XMLSerializer serializer,
054            Object parentObject, String fieldName, Object childObject ) throws AbortSerializationException {
055        
056         ValidationEvent ve = new ValidationEventImpl(
057            ValidationEvent.ERROR, // maybe it should be a fatal error.
058            Messages.format(Messages.ERR_TYPE_MISMATCH,
059                getUserFriendlyTypeName(parentObject),
060                fieldName,
061                getUserFriendlyTypeName(childObject) ),
062            new ValidationEventLocatorExImpl(parentObject,fieldName) );
063         
064        serializer.reportError(ve);
065    }
066    
067    private static String getUserFriendlyTypeName( Object o ) {
068        if( o instanceof ValidatableObject )
069            return ((ValidatableObject)o).getPrimaryInterface().getName();
070        else
071            return o.getClass().getName();
072    }
073}