com.regnosys.rosetta.translate.BackwardCompatibilityGenerator Maven / Gradle / Ivy
package com.regnosys.rosetta.translate;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
/**
* Generator provides backward compatibility for models stuck on earlier model versions.
*/
@SuppressWarnings("all")
public class BackwardCompatibilityGenerator {
public Map generate() {
final HashMap map = CollectionLiterals.newHashMap();
StringConcatenation _builder = new StringConcatenation();
_builder.append("package com.regnosys.rosetta.translate;");
_builder.newLine();
_builder.newLine();
_builder.append("import static com.regnosys.rosetta.translate.GeneratorPathUtil.anyMatch;");
_builder.newLine();
_builder.append("import static com.regnosys.rosetta.translate.GeneratorPathUtil.matches;");
_builder.newLine();
_builder.newLine();
_builder.append("import java.util.Arrays;");
_builder.newLine();
_builder.append("import java.util.Collection;");
_builder.newLine();
_builder.append("import java.util.HashMap;");
_builder.newLine();
_builder.append("import java.util.Iterator;");
_builder.newLine();
_builder.append("import java.util.List;");
_builder.newLine();
_builder.append("import java.util.Map;");
_builder.newLine();
_builder.append("import java.util.Optional;");
_builder.newLine();
_builder.append("import java.util.Set;");
_builder.newLine();
_builder.append("import java.util.function.Consumer;");
_builder.newLine();
_builder.append("import java.util.function.Predicate;");
_builder.newLine();
_builder.append("import java.util.function.Supplier;");
_builder.newLine();
_builder.append("import java.util.stream.Collectors;");
_builder.newLine();
_builder.newLine();
_builder.append("import org.slf4j.Logger;");
_builder.newLine();
_builder.append("import org.slf4j.LoggerFactory;");
_builder.newLine();
_builder.newLine();
_builder.append("import com.google.common.collect.Multimap;");
_builder.newLine();
_builder.append("import com.google.inject.Injector;");
_builder.newLine();
_builder.append("import com.regnosys.rosetta.common.translation.MappingContext;");
_builder.newLine();
_builder.append("import com.regnosys.rosetta.common.translation.MappingProcessor;");
_builder.newLine();
_builder.append("import com.regnosys.rosetta.common.translation.Path;");
_builder.newLine();
_builder.append("import com.regnosys.rosetta.translate.basic.BasicParseHandler;");
_builder.newLine();
_builder.append("import com.rosetta.model.lib.RosettaModelObjectBuilder;");
_builder.newLine();
_builder.append("import com.rosetta.model.lib.path.RosettaPath;");
_builder.newLine();
_builder.newLine();
_builder.append("/**");
_builder.newLine();
_builder.append(" ");
_builder.append("* Generated by the ROMParseHandler");
_builder.newLine();
_builder.append(" ");
_builder.append("* ");
_builder.newLine();
_builder.append(" ");
_builder.append("* RosettaModelObject parse handler.");
_builder.newLine();
_builder.append(" ");
_builder.append("* ");
_builder.newLine();
_builder.append(" ");
_builder.append("* Note: ROM is a misspelt acronym for Rosetta Model Object.");
_builder.newLine();
_builder.append(" ");
_builder.append("*/");
_builder.newLine();
_builder.append("public abstract class ROMParseHandler extends ParseHandler {");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("private static final Logger LOGGER = LoggerFactory.getLogger(ROMParseHandler.class);");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("private final Map> conditionPathCache = new HashMap<>();");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append(" ");
_builder.append("public static ");
_builder.newLine();
_builder.append(" \t\t");
_builder.append("void evaluateConditionalPaths(ROMParseHandler rmoXmlParseHandler,");
_builder.newLine();
_builder.append(" ");
_builder.append("HandlerCache underlyers,");
_builder.newLine();
_builder.append(" ");
_builder.append("Consumer> setter,");
_builder.newLine();
_builder.append(" ");
_builder.append("Collection path) {");
_builder.newLine();
_builder.append(" \t");
_builder.append("rmoXmlParseHandler.evaluateConditionalPaths(underlyers, setter, path);");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("protected final Injector injector;");
_builder.newLine();
_builder.append(" ");
_builder.append("protected final MappingContext mappingContext;");
_builder.newLine();
_builder.append(" ");
_builder.append("protected Multimap, HandlerSupplier> handlers;");
_builder.newLine();
_builder.append(" ");
_builder.append("protected Multimap, MappingProcessorSupplier> mappingProcessors;");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("private T underlying;");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("public ROMParseHandler(Injector injector, MappingContext mappingContext) {");
_builder.newLine();
_builder.append(" ");
_builder.append("this.rosettaPath = new Path();");
_builder.newLine();
_builder.append(" ");
_builder.append("this.injector = injector;");
_builder.newLine();
_builder.append(" ");
_builder.append("this.mappingContext = mappingContext;");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("public T getUnderlying() {");
_builder.newLine();
_builder.append(" ");
_builder.append("return underlying;");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("public void setUnderlying(T underlying) {");
_builder.newLine();
_builder.append(" ");
_builder.append("this.underlying = underlying;");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("@SuppressWarnings(\"unchecked\")");
_builder.newLine();
_builder.append(" ");
_builder.append("protected > H findOrNew(HandlerCache underlyers,");
_builder.newLine();
_builder.append("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ");
_builder.append("String attribute,");
_builder.newLine();
_builder.append("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ");
_builder.append("Path fullPath,");
_builder.newLine();
_builder.append("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ");
_builder.append("Supplier supplier) {");
_builder.newLine();
_builder.append(" ");
_builder.append("return (H) underlyers.getOrCreateHandler(attribute, fullPath, supplier);");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("protected List toStrings(Path path) {");
_builder.newLine();
_builder.append(" ");
_builder.append("return path.getElements().stream()");
_builder.newLine();
_builder.append(" ");
_builder.append(".map(p -> p.getPathName())");
_builder.newLine();
_builder.append(" ");
_builder.append(".collect(Collectors.toList());");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("public Collection> getMappingProcessorForPath(Path localPath,");
_builder.newLine();
_builder.append("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ");
_builder.append("Path parentPath,");
_builder.newLine();
_builder.append("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t ");
_builder.append("MappingContext mappingContext) {");
_builder.newLine();
_builder.append(" ");
_builder.append("return mappingProcessors.get(toStrings(localPath)).stream()");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append(".map(mp -> mp.apply(parentPath, mappingContext))");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append(".collect(Collectors.toList());");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("public Collection> getMappingProcessorMatchPaths() {");
_builder.newLine();
_builder.append(" ");
_builder.append("return mappingProcessors.keySet();");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("public Collection> getHandlerMatchPaths() {");
_builder.newLine();
_builder.append(" ");
_builder.append("return handlers.keySet();");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("public Collection getHandlersForPath(Path localPath, Path parentPath) {");
_builder.newLine();
_builder.append(" ");
_builder.append("return handlers.get(toStrings(localPath)).stream()");
_builder.newLine();
_builder.append(" ");
_builder.append(".map(h -> h.apply(localPath, parentPath))");
_builder.newLine();
_builder.append(" ");
_builder.append(".collect(Collectors.toList());");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("protected H useOrNew(H handlerField, Supplier supplier) {");
_builder.newLine();
_builder.append(" ");
_builder.append("if (handlerField == null) {");
_builder.newLine();
_builder.append(" ");
_builder.append("H handler = supplier.get();");
_builder.newLine();
_builder.append(" ");
_builder.append("return handler;");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.append(" ");
_builder.append("return handlerField;");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("protected void evaluateConditionalPaths(HandlerCache underlyers,");
_builder.newLine();
_builder.append(" ");
_builder.append("Consumer> setter,");
_builder.newLine();
_builder.append(" ");
_builder.append("Collection conditionalPaths) {");
_builder.newLine();
_builder.append(" ");
_builder.append("evaluateConditionalPaths(underlyers, setter, conditionalPaths, a -> true);");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("protected void evaluateConditionalPaths(ROMParseHandler underlyer,");
_builder.newLine();
_builder.append(" ");
_builder.append("Set xmlPathsToHere,");
_builder.newLine();
_builder.append(" ");
_builder.append("Consumer> setter,");
_builder.newLine();
_builder.append(" ");
_builder.append("List conditionalPaths) {");
_builder.newLine();
_builder.append(" ");
_builder.append("evaluateConditionalPaths(underlyer, xmlPathsToHere, setter, conditionalPaths.stream()");
_builder.newLine();
_builder.append(" \t\t");
_builder.append(".map(p -> Path.parse(p))");
_builder.newLine();
_builder.append(" \t\t");
_builder.append(".collect(Collectors.toList()), a -> true);");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("protected void evaluateConditionalPaths(HandlerCache underlyers,");
_builder.newLine();
_builder.append(" ");
_builder.append("Consumer> setter,");
_builder.newLine();
_builder.append(" ");
_builder.append("Collection conditionalPaths,");
_builder.newLine();
_builder.append(" ");
_builder.append("Predicate> additionalTest) {");
_builder.newLine();
_builder.append(" ");
_builder.append("for (ROMParseHandler handler : underlyers.getAllHandlers()) {");
_builder.newLine();
_builder.append(" ");
_builder.append("if (matches(handler.getXmlPathsToHere(), conditionalPaths) && additionalTest.test(handler.getXmlPathsToHere())) {");
_builder.newLine();
_builder.append(" ");
_builder.append("setter.accept(handler);");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("protected void evaluateConditionalPaths(ROMParseHandler handler,");
_builder.newLine();
_builder.append(" ");
_builder.append("Set xmlPathsToHere,");
_builder.newLine();
_builder.append(" ");
_builder.append("Consumer> setter,");
_builder.newLine();
_builder.append(" ");
_builder.append("List conditionalPaths,");
_builder.newLine();
_builder.append(" ");
_builder.append("Predicate> additionalTest) {");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("if (handler != null && xmlPathsToHere.stream().anyMatch(p -> anyMatch(p, conditionalPaths)) && additionalTest.test(handler.getXmlPathsToHere())) {");
_builder.newLine();
_builder.append(" ");
_builder.append("setter.accept(handler);");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("protected void useConditionalCache(BasicParseHandler handler, Path path, Map values) {");
_builder.newLine();
_builder.append(" ");
_builder.append("handler.setParentSetter(v -> values.put(path, v));");
_builder.newLine();
_builder.append(" ");
_builder.append("handler.setParentSupplier(() -> Optional.ofNullable(values.get(path)));");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("protected boolean areEqual(String a, String b) {");
_builder.newLine();
_builder.append(" ");
_builder.append("if (a == null) return b == null;");
_builder.newLine();
_builder.append(" ");
_builder.append("return a.equals(b);");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("protected boolean areEqual(String a, Boolean b) {");
_builder.newLine();
_builder.append(" ");
_builder.append("if (a == null) return b == null;");
_builder.newLine();
_builder.append(" ");
_builder.append("return Boolean.valueOf(a).equals(b);");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("protected boolean areEqual(Boolean b, String a) {");
_builder.newLine();
_builder.append(" ");
_builder.append("return areEqual(a, b);");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("protected boolean areEqual(String a, Integer b) {");
_builder.newLine();
_builder.append(" ");
_builder.append("if (a == null) return b == null;");
_builder.newLine();
_builder.append(" ");
_builder.append("return Integer.valueOf(a).equals(b);");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("protected boolean areEqual(Integer b, String a) {");
_builder.newLine();
_builder.append(" ");
_builder.append("return areEqual(a, b);");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("protected static class AnonHandler extends ROMParseHandler {");
_builder.newLine();
_builder.append(" ");
_builder.append("public AnonHandler(Injector injector, MappingContext mappingContext, Path rosettaPath, T underlying) {");
_builder.newLine();
_builder.append(" ");
_builder.append("super(injector, mappingContext);");
_builder.newLine();
_builder.append(" ");
_builder.append("this.rosettaPath = rosettaPath;");
_builder.newLine();
_builder.append(" ");
_builder.append("setUnderlying(underlying);");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("protected int getMappingListIndex(Collection> listAttribute, List mergeSynonyms) {");
_builder.newLine();
_builder.append(" ");
_builder.append("if (!mergeSynonyms.isEmpty()) {");
_builder.newLine();
_builder.append(" ");
_builder.append("// Merge (e.g. map onto same list item, rather than create new list item), unless excluded path");
_builder.newLine();
_builder.append(" ");
_builder.append("List excludedPathElements = mergeSynonyms.stream()");
_builder.newLine();
_builder.append(" ");
_builder.append(".map(MergeSynonymValue::getExcludePath)");
_builder.newLine();
_builder.append(" ");
_builder.append(".collect(Collectors.toList());");
_builder.newLine();
_builder.append(" ");
_builder.append("boolean excludedPath = getXmlPathsToHere().stream()");
_builder.newLine();
_builder.append(" ");
_builder.append(".flatMap(synPath -> synPath.getElements().stream())");
_builder.newLine();
_builder.append(" ");
_builder.append(".map(Path.PathElement::getPathName)");
_builder.newLine();
_builder.append(" ");
_builder.append(".anyMatch(pathElementName -> excludedPathElements.contains(pathElementName));");
_builder.newLine();
_builder.append(" ");
_builder.append("// If excluded, use index of the list size (e.g. map on to a new list item at the end of the list)");
_builder.newLine();
_builder.append(" ");
_builder.append("// If included, use index of the list size minus one (e.g. map on to the last existing item of the list)");
_builder.newLine();
_builder.append(" ");
_builder.append("return Math.max(0, sizeOf(listAttribute) - (excludedPath ? 0 : 1));");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.append(" ");
_builder.append("// if not merge synonym is specified then always create a new list item");
_builder.newLine();
_builder.append(" ");
_builder.append("return sizeOf(listAttribute);");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("/**");
_builder.newLine();
_builder.append(" ");
_builder.append("* I\'m not sure if getting the last path is the correct thing to do, but there should be only one path for this.");
_builder.newLine();
_builder.append(" ");
_builder.append("*");
_builder.newLine();
_builder.append(" ");
_builder.append("* @param synonymPaths - the synonym paths to this attribute (not including what is inside [synonym value=\"...\"]");
_builder.newLine();
_builder.append(" ");
_builder.append("* @param synonymValue - the synonym path specified inside the attribute synonym [synonym value=\"...\"] - dot separated string representing path");
_builder.newLine();
_builder.append(" ");
_builder.append("* @return the last synonym path and the synonym value combined");
_builder.newLine();
_builder.append(" ");
_builder.append("*/");
_builder.newLine();
_builder.append(" ");
_builder.append("protected String getSynonymPathForConditionFunc(Set synonymPaths, String synonymValue) {");
_builder.newLine();
_builder.append(" ");
_builder.append("Iterator iterator = synonymPaths.iterator();");
_builder.newLine();
_builder.append(" ");
_builder.append("Path lastPath = null;");
_builder.newLine();
_builder.append(" ");
_builder.append("while (iterator.hasNext()) {");
_builder.newLine();
_builder.append(" ");
_builder.append("lastPath = iterator.next();");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.append(" ");
_builder.append("if (lastPath != null) {");
_builder.newLine();
_builder.append(" ");
_builder.append("Path fullPath = lastPath.append(Path.parse(synonymValue));");
_builder.newLine();
_builder.append(" ");
_builder.append("return formatPathWithNoIndexesAndSeparators(fullPath);");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.append(" ");
_builder.append("return \"\"; // top level");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("protected String formatPathWithNoIndexesAndSeparators(Path path) {");
_builder.newLine();
_builder.append(" ");
_builder.append("return path.getElements().stream()");
_builder.newLine();
_builder.append(" ");
_builder.append(".map(e -> e.getPathName())");
_builder.newLine();
_builder.append(" ");
_builder.append(".collect(Collectors.joining(\"->\"));");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("// for backwards compatibility");
_builder.newLine();
_builder.append(" ");
_builder.append("protected String getValueFromConditionPath(String... conditionPathEndsWith) {");
_builder.newLine();
_builder.append(" \t");
_builder.append("return Optional.ofNullable(getValuesFromConditionPath(conditionPathEndsWith))");
_builder.newLine();
_builder.append(" \t\t\t");
_builder.append(".flatMap(values -> values.stream().findFirst())");
_builder.newLine();
_builder.append(" \t\t\t");
_builder.append(".orElse(null);");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.append(" ");
_builder.newLine();
_builder.append(" ");
_builder.append("protected List getValuesFromConditionPath(String... conditionPathEndsWith) {");
_builder.newLine();
_builder.append(" \t");
_builder.append("if (conditionPathEndsWith == null || conditionPathEndsWith.length == 0) {");
_builder.newLine();
_builder.append(" \t\t");
_builder.append("return null;");
_builder.newLine();
_builder.append(" \t");
_builder.append("}");
_builder.newLine();
_builder.append(" \t");
_builder.append("String conditionPathEndsWithAsStr = Arrays.toString(conditionPathEndsWith);");
_builder.newLine();
_builder.append(" \t");
_builder.append("return conditionPathCache.computeIfAbsent(Arrays.toString(conditionPathEndsWith), key -> {");
_builder.newLine();
_builder.append(" \t\t");
_builder.append("List values = mappingContext.getMappings().stream()");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append(".filter(m -> m.getXmlValue() != null)");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append(".filter(m -> m.getXmlPath().endsWith(conditionPathEndsWith))");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append(".map(m -> m.getXmlValue())");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append(".map(o -> String.valueOf(o))");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append(".distinct()");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append(".collect(Collectors.toList());");
_builder.newLine();
_builder.append(" \t\t");
_builder.append("LOGGER.debug(\"Condition-func path {} found values {}\", conditionPathEndsWithAsStr, values);");
_builder.newLine();
_builder.append(" \t\t");
_builder.append("return values;");
_builder.newLine();
_builder.append(" \t");
_builder.append("});");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.append(" ");
_builder.newLine();
_builder.append("\t");
_builder.append("public Injector getInjector() {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("return injector;");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("public MappingContext getMappingContext() {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("return mappingContext;");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.newLine();
map.put("com/regnosys/rosetta/translate/ROMParseHandler", _builder.toString());
return map;
}
}