1 57 58 package org.enhydra.apache.xerces.validators.common; 59 60 import org.enhydra.apache.xerces.framework.XMLContentSpec; 61 import org.enhydra.apache.xerces.framework.XMLErrorReporter; 62 import org.enhydra.apache.xerces.utils.QName; 63 import org.enhydra.apache.xerces.utils.StringPool; 64 import org.enhydra.apache.xerces.validators.schema.SchemaMessageProvider; 65 import org.enhydra.apache.xerces.validators.schema.SubstitutionGroupComparator; 66 67 70 public class ElementWildcard { 71 private ElementWildcard(){} 72 73 private static boolean uriInWildcard(QName qname, int wildcard, int wtype, 74 SubstitutionGroupComparator comparator) throws Exception { 75 int type = wtype & 0x0f; 76 77 if (type == XMLContentSpec.CONTENTSPECNODE_ANY) { 78 return true; 79 } 80 else if (type == XMLContentSpec.CONTENTSPECNODE_ANY_NS) { 81 if (comparator != null) { 83 if (comparator.isAllowedByWildcard(qname, wildcard, false)) 84 return true; 85 } else { 86 if (qname.uri == wildcard) 87 return true; 88 } 89 } 90 else if (type == XMLContentSpec.CONTENTSPECNODE_ANY_OTHER) { 91 if (comparator != null) { 93 if (comparator.isAllowedByWildcard(qname, wildcard, true)) 94 return true; 95 } else { 96 if (wildcard != qname.uri) 98 return true; 99 } 100 } 101 102 return false; 103 } 104 105 private static boolean wildcardIntersect(int w1, int t1, int w2, int t2) { 106 int type1 = t1 & 0x0f, type2 = t2 & 0x0f; 107 108 if (type1 == XMLContentSpec.CONTENTSPECNODE_ANY || 110 type2 == XMLContentSpec.CONTENTSPECNODE_ANY) { 111 return true; 112 } 113 114 if (type1 == XMLContentSpec.CONTENTSPECNODE_ANY_NS && 116 type2 == XMLContentSpec.CONTENTSPECNODE_ANY_NS && 117 w1 == w2) { 118 return true; 119 } 120 121 if (type1 == XMLContentSpec.CONTENTSPECNODE_ANY_OTHER && 123 type2 == XMLContentSpec.CONTENTSPECNODE_ANY_OTHER) { 124 return true; 125 } 126 127 if ((type1 == XMLContentSpec.CONTENTSPECNODE_ANY_NS && 129 type2 == XMLContentSpec.CONTENTSPECNODE_ANY_OTHER || 130 type1 == XMLContentSpec.CONTENTSPECNODE_ANY_OTHER && 131 type2 == XMLContentSpec.CONTENTSPECNODE_ANY_NS) && 132 w1 != w2) { 133 return true; 134 } 135 136 return false; 137 } 138 139 private static boolean conflic(int type1, int local1, int uri1, 141 int type2, int local2, int uri2, 142 SubstitutionGroupComparator comparator) throws Exception { 143 QName q1 = new QName(), q2 = new QName(); 144 q1.localpart = local1; 145 q1.uri = uri1; 146 q2.localpart = local2; 147 q2.uri = uri2; 148 149 if (type1 == XMLContentSpec.CONTENTSPECNODE_LEAF && 150 type2 == XMLContentSpec.CONTENTSPECNODE_LEAF) { 151 if (comparator != null) { 152 if (comparator.isEquivalentTo(q1, q2) || 153 comparator.isEquivalentTo(q2, q1)) 154 return true; 155 } else { 156 if (q1.localpart == q2.localpart && 157 q1.uri == q2.uri) 158 return true; 159 } 160 } else if (type1 == XMLContentSpec.CONTENTSPECNODE_LEAF) { 161 if (uriInWildcard(q1, uri2, type2, comparator)) 162 return true; 163 } else if (type2 == XMLContentSpec.CONTENTSPECNODE_LEAF) { 164 if (uriInWildcard(q2, uri1, type1, comparator)) 165 return true; 166 } else { 167 if (wildcardIntersect(uri1, type1, uri2, type2)) 168 return true; 169 } 170 171 return false; 172 } 173 174 public static boolean conflict(int type1, int local1, int uri1, 175 int type2, int local2, int uri2, 176 SubstitutionGroupComparator comparator) throws Exception { 177 boolean ret = conflic(type1, local1, uri1, type2, local2, uri2, comparator); 178 179 if (ret && comparator != null) { 180 StringPool stringPool = comparator.getStringPool(); 181 XMLErrorReporter err = comparator.getErrorReporter(); 182 err.reportError(err.getLocator(), 183 SchemaMessageProvider.SCHEMA_DOMAIN, 184 SchemaMessageProvider.UniqueParticleAttribution, 185 SchemaMessageProvider.MSG_NONE, 186 new Object []{eleString(type1, local1, uri1, stringPool), 187 eleString(type2, local2, uri2, stringPool)}, 188 XMLErrorReporter.ERRORTYPE_RECOVERABLE_ERROR); 189 } 190 191 return ret; 192 } 193 194 private static String eleString(int type, int local, int uri, StringPool stringPool) { 195 switch (type & 0x0f) { 196 case XMLContentSpec.CONTENTSPECNODE_LEAF: 197 return stringPool.toString(uri) + "," + stringPool.toString(local); 198 case XMLContentSpec.CONTENTSPECNODE_ANY: 199 return "##any,*"; 200 case XMLContentSpec.CONTENTSPECNODE_ANY_NS: 201 return "##any(" + stringPool.toString(uri) + "),*"; 202 case XMLContentSpec.CONTENTSPECNODE_ANY_OTHER: 203 return "##other(" + stringPool.toString(uri) + "),*"; 204 } 205 206 return ""; 207 } 208 } | Popular Tags |