| package org.eclipse.osbp.xtext.datamartdsl.jvmmodel; |
| |
| import java.io.File; |
| import java.io.FileWriter; |
| import java.io.IOException; |
| import java.nio.file.Files; |
| import java.nio.file.Paths; |
| import java.util.Set; |
| |
| import org.eclipse.osbp.xtext.datamartdsl.DatamartDSLStandaloneSetup; |
| import org.eclipse.osbp.xtext.datamartdsl.services.DatamartDSLGrammarAccess; |
| import org.eclipse.xtext.GrammarUtil; |
| |
| public abstract class DatamartDSLKeywordCreator { |
| |
| private static final String DIRECTORY = "src/org/eclipse/osbp/xtext/datamartdsl/jvmmodel"; |
| private static final String FILENAME = "DatamartDSLKeyword.java"; |
| |
| public static void createOrUpdateKeywordList() { |
| String head = |
| "package org.eclipse.osbp.xtext.datamartdsl.jvmmodel;\n\nimport java.util.HashMap;\n\n" |
| +"public enum DatamartDSLKeyword {\n\n"; |
| |
| String tail = |
| ";\n\nprivate static HashMap<String, DatamartDSLKeyword> keywords = new HashMap<>();\n\n" |
| + "static {\n" |
| + "for(DatamartDSLKeyword keyword : values()){\n" |
| +"keywords.put(keyword.getName(), keyword);\n}\n}\n\n" |
| + "private String name;\n\n" |
| + "private DatamartDSLKeyword(String name){\n\t" |
| + "this.name = name;\n}\n\n" |
| + "public String getName() {\n\t" |
| + "return name;\n}" |
| +"\n\npublic static boolean isKeyword(String keyword){\n" |
| +"if(keyword != null && !keyword.isEmpty()){\n" |
| +" return keywords.containsKey(keyword);\n" |
| +"}\n" |
| +"return false;\n}\n\n}"; |
| |
| File file = Paths.get(DIRECTORY).resolve(FILENAME).toFile(); |
| try (FileWriter out = new FileWriter(file)) { |
| Set<String> datamartDSLkeywords = GrammarUtil.getAllKeywords(DatamartDSLStandaloneSetup.doSetup().getInstance(DatamartDSLGrammarAccess.class).getGrammar()); |
| if( !file.exists() ){ |
| file = Files.createFile(Paths.get(DIRECTORY).resolve(FILENAME)).toFile(); |
| } |
| if ( file.exists() && datamartDSLkeywords != null ) { |
| String result = ""; |
| int count = 1; |
| for(String value: datamartDSLkeywords){ |
| result += "KEY"+ count++ +"(\""+value+"\"), "; |
| } |
| out.write(head.concat(result.substring(0, result.length()-2).concat(tail))); |
| } |
| } catch (IOException e) { |
| System.err.println(e); |
| } |
| } |
| |
| public static void main(String[] args) { |
| createOrUpdateKeywordList(); |
| } |
| } |