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