]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.commands/src/org/simantics/scl/commands/internal/serialization/CommandSerializerFactory.java
70d2ad6a24f05e93bd8d1595f43421e9badde771
[simantics/platform.git] / bundles / org.simantics.scl.commands / src / org / simantics / scl / commands / internal / serialization / CommandSerializerFactory.java
1 package org.simantics.scl.commands.internal.serialization;
2
3 import org.simantics.db.Resource;
4 import org.simantics.scl.commands.internal.StringConverterFactory;
5 import org.simantics.scl.compiler.errors.CompilationErrorFormatter;
6 import org.simantics.scl.compiler.module.repository.ImportFailureException;
7 import org.simantics.scl.compiler.top.SCLExpressionCompilationException;
8 import org.simantics.scl.compiler.types.Type;
9 import org.simantics.scl.runtime.function.Function;
10 import org.simantics.scl.runtime.function.Function2;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13
14 public class CommandSerializerFactory {
15     private static final Logger LOGGER = LoggerFactory.getLogger(CommandSerializerFactory.class);
16     @SuppressWarnings("unchecked")
17     public static CommandSerializer create(String name, Type[] parameterTypes) {
18         Function2<Resource,Object,String>[] stringConverters = new Function[parameterTypes.length];
19         for(int i=0;i<parameterTypes.length;++i) {
20             try {
21                 stringConverters[i] = StringConverterFactory.stringConverterFor(parameterTypes[i]);
22             } catch(SCLExpressionCompilationException e) {
23                 LOGGER.error("Failed to create a string converter for type " + parameterTypes[i] + ".");
24                 LOGGER.error(CompilationErrorFormatter.toString(e.getErrors()), e);
25                 return new ErrorSerializer(name);
26             } catch (ImportFailureException e) {
27                 LOGGER.error("Failed to create a string converter for type " + parameterTypes[i] + ".");
28                 LOGGER.error("Didn't find Simantics/GShow/gshow.", e);
29                 return new ErrorSerializer(name);
30             }
31         }
32         return new CommandSerializerImpl(name, stringConverters);
33     }
34 }