1 22 package org.jboss.xb.binding.sunday.marshalling; 23 24 import java.util.Arrays ; 25 import java.util.List ; 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.SimpleTypeBindings; 30 import org.jboss.xb.binding.Util; 31 import org.jboss.xb.binding.introspection.FieldInfo; 32 import org.jboss.xb.binding.metadata.PropertyMetaData; 33 import org.jboss.xb.binding.sunday.unmarshalling.AttributeBinding; 34 import org.jboss.xb.binding.sunday.unmarshalling.SchemaBinding; 35 import org.jboss.xb.binding.sunday.unmarshalling.TypeBinding; 36 37 38 42 public class DefaultAttributeMarshaller 43 extends AbstractAttributeMarshaller 44 { 45 public static final DefaultAttributeMarshaller INSTANCE = new DefaultAttributeMarshaller(); 46 47 public Object getValue(MarshallingContext ctx) 48 { 49 Object owner = ctx.peek(); 50 SchemaBinding schema = ctx.getSchemaBinding(); 51 AttributeBinding binding = ctx.getAttributeBinding(); 52 QName qName = binding.getQName(); 53 54 String fieldName = null; 55 PropertyMetaData propertyMetaData = binding.getPropertyMetaData(); 56 if(propertyMetaData != null) 57 { 58 fieldName = propertyMetaData.getName(); 59 } 60 61 if(fieldName == null) 62 { 63 fieldName = 64 Util.xmlNameToFieldName(qName.getLocalPart(), schema.isIgnoreLowLine()); 65 } 66 67 FieldInfo fieldInfo = FieldInfo.getFieldInfo( 68 owner.getClass(), fieldName, binding.getRequired() && !schema.isIgnoreUnresolvedFieldOrClass() 69 ); 70 Object value = null; 71 if(fieldInfo != null) 72 { 73 value = fieldInfo.getValue(owner); 74 } 75 76 return value; 77 } 78 79 public String marshalValue(MarshallingContext ctx, Object value) 80 { 81 if(value == null) 82 { 83 return null; 84 } 85 86 String marshalled; 87 88 AttributeBinding binding = ctx.getAttributeBinding(); 89 TypeBinding attrType = binding.getType(); 90 91 if(attrType.getItemType() != null) 92 { 93 TypeBinding itemType = attrType.getItemType(); 94 if(Constants.NS_XML_SCHEMA.equals(itemType.getQName().getNamespaceURI())) 95 { 96 List list; 97 if(value instanceof List ) 98 { 99 list = (List )value; 100 } 101 else if(value.getClass().isArray()) 102 { 103 list = Arrays.asList((Object [])value); 104 } 105 else 106 { 107 throw new JBossXBRuntimeException("Expected value for list type is an array or " + 108 List .class.getName() + 109 " but got: " + 110 value 111 ); 112 } 113 114 if(Constants.QNAME_QNAME.getLocalPart().equals(itemType.getQName().getLocalPart())) 115 { 116 String attrLocal = binding.getQName().getLocalPart(); 117 for(int listInd = 0; listInd < list.size(); ++listInd) 118 { 119 QName item = (QName )list.get(listInd); 120 String itemNs = item.getNamespaceURI(); 121 if(itemNs != null && itemNs.length() > 0) 122 { 123 String itemPrefix = ctx.getPrefix(itemNs); 124 if(itemPrefix == null) 125 { 126 itemPrefix = item.getPrefix(); 127 if(itemPrefix == null || itemPrefix.length() == 0) 128 { 129 itemPrefix = attrLocal + listInd; 130 } 131 ctx.declareNamespace(itemPrefix, itemNs); 132 } 133 134 if(!itemPrefix.equals(item.getPrefix())) 135 { 136 item = new QName (item.getNamespaceURI(), item.getLocalPart(), itemPrefix); 137 list.set(listInd, item); 138 } 139 } 140 } 141 } 142 143 marshalled = SimpleTypeBindings.marshalList(itemType.getQName().getLocalPart(), list, null); 144 } 145 else 146 { 147 throw new JBossXBRuntimeException("Marshalling of list types with item types not from " + 148 Constants.NS_XML_SCHEMA + " is not supported." 149 ); 150 } 151 } 152 else if(attrType.getLexicalPattern() != null && 153 attrType.getBaseType() != null && 154 Constants.QNAME_BOOLEAN.equals(attrType.getBaseType().getQName())) 155 { 156 String item = (String )attrType.getLexicalPattern().get(0); 157 if(item.indexOf('0') != -1 && item.indexOf('1') != -1) 158 { 159 marshalled = ((Boolean )value).booleanValue() ? "1" : "0"; 160 } 161 else 162 { 163 marshalled = ((Boolean )value).booleanValue() ? "true" : "false"; 164 } 165 } 166 else if(Constants.QNAME_QNAME.equals(attrType.getQName())) 167 { 168 boolean removePrefix = false; 169 String prefix = null; 170 String ns = ((QName )value).getNamespaceURI(); 171 if(ns != null && ns.length() > 0) 172 { 173 prefix = ctx.getPrefix(ns); 174 if(prefix == null) 175 { 176 prefix = ((QName )value).getPrefix(); 177 if(prefix == null || prefix.length() == 0) 178 { 179 prefix = "ns_" + ((QName )value).getLocalPart(); 180 } 181 ctx.declareNamespace(prefix, ns); 182 } 183 ctx.getNamespaceContext().addPrefixMapping(prefix, ns); 184 removePrefix = true; 185 } 186 187 marshalled = SimpleTypeBindings.marshalQName((QName )value, ctx.getNamespaceContext()); 188 189 if(removePrefix) 190 { 191 ctx.getNamespaceContext().removePrefixMapping(prefix); 192 } 193 } 194 else 195 { 196 marshalled = value.toString(); 197 } 198 199 return marshalled; 200 } 201 202 } 203 | Popular Tags |