Search in sources :

Example 1 with ViewFactoryImpl

use of org.kie.workbench.common.stunner.svg.gen.model.impl.ViewFactoryImpl 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 ViewFactoryImpl

use of org.kie.workbench.common.stunner.svg.gen.model.impl.ViewFactoryImpl in project kie-wb-common by kiegroup.

the class SVGViewDefinitionGeneratorTest method testGenerate.

@Test
public void testGenerate() throws Exception {
    final ViewFactory viewFactory = new ViewFactoryImpl("view1", "org.kie.test", "MyType.¢lass");
    final RectDefinition mainDef = new RectDefinition("rect1", 1, 2, 0);
    final CircleDefinition circleDefinition = new CircleDefinition("circle1", 25);
    final ViewDefinitionImpl viewDefinition = new ViewDefinitionImpl(ID, X, Y, WIDTH, HEIGHT, styleSheetDefinition, new ViewBoxDefinitionImpl(VBOX_MIN_X, VBOX_MIN_Y, VBOX_WIDTH, VBOX_HEIGHT), mainDef, circleDefinition);
    viewDefinition.setFactoryMethodName("svgViewTest");
    viewDefinition.setPath("svg-view-test.svg");
    final String generated = tested.generate(viewFactory, viewDefinition).toString();
    assertTrue(generated.contains("public SVGShapeViewResource svgViewTest()"));
    assertTrue(generated.contains("private SVGShapeView svgViewTestView(final boolean resizable)"));
    assertTrue(generated.contains("return this.svgViewTestView(25.50d, 225.45d, resizable);"));
    assertTrue(generated.contains("private SVGShapeView svgViewTestView(final double width, final double height, final boolean resizable) {"));
    assertTrue(generated.contains("final SVGShapeViewImpl view = new SVGShapeViewImpl(\"viewDef1\", mainShape, width, height, resizable)"));
    assertTrue(generated.contains("private SVGBasicShapeView svgViewTestBasicView() {"));
    assertTrue(generated.contains("return this.svgViewTestBasicView(25.50d, 225.45d);"));
    assertTrue(generated.contains("private SVGBasicShapeView svgViewTestBasicView(final double width, final double height) {"));
    assertTrue(generated.contains("final SVGBasicShapeViewImpl view = new SVGBasicShapeViewImpl(\"viewDef1\", mainShape, width, height)"));
}
Also used : CircleDefinition(org.kie.workbench.common.stunner.svg.gen.model.impl.CircleDefinition) ViewDefinitionImpl(org.kie.workbench.common.stunner.svg.gen.model.impl.ViewDefinitionImpl) ViewFactoryImpl(org.kie.workbench.common.stunner.svg.gen.model.impl.ViewFactoryImpl) ViewFactory(org.kie.workbench.common.stunner.svg.gen.model.ViewFactory) ViewBoxDefinitionImpl(org.kie.workbench.common.stunner.svg.gen.model.impl.ViewBoxDefinitionImpl) RectDefinition(org.kie.workbench.common.stunner.svg.gen.model.impl.RectDefinition) Test(org.junit.Test)

Example 3 with ViewFactoryImpl

use of org.kie.workbench.common.stunner.svg.gen.model.impl.ViewFactoryImpl in project kie-wb-common by kiegroup.

the class SVGViewDefinitionGeneratorTest method testCheckReferencesExist.

@Test(expected = RuntimeException.class)
public void testCheckReferencesExist() throws Exception {
    final ViewFactory viewFactory = new ViewFactoryImpl("view1", "org.kie.test", "MyType.¢lass");
    final RectDefinition mainDef = new RectDefinition("rect1", 1, 2, 0);
    final CircleDefinition circleDefinition = new CircleDefinition("circle1", 25);
    final ViewDefinitionImpl viewDefinition = new ViewDefinitionImpl(ID, X, Y, WIDTH, HEIGHT, styleSheetDefinition, new ViewBoxDefinitionImpl(VBOX_MIN_X, VBOX_MIN_Y, VBOX_WIDTH, VBOX_HEIGHT), null, mainDef, circleDefinition);
    viewDefinition.setFactoryMethodName("svgViewTest");
    viewDefinition.setPath("svg-view-test.svg");
    final ViewRefDefinition refDefinition = new ViewRefDefinitionImpl("#circle1", "rect1", "circle1", "circle1");
    viewDefinition.getSVGViewRefs().add(refDefinition);
    tested.generate(viewFactory, viewDefinition).toString();
}
Also used : CircleDefinition(org.kie.workbench.common.stunner.svg.gen.model.impl.CircleDefinition) ViewRefDefinitionImpl(org.kie.workbench.common.stunner.svg.gen.model.impl.ViewRefDefinitionImpl) ViewDefinitionImpl(org.kie.workbench.common.stunner.svg.gen.model.impl.ViewDefinitionImpl) ViewFactoryImpl(org.kie.workbench.common.stunner.svg.gen.model.impl.ViewFactoryImpl) ViewFactory(org.kie.workbench.common.stunner.svg.gen.model.ViewFactory) ViewRefDefinition(org.kie.workbench.common.stunner.svg.gen.model.ViewRefDefinition) ViewBoxDefinitionImpl(org.kie.workbench.common.stunner.svg.gen.model.impl.ViewBoxDefinitionImpl) RectDefinition(org.kie.workbench.common.stunner.svg.gen.model.impl.RectDefinition) Test(org.junit.Test)

Aggregations

ViewDefinitionImpl (org.kie.workbench.common.stunner.svg.gen.model.impl.ViewDefinitionImpl)3 ViewFactoryImpl (org.kie.workbench.common.stunner.svg.gen.model.impl.ViewFactoryImpl)3 Test (org.junit.Test)2 ViewFactory (org.kie.workbench.common.stunner.svg.gen.model.ViewFactory)2 CircleDefinition (org.kie.workbench.common.stunner.svg.gen.model.impl.CircleDefinition)2 RectDefinition (org.kie.workbench.common.stunner.svg.gen.model.impl.RectDefinition)2 ViewBoxDefinitionImpl (org.kie.workbench.common.stunner.svg.gen.model.impl.ViewBoxDefinitionImpl)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 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 SVGGenerator (org.kie.workbench.common.stunner.svg.gen.SVGGenerator)1