View Javadoc
1   package org.sentrysoftware.jawk.util;
2   
3   /*-
4    * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
5    * Jawk
6    * ჻჻჻჻჻჻
7    * Copyright (C) 2006 - 2023 Sentry Software
8    * ჻჻჻჻჻჻
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   *
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   *
19   * You should have received a copy of the GNU General Lesser Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
23   */
24  
25  import java.io.FileInputStream;
26  import java.io.FileNotFoundException;
27  import java.io.FileReader;
28  import java.io.InputStream;
29  import java.io.Reader;
30  import org.slf4j.Logger;
31  
32  /**
33   * Represents one AWK-script file content source.
34   *
35   * @author Danny Daglas
36   */
37  public class ScriptFileSource extends ScriptSource {
38  
39  	private static final Logger LOG = AwkLogger.getLogger(ScriptFileSource.class);
40  
41  	private String filePath;
42  	private Reader fileReader;
43  	private InputStream fileInputStream;
44  
45  	/**
46  	 * <p>Constructor for ScriptFileSource.</p>
47  	 *
48  	 * @param filePath a {@link java.lang.String} object
49  	 */
50  	public ScriptFileSource(String filePath) {
51  		super(filePath, null, filePath.endsWith(".ai"));
52  
53  		this.filePath = filePath;
54  		this.fileReader = null;
55  		this.fileInputStream = null;
56  	}
57  
58  	/**
59  	 * <p>Getter for the field <code>filePath</code>.</p>
60  	 *
61  	 * @return a {@link java.lang.String} object
62  	 */
63  	public String getFilePath() {
64  		return filePath;
65  	}
66  
67  	/** {@inheritDoc} */
68  	@Override
69  	public Reader getReader() {
70  
71  		if ((fileReader == null) && !isIntermediate()) {
72  			try {
73  				fileReader = new FileReader(filePath);
74  			} catch (FileNotFoundException ex) {
75  				LOG.error("Failed to open script source for reading: " + filePath, ex);
76  			}
77  		}
78  
79  		return fileReader;
80  	}
81  
82  	/** {@inheritDoc} */
83  	@Override
84  	public InputStream getInputStream() {
85  
86  		if ((fileInputStream == null) && isIntermediate()) {
87  			try {
88  				fileInputStream = new FileInputStream(filePath);
89  			} catch (FileNotFoundException ex) {
90  				LOG.error("Failed to open script source for reading: " + filePath, ex);
91  			}
92  		}
93  
94  		return fileInputStream;
95  	}
96  }