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 java.io.BufferedReader;
22  import java.io.IOException;
23  import java.io.Reader;
24  
25  import org.apache.rat.api.Document;
26  import org.apache.rat.api.MetaData;
27  
28  /**
29   * <p>Reads from a stream to check license.</p>
30   * <p><strong>Note</strong> that this class is not thread safe.</p> 
31   */
32  class HeaderCheckWorker {
33  
34      public static final int DEFAULT_NUMBER_OF_RETAINED_HEADER_LINES = 50;
35  	
36  	private final int numberOfRetainedHeaderLines;
37  	private final BufferedReader reader;
38  	private final IHeaderMatcher matcher;
39      private final Document subject;
40      
41  	private boolean match = false;
42  	
43  	private int headerLinesToRead;
44  	private boolean finished = false;
45  
46  	public HeaderCheckWorker(Reader reader, int numberOfRetainedHeaderLine, 
47              final IHeaderMatcher matcher, final Document name) {
48  		this(new BufferedReader(reader), numberOfRetainedHeaderLine, matcher, name);
49  	}
50  	
51  	
52  	/**
53  	 * Convenience constructor wraps given <code>Reader</code> 
54  	 * in a <code>BufferedReader</code>.
55  	 * @param reader a <code>Reader</code> for the content, not null
56  	 * @param name the name of the checked content, possibly null
57  	 */
58  	public HeaderCheckWorker(Reader reader, final IHeaderMatcher matcher, final Document name) {
59  		this(new BufferedReader(reader), matcher, name);
60  	}
61  	
62  	public HeaderCheckWorker(BufferedReader reader, final IHeaderMatcher matcher,
63              final Document name) {
64  		this(reader, DEFAULT_NUMBER_OF_RETAINED_HEADER_LINES, matcher, name);
65  	}
66  	
67  	public HeaderCheckWorker(BufferedReader reader, int numberOfRetainedHeaderLine, final IHeaderMatcher matcher,
68              final Document name) {
69  		this.reader = reader;
70  		this.numberOfRetainedHeaderLines = numberOfRetainedHeaderLine;
71  		this.matcher = matcher;
72          this.subject = name;
73  	}
74  
75  	public boolean isFinished() {
76  		return finished;
77  	}
78  
79  	public void read() throws RatHeaderAnalysisException {
80  		if (!finished) {
81  			final StringBuffer headers = new StringBuffer();
82  			headerLinesToRead = numberOfRetainedHeaderLines;
83  			try {
84  				while(readLine(headers));
85  				if (!match) {
86  					final String notes = headers.toString();
87                      final MetaData metaData = subject.getMetaData();
88                      metaData.set(new MetaData.Datum(MetaData.RAT_URL_HEADER_SAMPLE, notes));
89                      metaData.set(new MetaData.Datum(MetaData.RAT_URL_HEADER_CATEGORY, MetaData.RAT_LICENSE_FAMILY_CATEGORY_VALUE_UNKNOWN));
90                      metaData.set(MetaData.RAT_LICENSE_FAMILY_NAME_DATUM_UNKNOWN);
91  				}
92  			} catch (IOException e) {
93                  throw new RatHeaderAnalysisException("Cannot read header for " + subject, e);
94              }
95  			try {
96  				reader.close();
97  			} catch (IOException e) {
98  				// swallow
99  			}
100             matcher.reset();
101 		}
102 		finished = true;
103 	}
104 	
105 	boolean readLine(StringBuffer headers) throws IOException, RatHeaderAnalysisException {
106 		String line = reader.readLine();
107 		boolean result = line != null;
108 		if (result) {
109 			if (headerLinesToRead-- > 0) {
110 				headers.append(line);
111 				headers.append('\n');
112 			}
113             match = matcher.match(subject, line);
114 			result = !match;
115 		}
116 		return result;
117 	}
118 }