@Test public void testGenerate() throws Exception { final ViewFactory viewFactory = new ViewFactoryImpl("view1", "org.kie.test", "MyType.¢lass", "MyViewBuilderType.class"); 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 SVGShapeView view = getViewBuilder().build(\"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)")); }
@Override @SuppressWarnings("unchecked") public StringBuffer generate(final ViewFactory viewFactory, final ViewDefinition<SVGShapeView> viewDefinition) throws GeneratorException { StringBuffer result = null; final String factoryName = viewFactory.getSimpleName(); final String viewId = viewDefinition.getId(); final String methodName = viewDefinition.getFactoryMethodName(); final ShapeDefinition main = viewDefinition.getMain(); if (null != main) { final Map<String, Object> root = new HashMap<>(); final List<String> childrenRaw = new LinkedList<>(); final List<PrimitiveDefinition> children = viewDefinition.getChildren(); for (final PrimitiveDefinition child : children) { final String childId = SVGGeneratorFormatUtils.getValidInstanceId(child); String childRaw = SVGPrimitiveGeneratorUtils .generateSvgPrimitive(childId, SVGViewDefinitionGenerator::getGenerator, child); if (null != childRaw) { childrenRaw.add(childRaw); childrenRaw.add(AbstractGenerator.formatString(PRIM_CHILD_TEMPLATE, childId)); } } final List<String> svgChildrenRaw = new LinkedList<>(); final List<ViewRefDefinition> svgViewRefs = viewDefinition.getSVGViewRefs(); svgViewRefs.forEach(viewRef -> { final String parent = viewRef.getParent(); final String svgName = viewRef.getFilePath(); final String viewRefId = viewRef.getViewRefId(); final boolean existReferencedView = viewFactory .getViewDefinitions().stream() .anyMatch(def -> viewRefId.equals(def.getId())); if (existReferencedView) { final String childRaw = formatString(SVG_CHILD_TEMPLATE, parent, factoryName, viewRefId); svgChildrenRaw.add(childRaw); } else { throw new RuntimeException("The view [" + viewRefId + "] references " + "another the view [" + svgName + "], but no factory method " + "for it exists in [" + viewFactory.getImplementedType() + "]"); } }); final List<ShapeDefinition> stateViews = new LinkedList<>(); SVGModelUtils.visit(viewDefinition, p -> { if (p instanceof ShapeDefinition && SVGPrimitiveGeneratorUtils.CAN_GENERATE_PRIMITIVE_CODE.test(p)) { final ShapeDefinition shapeDefinition = (ShapeDefinition) p; shapeDefinition.getStateDefinition() .ifPresent(s -> stateViews.add((ShapeDefinition) p)); } }); final String stateViewIds = stateViews.isEmpty() ? "view" : stateViews.stream() .map(d -> SVGGeneratorFormatUtils.getValidInstanceId(d.getId())) .collect(Collectors.joining(",")); final String stateViewPolicyType = stateViews.isEmpty() ? ShapeStateDefaultHandler.RenderType.STROKE.name() : ((ShapeDefinition.ShapeStateDefinition) stateViews.get(0).getStateDefinition().get()).name(); final String stateViewPolicy = ShapeStateDefaultHandler.RenderType.class.getName().replace("$", ".") + "." + stateViewPolicyType.toUpperCase(); final PrimitiveDefinitionGenerator<PrimitiveDefinition<?>> mainGenerator = getGenerator(main); final StringBuffer mainBuffer = mainGenerator.generate(main); final LayoutDefinition mainLayoutDefinition = main.getLayoutDefinition(); final String mainLayoutRaw = SVGPrimitiveGeneratorUtils.formatLayout(mainLayoutDefinition); final StyleSheetDefinition globalStyleSheetDefinition = ((ViewDefinitionImpl) viewDefinition).getGlobalStyleSheetDefinition(); final String viewTextRaw = null != globalStyleSheetDefinition ? SVGShapeTextCodeBuilder.generate("view", viewId, globalStyleSheetDefinition) : ""; root.put("viewId", viewId); root.put("name", methodName); root.put("mainShape", mainBuffer.toString()); root.put("layout", mainLayoutRaw); root.put("width", formatDouble(viewDefinition.getWidth())); root.put("height", formatDouble(viewDefinition.getHeight())); root.put("text", viewTextRaw); root.put("stateViewIds", stateViewIds); root.put("stateViewPolicy", stateViewPolicy); root.put("children", childrenRaw); root.put("svgChildren", svgChildrenRaw); try { result = writeTemplate(root); } catch (final GenerationException e) { throw new GeneratorException(e); } } return result; }
SVGViewDefinitionGenerator extends AbstractGenerator implements ViewDefinitionGenerator<ViewDefinition<SVGShapeView>> { @Override @SuppressWarnings("unchecked") public StringBuffer generate(final ViewFactory viewFactory, final ViewDefinition<SVGShapeView> viewDefinition) throws GeneratorException { StringBuffer result = null; final String factoryName = viewFactory.getSimpleName(); final String viewId = viewDefinition.getId(); final String methodName = viewDefinition.getFactoryMethodName(); final ShapeDefinition main = viewDefinition.getMain(); if (null != main) { final Map<String, Object> root = new HashMap<>(); final List<String> childrenRaw = new LinkedList<>(); final List<PrimitiveDefinition> children = viewDefinition.getChildren(); for (final PrimitiveDefinition child : children) { final String childId = SVGGeneratorFormatUtils.getValidInstanceId(child); String childRaw = SVGPrimitiveGeneratorUtils .generateSvgPrimitive(childId, SVGViewDefinitionGenerator::getGenerator, child); if (null != childRaw) { childrenRaw.add(childRaw); childrenRaw.add(AbstractGenerator.formatString(PRIM_CHILD_TEMPLATE, childId)); } } final List<String> svgChildrenRaw = new LinkedList<>(); final List<ViewRefDefinition> svgViewRefs = viewDefinition.getSVGViewRefs(); svgViewRefs.forEach(viewRef -> { final String parent = viewRef.getParent(); final String svgName = viewRef.getFilePath(); final String viewRefId = viewRef.getViewRefId(); final boolean existReferencedView = viewFactory .getViewDefinitions().stream() .anyMatch(def -> viewRefId.equals(def.getId())); if (existReferencedView) { final String childRaw = formatString(SVG_CHILD_TEMPLATE, parent, factoryName, viewRefId); svgChildrenRaw.add(childRaw); } else { throw new RuntimeException("The view [" + viewRefId + "] references " + "another the view [" + svgName + "], but no factory method " + "for it exists in [" + viewFactory.getImplementedType() + "]"); } }); final List<ShapeDefinition> stateViews = new LinkedList<>(); SVGModelUtils.visit(viewDefinition, p -> { if (p instanceof ShapeDefinition && SVGPrimitiveGeneratorUtils.CAN_GENERATE_PRIMITIVE_CODE.test(p)) { final ShapeDefinition shapeDefinition = (ShapeDefinition) p; shapeDefinition.getStateDefinition() .ifPresent(s -> stateViews.add((ShapeDefinition) p)); } }); final String stateViewIds = stateViews.isEmpty() ? "view" : stateViews.stream() .map(d -> SVGGeneratorFormatUtils.getValidInstanceId(d.getId())) .collect(Collectors.joining(",")); final String stateViewPolicyType = stateViews.isEmpty() ? ShapeStateDefaultHandler.RenderType.STROKE.name() : ((ShapeDefinition.ShapeStateDefinition) stateViews.get(0).getStateDefinition().get()).name(); final String stateViewPolicy = ShapeStateDefaultHandler.RenderType.class.getName().replace("$", ".") + "." + stateViewPolicyType.toUpperCase(); final PrimitiveDefinitionGenerator<PrimitiveDefinition<?>> mainGenerator = getGenerator(main); final StringBuffer mainBuffer = mainGenerator.generate(main); final LayoutDefinition mainLayoutDefinition = main.getLayoutDefinition(); final String mainLayoutRaw = SVGPrimitiveGeneratorUtils.formatLayout(mainLayoutDefinition); final StyleSheetDefinition globalStyleSheetDefinition = ((ViewDefinitionImpl) viewDefinition).getGlobalStyleSheetDefinition(); final String viewTextRaw = null != globalStyleSheetDefinition ? SVGShapeTextCodeBuilder.generate("view", viewId, globalStyleSheetDefinition) : ""; root.put("viewId", viewId); root.put("name", methodName); root.put("mainShape", mainBuffer.toString()); root.put("layout", mainLayoutRaw); root.put("width", formatDouble(viewDefinition.getWidth())); root.put("height", formatDouble(viewDefinition.getHeight())); root.put("text", viewTextRaw); root.put("stateViewIds", stateViewIds); root.put("stateViewPolicy", stateViewPolicy); root.put("children", childrenRaw); root.put("svgChildren", svgChildrenRaw); try { result = writeTemplate(root); } catch (final GenerationException e) { throw new GeneratorException(e); } } return result; } }
SVGViewDefinitionGenerator extends AbstractGenerator implements ViewDefinitionGenerator<ViewDefinition<SVGShapeView>> { @Override @SuppressWarnings("unchecked") public StringBuffer generate(final ViewFactory viewFactory, final ViewDefinition<SVGShapeView> viewDefinition) throws GeneratorException { StringBuffer result = null; final String factoryName = viewFactory.getSimpleName(); final String viewId = viewDefinition.getId(); final String methodName = viewDefinition.getFactoryMethodName(); final ShapeDefinition main = viewDefinition.getMain(); if (null != main) { final Map<String, Object> root = new HashMap<>(); final List<String> childrenRaw = new LinkedList<>(); final List<PrimitiveDefinition> children = viewDefinition.getChildren(); for (final PrimitiveDefinition child : children) { final String childId = SVGGeneratorFormatUtils.getValidInstanceId(child); String childRaw = SVGPrimitiveGeneratorUtils .generateSvgPrimitive(childId, SVGViewDefinitionGenerator::getGenerator, child); if (null != childRaw) { childrenRaw.add(childRaw); childrenRaw.add(AbstractGenerator.formatString(PRIM_CHILD_TEMPLATE, childId)); } } final List<String> svgChildrenRaw = new LinkedList<>(); final List<ViewRefDefinition> svgViewRefs = viewDefinition.getSVGViewRefs(); svgViewRefs.forEach(viewRef -> { final String parent = viewRef.getParent(); final String svgName = viewRef.getFilePath(); final String viewRefId = viewRef.getViewRefId(); final boolean existReferencedView = viewFactory .getViewDefinitions().stream() .anyMatch(def -> viewRefId.equals(def.getId())); if (existReferencedView) { final String childRaw = formatString(SVG_CHILD_TEMPLATE, parent, factoryName, viewRefId); svgChildrenRaw.add(childRaw); } else { throw new RuntimeException("The view [" + viewRefId + "] references " + "another the view [" + svgName + "], but no factory method " + "for it exists in [" + viewFactory.getImplementedType() + "]"); } }); final List<ShapeDefinition> stateViews = new LinkedList<>(); SVGModelUtils.visit(viewDefinition, p -> { if (p instanceof ShapeDefinition && SVGPrimitiveGeneratorUtils.CAN_GENERATE_PRIMITIVE_CODE.test(p)) { final ShapeDefinition shapeDefinition = (ShapeDefinition) p; shapeDefinition.getStateDefinition() .ifPresent(s -> stateViews.add((ShapeDefinition) p)); } }); final String stateViewIds = stateViews.isEmpty() ? "view" : stateViews.stream() .map(d -> SVGGeneratorFormatUtils.getValidInstanceId(d.getId())) .collect(Collectors.joining(",")); final String stateViewPolicyType = stateViews.isEmpty() ? ShapeStateDefaultHandler.RenderType.STROKE.name() : ((ShapeDefinition.ShapeStateDefinition) stateViews.get(0).getStateDefinition().get()).name(); final String stateViewPolicy = ShapeStateDefaultHandler.RenderType.class.getName().replace("$", ".") + "." + stateViewPolicyType.toUpperCase(); final PrimitiveDefinitionGenerator<PrimitiveDefinition<?>> mainGenerator = getGenerator(main); final StringBuffer mainBuffer = mainGenerator.generate(main); final LayoutDefinition mainLayoutDefinition = main.getLayoutDefinition(); final String mainLayoutRaw = SVGPrimitiveGeneratorUtils.formatLayout(mainLayoutDefinition); final StyleSheetDefinition globalStyleSheetDefinition = ((ViewDefinitionImpl) viewDefinition).getGlobalStyleSheetDefinition(); final String viewTextRaw = null != globalStyleSheetDefinition ? SVGShapeTextCodeBuilder.generate("view", viewId, globalStyleSheetDefinition) : ""; root.put("viewId", viewId); root.put("name", methodName); root.put("mainShape", mainBuffer.toString()); root.put("layout", mainLayoutRaw); root.put("width", formatDouble(viewDefinition.getWidth())); root.put("height", formatDouble(viewDefinition.getHeight())); root.put("text", viewTextRaw); root.put("stateViewIds", stateViewIds); root.put("stateViewPolicy", stateViewPolicy); root.put("children", childrenRaw); root.put("svgChildren", svgChildrenRaw); try { result = writeTemplate(root); } catch (final GenerationException e) { throw new GeneratorException(e); } } return result; }  }
SVGViewDefinitionGenerator extends AbstractGenerator implements ViewDefinitionGenerator<ViewDefinition<SVGShapeView>> { @Override @SuppressWarnings("unchecked") public StringBuffer generate(final ViewFactory viewFactory, final ViewDefinition<SVGShapeView> viewDefinition) throws GeneratorException { StringBuffer result = null; final String factoryName = viewFactory.getSimpleName(); final String viewId = viewDefinition.getId(); final String methodName = viewDefinition.getFactoryMethodName(); final ShapeDefinition main = viewDefinition.getMain(); if (null != main) { final Map<String, Object> root = new HashMap<>(); final List<String> childrenRaw = new LinkedList<>(); final List<PrimitiveDefinition> children = viewDefinition.getChildren(); for (final PrimitiveDefinition child : children) { final String childId = SVGGeneratorFormatUtils.getValidInstanceId(child); String childRaw = SVGPrimitiveGeneratorUtils .generateSvgPrimitive(childId, SVGViewDefinitionGenerator::getGenerator, child); if (null != childRaw) { childrenRaw.add(childRaw); childrenRaw.add(AbstractGenerator.formatString(PRIM_CHILD_TEMPLATE, childId)); } } final List<String> svgChildrenRaw = new LinkedList<>(); final List<ViewRefDefinition> svgViewRefs = viewDefinition.getSVGViewRefs(); svgViewRefs.forEach(viewRef -> { final String parent = viewRef.getParent(); final String svgName = viewRef.getFilePath(); final String viewRefId = viewRef.getViewRefId(); final boolean existReferencedView = viewFactory .getViewDefinitions().stream() .anyMatch(def -> viewRefId.equals(def.getId())); if (existReferencedView) { final String childRaw = formatString(SVG_CHILD_TEMPLATE, parent, factoryName, viewRefId); svgChildrenRaw.add(childRaw); } else { throw new RuntimeException("The view [" + viewRefId + "] references " + "another the view [" + svgName + "], but no factory method " + "for it exists in [" + viewFactory.getImplementedType() + "]"); } }); final List<ShapeDefinition> stateViews = new LinkedList<>(); SVGModelUtils.visit(viewDefinition, p -> { if (p instanceof ShapeDefinition && SVGPrimitiveGeneratorUtils.CAN_GENERATE_PRIMITIVE_CODE.test(p)) { final ShapeDefinition shapeDefinition = (ShapeDefinition) p; shapeDefinition.getStateDefinition() .ifPresent(s -> stateViews.add((ShapeDefinition) p)); } }); final String stateViewIds = stateViews.isEmpty() ? "view" : stateViews.stream() .map(d -> SVGGeneratorFormatUtils.getValidInstanceId(d.getId())) .collect(Collectors.joining(",")); final String stateViewPolicyType = stateViews.isEmpty() ? ShapeStateDefaultHandler.RenderType.STROKE.name() : ((ShapeDefinition.ShapeStateDefinition) stateViews.get(0).getStateDefinition().get()).name(); final String stateViewPolicy = ShapeStateDefaultHandler.RenderType.class.getName().replace("$", ".") + "." + stateViewPolicyType.toUpperCase(); final PrimitiveDefinitionGenerator<PrimitiveDefinition<?>> mainGenerator = getGenerator(main); final StringBuffer mainBuffer = mainGenerator.generate(main); final LayoutDefinition mainLayoutDefinition = main.getLayoutDefinition(); final String mainLayoutRaw = SVGPrimitiveGeneratorUtils.formatLayout(mainLayoutDefinition); final StyleSheetDefinition globalStyleSheetDefinition = ((ViewDefinitionImpl) viewDefinition).getGlobalStyleSheetDefinition(); final String viewTextRaw = null != globalStyleSheetDefinition ? SVGShapeTextCodeBuilder.generate("view", viewId, globalStyleSheetDefinition) : ""; root.put("viewId", viewId); root.put("name", methodName); root.put("mainShape", mainBuffer.toString()); root.put("layout", mainLayoutRaw); root.put("width", formatDouble(viewDefinition.getWidth())); root.put("height", formatDouble(viewDefinition.getHeight())); root.put("text", viewTextRaw); root.put("stateViewIds", stateViewIds); root.put("stateViewPolicy", stateViewPolicy); root.put("children", childrenRaw); root.put("svgChildren", svgChildrenRaw); try { result = writeTemplate(root); } catch (final GenerationException e) { throw new GeneratorException(e); } } return result; }  @Override @SuppressWarnings("unchecked") StringBuffer generate(final ViewFactory viewFactory,
                                 final ViewDefinition<SVGShapeView> viewDefinition); }
SVGViewDefinitionGenerator extends AbstractGenerator implements ViewDefinitionGenerator<ViewDefinition<SVGShapeView>> { @Override @SuppressWarnings("unchecked") public StringBuffer generate(final ViewFactory viewFactory, final ViewDefinition<SVGShapeView> viewDefinition) throws GeneratorException { StringBuffer result = null; final String factoryName = viewFactory.getSimpleName(); final String viewId = viewDefinition.getId(); final String methodName = viewDefinition.getFactoryMethodName(); final ShapeDefinition main = viewDefinition.getMain(); if (null != main) { final Map<String, Object> root = new HashMap<>(); final List<String> childrenRaw = new LinkedList<>(); final List<PrimitiveDefinition> children = viewDefinition.getChildren(); for (final PrimitiveDefinition child : children) { final String childId = SVGGeneratorFormatUtils.getValidInstanceId(child); String childRaw = SVGPrimitiveGeneratorUtils .generateSvgPrimitive(childId, SVGViewDefinitionGenerator::getGenerator, child); if (null != childRaw) { childrenRaw.add(childRaw); childrenRaw.add(AbstractGenerator.formatString(PRIM_CHILD_TEMPLATE, childId)); } } final List<String> svgChildrenRaw = new LinkedList<>(); final List<ViewRefDefinition> svgViewRefs = viewDefinition.getSVGViewRefs(); svgViewRefs.forEach(viewRef -> { final String parent = viewRef.getParent(); final String svgName = viewRef.getFilePath(); final String viewRefId = viewRef.getViewRefId(); final boolean existReferencedView = viewFactory .getViewDefinitions().stream() .anyMatch(def -> viewRefId.equals(def.getId())); if (existReferencedView) { final String childRaw = formatString(SVG_CHILD_TEMPLATE, parent, factoryName, viewRefId); svgChildrenRaw.add(childRaw); } else { throw new RuntimeException("The view [" + viewRefId + "] references " + "another the view [" + svgName + "], but no factory method " + "for it exists in [" + viewFactory.getImplementedType() + "]"); } }); final List<ShapeDefinition> stateViews = new LinkedList<>(); SVGModelUtils.visit(viewDefinition, p -> { if (p instanceof ShapeDefinition && SVGPrimitiveGeneratorUtils.CAN_GENERATE_PRIMITIVE_CODE.test(p)) { final ShapeDefinition shapeDefinition = (ShapeDefinition) p; shapeDefinition.getStateDefinition() .ifPresent(s -> stateViews.add((ShapeDefinition) p)); } }); final String stateViewIds = stateViews.isEmpty() ? "view" : stateViews.stream() .map(d -> SVGGeneratorFormatUtils.getValidInstanceId(d.getId())) .collect(Collectors.joining(",")); final String stateViewPolicyType = stateViews.isEmpty() ? ShapeStateDefaultHandler.RenderType.STROKE.name() : ((ShapeDefinition.ShapeStateDefinition) stateViews.get(0).getStateDefinition().get()).name(); final String stateViewPolicy = ShapeStateDefaultHandler.RenderType.class.getName().replace("$", ".") + "." + stateViewPolicyType.toUpperCase(); final PrimitiveDefinitionGenerator<PrimitiveDefinition<?>> mainGenerator = getGenerator(main); final StringBuffer mainBuffer = mainGenerator.generate(main); final LayoutDefinition mainLayoutDefinition = main.getLayoutDefinition(); final String mainLayoutRaw = SVGPrimitiveGeneratorUtils.formatLayout(mainLayoutDefinition); final StyleSheetDefinition globalStyleSheetDefinition = ((ViewDefinitionImpl) viewDefinition).getGlobalStyleSheetDefinition(); final String viewTextRaw = null != globalStyleSheetDefinition ? SVGShapeTextCodeBuilder.generate("view", viewId, globalStyleSheetDefinition) : ""; root.put("viewId", viewId); root.put("name", methodName); root.put("mainShape", mainBuffer.toString()); root.put("layout", mainLayoutRaw); root.put("width", formatDouble(viewDefinition.getWidth())); root.put("height", formatDouble(viewDefinition.getHeight())); root.put("text", viewTextRaw); root.put("stateViewIds", stateViewIds); root.put("stateViewPolicy", stateViewPolicy); root.put("children", childrenRaw); root.put("svgChildren", svgChildrenRaw); try { result = writeTemplate(root); } catch (final GenerationException e) { throw new GeneratorException(e); } } return result; }  @Override @SuppressWarnings("unchecked") StringBuffer generate(final ViewFactory viewFactory,
                                 final ViewDefinition<SVGShapeView> viewDefinition); static final String PRIM_CHILD_TEMPLATE; }