1 57 58 package org.enhydra.apache.xerces.validators.schema.identity; 59 60 import org.enhydra.apache.xerces.framework.XMLAttrList; 61 import org.enhydra.apache.xerces.utils.NamespacesScope; 62 import org.enhydra.apache.xerces.utils.QName; 63 import org.enhydra.apache.xerces.utils.StringPool; 64 import org.enhydra.apache.xerces.validators.schema.SchemaGrammar; 65 import org.xml.sax.SAXException ; 66 67 73 public class Selector { 74 75 79 80 protected Selector.XPath fXPath; 81 82 83 protected IdentityConstraint fIdentityConstraint; 84 85 89 90 public Selector(Selector.XPath xpath, 91 IdentityConstraint identityConstraint) { 92 fXPath = xpath; 93 fIdentityConstraint = identityConstraint; 94 } 96 100 101 public org.enhydra.apache.xerces.validators.schema.identity.XPath getXPath() { 102 return fXPath; 103 } 105 106 public IdentityConstraint getIdentityConstraint() { 107 return fIdentityConstraint; 108 } 110 112 113 public XPathMatcher createMatcher(FieldActivator activator) { 114 return new Selector.Matcher(fXPath, activator); 115 } 117 121 122 public String toString() { 123 return fXPath.toString(); 124 } 126 130 136 public static class XPath 137 extends org.enhydra.apache.xerces.validators.schema.identity.XPath { 138 139 143 144 public XPath(String xpath, StringPool stringPool, 145 NamespacesScope context) throws XPathException { 146 super(((xpath.trim().startsWith("/") ||xpath.trim().startsWith("."))? 153 xpath:"./"+xpath), stringPool, context); 154 155 for (int i=0;i<fLocationPaths.length;i++) { 157 org.enhydra.apache.xerces.validators.schema.identity.XPath.Axis axis = 158 fLocationPaths[i].steps[fLocationPaths[i].steps.length-1].axis; 159 if (axis.type == Axis.ATTRIBUTE) { 160 throw new XPathException("selectors cannot select attributes"); 161 } 162 } 163 164 } 166 } 168 173 protected class Matcher 174 extends XPathMatcher { 175 176 180 181 protected FieldActivator fFieldActivator; 182 183 184 protected int fElementDepth; 185 186 187 protected int fMatchedDepth; 188 189 193 194 public Matcher(Selector.XPath xpath, FieldActivator activator) { 195 super(xpath, false, Selector.this.fIdentityConstraint); 196 fFieldActivator = activator; 197 } 199 203 public void startDocumentFragment(StringPool stringPool) 204 throws Exception { 205 super.startDocumentFragment(stringPool); 206 fElementDepth = 0; 207 fMatchedDepth = -1; 208 } 210 223 public void startElement(QName element, XMLAttrList attributes, 224 int handle, int elemIndex, SchemaGrammar grammar) throws Exception { 225 super.startElement(element, attributes, handle, elemIndex, grammar); 226 fElementDepth++; 227 228 if (fMatchedDepth == -1 && isMatched()) { 230 fMatchedDepth = fElementDepth; 231 fFieldActivator.startValueScopeFor(fIdentityConstraint); 232 int count = fIdentityConstraint.getFieldCount(); 233 for (int i = 0; i < count; i++) { 234 Field field = fIdentityConstraint.getFieldAt(i); 235 XPathMatcher matcher = fFieldActivator.activateField(field); 236 matcher.startElement(element, attributes, handle, elemIndex, grammar); 237 } 238 } 239 240 } 242 public void endElement(QName element, int elemIndex, SchemaGrammar grammar) throws Exception { 243 super.endElement(element, elemIndex, grammar); 244 if (fElementDepth-- == fMatchedDepth) { 245 fMatchedDepth = -1; 246 fFieldActivator.endValueScopeFor(fIdentityConstraint); 247 } 248 } 249 250 } 252 } | Popular Tags |