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.IOException;
26  import java.io.InputStream;
27  import java.io.Reader;
28  
29  /**
30   * Represents one AWK-script content source.
31   * This is usually either a string,
32   * given on the command line with the first non-"-" parameter,
33   * or an "*.awk" (normal) or "*.ai" (intermediate) script,
34   * given as a path with a "-f" command line switch.
35   *
36   * @author Danny Daglas
37   */
38  public class ScriptSource {
39  
40  	/** Constant <code>DESCRIPTION_COMMAND_LINE_SCRIPT="&lt;command-line-supplied-script&gt;"</code> */
41  	public static final String DESCRIPTION_COMMAND_LINE_SCRIPT
42  			= "<command-line-supplied-script>";
43  
44  	private String description;
45  	private Reader reader;
46  	private boolean intermediate;
47  
48  	/**
49  	 * <p>Constructor for ScriptSource.</p>
50  	 *
51  	 * @param description a {@link java.lang.String} object
52  	 * @param reader a {@link java.io.Reader} object
53  	 * @param intermediate a boolean
54  	 */
55  	public ScriptSource(String description, Reader reader, boolean intermediate) {
56  
57  		this.description = description;
58  		this.reader = reader;
59  		this.intermediate = intermediate;
60  	}
61  
62  	/**
63  	 * <p>Getter for the field <code>description</code>.</p>
64  	 *
65  	 * @return a {@link java.lang.String} object
66  	 */
67  	public final String getDescription() {
68  		return description;
69  	}
70  
71  	/**
72  	 * Obtain the InputStream containing the intermediate file.
73  	 * This returns non-null only if {@see #isIntermediate()}
74  	 * returns <code>false</code>.
75  	 *
76  	 * @return The reader which contains the intermediate file, null if
77  	 *   either the -f argument is not used, or the argument does not
78  	 *   refer to an intermediate file.
79  	 * @throws java.io.IOException if any.
80  	 */
81  	public Reader getReader()
82  			throws IOException
83  	{
84  		return reader;
85  	}
86  
87  	/**
88  	 * Returns the <code>InputStream</code> serving the contents of this source.
89  	 * This returns non-<code>null</code> only if {@see #isIntermediate()}
90  	 * returns <code>true</code>.
91  	 *
92  	 * @return a {@link java.io.InputStream} object
93  	 * @throws java.io.IOException if any.
94  	 */
95  	public InputStream getInputStream()
96  			throws IOException
97  	{
98  		return null;
99  	}
100 
101 	/**
102 	 * Indicates whether the underlying source is an intermediate file.
103 	 * Intermediate files end with the ".ai" extension.
104 	 * No other determination is made whether the file is an intermediate
105 	 * one or not.
106 	 * That is, the content of the file is not checked.
107 	 *
108 	 * @return <code>true</code> if the "-f optarg" is an intermediate file
109 	 *   (a file ending in ".ai"), <code>false</code> otherwise.
110 	 */
111 	public final boolean isIntermediate() {
112 		return intermediate;
113 	}
114 
115 	/** {@inheritDoc} */
116 	@Override
117 	public String toString() {
118 		return getDescription();
119 	}
120 }