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.document.impl;
20  
21  import java.io.File;
22  import java.io.FileInputStream;
23  import java.io.FileReader;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.io.InputStreamReader;
27  import java.io.Reader;
28  import java.net.URL;
29  
30  import org.apache.rat.api.Document;
31  
32  
33  public class MonolithicFileDocument extends AbstractMonolithicDocument {
34  	private final File file;
35  
36      /**
37       * Creates a new instance. The document is read from the
38       * given URL.
39       */
40      public static Document newInstance(final URL url) {
41      	if ("file".equals(url.getProtocol())) {
42      		final File f = new File(url.getFile());
43      		return new MonolithicFileDocument(f);
44      	}
45      	return new AbstractMonolithicDocument(url.toExternalForm()){
46  			public Reader reader() throws IOException {
47  				return new InputStreamReader(inputStream(), "UTF-8");
48  			}
49  
50              public InputStream inputStream() throws IOException {
51                  return url.openStream();
52              }
53      	};
54      }
55  
56      public MonolithicFileDocument(final File file) {
57      	super(DocumentImplUtils.toName(file));
58          this.file = file;
59      }
60  
61      public Reader reader() throws IOException {
62          return new FileReader(file);
63      }
64  
65      public InputStream inputStream() throws IOException {
66          return new FileInputStream(file);
67      }
68  }