Search in sources :

Example 1 with SVGGeneratorRequest

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);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ViewFactoryImpl(org.kie.workbench.common.stunner.svg.gen.model.impl.ViewFactoryImpl) SVGViewFactoryGenerator(org.kie.workbench.common.stunner.svg.gen.codegen.impl.SVGViewFactoryGenerator) StyleSheetDefinition(org.kie.workbench.common.stunner.svg.gen.model.StyleSheetDefinition) SVGGeneratorFormatUtils(org.kie.workbench.common.stunner.svg.gen.codegen.impl.SVGGeneratorFormatUtils) ViewDefinition(org.kie.workbench.common.stunner.svg.gen.model.ViewDefinition) ViewDefinitionImpl(org.kie.workbench.common.stunner.svg.gen.model.impl.ViewDefinitionImpl) SVGTranslatorContext(org.kie.workbench.common.stunner.svg.gen.translator.SVGTranslatorContext) Document(org.w3c.dom.Document) Map(java.util.Map) LinkedList(java.util.LinkedList) SVGGeneratorRequest(org.kie.workbench.common.stunner.svg.gen.SVGGeneratorRequest) Path(java.nio.file.Path) SVGDocumentTranslator(org.kie.workbench.common.stunner.svg.gen.translator.SVGDocumentTranslator) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) Consumer(java.util.function.Consumer) SVGGenerator(org.kie.workbench.common.stunner.svg.gen.SVGGenerator) List(java.util.List) Paths(java.nio.file.Paths) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SVGStyleTranslator(org.kie.workbench.common.stunner.svg.gen.translator.css.SVGStyleTranslator) GeneratorException(org.kie.workbench.common.stunner.svg.gen.exception.GeneratorException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) InputStream(java.io.InputStream) InputStream(java.io.InputStream) ViewDefinition(org.kie.workbench.common.stunner.svg.gen.model.ViewDefinition) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) GeneratorException(org.kie.workbench.common.stunner.svg.gen.exception.GeneratorException) LinkedList(java.util.LinkedList) ViewFactoryImpl(org.kie.workbench.common.stunner.svg.gen.model.impl.ViewFactoryImpl) StyleSheetDefinition(org.kie.workbench.common.stunner.svg.gen.model.StyleSheetDefinition)

Example 2 with SVGGeneratorRequest

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));
}
Also used : SVGGeneratorRequest(org.kie.workbench.common.stunner.svg.gen.SVGGeneratorRequest) Messager(javax.annotation.processing.Messager) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ViewFactory(org.kie.workbench.common.stunner.svg.gen.model.ViewFactory) Test(org.junit.Test)

Example 3 with SVGGeneratorRequest

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;
}
Also used : SVGGeneratorRequest(org.kie.workbench.common.stunner.svg.gen.SVGGeneratorRequest) SVGViewFactory(org.kie.workbench.common.stunner.svg.annotation.SVGViewFactory) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) PackageElement(javax.lang.model.element.PackageElement) SVGSource(org.kie.workbench.common.stunner.svg.annotation.SVGSource)

Example 4 with SVGGeneratorRequest

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;
}
Also used : Messager(javax.annotation.processing.Messager) SVGGeneratorRequest(org.kie.workbench.common.stunner.svg.gen.SVGGeneratorRequest)

Aggregations

SVGGeneratorRequest (org.kie.workbench.common.stunner.svg.gen.SVGGeneratorRequest)4 Messager (javax.annotation.processing.Messager)2 InputStream (java.io.InputStream)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 LinkedHashSet (java.util.LinkedHashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Consumer (java.util.function.Consumer)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 PackageElement (javax.lang.model.element.PackageElement)1 TypeElement (javax.lang.model.element.TypeElement)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Test (org.junit.Test)1 SVGSource (org.kie.workbench.common.stunner.svg.annotation.SVGSource)1 SVGViewFactory (org.kie.workbench.common.stunner.svg.annotation.SVGViewFactory)1