<commit_before>/*
 * Zed Attack Proxy (ZAP) and its related class files.
 * 
 * ZAP is an HTTP/HTTPS proxy for assessing web application security.
 * 
 * Copyright 2013 ZAP development team
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0 
 *   
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.  
 */
package org.zaproxy.zap.extension.scripts.dialogs;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import org.parosproxy.paros.Constant;
import org.zaproxy.zap.extension.script.ScriptEngineWrapper;
import org.zaproxy.zap.extension.script.ScriptType;
import org.zaproxy.zap.extension.script.ScriptWrapper;
import org.zaproxy.zap.extension.scripts.ExtensionScriptsUI;
import org.zaproxy.zap.view.StandardFieldsDialog;

public class NewScriptDialog extends StandardFieldsDialog {

	private static final String FIELD_NAME = "scripts.dialog.script.label.name"; 
	private static final String FIELD_ENGINE = "scripts.dialog.script.label.engine"; 
	private static final String FIELD_DESC = "scripts.dialog.script.label.desc";
	private static final String FIELD_TEMPLATE = "scripts.dialog.script.label.template";
	private static final String FIELD_TYPE = "scripts.dialog.script.label.type";
	private static final String FIELD_LOAD = "scripts.dialog.script.label.load";

	private static final long serialVersionUID = 1L;

	private ExtensionScriptsUI extension = null;
	
	public NewScriptDialog(ExtensionScriptsUI ext, Frame owner, Dimension dim, ScriptWrapper template) {
		super(owner, "scripts.dialog.script.new.title", dim);
		this.extension = ext;
		init(template);
	}

	public void init (ScriptType type) {
		this.setFieldValue(FIELD_NAME, "");
		this.setFieldValue(FIELD_DESC, "");
		this.setComboFields(FIELD_TYPE, this.getTypes(), "");
		this.setComboFields(FIELD_ENGINE, this.getEngines(), "");
		this.setComboFields(FIELD_TEMPLATE, this.getTemplates(), "");
		
		if (type == null) {
			this.setFieldValue(FIELD_TYPE, "");
		} else {
			this.setFieldValue(FIELD_TYPE, Constant.messages.getString(type.getI18nKey()));
		}
	}

	public void init (ScriptWrapper template) {
		String name = "";
		String desc = "";
		String type = "";
		String engine = "";
		String templateName = "";

		if (template != null) {
			name = template.getName();
			desc = template.getDescription();
			type = Constant.messages.getString(template.getType().getI18nKey());
			engine = template.getEngineName();
			try {
				// Nasty, but it works ;)
				engine = extension.getExtScript().getEngineNameForExtension(template.getEngine().getExtensions().get(0));
			} catch (Exception e1) {
				// Ignore
			}
			templateName = template.getName();
		}

		this.removeAllFields();
		
		this.setTitle(Constant.messages.getString("scripts.dialog.script.new.title"));
		this.addTextField(FIELD_NAME, name);
		this.addComboField(FIELD_TYPE, this.getTypes(), type);
		this.addComboField(FIELD_ENGINE, this.getEngines(), engine);
		this.addComboField(FIELD_TEMPLATE, this.getTemplates(), templateName);
		this.addMultilineField(FIELD_DESC, desc);
		this.addCheckBoxField(FIELD_LOAD, true);

		this.addFieldListener(FIELD_TYPE, new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (isEmptyField(FIELD_TYPE)) {
					// Unset the template, otherwise it just resets the type again ;)
					if (!isEmptyField(FIELD_ENGINE)) {
						setFieldValue(FIELD_ENGINE, "");
					}
					setFieldValue(FIELD_TEMPLATE, "");
				}
				resetTemplates();
			}});

		this.addFieldListener(FIELD_ENGINE, new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (isEmptyField(FIELD_ENGINE)) {
					// Unset the template, otherwise it just resets the engine again ;)
					setFieldValue(FIELD_TEMPLATE, "");
				}
				resetTemplates();
			}});

		this.addPadding();
	}
	
	private void resetTemplates() {
		List<String> templates = getTemplates();
		if (templates.size() == 0) {
			// None to select :(
			setComboFields(FIELD_TEMPLATE, templates, "");
		} else if (! this.isEmptyField(FIELD_TEMPLATE) && templates.contains(getStringValue(FIELD_TEMPLATE))) {
			// Current selected one is still valid
			setComboFields(FIELD_TEMPLATE, templates, getStringValue(FIELD_TEMPLATE));
		} else if (templates.size() == 1 ) {
			// Only one - select that template
			setComboFields(FIELD_TEMPLATE, templates, templates.get(0));
		} else {
			// Default to none
			setComboFields(FIELD_TEMPLATE, templates, "");
		}
	}
	
	private List<String> getEngines() {
		ArrayList<String> list = new ArrayList<String>();
		list.add("");
		list.addAll(extension.getExtScript().getScriptingEngines());
		return list;
	}

	private List<String> getTypes() {
		ArrayList<String> list = new ArrayList<String>();
		list.add("");
		for (ScriptType type : extension.getExtScript().getScriptTypes()) {
			list.add(Constant.messages.getString(type.getI18nKey()));
		}
		return list;
	}
	
	private ScriptType nameToType (String name) {
		for (ScriptType type : extension.getExtScript().getScriptTypes()) {
			if (Constant.messages.getString(type.getI18nKey()).equals(name)) {
				return type;
			}
		}
		return null;
	}
	
	private ScriptEngineWrapper getSelectedEngine() {
		if (this.isEmptyField(FIELD_ENGINE)) {
			return null;
		}
		return extension.getExtScript().getEngineWrapper(this.getStringValue(FIELD_ENGINE));
	}

	private List<String> getTemplates() {
		ArrayList<String> list = new ArrayList<String>();
		for (ScriptWrapper template : extension.getExtScript().getTreeModel().getTemplates(
				this.nameToType(this.getStringValue(FIELD_TYPE)))) {
			
			if (getSelectedEngine() == null
					|| (template.getEngine() != null && template.getEngine().equals(getSelectedEngine()))) {
				list.add(template.getName());
			}
		}
		if (list.size() > 1) {
			// Only give an empty choice if theres more than one
			list.add("");
		}
		return list;<commit_msg>Issue 1504 - Exception when a script engine is not installed/found<commit_after>/*
 * Zed Attack Proxy (ZAP) and its related class files.
 * 
 * ZAP is an HTTP/HTTPS proxy for assessing web application security.
 * 
 * Copyright 2013 ZAP development team
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0 
 *   
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.  
 */
package org.zaproxy.zap.extension.scripts.dialogs;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import org.parosproxy.paros.Constant;
import org.zaproxy.zap.extension.script.ScriptEngineWrapper;
import org.zaproxy.zap.extension.script.ScriptType;
import org.zaproxy.zap.extension.script.ScriptWrapper;
import org.zaproxy.zap.extension.scripts.ExtensionScriptsUI;
import org.zaproxy.zap.view.StandardFieldsDialog;

public class NewScriptDialog extends StandardFieldsDialog {

	private static final String FIELD_NAME = "scripts.dialog.script.label.name"; 
	private static final String FIELD_ENGINE = "scripts.dialog.script.label.engine"; 
	private static final String FIELD_DESC = "scripts.dialog.script.label.desc";
	private static final String FIELD_TEMPLATE = "scripts.dialog.script.label.template";
	private static final String FIELD_TYPE = "scripts.dialog.script.label.type";
	private static final String FIELD_LOAD = "scripts.dialog.script.label.load";

	private static final long serialVersionUID = 1L;

	private ExtensionScriptsUI extension = null;
	
	public NewScriptDialog(ExtensionScriptsUI ext, Frame owner, Dimension dim, ScriptWrapper template) {
		super(owner, "scripts.dialog.script.new.title", dim);
		this.extension = ext;
		init(template);
	}

	public void init (ScriptType type) {
		this.setFieldValue(FIELD_NAME, "");
		this.setFieldValue(FIELD_DESC, "");
		this.setComboFields(FIELD_TYPE, this.getTypes(), "");
		this.setComboFields(FIELD_ENGINE, this.getEngines(), "");
		this.setComboFields(FIELD_TEMPLATE, this.getTemplates(), "");
		
		if (type == null) {
			this.setFieldValue(FIELD_TYPE, "");
		} else {
			this.setFieldValue(FIELD_TYPE, Constant.messages.getString(type.getI18nKey()));
		}
	}

	public void init (ScriptWrapper template) {
		String name = "";
		String desc = "";
		String type = "";
		String engine = "";
		String templateName = "";

		if (template != null) {
			name = template.getName();
			desc = template.getDescription();
			type = Constant.messages.getString(template.getType().getI18nKey());
			engine = template.getEngineName();
			try {
				// Nasty, but it works ;)
				engine = extension.getExtScript().getEngineNameForExtension(template.getEngine().getExtensions().get(0));
			} catch (Exception e1) {
				// Ignore
			}
			templateName = template.getName();
		}

		this.removeAllFields();
		
		this.setTitle(Constant.messages.getString("scripts.dialog.script.new.title"));
		this.addTextField(FIELD_NAME, name);
		this.addComboField(FIELD_TYPE, this.getTypes(), type);
		this.addComboField(FIELD_ENGINE, this.getEngines(), engine);
		this.addComboField(FIELD_TEMPLATE, this.getTemplates(), templateName);
		this.addMultilineField(FIELD_DESC, desc);
		this.addCheckBoxField(FIELD_LOAD, true);

		this.addFieldListener(FIELD_TYPE, new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (isEmptyField(FIELD_TYPE)) {
					// Unset the template, otherwise it just resets the type again ;)
					if (!isEmptyField(FIELD_ENGINE)) {
						setFieldValue(FIELD_ENGINE, "");
					}
					setFieldValue(FIELD_TEMPLATE, "");
				}
				resetTemplates();
			}});

		this.addFieldListener(FIELD_ENGINE, new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (isEmptyField(FIELD_ENGINE)) {
					// Unset the template, otherwise it just resets the engine again ;)
					setFieldValue(FIELD_TEMPLATE, "");
				}
				resetTemplates();
			}});

		this.addPadding();
	}
	
	private void resetTemplates() {
		List<String> templates = getTemplates();
		if (templates.size() == 0) {
			// None to select :(
			setComboFields(FIELD_TEMPLATE, templates, "");
		} else if (! this.isEmptyField(FIELD_TEMPLATE) && templates.contains(getStringValue(FIELD_TEMPLATE))) {
			// Current selected one is still valid
			setComboFields(FIELD_TEMPLATE, templates, getStringValue(FIELD_TEMPLATE));
		} else if (templates.size() == 1 ) {
			// Only one - select that template
			setComboFields(FIELD_TEMPLATE, templates, templates.get(0));
		} else {
			// Default to none
			setComboFields(FIELD_TEMPLATE, templates, "");
		}
	}
	
	private List<String> getEngines() {
		ArrayList<String> list = new ArrayList<String>();
		list.add("");
		list.addAll(extension.getExtScript().getScriptingEngines());
		return list;
	}

	private List<String> getTypes() {
		ArrayList<String> list = new ArrayList<String>();
		list.add("");
		for (ScriptType type : extension.getExtScript().getScriptTypes()) {
			list.add(Constant.messages.getString(type.getI18nKey()));
		}
		return list;
	}
	
	private ScriptType nameToType (String name) {
		for (ScriptType type : extension.getExtScript().getScriptTypes()) {
			if (Constant.messages.getString(type.getI18nKey()).equals(name)) {
				return type;
			}
		}
		return null;
	}
	
	private ScriptEngineWrapper getSelectedEngine() {
		if (this.isEmptyField(FIELD_ENGINE)) {
			return null;
		}
		return extension.getExtScript().getEngineWrapper(this.getStringValue(FIELD_ENGINE));
	}

	private List<String> getTemplates() {
		ArrayList<String> list = new ArrayList<String>();
		for (ScriptWrapper template : extension.getExtScript().getTreeModel().getTemplates(
				this.nameToType(this.getStringValue(FIELD_TYPE)))) {
			
			if (template.getEngine() != null
					&& (getSelectedEngine() == null || template.getEngine().equals(getSelectedEngine()))) {
				list.add(template.getName());
			}
		}
		if (list.size() > 1) {
			// Only give an empty choice if theres more than one
			list.add("");
		}
		return list;
{'fraction_non_alphanumeric': 0.12406102377449083, 'fraction_numerical': 0.003872066909316193, 'mean_word_length': 4.405609041439933, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3228190', 'n_tokens_mistral': 4366, 'n_tokens_neox': 3886, 'n_words': 906}