1 22 package org.jboss.xb.binding.sunday.unmarshalling.impl.runtime; 23 24 import java.lang.reflect.Method ; 25 import javax.xml.namespace.NamespaceContext ; 26 import javax.xml.namespace.QName ; 27 import org.jboss.xb.binding.Constants; 28 import org.jboss.xb.binding.JBossXBRuntimeException; 29 import org.jboss.xb.binding.Util; 30 import org.jboss.xb.binding.metadata.CharactersMetaData; 31 import org.jboss.xb.binding.metadata.PropertyMetaData; 32 import org.jboss.xb.binding.metadata.ValueMetaData; 33 import org.jboss.xb.binding.sunday.unmarshalling.CharactersHandler; 34 import org.jboss.xb.binding.sunday.unmarshalling.ElementBinding; 35 import org.jboss.xb.binding.sunday.unmarshalling.TypeBinding; 36 import org.jboss.util.Classes; 37 38 42 public class RtCharactersHandler 43 extends CharactersHandler 44 { 45 public static final RtCharactersHandler INSTANCE = new RtCharactersHandler(); 46 47 public Object unmarshal(QName qName, 48 TypeBinding typeBinding, 49 NamespaceContext nsCtx, 50 ValueMetaData valueMetaData, 51 String value) 52 { 53 Object unmarshalled = null; 54 if(valueMetaData != null) 55 { 56 Method unmarshalMethod = RtUtil.getUnmarshalMethod(qName, valueMetaData); 57 Object args[] = unmarshalMethod.getParameterTypes().length == 1 ? 58 new Object []{value} : 59 new Object []{value, nsCtx}; 60 unmarshalled = RtUtil.invokeUnmarshalMethod(unmarshalMethod, args, qName); 61 } 62 else 63 { 64 unmarshalled = super.unmarshal(qName, typeBinding, nsCtx, valueMetaData, value); 65 66 if(typeBinding.isSimple()) 67 { 68 String clsName = null; 69 boolean failIfNotFound = false; 70 if(typeBinding.getClassMetaData() != null) 71 { 72 clsName = typeBinding.getClassMetaData().getImpl(); 73 failIfNotFound = true; 74 } 75 else 76 { 77 QName typeName = typeBinding.getQName(); 78 if(typeName != null && !Constants.NS_XML_SCHEMA.equals(typeName.getNamespaceURI())) 79 { 80 boolean ignoreLowLine = typeBinding.getSchemaBinding() != null ? 81 typeBinding.getSchemaBinding().isIgnoreLowLine() : 82 true; 83 clsName = 84 Util.xmlNameToClassName(typeName.getNamespaceURI(), typeName.getLocalPart(), ignoreLowLine); 85 } 86 } 87 88 Class cls = clsName == null ? null : RtUtil.loadClass(clsName, failIfNotFound); 89 if(cls != null && !cls.isPrimitive()) 90 { 91 if(cls.getConstructors().length == 0) 95 { 96 Class valueType = unmarshalled.getClass(); 97 if(Classes.isPrimitiveWrapper(valueType)) 99 { 100 valueType = Classes.getPrimitive(valueType); 101 } 102 103 unmarshalled = unmarshalled == null ? null : 105 RtUtil.invokeUnmarshalMethod(cls, "fromValue", unmarshalled, valueType, nsCtx, qName); 106 } 107 else 108 { 109 throw new JBossXBRuntimeException("This case is not yet supported (create a feature request): " + 110 "simple type (" + 111 typeBinding.getQName() + 112 ") is bound to a class (" + 113 cls + 114 ") with optional property metadata with " + 115 "default value for the property name 'value'." 116 ); 117 } 118 } 119 } 120 } 121 122 return unmarshalled; 123 } 124 125 public void setValue(QName qName, ElementBinding element, Object owner, Object value) 126 { 127 129 if(owner == null) { 131 return; 132 } 133 134 if (owner instanceof MapEntry) 135 { 136 TypeBinding type = element.getType(); 137 CharactersMetaData characters = type.getCharactersMetaData(); 138 if (characters != null) 139 { 140 if (characters.isMapEntryKey()) 141 { 142 ((MapEntry) owner).setKey(value); 143 } 144 else if (characters.isMapEntryValue()) 145 { 146 ((MapEntry) owner).setValue(value); 147 } 148 else 149 { 150 throw new JBossXBRuntimeException("Parent object is a map entry but characters of element " + qName 151 + " of type " + type.getQName() + " were bound to niether key nor value in a map entry."); 152 } 153 } 154 else 155 { 156 throw new JBossXBRuntimeException("Parent object is a map entry but characters of element " + qName 157 + " of type " + type.getQName() + " were bound to niether key nor value in a map entry."); 158 } 159 } 160 else 161 { 162 String propName = null; 163 String colType = null; 164 TypeBinding type = element.getType(); 165 if (type != null && !type.isSimple()) 166 { 167 PropertyMetaData propertyMetaData = type.getPropertyMetaData(); 168 if (propertyMetaData == null) 169 { 170 CharactersMetaData charactersMetaData = type.getCharactersMetaData(); 171 propertyMetaData = charactersMetaData == null ? null : charactersMetaData.getProperty(); 172 } 173 174 if (propertyMetaData != null) 175 { 176 propName = propertyMetaData.getName(); 177 colType = propertyMetaData.getCollectionType(); 178 } 179 180 if (propName == null) 181 { 182 propName = type.getSchemaBinding().getSimpleContentProperty(); 183 } 184 } 185 else 186 { 187 PropertyMetaData PropertyMetaData = element.getPropertyMetaData(); 188 if (PropertyMetaData != null) 189 { 190 propName = PropertyMetaData.getName(); 191 colType = PropertyMetaData.getCollectionType(); 192 } 193 194 if (propName == null) 195 { 196 propName = Util.xmlNameToFieldName(qName.getLocalPart(), element.getSchema().isIgnoreLowLine()); 197 } 198 } 199 200 RtUtil.set(owner, value, propName, colType, element.getSchema().isIgnoreUnresolvedFieldOrClass(), element 201 .getValueAdapter()); 202 } 203 } 204 } 205 | Popular Tags |