| package org.eclipse.ease.lang.javascript; |
| |
| import java.util.Random; |
| import java.util.regex.Pattern; |
| |
| import org.eclipse.ease.service.IScriptService; |
| import org.eclipse.ease.service.ScriptService; |
| import org.eclipse.ease.service.ScriptType; |
| |
| public final class JavaScriptHelper { |
| |
| /** Script type identifier for JavaScript. Must match with the script type 'name' from plugin.xml. */ |
| public static final String SCRIPT_TYPE_JAVASCRIPT = "JavaScript"; |
| |
| public static String getSaveName(final String identifier) { |
| // check if name is already valid |
| if (isSaveName(identifier)) |
| return identifier; |
| |
| // not valid, convert string to valid format |
| final StringBuilder buffer = new StringBuilder(identifier.replaceAll("[^a-zA-Z0-9_$]", "_")); |
| |
| // check for valid first character |
| if (buffer.length() > 0) { |
| final char start = buffer.charAt(0); |
| if (((start < 65) || ((start > 90) && (start < 97)) || (start > 122)) && (start != '_')) |
| buffer.insert(0, '_'); |
| } else { |
| // buffer is empty, create a random string of lowercase letters |
| buffer.append('_'); |
| for (int index = 0; index < new Random().nextInt(20); index++) |
| buffer.append('a' + new Random().nextInt(26)); |
| } |
| |
| return buffer.toString(); |
| } |
| |
| public static boolean isSaveName(final String identifier) { |
| return Pattern.matches("[a-zA-Z_$][a-zA-Z0-9_$]*", identifier); |
| } |
| |
| /** |
| * Get the {@link ScriptType} for java script. |
| * |
| * @return script type definition |
| */ |
| public static ScriptType getScriptType() { |
| final IScriptService scriptService = ScriptService.getInstance(); |
| return scriptService.getAvailableScriptTypes().get(SCRIPT_TYPE_JAVASCRIPT); |
| } |
| } |