1 22 package org.jboss.xb.binding.sunday.unmarshalling; 23 24 import javax.xml.namespace.QName ; 25 import org.jboss.logging.Logger; 26 import org.jboss.xb.binding.JBossXBRuntimeException; 27 import org.jboss.xb.binding.Util; 28 import org.xml.sax.Attributes ; 29 30 34 public class WildcardBinding 35 extends TermBinding 36 { 37 private static final Logger log = Logger.getLogger(WildcardBinding.class); 38 39 private static final short PC_LAX = 3; 40 private static final short PC_SKIP = 2; 41 private static final short PC_STRICT = 1; 42 43 private QName qName; 44 private SchemaBindingResolver schemaResolver; 45 private short pc; 46 47 private ParticleHandler unresolvedElementHandler; 48 private CharactersHandler unresolvedCharactersHandler; 49 private ParticleHandler wildcardHandler; 50 51 public WildcardBinding(SchemaBinding schema) 52 { 53 super(schema); 54 } 55 56 public QName getQName() 57 { 58 return qName; 59 } 60 61 public void setQName(QName qName) 62 { 63 this.qName = qName; 64 } 65 66 public SchemaBindingResolver getSchemaResolver() 67 { 68 return schemaResolver; 69 } 70 71 public void setSchemaResolver(SchemaBindingResolver schemaResolver) 72 { 73 this.schemaResolver = schemaResolver; 74 } 75 76 public short getProcessContents() 77 { 78 return pc; 79 } 80 81 public void setProcessContents(short pc) 82 { 83 this.pc = pc; 84 if(pc != PC_LAX && pc != PC_SKIP && pc != PC_STRICT) 85 { 86 throw new JBossXBRuntimeException("Unexpected value for process contents: " + pc); 87 } 88 } 89 90 public boolean isProcessContentsLax() 91 { 92 return pc == PC_LAX; 93 } 94 95 public boolean isProcessContentsSkip() 96 { 97 return pc == PC_SKIP; 98 } 99 100 public boolean isProcessContentsStrict() 101 { 102 return pc == PC_STRICT; 103 } 104 105 public void setWildcardHandler(ParticleHandler wildcardHandler) 106 { 107 this.wildcardHandler = wildcardHandler; 108 } 109 110 public ParticleHandler getWildcardHandler() 111 { 112 return wildcardHandler; 113 } 114 115 public ParticleHandler getUnresolvedElementHandler() 116 { 117 return unresolvedElementHandler; 118 } 119 120 public void setUnresolvedElementHandler(ParticleHandler unresolvedElementHandler) 121 { 122 this.unresolvedElementHandler = unresolvedElementHandler; 123 } 124 125 public CharactersHandler getUnresolvedCharactersHandler() 126 { 127 return unresolvedCharactersHandler; 128 } 129 130 public void setUnresolvedCharactersHandler(CharactersHandler unresolvedCharactersHandler) 131 { 132 this.unresolvedCharactersHandler = unresolvedCharactersHandler; 133 } 134 135 public ElementBinding getElement(QName qName, Attributes attrs) 136 { 137 if(pc == PC_SKIP) 138 { 139 return getUnresolvedElement(qName, false); 140 } 141 142 ElementBinding element = null; 143 if(schema != null) 145 { 146 element = schema.getElement(qName); 147 } 148 149 if(element == null) 150 { 151 SchemaBindingResolver resolver = schemaResolver; 152 if(resolver == null && schema != null) 153 { 154 resolver = schema.getSchemaResolver(); 155 } 156 157 if(resolver != null) 158 { 159 String schemaLocation = attrs == null ? null : Util.getSchemaLocation(attrs, qName.getNamespaceURI()); 161 SchemaBinding schema = resolver.resolve(qName.getNamespaceURI(), null, schemaLocation); 162 if(schema != null) 163 { 164 element = schema.getElement(qName); 165 } 166 } 167 } 168 169 if(element == null && pc == PC_LAX) 170 { 171 element = getUnresolvedElement(qName, false); 172 } 173 174 return element; 175 } 176 177 184 private ElementBinding getUnresolvedElement(QName qName, boolean required) 185 { 186 if(log.isTraceEnabled()) 187 { 188 log.trace( 189 "getUnresolvedElement for " + qName + ", required=" + required 190 + ", unresolvedElementHandler=" + unresolvedElementHandler 191 ); 192 } 193 194 if(unresolvedElementHandler == null) 195 { 196 if(required) 197 { 198 throw new JBossXBRuntimeException("Schema could not be resolved for wildcard content element " + 199 qName + 200 " and particle handler for unresolved wildcard content elements is not initialized." 201 ); 202 } 203 else 204 { 205 ParticleBinding particle = new ParticleBinding(this); 207 SequenceBinding sequence = new SequenceBinding(schema); 208 sequence.addParticle(particle); 209 210 TypeBinding type = new TypeBinding(); 211 type.setParticle(new ParticleBinding(sequence)); 212 213 ElementBinding element = new ElementBinding(schema, qName, type); 214 element.setSkip(Boolean.TRUE); 216 return element; 217 } 218 } 219 220 ParticleBinding particle = new ParticleBinding(this); 222 SequenceBinding sequence = new SequenceBinding(schema); 223 sequence.addParticle(particle); 224 225 TypeBinding type = new TypeBinding(); 226 type.setHandler(unresolvedElementHandler); 227 type.setSimpleType(unresolvedCharactersHandler); 228 type.setParticle(new ParticleBinding(sequence)); 229 230 return new ElementBinding(schema, qName, type); 231 } 232 233 public boolean isSkip() 234 { 235 return skip == null ? false : skip.booleanValue(); 236 } 237 238 public boolean isModelGroup() 239 { 240 return false; 241 } 242 243 public boolean isWildcard() 244 { 245 return true; 246 } 247 248 public boolean isElement() 249 { 250 return false; 251 } 252 } 253 | Popular Tags |