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      public HeaderCheckWorker(Reader reader, int numberOfRetainedHeaderLine,
48              final IHeaderMatcher matcher, final Document name) {
49          this(new BufferedReader(reader), numberOfRetainedHeaderLine, matcher, name);
50      }
51  
52  
53      /**
54       * Convenience constructor wraps given <code>Reader</code>
55       * in a <code>BufferedReader</code>.
56       * @param reader a <code>Reader</code> for the content, not null
57       * @param name the name of the checked content, possibly null
58       */
59      public HeaderCheckWorker(Reader reader, final IHeaderMatcher matcher, final Document name) {
60          this(new BufferedReader(reader), matcher, name);
61      }
62  
63      public HeaderCheckWorker(BufferedReader reader, final IHeaderMatcher matcher,
64              final Document name) {
65          this(reader, DEFAULT_NUMBER_OF_RETAINED_HEADER_LINES, matcher, name);
66      }
67  
68      public HeaderCheckWorker(BufferedReader reader, int numberOfRetainedHeaderLine, final IHeaderMatcher matcher,
69              final Document name) {
70          this.reader = reader;
71          this.numberOfRetainedHeaderLines = numberOfRetainedHeaderLine;
72          this.matcher = matcher;
73          this.subject = name;
74      }
75  
76      public boolean isFinished() {
77          return finished;
78      }
79  
80      public void read() throws RatHeaderAnalysisException {
81          if (!finished) {
82              final StringBuilder headers = new StringBuilder();
83              headerLinesToRead = numberOfRetainedHeaderLines;
84              try {
85                  while(readLine(headers)) {
86                      // do nothing
87                  }
88                  if (!match) {
89                      final String notes = headers.toString();
90                      final MetaData metaData = subject.getMetaData();
91                      metaData.set(new MetaData.Datum(MetaData.RAT_URL_HEADER_SAMPLE, notes));
92                      metaData.set(new MetaData.Datum(MetaData.RAT_URL_HEADER_CATEGORY, MetaData.RAT_LICENSE_FAMILY_CATEGORY_VALUE_UNKNOWN));
93                      metaData.set(MetaData.RAT_LICENSE_FAMILY_NAME_DATUM_UNKNOWN);
94                  }
95              } catch (IOException e) {
96                  throw new RatHeaderAnalysisException("Cannot read header for " + subject, e);
97              }
98              IOUtils.closeQuietly(reader);
99              matcher.reset();
100         }
101         finished = true;
102     }
103 
104     boolean readLine(StringBuilder headers) throws IOException, RatHeaderAnalysisException {
105         String line = reader.readLine();
106         boolean result = line != null;
107         if (result) {
108             if (headerLinesToRead-- > 0) {
109                 headers.append(line);
110                 headers.append('\n');
111             }
112             match = matcher.match(subject, line);
113             result = !match;
114         }
115         return result;
116     }
117 }