1 /*
   2  * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.bind.v2.runtime;
  27 
  28 import java.util.Collections;
  29 import java.util.HashMap;
  30 import java.util.Map;
  31 
  32 import javax.xml.bind.ValidationEvent;
  33 import javax.xml.bind.annotation.adapters.XmlAdapter;
  34 import javax.xml.bind.helpers.PrintConversionEventImpl;
  35 import javax.xml.bind.helpers.ValidationEventImpl;
  36 import javax.xml.bind.helpers.ValidationEventLocatorImpl;
  37 
  38 import com.sun.xml.internal.bind.util.ValidationEventLocatorExImpl;
  39 
  40 import org.xml.sax.SAXException;
  41 
  42 
  43 /**
  44  * @author Kohsuke Kawaguchi
  45  */
  46 public class RuntimeUtil {
  47     /**
  48      * XmlAdapter for printing arbitrary object by using {@link Object#toString()}.
  49      */
  50     public static final class ToStringAdapter extends XmlAdapter<String,Object> {
  51         public Object unmarshal(String s) {
  52             throw new UnsupportedOperationException();
  53         }
  54 
  55         public String marshal(Object o) {
  56             if(o==null)     return null;
  57             return o.toString();
  58         }
  59     }
  60 
  61     /**
  62      * Map from {@link Class} objects representing primitive types
  63      * to {@link Class} objects representing their boxed types.
  64      * <p>
  65      * e.g., int -> Integer.
  66      */
  67     public static final Map<Class,Class> boxToPrimitive;
  68 
  69     /**
  70      * Reverse map of {@link #boxToPrimitive}.
  71      */
  72     public static final Map<Class,Class> primitiveToBox;
  73 
  74     static {
  75         Map<Class,Class> b = new HashMap<Class,Class>();
  76         b.put(Byte.TYPE,Byte.class);
  77         b.put(Short.TYPE,Short.class);
  78         b.put(Integer.TYPE,Integer.class);
  79         b.put(Long.TYPE,Long.class);
  80         b.put(Character.TYPE,Character.class);
  81         b.put(Boolean.TYPE,Boolean.class);
  82         b.put(Float.TYPE,Float.class);
  83         b.put(Double.TYPE,Double.class);
  84         b.put(Void.TYPE,Void.class);
  85 
  86         primitiveToBox = Collections.unmodifiableMap(b);
  87 
  88         Map<Class,Class> p = new HashMap<Class,Class>();
  89         for( Map.Entry<Class,Class> e :  b.entrySet() )
  90             p.put(e.getValue(),e.getKey());
  91 
  92         boxToPrimitive = Collections.unmodifiableMap(p);
  93     }
  94 
  95     /**
  96      * Reports a print conversion error while marshalling.
  97      */
  98     public static void handlePrintConversionException(
  99         Object caller, Exception e, XMLSerializer serializer ) throws SAXException {
 100 
 101         if( e instanceof SAXException )
 102             // assume this exception is not from application.
 103             // (e.g., when a marshaller aborts the processing, this exception
 104             //        will be thrown)
 105             throw (SAXException)e;
 106 
 107         ValidationEvent ve = new PrintConversionEventImpl(
 108             ValidationEvent.ERROR, e.getMessage(),
 109             new ValidationEventLocatorImpl(caller), e );
 110         serializer.reportError(ve);
 111     }
 112 
 113     /**
 114      * Reports that the type of an object in a property is unexpected.
 115      */
 116     public static void handleTypeMismatchError( XMLSerializer serializer,
 117             Object parentObject, String fieldName, Object childObject ) throws SAXException {
 118 
 119          ValidationEvent ve = new ValidationEventImpl(
 120             ValidationEvent.ERROR, // maybe it should be a fatal error.
 121              Messages.TYPE_MISMATCH.format(
 122                 getTypeName(parentObject),
 123                 fieldName,
 124                 getTypeName(childObject) ),
 125             new ValidationEventLocatorExImpl(parentObject,fieldName) );
 126 
 127         serializer.reportError(ve);
 128     }
 129 
 130     private static String getTypeName( Object o ) {
 131         return o.getClass().getName();
 132     }
 133 }