1 16 17 package org.apache.xerces.impl.xs.identity; 18 19 import org.apache.xerces.impl.xpath.XPathException; 20 import org.apache.xerces.util.SymbolTable; 21 import org.apache.xerces.xni.NamespaceContext; 22 import org.apache.xerces.xni.QName; 23 import org.apache.xerces.xni.XMLAttributes; 24 import org.apache.xerces.xs.ShortList; 25 import org.apache.xerces.xs.XSTypeDefinition; 26 27 35 public class Selector { 36 37 41 42 protected Selector.XPath fXPath; 43 44 45 protected IdentityConstraint fIdentityConstraint; 46 47 protected IdentityConstraint fIDConstraint; 50 51 55 56 public Selector(Selector.XPath xpath, 57 IdentityConstraint identityConstraint) { 58 fXPath = xpath; 59 fIdentityConstraint = identityConstraint; 60 } 62 66 67 public org.apache.xerces.impl.xpath.XPath getXPath() { 68 return fXPath; 69 } 71 72 public IdentityConstraint getIDConstraint() { 73 return fIdentityConstraint; 74 } 76 78 83 public XPathMatcher createMatcher(FieldActivator activator, int initialDepth) { 84 return new Selector.Matcher(fXPath, activator, initialDepth); 85 } 87 91 92 public String toString() { 93 return fXPath.toString(); 94 } 96 100 106 public static class XPath 107 extends org.apache.xerces.impl.xpath.XPath { 108 109 113 114 public XPath(String xpath, SymbolTable symbolTable, 115 NamespaceContext context) throws XPathException { 116 super(normalize(xpath), symbolTable, context); 117 for (int i=0;i<fLocationPaths.length;i++) { 119 org.apache.xerces.impl.xpath.XPath.Axis axis = 120 fLocationPaths[i].steps[fLocationPaths[i].steps.length-1].axis; 121 if (axis.type == XPath.Axis.ATTRIBUTE) { 122 throw new XPathException("c-selector-xpath"); 123 } 124 } 125 126 } 128 private static String normalize(String xpath) { 129 StringBuffer modifiedXPath = new StringBuffer (xpath.length()+5); 137 int unionIndex = -1; 138 do { 139 if(!(xpath.trim().startsWith("/") ||xpath.trim().startsWith("."))) { 140 modifiedXPath.append("./"); 141 } 142 unionIndex = xpath.indexOf('|'); 143 if(unionIndex == -1) { 144 modifiedXPath.append(xpath); 145 break; 146 } 147 modifiedXPath.append(xpath.substring(0,unionIndex+1)); 148 xpath = xpath.substring(unionIndex+1, xpath.length()); 149 } while(true); 150 return modifiedXPath.toString(); 151 } 152 153 } 155 160 public class Matcher 161 extends XPathMatcher { 162 163 167 168 protected FieldActivator fFieldActivator; 169 170 171 protected int fInitialDepth; 172 173 174 protected int fElementDepth; 175 176 177 protected int fMatchedDepth; 178 179 183 184 public Matcher(Selector.XPath xpath, FieldActivator activator, 185 int initialDepth) { 186 super(xpath); 187 fFieldActivator = activator; 188 fInitialDepth = initialDepth; 189 } 191 195 public void startDocumentFragment(){ 196 super.startDocumentFragment(); 197 fElementDepth = 0; 198 fMatchedDepth = -1; 199 } 201 210 public void startElement(QName element, XMLAttributes attributes) { 211 super.startElement(element, attributes); 212 fElementDepth++; 213 216 if (isMatched()) { 217 219 fMatchedDepth = fElementDepth; 220 fFieldActivator.startValueScopeFor(fIdentityConstraint, fInitialDepth); 221 int count = fIdentityConstraint.getFieldCount(); 222 for (int i = 0; i < count; i++) { 223 Field field = fIdentityConstraint.getFieldAt(i); 224 XPathMatcher matcher = fFieldActivator.activateField(field, fInitialDepth); 225 matcher.startElement(element, attributes); 226 } 227 } 228 229 } 231 public void endElement(QName element, XSTypeDefinition type, boolean nillable, Object actualValue, short valueType, ShortList itemValueType) { 232 super.endElement(element, type, nillable, actualValue, valueType, itemValueType); 233 if (fElementDepth-- == fMatchedDepth) { 234 fMatchedDepth = -1; 235 fFieldActivator.endValueScopeFor(fIdentityConstraint, fInitialDepth); 236 } 237 } 238 239 240 public IdentityConstraint getIdentityConstraint() { 241 return fIdentityConstraint; 242 } 244 245 public int getInitialDepth() { 246 return fInitialDepth; 247 } 249 250 } 252 } | Popular Tags |