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