1 22 package org.jboss.xb.binding.group; 23 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 import java.util.Collections ; 27 import java.util.List ; 28 import java.util.Map ; 29 import javax.xml.namespace.QName ; 30 31 import org.jboss.xb.binding.JBossXBRuntimeException; 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.ParticleBinding; 35 import org.jboss.xb.binding.sunday.unmarshalling.CharactersHandler; 36 import org.jboss.xb.binding.sunday.unmarshalling.impl.runtime.RtUtil; 37 38 42 public class ValueList 43 { 44 private final ValueListInitializer initializer; 45 private final ValueListHandler handler; 46 private final Class targetClass; 47 49 private List nonRequiredValues = Collections.EMPTY_LIST; 54 55 ValueList(ValueListInitializer initializer, ValueListHandler handler, Class targetClass) 56 { 57 this.initializer = initializer; 58 this.handler = handler; 59 this.targetClass = targetClass; 60 } 63 64 void setRequiredValue(int index, int stateIncrement, Object value) 65 { 66 throw new UnsupportedOperationException (); 67 77 } 78 79 Object getRequiredValue(int index) 80 { 81 90 throw new UnsupportedOperationException (); 91 } 92 93 int getState() 94 { 95 throw new UnsupportedOperationException (); 97 } 98 99 void setAttributeValue(QName qName, AttributeBinding binding, Object value) 100 { 101 setNonRequiredValue(qName, binding, null, value, null); 102 } 103 104 void addTextValue(QName qName, ParticleBinding particle, CharactersHandler handler, Object value) 105 { 106 setNonRequiredValue(qName, particle, handler, value, null); 107 } 108 109 void addTermValue(QName qName, ParticleBinding binding, Object handler, Object value, ParticleBinding parentParticle) 110 { 111 setNonRequiredValue(qName, binding, handler, value, parentParticle); 112 } 113 114 void addRepeatableTermValue(QName qName, ParticleBinding binding, Object handler, Object value, ParticleBinding parentParticle) 115 { 116 NonRequiredValue last = (NonRequiredValue) (nonRequiredValues.isEmpty() ? null : nonRequiredValues.get(nonRequiredValues.size() - 1)); 117 if (last == null || last.binding != binding) 118 { 119 Collection col; 120 PropertyMetaData propMetaData = binding.getTerm().getPropertyMetaData(); 121 if(propMetaData != null && propMetaData.getCollectionType() != null) 122 { 123 Class colCls = RtUtil.loadClass(propMetaData.getCollectionType(), true); 124 try 125 { 126 col = (Collection ) colCls.newInstance(); 127 } 128 catch (Exception e) 129 { 130 throw new JBossXBRuntimeException("Failed to create an instance of " + colCls.getName() + " for property " + propMetaData.getName()); 131 } 132 } 133 else 134 { 135 col = new ArrayList (); 136 } 137 138 col.add(value); 139 setNonRequiredValue(qName, binding, handler, col, parentParticle); 140 } 141 else 142 { 143 Collection col = (Collection ) last.value; 144 col.add(value); 145 } 146 } 147 148 void setNonRequiredValue(QName qName, Object binding, Object handler, Object value, ParticleBinding parentParticle) 149 { 150 NonRequiredValue val = new NonRequiredValue(qName, binding, handler, value, parentParticle); 151 switch(nonRequiredValues.size()) 152 { 153 case 0: 154 nonRequiredValues = Collections.singletonList(val); 155 break; 156 case 1: 157 nonRequiredValues = new ArrayList (nonRequiredValues); 158 default: 159 nonRequiredValues.add(val); 160 } 161 } 162 163 Object getNonRequiredValue(QName qName) 164 { 165 throw new UnsupportedOperationException (); 167 } 168 169 public ValueListInitializer getInitializer() 170 { 171 return initializer; 172 } 173 174 public List getRequiredValues() 175 { 176 throw new UnsupportedOperationException (); 178 } 179 180 public Map getNonRequiredValues() 181 { 182 throw new UnsupportedOperationException (); 184 } 185 186 public List getNonRequiredBindings() 187 { 188 throw new UnsupportedOperationException (); 190 } 191 192 public ValueListHandler getHandler() 193 { 194 return handler; 195 } 196 197 public Class getTargetClass() 198 { 199 return targetClass; 200 } 201 202 public NonRequiredValue getValue(int i) 203 { 204 return (NonRequiredValue)nonRequiredValues.get(i); 205 } 206 207 public int size() 208 { 209 return nonRequiredValues.size(); 210 } 211 212 public static final class NonRequiredValue 213 { 214 public final QName qName; 215 public final Object binding; 216 public final Object handler; 217 public Object value; 218 public ParticleBinding parentParticle; 219 220 public NonRequiredValue(QName qName, Object binding, Object handler, Object value, ParticleBinding parentParticle) 221 { 222 this.qName = qName; 223 this.binding = binding; 224 this.handler = handler; 225 this.value = value; 226 this.parentParticle = parentParticle; 227 } 228 } 229 } 230 | Popular Tags |