001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.bind.v2.runtime;
038:
039: import java.util.Collections;
040: import java.util.HashMap;
041: import java.util.Map;
042:
043: import javax.xml.bind.ValidationEvent;
044: import javax.xml.bind.annotation.adapters.XmlAdapter;
045: import javax.xml.bind.helpers.PrintConversionEventImpl;
046: import javax.xml.bind.helpers.ValidationEventImpl;
047: import javax.xml.bind.helpers.ValidationEventLocatorImpl;
048:
049: import com.sun.xml.bind.util.ValidationEventLocatorExImpl;
050:
051: import org.xml.sax.SAXException;
052:
053: /**
054: * @author Kohsuke Kawaguchi
055: */
056: public class RuntimeUtil {
057: /**
058: * XmlAdapter for printing arbitrary object by using {@link Object#toString()}.
059: */
060: public static final class ToStringAdapter extends
061: XmlAdapter<String, Object> {
062: public Object unmarshal(String s) {
063: throw new UnsupportedOperationException();
064: }
065:
066: public String marshal(Object o) {
067: if (o == null)
068: return null;
069: return o.toString();
070: }
071: }
072:
073: /**
074: * Map from {@link Class} objects representing primitive types
075: * to {@link Class} objects representing their boxed types.
076: * <p>
077: * e.g., int -> Integer.
078: */
079: public static final Map<Class, Class> boxToPrimitive;
080:
081: /**
082: * Reverse map of {@link #boxToPrimitive}.
083: */
084: public static final Map<Class, Class> primitiveToBox;
085:
086: static {
087: Map<Class, Class> b = new HashMap<Class, Class>();
088: b.put(Byte.TYPE, Byte.class);
089: b.put(Short.TYPE, Short.class);
090: b.put(Integer.TYPE, Integer.class);
091: b.put(Long.TYPE, Long.class);
092: b.put(Character.TYPE, Character.class);
093: b.put(Boolean.TYPE, Boolean.class);
094: b.put(Float.TYPE, Float.class);
095: b.put(Double.TYPE, Double.class);
096: b.put(Void.TYPE, Void.class);
097:
098: primitiveToBox = Collections.unmodifiableMap(b);
099:
100: Map<Class, Class> p = new HashMap<Class, Class>();
101: for (Map.Entry<Class, Class> e : b.entrySet())
102: p.put(e.getValue(), e.getKey());
103:
104: boxToPrimitive = Collections.unmodifiableMap(p);
105: }
106:
107: /**
108: * Reports a print conversion error while marshalling.
109: */
110: public static void handlePrintConversionException(Object caller,
111: Exception e, XMLSerializer serializer) throws SAXException {
112:
113: if (e instanceof SAXException)
114: // assume this exception is not from application.
115: // (e.g., when a marshaller aborts the processing, this exception
116: // will be thrown)
117: throw (SAXException) e;
118:
119: ValidationEvent ve = new PrintConversionEventImpl(
120: ValidationEvent.ERROR, e.getMessage(),
121: new ValidationEventLocatorImpl(caller), e);
122: serializer.reportError(ve);
123: }
124:
125: /**
126: * Reports that the type of an object in a property is unexpected.
127: */
128: public static void handleTypeMismatchError(
129: XMLSerializer serializer, Object parentObject,
130: String fieldName, Object childObject) throws SAXException {
131:
132: ValidationEvent ve = new ValidationEventImpl(
133: ValidationEvent.ERROR, // maybe it should be a fatal error.
134: Messages.TYPE_MISMATCH.format(
135: getTypeName(parentObject), fieldName,
136: getTypeName(childObject)),
137: new ValidationEventLocatorExImpl(parentObject,
138: fieldName));
139:
140: serializer.reportError(ve);
141: }
142:
143: private static String getTypeName(Object o) {
144: return o.getClass().getName();
145: }
146: }
|