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.header;
20
21 import org.apache.rat.DeprecationReporter;
22
23 import java.io.IOException;
24 import java.io.Reader;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27
28 /**
29 * <p>Matches headers.</p>
30 * <p><strong>Usage:</strong></p>
31 * <ol>
32 * <li>{@link #read(Reader)} content</li>
33 * <li>{@link #matches(Pattern)} against filtered content</li>
34 * </ol>
35 * <p><strong>Note:</strong> use only from a single thread.</p>
36 *
37 */
38 @Deprecated // since 0.17
39 @DeprecationReporter.Info(since = "0.17", forRemoval = true)
40 public class HeaderMatcher {
41
42 private final FilteringSequenceFactory factory;
43 private final HeaderBean[] headers;
44 private CharSequence read;
45 private int lines;
46
47 public HeaderMatcher(final CharFilter filter, final int capacity) {
48 this(filter, capacity, null);
49 }
50
51 public HeaderMatcher(final CharFilter filter, final int capacity, final HeaderBean[] headers) {
52 DeprecationReporter.logDeprecated(this.getClass());
53 factory = new FilteringSequenceFactory(capacity, filter);
54 read = null;
55 this.headers = headers;
56 }
57
58 public void read(Reader reader) throws IOException {
59 final LineNumberReader lineNumberReader = new LineNumberReader(reader);
60 read = factory.filter(lineNumberReader);
61 if (lineNumberReader.read() == -1) {
62 lines = lineNumberReader.getLineNumber();
63 } else {
64 lines = -1;
65 }
66 if (headers != null) {
67 for (final HeaderBean headerBean : headers) {
68 if (headerBean != null) {
69 final Pattern headerPattern = headerBean.getHeaderPattern();
70 if (headerPattern != null) {
71 final boolean matches = matches(headerPattern);
72 headerBean.setMatch(matches);
73 }
74 }
75 }
76 }
77 }
78
79 /**
80 * <p>Seeks a match in the last headers read.</p>
81 * <p><strong>Note</strong> that this pattern
82 * must not contain filtered characters.
83 * </p>
84 * @param pattern <code>Pattern</code> to match
85 * @return true if the pattern matches,
86 * false otherwise or if {@link #read(Reader)} has not been
87 * called
88 */
89 public boolean matches(Pattern pattern) {
90 boolean result = false;
91 if (read != null) {
92 final Matcher matcher = pattern.matcher(read);
93 result = matcher.matches();
94 }
95 return result;
96 }
97
98 /**
99 * Number of lines read.
100 * @return the number of lines in the file
101 * or -1 if the file has more lines than were read
102 */
103 public int lines() {
104 return lines;
105 }
106 }