1/*
2 * Copyright (c) 1997, 2015, 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
26package com.sun.xml.internal.bind.v2.runtime;
27
28import java.util.Collections;
29import java.util.HashMap;
30import java.util.Map;
31
32import javax.xml.bind.annotation.adapters.XmlAdapter;
33
34/**
35 * @author Kohsuke Kawaguchi
36 */
37public class RuntimeUtil {
38    /**
39     * XmlAdapter for printing arbitrary object by using {@link Object#toString()}.
40     */
41    public static final class ToStringAdapter extends XmlAdapter<String,Object> {
42        public Object unmarshal(String s) {
43            throw new UnsupportedOperationException();
44        }
45
46        public String marshal(Object o) {
47            if(o==null)     return null;
48            return o.toString();
49        }
50    }
51
52    /**
53     * Map from {@link Class} objects representing primitive types
54     * to {@link Class} objects representing their boxed types.
55     * <p>
56     * e.g., {@code int -> Integer}.
57     */
58    public static final Map<Class,Class> boxToPrimitive;
59
60    /**
61     * Reverse map of {@link #boxToPrimitive}.
62     */
63    public static final Map<Class,Class> primitiveToBox;
64
65    static {
66        Map<Class,Class> b = new HashMap<Class,Class>();
67        b.put(Byte.TYPE,Byte.class);
68        b.put(Short.TYPE,Short.class);
69        b.put(Integer.TYPE,Integer.class);
70        b.put(Long.TYPE,Long.class);
71        b.put(Character.TYPE,Character.class);
72        b.put(Boolean.TYPE,Boolean.class);
73        b.put(Float.TYPE,Float.class);
74        b.put(Double.TYPE,Double.class);
75        b.put(Void.TYPE,Void.class);
76
77        primitiveToBox = Collections.unmodifiableMap(b);
78
79        Map<Class,Class> p = new HashMap<Class,Class>();
80        for( Map.Entry<Class,Class> e :  b.entrySet() )
81            p.put(e.getValue(),e.getKey());
82
83        boxToPrimitive = Collections.unmodifiableMap(p);
84    }
85
86    /**
87     * Reports a print conversion error while marshalling.
88     */
89/*
90    public static void handlePrintConversionException(
91        Object caller, Exception e, XMLSerializer serializer ) throws SAXException {
92
93        if( e instanceof SAXException )
94            // assume this exception is not from application.
95            // (e.g., when a marshaller aborts the processing, this exception
96            //        will be thrown)
97            throw (SAXException)e;
98
99        ValidationEvent ve = new PrintConversionEventImpl(
100            ValidationEvent.ERROR, e.getMessage(),
101            new ValidationEventLocatorImpl(caller), e );
102        serializer.reportError(ve);
103    }
104*/
105
106    /**
107     * Reports that the type of an object in a property is unexpected.
108     */
109/*
110    public static void handleTypeMismatchError( XMLSerializer serializer,
111            Object parentObject, String fieldName, Object childObject ) throws SAXException {
112
113         ValidationEvent ve = new ValidationEventImpl(
114            ValidationEvent.ERROR, // maybe it should be a fatal error.
115             Messages.TYPE_MISMATCH.format(
116                getTypeName(parentObject),
117                fieldName,
118                getTypeName(childObject) ),
119            new ValidationEventLocatorExImpl(parentObject,fieldName) );
120
121        serializer.reportError(ve);
122    }
123*/
124
125    private static String getTypeName( Object o ) {
126        return o.getClass().getName();
127    }
128}
129