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 (int i=0;i<length;i++) {
65                  final HeaderBean headerBean = headers[i];
66                  if (headerBean != null) {
67                      final Pattern headerPattern = headerBean.getHeaderPattern();
68                      if (headerPattern != null) {
69                          final boolean matches = matches(headerPattern);
70                          headerBean.setMatch(matches);
71                      }
72                  }
73              }
74          }
75      }
76      
77      /**
78       * <p>Seeks a match in the last headers read.</p>
79       * <p><strong>Note</strong> that this pattern
80       * must not contain filtered characters.
81       * </p>
82       * @param pattern <code>Pattern</code> to match
83       * @return true if the pattern matches,
84       * false otherwise or if {@link #read(Reader)} has not been
85       * called
86       */
87      public boolean matches(Pattern pattern) {
88          boolean result = false;
89          if (read != null) {
90              final Matcher matcher = pattern.matcher(read);
91              result = matcher.matches();
92          }
93          return result;
94      }
95      
96      /**
97       * Number of lines read.
98       * @return the number of lines in the file
99       * or -1 if the file has more lines than were read
100      */
101     public int lines() {
102         return lines;
103     }
104 }