1 2 58 package org.enhydra.apache.xerces.validators.schema; 59 60 import java.util.Vector ; 61 62 import org.enhydra.apache.xerces.framework.XMLErrorReporter; 63 import org.enhydra.apache.xerces.utils.QName; 64 import org.enhydra.apache.xerces.utils.StringPool; 65 import org.enhydra.apache.xerces.utils.XMLMessages; 66 import org.enhydra.apache.xerces.validators.common.GrammarResolver; 67 import org.enhydra.apache.xerces.validators.common.XMLElementDecl; 68 import org.enhydra.apache.xerces.validators.datatype.DatatypeValidator; 69 import org.xml.sax.SAXException ; 70 71 83 84 public class SubstitutionGroupComparator { 85 86 private final int TOP_LEVEL_SCOPE = -1; 88 89 private StringPool fStringPool = null; 91 private GrammarResolver fGrammarResolver = null; 92 private XMLErrorReporter fErrorReporter = null; 93 94 private SubstitutionGroupComparator(){ 96 } 98 public SubstitutionGroupComparator(GrammarResolver grammarResolver, StringPool stringPool, XMLErrorReporter errorReporter){ 99 fGrammarResolver = grammarResolver; 100 fStringPool = stringPool; 101 fErrorReporter = errorReporter; 102 } 103 104 public StringPool getStringPool() { 105 return fStringPool; 106 } 107 108 public XMLErrorReporter getErrorReporter () { 109 return fErrorReporter; 110 } 111 112 public boolean isEquivalentTo(QName anElement, QName exemplar) throws Exception { 114 if (anElement.localpart==exemplar.localpart && anElement.uri==exemplar.uri ) { 115 return true; } 117 118 if (fGrammarResolver == null || fStringPool == null) { 119 throw new SAXException ("Internal error; tried to check an element against a substitutionGroup, but no GrammarResolver is defined"); 120 } 121 122 126 int uriIndex = anElement.uri; 127 int localpartIndex = anElement.localpart; 128 String uri = fStringPool.toString(anElement.uri); 129 String localpart = fStringPool.toString(anElement.localpart); 130 131 if(uri==null) { 138 return false; 139 } 140 SchemaGrammar sGrammar = null; 141 try { 142 sGrammar = (SchemaGrammar) fGrammarResolver.getGrammar(uri); 143 } 144 catch ( ClassCastException ce) { 145 String er = "Grammar with URI " + uri + " is not a schema grammar!"; 147 Object [] a = {er}; 148 fErrorReporter.reportError(fErrorReporter.getLocator(), 149 XMLMessages.XML_DOMAIN, 150 XMLMessages.MSG_GENERIC_SCHEMA_ERROR, 151 XMLMessages.SCHEMA_GENERIC_ERROR, 152 a, XMLErrorReporter.ERRORTYPE_RECOVERABLE_ERROR); 153 return false; 154 } 155 if(sGrammar == null) { 156 return false; 157 } 158 159 int elementIndex = sGrammar.getElementDeclIndex(uriIndex, localpartIndex, TOP_LEVEL_SCOPE); 161 int anElementIndex = elementIndex; 162 163 String substitutionGroupFullName = sGrammar.getElementDeclSubstitutionGroupAffFullName(elementIndex); 164 boolean foundIt = false; 165 while (substitutionGroupFullName != null) { 166 int commaAt = substitutionGroupFullName.indexOf(","); 167 uri = ""; 168 localpart = substitutionGroupFullName; 169 if ( commaAt >= 0 ) { 170 if (commaAt > 0 ) { 171 uri = substitutionGroupFullName.substring(0,commaAt); 172 } 173 localpart = substitutionGroupFullName.substring(commaAt+1); 174 } 175 if(uri==null) { 176 return false; 177 } 178 try { 179 sGrammar = (SchemaGrammar) fGrammarResolver.getGrammar(uri); 180 } 181 catch ( ClassCastException ce) { 182 String er = "Grammar with URI " + uri + " is not a schema grammar!"; 184 Object [] a = {er}; 185 fErrorReporter.reportError(fErrorReporter.getLocator(), 186 XMLMessages.XML_DOMAIN, 187 XMLMessages.MSG_GENERIC_SCHEMA_ERROR, 188 XMLMessages.SCHEMA_GENERIC_ERROR, 189 a, XMLErrorReporter.ERRORTYPE_RECOVERABLE_ERROR); 190 return false; 191 } 192 if(sGrammar == null) { 193 return false; 194 } 195 uriIndex = fStringPool.addSymbol(uri); 196 localpartIndex = fStringPool.addSymbol(localpart); 197 elementIndex = sGrammar.getElementDeclIndex(uriIndex, localpartIndex, TOP_LEVEL_SCOPE); 198 if (elementIndex == -1) { 199 return false; 200 } 201 202 if (uriIndex == exemplar.uri && localpartIndex == exemplar.localpart) { 203 if((sGrammar.getElementDeclBlockSet(elementIndex) & SchemaSymbols.SUBSTITUTION) != 0) { 205 return false; 206 } 207 foundIt = true; 208 break; 209 } 210 211 substitutionGroupFullName = sGrammar.getElementDeclSubstitutionGroupAffFullName(elementIndex); 212 213 } 214 215 if (!foundIt) { 216 return false; 217 } 218 TraverseSchema.ComplexTypeInfo aComplexType = sGrammar.getElementComplexTypeInfo(anElementIndex); 220 int exemplarBlockSet = sGrammar.getElementDeclBlockSet(elementIndex); 222 if(aComplexType == null) { 223 XMLElementDecl anElementDecl = new XMLElementDecl(); 225 sGrammar.getElementDecl(anElementIndex, anElementDecl); 226 DatatypeValidator anElementDV = anElementDecl.datatypeValidator; 227 XMLElementDecl exemplarDecl = new XMLElementDecl(); 228 sGrammar.getElementDecl(elementIndex, exemplarDecl); 229 DatatypeValidator exemplarDV = exemplarDecl.datatypeValidator; 230 return((anElementDV == null) || 231 ((anElementDV == exemplarDV) || 232 ((exemplarBlockSet & SchemaSymbols.RESTRICTION) == 0))); 233 } 234 int anElementDerivationMethod = aComplexType.derivedBy; 236 if((anElementDerivationMethod & exemplarBlockSet) != 0) return false; 237 TraverseSchema.ComplexTypeInfo exemplarComplexType = sGrammar.getElementComplexTypeInfo(elementIndex); 239 for(TraverseSchema.ComplexTypeInfo tempType = aComplexType; 240 tempType != null && tempType != exemplarComplexType; 241 tempType = tempType.baseComplexTypeInfo) { 242 if((tempType.blockSet & anElementDerivationMethod) != 0) { 243 return false; 244 } 245 } 246 return true; 247 } 248 249 259 public boolean isAllowedByWildcard(QName element, int wuri, boolean wother) throws Exception { 260 if (!wother && element.uri == wuri || 262 wother && element.uri != wuri) { 264 return true; 265 } 266 267 if (fGrammarResolver == null || fStringPool == null) { 268 throw new SAXException ("Internal error; tried to check an element against a substitutionGroup, but no GrammarResolver is defined"); 269 } 270 271 String uri = fStringPool.toString(element.uri); 273 if(uri==null) 274 return false; 275 SchemaGrammar sGrammar = null; 276 try { 277 sGrammar = (SchemaGrammar) fGrammarResolver.getGrammar(uri); 278 } 279 catch ( ClassCastException ce) { 280 String er = "Grammar with URI " + uri + " is not a schema grammar!"; 282 Object [] a = {er}; 283 fErrorReporter.reportError(fErrorReporter.getLocator(), 284 XMLMessages.XML_DOMAIN, 285 XMLMessages.MSG_GENERIC_SCHEMA_ERROR, 286 XMLMessages.SCHEMA_GENERIC_ERROR, 287 a, XMLErrorReporter.ERRORTYPE_RECOVERABLE_ERROR); 288 return false; 289 } 290 if(sGrammar == null) 291 return false; 292 293 int elementIndex = sGrammar.getElementDeclIndex(element, TOP_LEVEL_SCOPE); 295 296 Vector substitutionGroupQNames = sGrammar.getElementDeclAllSubstitutionGroupQNames(elementIndex, fGrammarResolver, fStringPool); 298 299 int size = substitutionGroupQNames == null ? 0 : substitutionGroupQNames.size(); 301 for (int i = 0; i < size; i++) { 302 QName name = ((SchemaGrammar.OneSubGroup)substitutionGroupQNames.elementAt(i)).name; 303 if (!wother && name.uri == wuri || 304 wother && name.uri != wuri) { 306 return true; 307 } 308 } 309 310 return false; 311 } 312 } 313 | Popular Tags |