ohair@286: /* ohair@286: * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package com.sun.xml.internal.bind.v2.runtime; ohair@286: ohair@286: import java.util.Collections; ohair@286: import java.util.HashMap; ohair@286: import java.util.Map; ohair@286: ohair@286: import javax.xml.bind.annotation.adapters.XmlAdapter; ohair@286: ohair@286: /** ohair@286: * @author Kohsuke Kawaguchi ohair@286: */ ohair@286: public class RuntimeUtil { ohair@286: /** ohair@286: * XmlAdapter for printing arbitrary object by using {@link Object#toString()}. ohair@286: */ ohair@286: public static final class ToStringAdapter extends XmlAdapter { ohair@286: public Object unmarshal(String s) { ohair@286: throw new UnsupportedOperationException(); ohair@286: } ohair@286: ohair@286: public String marshal(Object o) { ohair@286: if(o==null) return null; ohair@286: return o.toString(); ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * Map from {@link Class} objects representing primitive types ohair@286: * to {@link Class} objects representing their boxed types. ohair@286: *

ohair@286: * e.g., int -> Integer. ohair@286: */ ohair@286: public static final Map boxToPrimitive; ohair@286: ohair@286: /** ohair@286: * Reverse map of {@link #boxToPrimitive}. ohair@286: */ ohair@286: public static final Map primitiveToBox; ohair@286: ohair@286: static { ohair@286: Map b = new HashMap(); ohair@286: b.put(Byte.TYPE,Byte.class); ohair@286: b.put(Short.TYPE,Short.class); ohair@286: b.put(Integer.TYPE,Integer.class); ohair@286: b.put(Long.TYPE,Long.class); ohair@286: b.put(Character.TYPE,Character.class); ohair@286: b.put(Boolean.TYPE,Boolean.class); ohair@286: b.put(Float.TYPE,Float.class); ohair@286: b.put(Double.TYPE,Double.class); ohair@286: b.put(Void.TYPE,Void.class); ohair@286: ohair@286: primitiveToBox = Collections.unmodifiableMap(b); ohair@286: ohair@286: Map p = new HashMap(); ohair@286: for( Map.Entry e : b.entrySet() ) ohair@286: p.put(e.getValue(),e.getKey()); ohair@286: ohair@286: boxToPrimitive = Collections.unmodifiableMap(p); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Reports a print conversion error while marshalling. ohair@286: */ alanb@368: /* ohair@286: public static void handlePrintConversionException( ohair@286: Object caller, Exception e, XMLSerializer serializer ) throws SAXException { ohair@286: ohair@286: if( e instanceof SAXException ) ohair@286: // assume this exception is not from application. ohair@286: // (e.g., when a marshaller aborts the processing, this exception ohair@286: // will be thrown) ohair@286: throw (SAXException)e; ohair@286: ohair@286: ValidationEvent ve = new PrintConversionEventImpl( ohair@286: ValidationEvent.ERROR, e.getMessage(), ohair@286: new ValidationEventLocatorImpl(caller), e ); ohair@286: serializer.reportError(ve); ohair@286: } alanb@368: */ ohair@286: ohair@286: /** ohair@286: * Reports that the type of an object in a property is unexpected. ohair@286: */ alanb@368: /* ohair@286: public static void handleTypeMismatchError( XMLSerializer serializer, ohair@286: Object parentObject, String fieldName, Object childObject ) throws SAXException { ohair@286: ohair@286: ValidationEvent ve = new ValidationEventImpl( ohair@286: ValidationEvent.ERROR, // maybe it should be a fatal error. ohair@286: Messages.TYPE_MISMATCH.format( ohair@286: getTypeName(parentObject), ohair@286: fieldName, ohair@286: getTypeName(childObject) ), ohair@286: new ValidationEventLocatorExImpl(parentObject,fieldName) ); ohair@286: ohair@286: serializer.reportError(ve); ohair@286: } alanb@368: */ ohair@286: ohair@286: private static String getTypeName( Object o ) { ohair@286: return o.getClass().getName(); ohair@286: } ohair@286: }