1 61 62 package org.jaxen.pattern; 63 64 import org.jaxen.Context; 65 import org.jaxen.Navigator; 66 67 72 public class NamespaceTest extends NodeTest { 73 74 75 private String prefix; 76 77 78 private short nodeType; 79 80 public NamespaceTest(String prefix, short nodeType) 81 { 82 if ( prefix == null ) 83 { 84 prefix = ""; 85 } 86 this.prefix = prefix; 87 this.nodeType = nodeType; 88 } 89 90 92 public boolean matches( Object node, Context context ) 93 { 94 Navigator navigator = context.getNavigator(); 95 String uri = getURI( node, context ); 96 97 if ( nodeType == Pattern.ELEMENT_NODE ) 98 { 99 return navigator.isElement( node ) 100 && uri.equals( navigator.getElementNamespaceUri( node ) ); 101 } 102 else if ( nodeType == Pattern.ATTRIBUTE_NODE ) 103 { 104 return navigator.isAttribute( node ) 105 && uri.equals( navigator.getAttributeNamespaceUri( node ) ); 106 } 107 return false; 108 } 109 110 public double getPriority() 111 { 112 return -0.25; 113 } 114 115 116 public short getMatchType() 117 { 118 return nodeType; 119 } 120 121 public String getText() 122 { 123 return prefix + ":"; 124 } 125 126 public String toString() 127 { 128 return super.toString() + "[ prefix: " + prefix + " type: " + nodeType + " ]"; 129 } 130 131 133 protected String getURI(Object node, Context context) 134 { 135 String uri = context.getNavigator().translateNamespacePrefixToUri( prefix, node ); 136 if ( uri == null ) 137 { 138 uri = context.getContextSupport().translateNamespacePrefixToUri( prefix ); 139 } 140 if ( uri == null ) 141 { 142 uri = ""; 143 } 144 return uri; 145 } 146 } 147 | Popular Tags |