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

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