1 22 23 package org.xquark.schema; 24 25 public class XPathExpr { 26 private static final String RCSRevision = "$Revision: 1.1 $"; 27 private static final String RCSName = "$Name: $"; 28 private java.util.ArrayList fields = new java.util.ArrayList (4); 29 private boolean anyLevel = false; 30 private boolean isAttribute = false; 31 32 public XPathExpr(boolean anyLevel) { 33 this.anyLevel = anyLevel; 34 } 35 36 public void addStep(String namespaceURI, String localName) { 37 fields.add(new XPathExpr.Step(namespaceURI, localName)); 38 } 39 40 public void setAttribute(boolean isAttribute) { 41 this.isAttribute = isAttribute; 42 } 43 44 public int getStepCount() { 45 return fields.size(); 46 } 47 48 public boolean matches(String namespaceURI, String localName, int index, boolean attr) { 49 if (index >= fields.size()) return false; 50 if (isAttribute && index == fields.size()-1 && !attr) return false; 51 if (attr && (!isAttribute || index != fields.size()-1)) return false; 52 XPathExpr.Step step = (XPathExpr.Step)fields.get(index); 53 return step.matches(namespaceURI, localName); 54 } 55 56 public boolean isAnyLevel() { 57 return anyLevel; 58 } 59 60 static class Step { 61 private static final String RCSRevision = "$Revision: 1.1 $"; 62 private static final String RCSName = "$Name: $"; 63 String namespaceURI; 64 String localName; 65 boolean isAny = false; 66 67 Step(String namespaceURI, String localName) { 68 this.namespaceURI = namespaceURI; 69 this.localName = localName; 70 if ("*".equals(localName)) { 71 this.localName = null; 72 if (namespaceURI == null) 73 isAny = true; 74 } 75 } 76 77 boolean matches(String ns, String name) { 78 if (isAny) 79 return true; 80 else if (localName == null) 81 return namespaceURI.equals(ns); 82 else 83 return localName.equals(name) 84 && ((namespaceURI == null && ns == null) || (namespaceURI != null && namespaceURI.equals(ns))); 85 } 86 } 87 } 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | Popular Tags |