1 61 62 package org.jaxen.pattern; 63 64 import org.jaxen.Context; 65 import org.jaxen.JaxenException; 66 67 72 public class UnionPattern extends Pattern { 73 74 private Pattern lhs; 75 private Pattern rhs; 76 private short nodeType = ANY_NODE; 77 private String matchesNodeName = null; 78 79 80 public UnionPattern() 81 { 82 } 83 84 public UnionPattern(Pattern lhs, Pattern rhs) 85 { 86 this.lhs = lhs; 87 this.rhs = rhs; 88 init(); 89 } 90 91 92 public Pattern getLHS() 93 { 94 return lhs; 95 } 96 97 public void setLHS(Pattern lhs) 98 { 99 this.lhs = lhs; 100 init(); 101 } 102 103 public Pattern getRHS() 104 { 105 return rhs; 106 } 107 108 public void setRHS(Pattern rhs) 109 { 110 this.rhs = rhs; 111 init(); 112 } 113 114 115 118 120 public boolean matches( Object node, Context context ) throws JaxenException 121 { 122 return lhs.matches( node, context ) || rhs.matches( node, context ); 123 } 124 125 public Pattern[] getUnionPatterns() 126 { 127 return new Pattern[] { lhs, rhs }; 128 } 129 130 131 public short getMatchType() 132 { 133 return nodeType; 134 } 135 136 137 public String getMatchesNodeName() 138 { 139 return matchesNodeName; 140 } 141 142 143 public Pattern simplify() 144 { 145 this.lhs = lhs.simplify(); 146 this.rhs = rhs.simplify(); 147 init(); 148 return this; 149 } 150 151 public String getText() 152 { 153 return lhs.getText() + " | " + rhs.getText(); 154 } 155 156 public String toString() 157 { 158 return super.toString() + "[ lhs: " + lhs + " rhs: " + rhs + " ]"; 159 } 160 161 private void init() 164 { 165 short type1 = lhs.getMatchType(); 166 short type2 = rhs.getMatchType(); 167 this.nodeType = ( type1 == type2 ) ? type1 : ANY_NODE; 168 169 String name1 = lhs.getMatchesNodeName(); 170 String name2 = rhs.getMatchesNodeName(); 171 172 this.matchesNodeName = null; 173 if ( name1 != null && name2 != null && name1.equals( name2 ) ) 174 { 175 this.matchesNodeName = name1; 176 } 177 } 178 } 179 | Popular Tags |