All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.reprezen.genflow.rapidml.xsd.DataModelSchemaGenerator Maven / Gradle / Ivy

/**
 * Copyright © 2013, 2016 Modelsolv, Inc.
 * All Rights Reserved.
 * 
 * NOTICE: All information contained herein is, and remains the property
 * of ModelSolv, Inc. See the file license.html in the root directory of
 * this project for further information.
 */
package com.reprezen.genflow.rapidml.xsd;

import com.reprezen.genflow.api.source.ISource;
import com.reprezen.genflow.api.template.IGenTemplateContext;
import com.reprezen.genflow.api.zenmodel.ZenModelExtractOutputItem;
import com.reprezen.genflow.rapidml.xsd.Helpers;
import com.reprezen.genflow.rapidml.xsd.XMLSchemaHelper;
import com.reprezen.rapidml.Constraint;
import com.reprezen.rapidml.DataModel;
import com.reprezen.rapidml.DataType;
import com.reprezen.rapidml.EnumConstant;
import com.reprezen.rapidml.Enumeration;
import com.reprezen.rapidml.PrimitiveType;
import com.reprezen.rapidml.SingleValueType;
import com.reprezen.rapidml.UserDefinedType;
import com.reprezen.rapidml.ZenModel;
import java.io.File;
import java.util.Arrays;
import org.eclipse.emf.common.util.EList;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.Extension;

@SuppressWarnings("all")
public class DataModelSchemaGenerator extends ZenModelExtractOutputItem {
  @Extension
  private XMLSchemaHelper xmlSchemaHelper;
  
  @Override
  public void init(final IGenTemplateContext context) {
    try {
      ISource _primarySource = context.getPrimarySource();
      Object _load = _primarySource.load();
      final ZenModel model = ((ZenModel) _load);
      final Helpers helpers = new Helpers(context, model);
      XMLSchemaHelper _xmlSchemaHelper = helpers.getXmlSchemaHelper();
      this.xmlSchemaHelper = _xmlSchemaHelper;
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
  
  @Override
  public File getOutputFile(final ZenModel zenModel, final DataModel dataModel) {
    String _xsdFileName = this.xmlSchemaHelper.xsdFileName(dataModel, zenModel);
    return new File(_xsdFileName);
  }
  
  @Override
  public String generate(final ZenModel zenModel, final DataModel dataModel) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("");
    _builder.newLine();
    String _generateXSDDoc = this.xmlSchemaHelper.generateXSDDoc(dataModel);
    _builder.append(_generateXSDDoc, "");
    _builder.newLineIfNotEmpty();
    {
      EList _ownedDataTypes = dataModel.getOwnedDataTypes();
      boolean _hasElements = false;
      for(final DataType dataType : _ownedDataTypes) {
        if (!_hasElements) {
          _hasElements = true;
        } else {
          _builder.appendImmediate("", "");
        }
        {
          boolean _or = false;
          if ((dataType instanceof Enumeration)) {
            _or = true;
          } else {
            _or = (dataType instanceof UserDefinedType);
          }
          if (_or) {
            CharSequence _generate = this.generate(((SingleValueType) dataType));
            _builder.append(_generate, "");
            _builder.newLineIfNotEmpty();
          }
        }
      }
    }
    _builder.append("");
    _builder.newLine();
    return _builder.toString();
  }
  
  private CharSequence generateNamespaceAdditions(final DataModel dataModel) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("xmlns:jaxb=\"http://java.sun.com/xml/ns/jaxb\"");
    _builder.newLine();
    _builder.append("jaxb:version=\"2.0\"");
    _builder.newLine();
    return _builder;
  }
  
  private CharSequence _generate(final Enumeration en) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("");
    _builder.newLineIfNotEmpty();
    CharSequence _generateEnumAnnotation = this.generateEnumAnnotation(en);
    _builder.append(_generateEnumAnnotation, "");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    _builder.append("");
    _builder.newLineIfNotEmpty();
    {
      EList _enumConstants = en.getEnumConstants();
      boolean _hasElements = false;
      for(final EnumConstant enumConstant : _enumConstants) {
        if (!_hasElements) {
          _hasElements = true;
        } else {
          _builder.appendImmediate("", "\t\t");
        }
        _builder.append("\t\t");
        CharSequence _generateEnumConstant = this.generateEnumConstant(enumConstant);
        _builder.append(_generateEnumConstant, "\t\t");
        _builder.newLineIfNotEmpty();
      }
    }
    _builder.append("\t");
    _builder.append("");
    _builder.newLine();
    _builder.append("");
    _builder.newLine();
    return _builder;
  }
  
  private CharSequence _generate(final UserDefinedType userDefinedType) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    String _baseTypeName = this.xmlSchemaHelper.getBaseTypeName(userDefinedType);
    EList _allConstraints = userDefinedType.getAllConstraints();
    CharSequence _generateRestriction = this.xmlSchemaHelper.generateRestriction(_baseTypeName, _allConstraints);
    _builder.append(_generateRestriction, "\t");
    _builder.newLineIfNotEmpty();
    _builder.append("");
    _builder.newLine();
    return _builder;
  }
  
  private CharSequence generateEnumAnnotation(final Enumeration en) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("");
    _builder.newLine();
    _builder.append("");
    _builder.newLine();
    return _builder;
  }
  
  private CharSequence generateEnumConstant(final EnumConstant enumConstant) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    CharSequence _generateEnumConstantAnnotation = this.generateEnumConstantAnnotation(enumConstant);
    _builder.append(_generateEnumConstantAnnotation, "\t");
    _builder.newLineIfNotEmpty();
    _builder.append("");
    _builder.newLine();
    return _builder;
  }
  
  private CharSequence generateEnumConstantAnnotation(final EnumConstant enumConstant) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    _builder.append("");
    _builder.newLine();
    _builder.append("");
    _builder.newLine();
    return _builder;
  }
  
  private CharSequence generate(final SingleValueType userDefinedType) {
    if (userDefinedType instanceof UserDefinedType) {
      return _generate((UserDefinedType)userDefinedType);
    } else if (userDefinedType instanceof Enumeration) {
      return _generate((Enumeration)userDefinedType);
    } else {
      throw new IllegalArgumentException("Unhandled parameter types: " +
        Arrays.asList(userDefinedType).toString());
    }
  }
}