1 57 58 package com.sun.org.apache.xerces.internal.impl.xs.identity; 59 60 import com.sun.org.apache.xerces.internal.impl.xpath.XPathException; 61 import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition; 62 import com.sun.org.apache.xerces.internal.util.SymbolTable; 63 import com.sun.org.apache.xerces.internal.xni.NamespaceContext; 64 import com.sun.org.apache.xerces.internal.xni.QName; 65 import com.sun.org.apache.xerces.internal.xni.XMLAttributes; 66 67 73 public class Selector { 74 75 79 80 protected Selector.XPath fXPath; 81 82 83 protected IdentityConstraint fIdentityConstraint; 84 85 protected IdentityConstraint fIDConstraint; 88 89 93 94 public Selector(Selector.XPath xpath, 95 IdentityConstraint identityConstraint) { 96 fXPath = xpath; 97 fIdentityConstraint = identityConstraint; 98 } 100 104 105 public com.sun.org.apache.xerces.internal.impl.xpath.XPath getXPath() { 106 return fXPath; 107 } 109 110 public IdentityConstraint getIDConstraint() { 111 return fIdentityConstraint; 112 } 114 116 121 public XPathMatcher createMatcher(FieldActivator activator, int initialDepth) { 122 return new Selector.Matcher(fXPath, activator, initialDepth); 123 } 125 129 130 public String toString() { 131 return fXPath.toString(); 132 } 134 138 144 public static class XPath 145 extends com.sun.org.apache.xerces.internal.impl.xpath.XPath { 146 147 151 152 public XPath(String xpath, SymbolTable symbolTable, 153 NamespaceContext context) throws XPathException { 154 super(normalize(xpath), symbolTable, context); 155 for (int i=0;i<fLocationPaths.length;i++) { 157 com.sun.org.apache.xerces.internal.impl.xpath.XPath.Axis axis = 158 fLocationPaths[i].steps[fLocationPaths[i].steps.length-1].axis; 159 if (axis.type == XPath.Axis.ATTRIBUTE) { 160 throw new XPathException("c-selector-xpath"); 161 } 162 } 163 164 } 166 private static String normalize(String xpath) { 167 StringBuffer modifiedXPath = new StringBuffer (xpath.length()+5); 175 int unionIndex = -1; 176 do { 177 if(!(xpath.trim().startsWith("/") ||xpath.trim().startsWith("."))) { 178 modifiedXPath.append("./"); 179 } 180 unionIndex = xpath.indexOf('|'); 181 if(unionIndex == -1) { 182 modifiedXPath.append(xpath); 183 break; 184 } 185 modifiedXPath.append(xpath.substring(0,unionIndex+1)); 186 xpath = xpath.substring(unionIndex+1, xpath.length()); 187 } while(true); 188 return modifiedXPath.toString(); 189 } 190 191 } 193 198 public class Matcher 199 extends XPathMatcher { 200 201 205 206 protected FieldActivator fFieldActivator; 207 208 209 protected int fInitialDepth; 210 211 212 protected int fElementDepth; 213 214 215 protected int fMatchedDepth; 216 217 221 222 public Matcher(Selector.XPath xpath, FieldActivator activator, 223 int initialDepth) { 224 super(xpath); 225 fFieldActivator = activator; 226 fInitialDepth = initialDepth; 227 } 229 233 public void startDocumentFragment(){ 234 super.startDocumentFragment(); 235 fElementDepth = 0; 236 fMatchedDepth = -1; 237 } 239 249 public void startElement(QName element, XMLAttributes attributes) { 250 super.startElement(element, attributes); 251 fElementDepth++; 252 255 if (isMatched()) { 256 258 fMatchedDepth = fElementDepth; 259 fFieldActivator.startValueScopeFor(fIdentityConstraint, fInitialDepth); 260 int count = fIdentityConstraint.getFieldCount(); 261 for (int i = 0; i < count; i++) { 262 Field field = fIdentityConstraint.getFieldAt(i); 263 XPathMatcher matcher = fFieldActivator.activateField(field, fInitialDepth); 264 matcher.startElement(element, attributes); 265 } 266 } 267 268 } 270 public void endElement(QName element, XSTypeDefinition type, boolean nillable, Object actualValue) { 271 super.endElement(element, type, nillable, actualValue); 272 if (fElementDepth-- == fMatchedDepth) { 273 fMatchedDepth = -1; 274 fFieldActivator.endValueScopeFor(fIdentityConstraint, fInitialDepth); 275 } 276 } 277 278 279 public IdentityConstraint getIdentityConstraint() { 280 return fIdentityConstraint; 281 } 283 284 public int getInitialDepth() { 285 return fInitialDepth; 286 } 288 289 } 291 } | Popular Tags |