use of org.kie.workbench.common.stunner.svg.gen.SVGGeneratorRequest in project kie-wb-common by kiegroup.
the class SVGGeneratorImpl method generate.
@Override
public StringBuffer generate(final SVGGeneratorRequest request) throws GeneratorException {
final String name = request.getName();
final String pkg = request.getPkg();
final String typeOf = request.getImplementedType();
final String cssPath = request.getCssPath();
final Map<String, String> viewSources = request.getViewSources();
final ViewFactoryImpl viewFactory = new ViewFactoryImpl(name, pkg, typeOf);
// Process the global CSS declaration specified in the factory, if any.
final StyleSheetDefinition[] styleSheetDefinition = new StyleSheetDefinition[1];
if (null != cssPath && cssPath.trim().length() > 0) {
final InputStream cssStream = loadResource(cssPath);
if (null != cssStream) {
try {
styleSheetDefinition[0] = SVGStyleTranslator.parseStyleSheetDefinition(cssPath, cssStream);
viewFactory.setStyleSheetDefinition(styleSheetDefinition[0]);
} catch (Exception e) {
throw new RuntimeException("Error while processing the glocal CSS file [" + cssPath + "] ", e);
}
}
}
// Process all SVG files specified in the factory.
// TODO: Hmmm
final Set<String> processedSvgIds = new LinkedHashSet<>();
viewSources.forEach((fMethodName, svgPath) -> {
parseSVGViewSource(fMethodName, svgPath, styleSheetDefinition[0], result -> {
result.setId(fMethodName);
result.setFactoryMethodName(fMethodName);
viewFactory.getViewDefinitions().add(result);
});
processedSvgIds.add(fMethodName);
});
// Parse referenced svg files as well, if any.
final List<ViewDefinition<?>> referencedViewDefinitions = new LinkedList<>();
viewFactory.getViewDefinitions().stream().flatMap(v -> v.getSVGViewRefs().stream()).filter(vd -> !processedSvgIds.contains(vd.getViewRefId())).forEach(vd -> parseSVGViewSource(vd.getViewRefId(), vd.getFilePath(), styleSheetDefinition[0], result -> {
final String id = SVGGeneratorFormatUtils.getValidInstanceId(result);
result.setFactoryMethodName(id);
referencedViewDefinitions.add(result);
processedSvgIds.add(id);
}));
viewFactory.getViewDefinitions().addAll(referencedViewDefinitions);
return viewFactoryGenerator.generate(viewFactory);
}
use of org.kie.workbench.common.stunner.svg.gen.SVGGeneratorRequest in project kie-wb-common by kiegroup.
the class SVGGeneratorImplTest method testGenerate.
@Test
public void testGenerate() throws Exception {
when(viewDefinition.getId()).thenReturn("svg-cancel");
doAnswer(new Answer<StringBuffer>() {
@Override
public StringBuffer answer(final InvocationOnMock invocationOnMock) throws Throwable {
final ViewFactory factoryArgument = (ViewFactory) invocationOnMock.getArguments()[0];
assertEquals(SVG_NAME, factoryArgument.getSimpleName());
assertEquals(SVG_PKG, factoryArgument.getPackage());
assertEquals(SVG_FQCN, factoryArgument.getImplementedType());
assertTrue(factoryArgument.getViewDefinitions().size() == 1);
assertEquals(viewDefinition, factoryArgument.getViewDefinitions().get(0));
return new StringBuffer("done");
}
}).when(viewFactoryGenerator).generate(any(ViewFactory.class));
final SVGGeneratorRequest request = new SVGGeneratorRequest(SVG_NAME, SVG_PKG, SVG_FQCN, "", mock(Messager.class));
request.getViewSources().put(SVG_CANCEL_NAME, SVG_CANCEL_PATH);
tested.generate(request);
verify(viewDefinition, times(1)).setFactoryMethodName(eq(SVG_CANCEL_NAME));
verify(viewFactoryGenerator, times(1)).generate(any(ViewFactory.class));
}
use of org.kie.workbench.common.stunner.svg.gen.SVGGeneratorRequest in project kie-wb-common by kiegroup.
the class SVGShapeProcessor method processSvgShapeViewFactory.
private boolean processSvgShapeViewFactory(final Set<? extends TypeElement> set, final Element e, final RoundEnvironment roundEnv) throws Exception {
final boolean isIface = e.getKind() == ElementKind.INTERFACE;
if (isIface) {
final TypeElement classElement = (TypeElement) e;
final PackageElement packageElement = (PackageElement) classElement.getEnclosingElement();
final String name = classElement.getSimpleName().toString();
// Obtain type element information and create a new generation request into the processor's context.
String packageName = packageElement.getQualifiedName().toString();
String fqcn = packageName + "." + name;
String absPkgPath = packageName.replaceAll("\\.", "/");
note("Discovered @SVGViewFactory for type [" + fqcn + "]");
SVGViewFactory svgViewFactoryAnn = classElement.getAnnotation(SVGViewFactory.class);
final SVGGeneratorRequest request = new SVGGeneratorRequest(name + GENERATED_TYPE_SUFFIX, packageName, fqcn, absPkgPath + "/" + svgViewFactoryAnn.value(), processingEnv.getMessager());
context.setGeneratorRequest(request);
// Find and process method annotation as @SVGSource.
List<ExecutableElement> methodElements = ElementFilter.methodsIn(classElement.getEnclosedElements());
methodElements.forEach(methodElement -> {
SVGSource svgSourceAnnotation = methodElement.getAnnotation(SVGSource.class);
if (null != svgSourceAnnotation) {
String fileName = svgSourceAnnotation.value();
String absPath = absPkgPath + "/" + fileName;
final String fieldName = methodElement.getSimpleName().toString();
note("Discovered @SVGSource to be processed at path [" + absPath + "]");
context.getGeneratorRequest().getViewSources().put(fieldName, absPath);
}
});
}
return true;
}
use of org.kie.workbench.common.stunner.svg.gen.SVGGeneratorRequest in project kie-wb-common by kiegroup.
the class SVGShapeProcessor method processLastRound.
private boolean processLastRound(final Set<? extends TypeElement> set, final RoundEnvironment roundEnv) throws Exception {
final Messager messager = processingEnv.getMessager();
try {
final SVGGeneratorRequest request = context.getGeneratorRequest();
final String classFQName = request.getPkg() + "." + request.getName();
messager.printMessage(Diagnostic.Kind.NOTE, "Starting generation for SVGShapeViewFactory named [" + classFQName + "]");
final StringBuffer result = generator.generate(request);
writeCode(request.getPkg(), request.getName(), result);
} catch (org.kie.workbench.common.stunner.svg.gen.exception.GeneratorException ge) {
final String msg = ge.getMessage();
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, msg);
}
return true;
}
Aggregations