1 28 29 package org.jibx.binding.model; 30 31 import java.util.ArrayList ; 32 import java.util.HashMap ; 33 34 44 45 public class DefinitionContext 46 { 47 48 private final DefinitionContext m_outerContext; 49 50 51 private NamespaceElement m_attributeDefault; 52 53 54 private NamespaceElement m_elementDefault; 55 56 57 private ArrayList m_namespaces; 58 59 60 private HashMap m_prefixMap; 61 62 63 private HashMap m_uriMap; 64 65 66 private ClassHierarchyContext m_formatContext; 67 68 69 private ClassHierarchyContext m_templateContext; 70 71 72 private HashMap m_namedStructureMap; 73 74 80 protected DefinitionContext(DefinitionContext outer) { 81 m_outerContext = outer; 82 } 83 84 90 public DefinitionContext getContaining() { 91 return m_outerContext; 92 } 93 94 100 private ClassHierarchyContext getContainingFormatContext() { 101 if (m_outerContext == null) { 102 return null; 103 } else { 104 return m_outerContext.getFormatContext(); 105 } 106 } 107 108 114 private ClassHierarchyContext getFormatContext() { 115 if (m_formatContext == null) { 116 return getContainingFormatContext(); 117 } else { 118 return m_formatContext; 119 } 120 } 121 122 128 private ClassHierarchyContext getContainingTemplateContext() { 129 if (m_outerContext == null) { 130 return null; 131 } else { 132 return m_outerContext.getTemplateContext(); 133 } 134 } 135 136 142 private ClassHierarchyContext getTemplateContext() { 143 if (m_templateContext == null) { 144 return getContainingTemplateContext(); 145 } else { 146 return m_templateContext; 147 } 148 } 149 150 156 public ValidationProblem addNamespace(NamespaceElement def) { 157 158 if (m_namespaces == null) { 160 m_namespaces = new ArrayList (); 161 m_prefixMap = new HashMap (); 162 m_uriMap = new HashMap (); 163 } 164 165 if (def.isAttributeDefault()) { 167 if (m_attributeDefault == null) { 168 m_attributeDefault = def; 169 } else { 170 return new ValidationProblem 171 ("Conflicting attribute namespaces", def); 172 } 173 } 174 175 if (def.isElementDefault()) { 177 if (m_elementDefault == null) { 178 m_elementDefault = def; 179 } else { 180 return new ValidationProblem 181 ("Conflicting element namespaces", def); 182 } 183 } 184 185 String prefix = def.getPrefix(); 187 if (m_prefixMap.get(prefix) != null) { 188 return new ValidationProblem("Namespace prefix conflict", def); 189 } 190 191 String uri = def.getUri(); 193 Object prior = m_uriMap.get(uri); 194 if (prior != null && ((NamespaceElement)prior).getPrefix() != null) { 195 return null; 197 } 198 199 m_namespaces.add(def); 201 m_prefixMap.put(prefix, def); 202 m_uriMap.put(uri, def); 203 return null; 204 } 205 206 213 public NamespaceElement getElementNamespace(NameAttributes name) { 214 String uri = name.getUri(); 215 String prefix = name.getPrefix(); 216 NamespaceElement ns = null; 217 if (uri != null) { 218 if (m_uriMap != null) { 219 ns = (NamespaceElement)m_uriMap.get(uri); 220 if (ns != null && prefix != null) { 221 if (!prefix.equals(ns.getPrefix())) { 222 ns = null; 223 } 224 } 225 } 226 } else if (prefix != null) { 227 if (m_prefixMap != null) { 228 ns = (NamespaceElement)m_prefixMap.get(prefix); 229 } 230 } else { 231 ns = m_elementDefault; 232 } 233 if (ns == null && m_outerContext != null) { 234 ns = m_outerContext.getElementNamespace(name); 235 } 236 return ns; 237 } 238 239 246 public NamespaceElement getAttributeNamespace(NameAttributes name) { 247 String uri = name.getUri(); 248 String prefix = name.getPrefix(); 249 NamespaceElement ns = null; 250 if (uri != null) { 251 if (m_uriMap != null) { 252 ns = (NamespaceElement)m_uriMap.get(uri); 253 if (ns != null && prefix != null) { 254 if (!prefix.equals(ns.getPrefix())) { 255 ns = null; 256 } 257 } 258 } 259 } else if (prefix != null) { 260 if (m_prefixMap != null) { 261 ns = (NamespaceElement)m_prefixMap.get(prefix); 262 } 263 } else { 264 ns = m_attributeDefault; 265 } 266 if (ns == null && m_outerContext != null) { 267 ns = m_outerContext.getAttributeNamespace(name); 268 } 269 return ns; 270 } 271 272 278 public void addFormat(FormatElement def, ValidationContext vctx) { 279 if (m_formatContext == null) { 280 m_formatContext = 281 new ClassHierarchyContext(getContainingFormatContext()); 282 } 283 if (def.isDefaultFormat()) { 284 IClass clas = def.getType(); 285 m_formatContext.addTypedComponent(clas, def, vctx); 286 } 287 if (def.getLabel() != null) { 288 m_formatContext.addNamedComponent(def.getLabel(), def, vctx); 289 } 290 } 291 292 301 public FormatElement getSpecificFormat(String type) { 302 ClassHierarchyContext ctx = getFormatContext(); 303 if (ctx == null) { 304 return null; 305 } else { 306 return (FormatElement)ctx.getSpecificComponent(type); 307 } 308 } 309 310 318 public FormatElement getNamedFormat(String name) { 319 ClassHierarchyContext ctx = getFormatContext(); 320 if (ctx == null) { 321 return null; 322 } else { 323 return (FormatElement)ctx.getNamedComponent(name); 324 } 325 } 326 327 336 public FormatElement getBestFormat(IClass clas) { 337 ClassHierarchyContext ctx = getFormatContext(); 338 if (ctx == null) { 339 return null; 340 } else { 341 return (FormatElement)ctx.getBestComponent(clas); 342 } 343 } 344 345 351 public void addTemplate(TemplateElementBase def, ValidationContext vctx) { 352 if (m_templateContext == null) { 353 m_templateContext = 354 new ClassHierarchyContext(getContainingTemplateContext()); 355 } 356 if (def.isDefaultTemplate()) { 357 IClass clas = def.getHandledClass(); 358 m_templateContext.addTypedComponent(clas, def, vctx); 359 } 360 if (def instanceof TemplateElement) { 361 TemplateElement tdef = (TemplateElement)def; 362 if (tdef.getLabel() != null) { 363 m_templateContext.addNamedComponent(tdef.getLabel(), def, vctx); 364 } 365 } 366 } 367 368 377 public TemplateElementBase getSpecificTemplate(String type) { 378 ClassHierarchyContext ctx = getTemplateContext(); 379 if (ctx == null) { 380 return null; 381 } else { 382 return (TemplateElementBase)ctx.getSpecificComponent(type); 383 } 384 } 385 386 394 public TemplateElement getNamedTemplate(String name) { 395 ClassHierarchyContext ctx = getTemplateContext(); 396 if (ctx == null) { 397 return null; 398 } else { 399 return (TemplateElement)ctx.getNamedComponent(name); 400 } 401 } 402 403 413 public TemplateElementBase getBestTemplate(IClass clas) { 414 ClassHierarchyContext ctx = getTemplateContext(); 415 if (ctx == null) { 416 return null; 417 } else { 418 return (TemplateElementBase)ctx.getBestComponent(clas); 419 } 420 } 421 422 430 public boolean isCompatibleTemplateType(IClass clas) { 431 ClassHierarchyContext chctx = getTemplateContext(); 432 if (chctx == null) { 433 return false; 434 } else { 435 return chctx.isCompatibleType(clas); 436 } 437 } 438 439 445 446 public ValidationProblem addNamedStructure(StructureElementBase def) { 447 448 if (m_namedStructureMap == null) { 450 m_namedStructureMap = new HashMap (); 451 } 452 453 String label = def.getLabel(); 455 if (m_namedStructureMap.get(label) == null) { 456 m_namedStructureMap.put(label, def); 457 return null; 458 } else { 459 return new ValidationProblem("Duplicate label " + label, def); 460 } 461 } 462 463 469 470 public StructureElementBase getNamedStructure(String label) { 471 if (m_namedStructureMap == null) { 472 return null; 473 } else { 474 return (StructureElementBase)m_namedStructureMap.get(label); 475 } 476 } 477 } | Popular Tags |