1 package io.jawk.util;
2
3 /*-
4 * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
5 * Jawk
6 * ჻჻჻჻჻჻
7 * Copyright (C) 2006 - 2026 MetricsHub
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.IOException;
26 import java.io.Reader;
27 import java.io.UncheckedIOException;
28 import java.nio.charset.StandardCharsets;
29 import java.nio.file.Files;
30 import java.nio.file.Paths;
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 String filePath;
40 private Reader fileReader;
41
42 /**
43 * <p>
44 * Constructor for ScriptFileSource.
45 * </p>
46 *
47 * @param filePath a {@link java.lang.String} object
48 */
49 public ScriptFileSource(String filePath) {
50 super(filePath, null);
51 this.filePath = filePath;
52 this.fileReader = null;
53 }
54
55 /**
56 * <p>
57 * Getter for the field <code>filePath</code>.
58 * </p>
59 *
60 * @return a {@link java.lang.String} object
61 */
62 public String getFilePath() {
63 return filePath;
64 }
65
66 /** {@inheritDoc} */
67 @Override
68 public Reader getReader() {
69 if (fileReader == null) {
70 try {
71 fileReader = Files.newBufferedReader(Paths.get(filePath), StandardCharsets.UTF_8);
72 } catch (IOException ex) {
73 throw new UncheckedIOException("Failed to open script source for reading: " + filePath, ex);
74 }
75 }
76
77 return fileReader;
78 }
79 }