1 28 29 package org.jibx.binding.model; 30 31 import java.util.HashMap ; 32 import java.util.HashSet ; 33 34 44 45 public class ClassHierarchyContext 46 { 47 48 private final ClassHierarchyContext m_outerContext; 49 50 51 private HashMap m_typeToComponentMap; 52 53 54 private HashSet m_compatibleTypeSet; 55 56 57 private HashMap m_nameToComponentMap; 58 59 64 protected ClassHierarchyContext(ClassHierarchyContext outer) { 65 m_outerContext = outer; 66 m_typeToComponentMap = new HashMap (); 67 m_compatibleTypeSet = new HashSet (); 68 m_nameToComponentMap = new HashMap (); 69 } 70 71 77 public ClassHierarchyContext getContaining() { 78 return m_outerContext; 79 } 80 81 89 public void addTypedComponent(IClass clas, ElementBase comp, 90 ValidationContext vctx) { 91 String type = clas.getName(); 92 if (m_typeToComponentMap.put(type, comp) == null) { 93 94 String [] interfaces = clas.getInterfaces(); 96 for (int i = 0; i < interfaces.length; i++) { 97 m_compatibleTypeSet.add(interfaces[i]); 98 } 99 IClass sclas = clas; 100 do { 101 m_compatibleTypeSet.add(sclas.getName()); 102 } while ((sclas = sclas.getSuperClass()) != null); 103 104 } else { 105 vctx.addError("Duplicate conversion defined for type " + type, 106 comp); 107 } 108 } 109 110 118 public void addNamedComponent(String label, ElementBase comp, 119 ValidationContext vctx) { 120 if (m_nameToComponentMap.put(label, comp) != null) { 121 vctx.addError("Duplicate label " + label, comp); 122 } 123 } 124 125 134 public ElementBase getSpecificComponent(String name) { 135 ElementBase comp = null; 136 if (m_typeToComponentMap != null) { 137 comp = (ElementBase)m_typeToComponentMap.get(name); 138 } 139 if (comp == null && m_outerContext != null) { 140 comp = m_outerContext.getSpecificComponent(name); 141 } 142 return comp; 143 } 144 145 154 public ElementBase getNamedComponent(String name) { 155 ElementBase comp = null; 156 if (m_nameToComponentMap != null) { 157 comp = (FormatElement)m_nameToComponentMap.get(name); 158 } 159 if (comp == null && m_outerContext != null) { 160 comp = m_outerContext.getNamedComponent(name); 161 } 162 return comp; 163 } 164 165 175 public ElementBase getBestComponent(IClass clas) { 176 ElementBase comp = getSpecificComponent(clas.getName()); 177 while (comp == null) { 178 IClass sclas = clas.getSuperClass(); 179 if (sclas == null) { 180 break; 181 } 182 clas = sclas; 183 comp = getSpecificComponent(clas.getName()); 184 } 185 return comp; 186 } 187 188 197 public boolean isCompatibleType(IClass clas) { 198 if (m_compatibleTypeSet.contains(clas.getName())) { 199 return true; 200 } else if (m_outerContext != null) { 201 return m_outerContext.isCompatibleType(clas); 202 } else { 203 return false; 204 } 205 } 206 } | Popular Tags |