View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.tika.sax.xpath;
18  
19  /**
20   * Composite XPath evaluation state. Used when XPath evaluation results
21   * in two or more branches of independent evaluation states.
22   */
23  public class CompositeMatcher extends Matcher {
24  
25      private final Matcher a;
26  
27      private final Matcher b;
28  
29      public CompositeMatcher(Matcher a, Matcher b) {
30          this.a = a;
31          this.b = b;
32      }
33  
34      public Matcher descend(String namespace, String name) {
35          Matcher a = this.a.descend(namespace, name);
36          Matcher b = this.b.descend(namespace, name);
37          if (a == FAIL) {
38              return b;
39          } else if (b == FAIL) {
40              return a;
41          } else if (this.a == a && this.b == b) {
42              return this;
43          } else {
44              return new CompositeMatcher(a, b);
45          }
46      }
47  
48      public boolean matchesElement() {
49          return a.matchesElement() || b.matchesElement();
50      }
51  
52      public boolean matchesAttribute(String namespace, String name) {
53          return a.matchesAttribute(namespace, name)
54              || b.matchesAttribute(namespace, name);
55      }
56  
57      public boolean matchesText() {
58          return a.matchesText() || b.matchesText();
59      }
60  
61  }