View Javadoc
1   package org.apache.rat.analysis;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one   *
4    * or more contributor license agreements.  See the NOTICE file *
5    * distributed with this work for additional information        *
6    * regarding copyright ownership.  The ASF licenses this file   *
7    * to you under the Apache License, Version 2.0 (the            *
8    * "License"); you may not use this file except in compliance   *
9    * with the License.  You may obtain a copy of the License at   *
10   *                                                              *
11   *   http://www.apache.org/licenses/LICENSE-2.0                 *
12   *                                                              *
13   * Unless required by applicable law or agreed to in writing,   *
14   * software distributed under the License is distributed on an  *
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
16   * KIND, either express or implied.  See the License for the    *
17   * specific language governing permissions and limitations      *
18   * under the License.                                           *
19   */
20  
21  import org.apache.commons.io.IOUtils;
22  import org.apache.rat.api.Document;
23  import org.apache.rat.api.MetaData;
24  
25  import java.io.BufferedReader;
26  import java.io.IOException;
27  import java.io.Reader;
28  
29  /**
30   * <p>Reads from a stream to check license.</p>
31   * <p><strong>Note</strong> that this class is not thread safe.</p> 
32   */
33  class HeaderCheckWorker {
34  
35      public static final int DEFAULT_NUMBER_OF_RETAINED_HEADER_LINES = 50;
36  
37      private final int numberOfRetainedHeaderLines;
38      private final BufferedReader reader;
39      private final IHeaderMatcher matcher;
40      private final Document subject;
41      
42      private boolean match = false;
43  
44      private int headerLinesToRead;
45      private boolean finished = false;
46  
47      /**
48       * Convenience constructor wraps given <code>Reader</code>
49       * in a <code>BufferedReader</code>.
50       * @param reader a <code>Reader</code> for the content, not null
51       * @param name the name of the checked content, possibly null
52       */
53      public HeaderCheckWorker(Reader reader, final IHeaderMatcher matcher, final Document name) {
54          this(new BufferedReader(reader), matcher, name);
55      }
56  
57      public HeaderCheckWorker(BufferedReader reader, final IHeaderMatcher matcher,
58              final Document name) {
59          this(reader, DEFAULT_NUMBER_OF_RETAINED_HEADER_LINES, matcher, name);
60      }
61  
62      public HeaderCheckWorker(BufferedReader reader, int numberOfRetainedHeaderLine, final IHeaderMatcher matcher,
63              final Document name) {
64          this.reader = reader;
65          this.numberOfRetainedHeaderLines = numberOfRetainedHeaderLine;
66          this.matcher = matcher;
67          this.subject = name;
68      }
69  
70      public boolean isFinished() {
71          return finished;
72      }
73  
74      public void read() throws RatHeaderAnalysisException {
75          if (!finished) {
76              final StringBuilder headers = new StringBuilder();
77              headerLinesToRead = numberOfRetainedHeaderLines;
78              try {
79                  while(readLine(headers)) {
80                      // do nothing
81                  }
82                  if (!match) {
83                      final String notes = headers.toString();
84                      final MetaData metaData = subject.getMetaData();
85                      metaData.set(new MetaData.Datum(MetaData.RAT_URL_HEADER_SAMPLE, notes));
86                      metaData.set(new MetaData.Datum(MetaData.RAT_URL_HEADER_CATEGORY, MetaData.RAT_LICENSE_FAMILY_CATEGORY_VALUE_UNKNOWN));
87                      metaData.set(MetaData.RAT_LICENSE_FAMILY_NAME_DATUM_UNKNOWN);
88                  }
89              } catch (IOException e) {
90                  throw new RatHeaderAnalysisException("Cannot read header for " + subject, e);
91              }
92              IOUtils.closeQuietly(reader);
93              matcher.reset();
94          }
95          finished = true;
96      }
97  
98      boolean readLine(StringBuilder headers) throws IOException, RatHeaderAnalysisException {
99          String line = reader.readLine();
100         boolean result = line != null;
101         if (result) {
102             if (headerLinesToRead-- > 0) {
103                 headers.append(line);
104                 headers.append('\n');
105             }
106             match = matcher.match(subject, line);
107             result = !match;
108         }
109         return result;
110     }
111 }