1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 */
19 package org.apache.rat.analysis.matchers;
20
21 import java.util.Collection;
22
23 import org.apache.rat.analysis.IHeaderMatcher;
24
25 /**
26 * A matcher that performs a logical {@code OR} across all the contained matchers.
27 */
28 public class OrMatcher extends AbstractMatcherContainer {
29
30 private State lastState;
31
32 /**
33 * Constructs the matcher from the enclosed matchers.
34 * @param enclosed the enclosed matchers.
35 */
36 public OrMatcher(Collection<? extends IHeaderMatcher> enclosed) {
37 this(null, enclosed);
38 }
39
40 /**
41 * Constructs the matcher with the specified id from the enclosed matchers.
42 * @param id the id to use.
43 * @param enclosed the enclosed matchers.
44 */
45 public OrMatcher(String id, Collection<? extends IHeaderMatcher> enclosed) {
46 super(id, enclosed);
47 lastState = State.i;
48 }
49
50 @Override
51 public State matches(String line) {
52 if (lastState == State.t) {
53 return State.t;
54 }
55 for (IHeaderMatcher matcher : enclosed) {
56 switch (matcher.matches(line)) {
57 case t:
58 lastState = State.t;
59 return lastState;
60 case f:
61 case i:
62 lastState = State.i;
63 }
64 }
65 return lastState;
66 }
67
68 @Override
69 public State currentState() {
70 if (lastState == State.t) {
71 return lastState;
72 }
73 for (IHeaderMatcher matcher : enclosed) {
74 switch (matcher.currentState()) {
75 case t:
76 lastState = State.t;
77 return lastState;
78 case i:
79 lastState = State.i;
80 return lastState;
81 case f:
82 // do nothing;
83 break;
84 }
85 }
86 lastState = State.f;
87 return lastState;
88 }
89
90 @Override
91 public void reset() {
92 super.reset();
93 lastState = State.i;
94 }
95 }