View Javadoc
1   package org.sentrysoftware.jawk.ext;
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 org.sentrysoftware.jawk.jrt.IllegalAwkArgumentException;
26  import org.sentrysoftware.jawk.jrt.JRT;
27  import org.sentrysoftware.jawk.jrt.VariableManager;
28  import org.sentrysoftware.jawk.util.AwkSettings;
29  
30  /**
31   * Base class of various extensions.
32   * <p>
33   * Provides functionality common to most extensions,
34   * such as VM and JRT variable management, and convenience
35   * methods such as checkNumArgs() and toAwkString().
36   *
37   * @author Danny Daglas
38   */
39  public abstract class AbstractExtension implements JawkExtension {
40  
41  	private JRT jrt;
42  	private VariableManager vm;
43  	private AwkSettings settings;
44  
45  	/** {@inheritDoc} */
46  	@Override
47  	public void init(VariableManager vm, JRT jrt, final AwkSettings settings) {
48  		this.vm = vm;
49  		this.jrt = jrt;
50  		this.settings = settings;
51  	}
52  
53  	/**
54  	 * Convert a Jawk variable to a Jawk string
55  	 * based on the value of the CONVFMT variable.
56  	 *
57  	 * @param obj The Jawk variable to convert to a Jawk string.
58  	 * @return A string representation of obj after CONVFMT
59  	 *   has been applied.
60  	 */
61  	protected final String toAwkString(Object obj) {
62  		return JRT.toAwkString(obj, getVm().getCONVFMT().toString(), settings.getLocale());
63  	}
64  
65  	/**
66  	 * {@inheritDoc}
67  	 *
68  	 * Assume no guarantee of any extension parameter being an
69  	 * associative array.
70  	 */
71  	@Override
72  	public int[] getAssocArrayParameterPositions(String extensionKeyword, int argCount) {
73  		return new int[0];
74  	}
75  
76  	/**
77  	 * Verifies that an exact number of arguments
78  	 * has been passed in by checking the length
79  	 * of the argument array.
80  	 *
81  	 * @param arr The arguments to check.
82  	 * @param expectedNum The expected number of arguments.
83  	 */
84  	protected static void checkNumArgs(Object[] arr, int expectedNum) {
85  		// some sanity checks on the arguments
86  		// (made into assertions so that
87  		// production code does not perform
88  		// these checks)
89  		assert arr != null;
90  		assert expectedNum >= 0;
91  
92  		if (arr.length != expectedNum) {
93  			throw new IllegalAwkArgumentException("Expecting " + expectedNum + " arg(s), got " + arr.length);
94  		}
95  	}
96  
97  	/**
98  	 * <p>Getter for the field <code>jrt</code>.</p>
99  	 *
100 	 * @return the Runtime
101 	 */
102 	protected JRT getJrt() {
103 		return jrt;
104 	}
105 
106 	/**
107 	 * <p>Getter for the field <code>vm</code>.</p>
108 	 *
109 	 * @return the Variable Manager
110 	 */
111 	protected VariableManager getVm() {
112 		return vm;
113 	}
114 
115 	/**
116 	 * <p>Getter for the field <code>settings</code>.</p>
117 	 *
118 	 * @return the Settings
119 	 */
120 	protected AwkSettings getSettings() {
121 		return settings;
122 	}
123 }