1 /**
2 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3 */
4 package net.sourceforge.pmd.util.datasource;
5
6 import java.io.Reader;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import org.apache.commons.io.input.ReaderInputStream;
10
11
12 /**
13 * DataSource implementation to read data from a Reader.
14 */
15 public class ReaderDataSource implements DataSource {
16 /**
17 * Reader
18 */
19 private Reader reader;
20
21 /**
22 * Real or pseudo filename or path name.
23 *
24 * <p>Including a file suffix mapped to the correct language helps assign the correct parser.
25 * </p>
26 */
27 private String dataSourceName;
28
29 /**
30 * Create the DataSource from the Reader.
31 */
32 public ReaderDataSource(Reader reader, String dataSourceName) {
33 this.reader = reader;
34 this.dataSourceName = dataSourceName;
35 }
36
37 /**
38 * Convert the Reader into an InputStream.
39 * <p>
40 * <strong>Note:</strong> This uses the default encoding.
41 * </p>
42 *
43 * @return Derived InputStream
44 * @throws IOException
45 */
46 @Override
47 public InputStream getInputStream() throws IOException {
48 return new ReaderInputStream(reader);
49 }
50
51 /**
52 * Return the dataSourceName via the {@link DataSource} Interface method.
53 *
54 * <p>Both the parameters are ignored
55 * </p>
56 *
57 * @param shortNames ignored
58 * @param inputFileName ignored
59 * @return
60 */
61 @Override
62 public String getNiceFileName(boolean shortNames, String inputFileName) {
63 return getDataSourceName() ;
64 }
65
66
67 /**
68 * @return the dataSourceName
69 */
70 public String getDataSourceName() {
71 return dataSourceName;
72 }
73
74 /**
75 * @param dataSourceName the dataSourceName to set
76 */
77 public void setDataSourceName(String dataSourceName) {
78 this.dataSourceName = dataSourceName;
79 }
80
81 @Override
82 public String toString() {
83 return dataSourceName;
84 }
85 }