12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.xml.internal.bind.v2.runtime; 27 28 import java.util.Collections; 29 import java.util.HashMap; 30 import java.util.Map; 31 32 import javax.xml.bind.ValidationEvent; 33 import javax.xml.bind.annotation.adapters.XmlAdapter; 34 import javax.xml.bind.helpers.PrintConversionEventImpl; 35 import javax.xml.bind.helpers.ValidationEventImpl; 36 import javax.xml.bind.helpers.ValidationEventLocatorImpl; 37 38 import com.sun.xml.internal.bind.util.ValidationEventLocatorExImpl; 39 40 import org.xml.sax.SAXException; 41 42 43 /** 44 * @author Kohsuke Kawaguchi 45 */ 46 public class RuntimeUtil { 47 /** 48 * XmlAdapter for printing arbitrary object by using {@link Object#toString()}. 49 */ 50 public static final class ToStringAdapter extends XmlAdapter<String,Object> { 51 public Object unmarshal(String s) { 52 throw new UnsupportedOperationException(); 53 } 54 55 public String marshal(Object o) { 56 if(o==null) return null; 57 return o.toString(); 58 } 59 } 60 61 /** 78 b.put(Integer.TYPE,Integer.class); 79 b.put(Long.TYPE,Long.class); 80 b.put(Character.TYPE,Character.class); 81 b.put(Boolean.TYPE,Boolean.class); 82 b.put(Float.TYPE,Float.class); 83 b.put(Double.TYPE,Double.class); 84 b.put(Void.TYPE,Void.class); 85 86 primitiveToBox = Collections.unmodifiableMap(b); 87 88 Map<Class,Class> p = new HashMap<Class,Class>(); 89 for( Map.Entry<Class,Class> e : b.entrySet() ) 90 p.put(e.getValue(),e.getKey()); 91 92 boxToPrimitive = Collections.unmodifiableMap(p); 93 } 94 95 /** 96 * Reports a print conversion error while marshalling. 97 */ 98 public static void handlePrintConversionException( 99 Object caller, Exception e, XMLSerializer serializer ) throws SAXException { 100 101 if( e instanceof SAXException ) 102 // assume this exception is not from application. 103 // (e.g., when a marshaller aborts the processing, this exception 104 // will be thrown) 105 throw (SAXException)e; 106 107 ValidationEvent ve = new PrintConversionEventImpl( 108 ValidationEvent.ERROR, e.getMessage(), 109 new ValidationEventLocatorImpl(caller), e ); 110 serializer.reportError(ve); 111 } 112 113 /** 114 * Reports that the type of an object in a property is unexpected. 115 */ 116 public static void handleTypeMismatchError( XMLSerializer serializer, 117 Object parentObject, String fieldName, Object childObject ) throws SAXException { 118 119 ValidationEvent ve = new ValidationEventImpl( 120 ValidationEvent.ERROR, // maybe it should be a fatal error. 121 Messages.TYPE_MISMATCH.format( 122 getTypeName(parentObject), 123 fieldName, 124 getTypeName(childObject) ), 125 new ValidationEventLocatorExImpl(parentObject,fieldName) ); 126 127 serializer.reportError(ve); 128 } 129 130 private static String getTypeName( Object o ) { 131 return o.getClass().getName(); 132 } 133 } | 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.xml.internal.bind.v2.runtime; 27 28 import java.util.Collections; 29 import java.util.HashMap; 30 import java.util.Map; 31 32 import javax.xml.bind.annotation.adapters.XmlAdapter; 33 34 /** 35 * @author Kohsuke Kawaguchi 36 */ 37 public class RuntimeUtil { 38 /** 39 * XmlAdapter for printing arbitrary object by using {@link Object#toString()}. 40 */ 41 public static final class ToStringAdapter extends XmlAdapter<String,Object> { 42 public Object unmarshal(String s) { 43 throw new UnsupportedOperationException(); 44 } 45 46 public String marshal(Object o) { 47 if(o==null) return null; 48 return o.toString(); 49 } 50 } 51 52 /** 69 b.put(Integer.TYPE,Integer.class); 70 b.put(Long.TYPE,Long.class); 71 b.put(Character.TYPE,Character.class); 72 b.put(Boolean.TYPE,Boolean.class); 73 b.put(Float.TYPE,Float.class); 74 b.put(Double.TYPE,Double.class); 75 b.put(Void.TYPE,Void.class); 76 77 primitiveToBox = Collections.unmodifiableMap(b); 78 79 Map<Class,Class> p = new HashMap<Class,Class>(); 80 for( Map.Entry<Class,Class> e : b.entrySet() ) 81 p.put(e.getValue(),e.getKey()); 82 83 boxToPrimitive = Collections.unmodifiableMap(p); 84 } 85 86 /** 87 * Reports a print conversion error while marshalling. 88 */ 89 /* 90 public static void handlePrintConversionException( 91 Object caller, Exception e, XMLSerializer serializer ) throws SAXException { 92 93 if( e instanceof SAXException ) 94 // assume this exception is not from application. 95 // (e.g., when a marshaller aborts the processing, this exception 96 // will be thrown) 97 throw (SAXException)e; 98 99 ValidationEvent ve = new PrintConversionEventImpl( 100 ValidationEvent.ERROR, e.getMessage(), 101 new ValidationEventLocatorImpl(caller), e ); 102 serializer.reportError(ve); 103 } 104 */ 105 106 /** 107 * Reports that the type of an object in a property is unexpected. 108 */ 109 /* 110 public static void handleTypeMismatchError( XMLSerializer serializer, 111 Object parentObject, String fieldName, Object childObject ) throws SAXException { 112 113 ValidationEvent ve = new ValidationEventImpl( 114 ValidationEvent.ERROR, // maybe it should be a fatal error. 115 Messages.TYPE_MISMATCH.format( 116 getTypeName(parentObject), 117 fieldName, 118 getTypeName(childObject) ), 119 new ValidationEventLocatorExImpl(parentObject,fieldName) ); 120 121 serializer.reportError(ve); 122 } 123 */ 124 125 private static String getTypeName( Object o ) { 126 return o.getClass().getName(); 127 } 128 } |