001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.xml.internal.bind.v2.runtime;
027:
028: import java.util.Collections;
029: import java.util.HashMap;
030: import java.util.Map;
031:
032: import javax.xml.bind.ValidationEvent;
033: import javax.xml.bind.annotation.adapters.XmlAdapter;
034: import javax.xml.bind.helpers.PrintConversionEventImpl;
035: import javax.xml.bind.helpers.ValidationEventImpl;
036: import javax.xml.bind.helpers.ValidationEventLocatorImpl;
037:
038: import com.sun.xml.internal.bind.util.ValidationEventLocatorExImpl;
039:
040: import org.xml.sax.SAXException;
041:
042: /**
043: * @author Kohsuke Kawaguchi
044: */
045: public class RuntimeUtil {
046: /**
047: * XmlAdapter for printing arbitrary object by using {@link Object#toString()}.
048: */
049: public static final class ToStringAdapter extends
050: XmlAdapter<String, Object> {
051: public Object unmarshal(String s) {
052: throw new UnsupportedOperationException();
053: }
054:
055: public String marshal(Object o) {
056: if (o == null)
057: return null;
058: return o.toString();
059: }
060: }
061:
062: /**
063: * Map from {@link Class} objects representing primitive types
064: * to {@link Class} objects representing their boxed types.
065: * <p>
066: * e.g., int -> Integer.
067: */
068: public static final Map<Class, Class> boxToPrimitive;
069:
070: /**
071: * Reverse map of {@link #boxToPrimitive}.
072: */
073: public static final Map<Class, Class> primitiveToBox;
074:
075: static {
076: Map<Class, Class> b = new HashMap<Class, Class>();
077: b.put(Byte.TYPE, Byte.class);
078: b.put(Short.TYPE, Short.class);
079: b.put(Integer.TYPE, Integer.class);
080: b.put(Long.TYPE, Long.class);
081: b.put(Character.TYPE, Character.class);
082: b.put(Boolean.TYPE, Boolean.class);
083: b.put(Float.TYPE, Float.class);
084: b.put(Double.TYPE, Double.class);
085: b.put(Void.TYPE, Void.class);
086:
087: primitiveToBox = Collections.unmodifiableMap(b);
088:
089: Map<Class, Class> p = new HashMap<Class, Class>();
090: for (Map.Entry<Class, Class> e : b.entrySet())
091: p.put(e.getValue(), e.getKey());
092:
093: boxToPrimitive = Collections.unmodifiableMap(p);
094: }
095:
096: /**
097: * Reports a print conversion error while marshalling.
098: */
099: public static void handlePrintConversionException(Object caller,
100: Exception e, XMLSerializer serializer) throws SAXException {
101:
102: if (e instanceof SAXException)
103: // assume this exception is not from application.
104: // (e.g., when a marshaller aborts the processing, this exception
105: // will be thrown)
106: throw (SAXException) e;
107:
108: ValidationEvent ve = new PrintConversionEventImpl(
109: ValidationEvent.ERROR, e.getMessage(),
110: new ValidationEventLocatorImpl(caller), e);
111: serializer.reportError(ve);
112: }
113:
114: /**
115: * Reports that the type of an object in a property is unexpected.
116: */
117: public static void handleTypeMismatchError(
118: XMLSerializer serializer, Object parentObject,
119: String fieldName, Object childObject) throws SAXException {
120:
121: ValidationEvent ve = new ValidationEventImpl(
122: ValidationEvent.ERROR, // maybe it should be a fatal error.
123: Messages.TYPE_MISMATCH.format(
124: getTypeName(parentObject), fieldName,
125: getTypeName(childObject)),
126: new ValidationEventLocatorExImpl(parentObject,
127: fieldName));
128:
129: serializer.reportError(ve);
130: }
131:
132: private static String getTypeName(Object o) {
133: return o.getClass().getName();
134: }
135: }
|