| /** |
| * Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation |
| * |
| * |
| * This copyright notice shows up in the generated Java code |
| */ |
| package org.eclipse.osbp.xtext.basic.generator; |
| |
| import java.io.File; |
| import java.io.FileInputStream; |
| import java.io.FileOutputStream; |
| import java.io.IOException; |
| import java.util.Collections; |
| import java.util.Enumeration; |
| import java.util.List; |
| import java.util.Properties; |
| import java.util.Set; |
| import java.util.TreeSet; |
| import org.apache.commons.lang.ClassUtils; |
| import org.eclipse.emf.common.util.EList; |
| import org.eclipse.osbp.utils.constants.GeneratorConstants; |
| import org.eclipse.xpand2.XpandExecutionContext; |
| import org.eclipse.xpand2.output.Outlet; |
| import org.eclipse.xpand2.output.Output; |
| import org.eclipse.xtend2.lib.StringConcatenation; |
| import org.eclipse.xtext.AbstractRule; |
| import org.eclipse.xtext.Grammar; |
| import org.eclipse.xtext.GrammarUtil; |
| import org.eclipse.xtext.Keyword; |
| import org.eclipse.xtext.generator.Generator; |
| import org.eclipse.xtext.generator.grammarAccess.GrammarAccessFragment; |
| import org.eclipse.xtext.generator.grammarAccess.ResourceSaveIndicator; |
| import org.eclipse.xtext.xbase.lib.Exceptions; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| /** |
| * <b>For the complete to-do-list support keyword information DSL editors <code>org.eclipse.osbp.xtext.basic.ui.BasicDSLUiModuleHelper</code></b> |
| * <hr> |
| * <ul> |
| * <li>Generate {your}DSLGrammarI18nGenerator: |
| * <pre> |
| * public class {your}DSLGrammarI18nGenerator extends BasicDSLGrammarI18nGenerator { |
| * } |
| * </pre> |
| * </li> |
| * <li>Append Grammar I18n generator at the end of the MWE2 workflow: |
| * <pre> |
| * // parse grammar and generate i18n grammar |
| * fragment = org.eclipse.osbp.xtext.{your}.generator.{your}DSLGrammarI18nGenerator auto-inject {} |
| * </pre> |
| * </li> |
| * </ul> |
| */ |
| @SuppressWarnings("all") |
| public class BasicDslGrammarI18nGenerator extends GrammarAccessFragment { |
| private final Logger LOGGER = LoggerFactory.getLogger(this.getClass()); |
| |
| /** |
| * generate a i18n property if it doesn't exist yet |
| * @param i18nProperties the properties to generate into |
| * @param i18nKey the key to generate |
| * @return 1 if generated, 0 otherwise |
| */ |
| protected int generateI18nProperty(final Properties i18nProperties, final String i18nKey) { |
| int modified = 0; |
| boolean _containsKey = i18nProperties.containsKey(i18nKey); |
| boolean _not = (!_containsKey); |
| if (_not) { |
| i18nProperties.put(i18nKey, ""); |
| modified = 1; |
| StringConcatenation _builder = new StringConcatenation(); |
| _builder.append(i18nKey, ""); |
| _builder.append(" created: "); |
| Object _get = i18nProperties.get(i18nKey); |
| _builder.append(_get, ""); |
| this.LOGGER.info(_builder.toString()); |
| } else { |
| StringConcatenation _builder_1 = new StringConcatenation(); |
| _builder_1.append(i18nKey, ""); |
| _builder_1.append(" already exists: "); |
| Object _get_1 = i18nProperties.get(i18nKey); |
| _builder_1.append(_get_1, ""); |
| this.LOGGER.debug(_builder_1.toString()); |
| } |
| return modified; |
| } |
| |
| /** |
| * traverse the given grammar for rules and keywords and generate non-existing i18n properies |
| */ |
| public void generate(final Grammar grammar, final XpandExecutionContext ctx) { |
| String _name = grammar.getName(); |
| final String grammarPackage = ClassUtils.getPackageCanonicalName(_name); |
| StringConcatenation _builder = new StringConcatenation(); |
| _builder.append("start extraction of rules and keywords from "); |
| _builder.append(grammarPackage, ""); |
| _builder.append(" for generation of i18n descriptions"); |
| this.LOGGER.debug(_builder.toString()); |
| Output _output = ctx.getOutput(); |
| Outlet _outlet = _output.getOutlet(Generator.PLUGIN_UI); |
| String _path = _outlet.getPath(); |
| String _plus = (_path + "/"); |
| final String i18nFilePath = (_plus + GeneratorConstants.I18N_RESOURCE_PATH_NAME); |
| final String i18nFullPath = ((i18nFilePath + "/") + GeneratorConstants.I18N_RESOURCE_FULL_FILE_NAME); |
| int modifications = 0; |
| ResourceSaveIndicator _resourceSaveIndicator = new ResourceSaveIndicator(); |
| final ResourceSaveIndicator isSaving = ((ResourceSaveIndicator) _resourceSaveIndicator); |
| Properties i18nProperties = new Properties() { |
| public synchronized Enumeration<Object> keys() { |
| Set<Object> _keySet = super.keySet(); |
| TreeSet<Object> _treeSet = new TreeSet<Object>(_keySet); |
| return Collections.<Object>enumeration(_treeSet); |
| } |
| }; |
| try { |
| FileInputStream _fileInputStream = new FileInputStream(i18nFullPath); |
| i18nProperties.load(_fileInputStream); |
| } catch (final Throwable _t) { |
| if (_t instanceof Exception) { |
| final Exception e = (Exception)_t; |
| } else { |
| throw Exceptions.sneakyThrow(_t); |
| } |
| } |
| if (isSaving!=null) { |
| isSaving.set(Boolean.TRUE); |
| } |
| try { |
| EList<AbstractRule> _rules = grammar.getRules(); |
| for (final AbstractRule rule : _rules) { |
| { |
| StringConcatenation _builder_1 = new StringConcatenation(); |
| _builder_1.append(grammarPackage, ""); |
| _builder_1.append("."); |
| String _name_1 = rule.getName(); |
| _builder_1.append(_name_1, ""); |
| final String i18nKey = _builder_1.toString(); |
| int _modifications = modifications; |
| int _generateI18nProperty = this.generateI18nProperty(i18nProperties, i18nKey); |
| modifications = (_modifications + _generateI18nProperty); |
| List<Keyword> _containedKeywords = GrammarUtil.containedKeywords(rule); |
| for (final Keyword keyword : _containedKeywords) { |
| { |
| StringConcatenation _builder_2 = new StringConcatenation(); |
| _builder_2.append(i18nKey, ""); |
| _builder_2.append("."); |
| String _value = keyword.getValue(); |
| _builder_2.append(_value, ""); |
| final String i18nEnumKey = _builder_2.toString(); |
| String _value_1 = keyword.getValue(); |
| int _length = _value_1.length(); |
| boolean _greaterThan = (_length > 1); |
| if (_greaterThan) { |
| int _modifications_1 = modifications; |
| int _generateI18nProperty_1 = this.generateI18nProperty(i18nProperties, i18nEnumKey); |
| modifications = (_modifications_1 + _generateI18nProperty_1); |
| } else { |
| StringConcatenation _builder_3 = new StringConcatenation(); |
| _builder_3.append(i18nKey, ""); |
| _builder_3.append(" ignored due to single character keyword length»"); |
| this.LOGGER.info(_builder_3.toString()); |
| } |
| } |
| } |
| } |
| } |
| } catch (final Throwable _t_1) { |
| if (_t_1 instanceof Exception) { |
| final Exception e_1 = (Exception)_t_1; |
| String _message = e_1.getMessage(); |
| this.LOGGER.error(_message, e_1); |
| } else { |
| throw Exceptions.sneakyThrow(_t_1); |
| } |
| } |
| try { |
| if ((modifications > 0)) { |
| final File i18nDirectory = new File(i18nFilePath); |
| boolean _exists = i18nDirectory.exists(); |
| boolean _not = (!_exists); |
| if (_not) { |
| i18nDirectory.mkdirs(); |
| } |
| FileOutputStream _fileOutputStream = new FileOutputStream(i18nFullPath); |
| i18nProperties.store(_fileOutputStream, ""); |
| } |
| } catch (final Throwable _t_2) { |
| if (_t_2 instanceof IOException) { |
| final IOException e_2 = (IOException)_t_2; |
| String _message_1 = e_2.getMessage(); |
| this.LOGGER.error(_message_1, e_2); |
| } else { |
| throw Exceptions.sneakyThrow(_t_2); |
| } |
| } finally { |
| if (isSaving!=null) { |
| isSaving.set(Boolean.FALSE); |
| } |
| } |
| StringConcatenation _builder_1 = new StringConcatenation(); |
| _builder_1.append("ended extraction of rules and keywords from "); |
| _builder_1.append(grammarPackage, ""); |
| _builder_1.append(" for generation of i18n descriptions: "); |
| _builder_1.append(modifications, ""); |
| _builder_1.append(" modification(s) found"); |
| this.LOGGER.debug(_builder_1.toString()); |
| } |
| } |