325N/A/*
325N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.xml.internal.bind.v2.runtime;
325N/A
325N/Aimport java.util.Collections;
325N/Aimport java.util.HashMap;
325N/Aimport java.util.Map;
325N/A
325N/Aimport javax.xml.bind.ValidationEvent;
325N/Aimport javax.xml.bind.annotation.adapters.XmlAdapter;
325N/Aimport javax.xml.bind.helpers.PrintConversionEventImpl;
325N/Aimport javax.xml.bind.helpers.ValidationEventImpl;
325N/Aimport javax.xml.bind.helpers.ValidationEventLocatorImpl;
325N/A
325N/Aimport com.sun.xml.internal.bind.util.ValidationEventLocatorExImpl;
325N/A
325N/Aimport org.xml.sax.SAXException;
325N/A
325N/A
325N/A/**
325N/A * @author Kohsuke Kawaguchi
325N/A */
325N/Apublic class RuntimeUtil {
325N/A /**
325N/A * XmlAdapter for printing arbitrary object by using {@link Object#toString()}.
325N/A */
325N/A public static final class ToStringAdapter extends XmlAdapter<String,Object> {
325N/A public Object unmarshal(String s) {
325N/A throw new UnsupportedOperationException();
325N/A }
325N/A
325N/A public String marshal(Object o) {
325N/A if(o==null) return null;
325N/A return o.toString();
325N/A }
325N/A }
325N/A
325N/A /**
325N/A * Map from {@link Class} objects representing primitive types
325N/A * to {@link Class} objects representing their boxed types.
325N/A * <p>
325N/A * e.g., int -> Integer.
325N/A */
325N/A public static final Map<Class,Class> boxToPrimitive;
325N/A
325N/A /**
325N/A * Reverse map of {@link #boxToPrimitive}.
325N/A */
325N/A public static final Map<Class,Class> primitiveToBox;
325N/A
325N/A static {
325N/A Map<Class,Class> b = new HashMap<Class,Class>();
325N/A b.put(Byte.TYPE,Byte.class);
325N/A b.put(Short.TYPE,Short.class);
325N/A b.put(Integer.TYPE,Integer.class);
325N/A b.put(Long.TYPE,Long.class);
325N/A b.put(Character.TYPE,Character.class);
325N/A b.put(Boolean.TYPE,Boolean.class);
325N/A b.put(Float.TYPE,Float.class);
325N/A b.put(Double.TYPE,Double.class);
325N/A b.put(Void.TYPE,Void.class);
325N/A
325N/A primitiveToBox = Collections.unmodifiableMap(b);
325N/A
325N/A Map<Class,Class> p = new HashMap<Class,Class>();
325N/A for( Map.Entry<Class,Class> e : b.entrySet() )
325N/A p.put(e.getValue(),e.getKey());
325N/A
325N/A boxToPrimitive = Collections.unmodifiableMap(p);
325N/A }
325N/A
325N/A /**
325N/A * Reports a print conversion error while marshalling.
325N/A */
325N/A public static void handlePrintConversionException(
325N/A Object caller, Exception e, XMLSerializer serializer ) throws SAXException {
325N/A
325N/A if( e instanceof SAXException )
325N/A // assume this exception is not from application.
325N/A // (e.g., when a marshaller aborts the processing, this exception
325N/A // will be thrown)
325N/A throw (SAXException)e;
325N/A
325N/A ValidationEvent ve = new PrintConversionEventImpl(
325N/A ValidationEvent.ERROR, e.getMessage(),
325N/A new ValidationEventLocatorImpl(caller), e );
325N/A serializer.reportError(ve);
325N/A }
325N/A
325N/A /**
325N/A * Reports that the type of an object in a property is unexpected.
325N/A */
325N/A public static void handleTypeMismatchError( XMLSerializer serializer,
325N/A Object parentObject, String fieldName, Object childObject ) throws SAXException {
325N/A
325N/A ValidationEvent ve = new ValidationEventImpl(
325N/A ValidationEvent.ERROR, // maybe it should be a fatal error.
325N/A Messages.TYPE_MISMATCH.format(
325N/A getTypeName(parentObject),
325N/A fieldName,
325N/A getTypeName(childObject) ),
325N/A new ValidationEventLocatorExImpl(parentObject,fieldName) );
325N/A
325N/A serializer.reportError(ve);
325N/A }
325N/A
325N/A private static String getTypeName( Object o ) {
325N/A return o.getClass().getName();
325N/A }
325N/A}