Search in sources :

Example 6 with GeneratorException

use of org.kie.workbench.common.stunner.svg.gen.exception.GeneratorException 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 7 with GeneratorException

use of org.kie.workbench.common.stunner.svg.gen.exception.GeneratorException in project kie-wb-common by kiegroup.

the class SVGGeneratorImpl method parseSVGView.

private ViewDefinitionImpl parseSVGView(final String viewId, final String svgPath, final InputStream svgStream, final StyleSheetDefinition styleSheetDefinition) throws Exception {
    ViewDefinitionImpl svgShapeViewSource = null;
    try {
        Document root = parse(svgStream);
        svgShapeViewSource = translate(viewId, svgPath, root, styleSheetDefinition);
    } catch (final Exception e) {
        throw new GeneratorException(e);
    }
    return svgShapeViewSource;
}
Also used : ViewDefinitionImpl(org.kie.workbench.common.stunner.svg.gen.model.impl.ViewDefinitionImpl) Document(org.w3c.dom.Document) GeneratorException(org.kie.workbench.common.stunner.svg.gen.exception.GeneratorException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) GeneratorException(org.kie.workbench.common.stunner.svg.gen.exception.GeneratorException)

Example 8 with GeneratorException

use of org.kie.workbench.common.stunner.svg.gen.exception.GeneratorException in project kie-wb-common by kiegroup.

the class ImageDefinitionGenerator method doGenerate.

@Override
public StringBuffer doGenerate(final ImageDefinition input) throws GeneratorException {
    final String href = input.getHref();
    final Map<String, Object> root = new HashMap<>();
    root.put("className", Picture.class.getName());
    root.put("href", href);
    // Generate the code using the given template.
    try {
        return writeTemplate(root);
    } catch (final GenerationException e) {
        throw new GeneratorException(e);
    }
}
Also used : HashMap(java.util.HashMap) Picture(com.ait.lienzo.client.core.shape.Picture) GenerationException(org.uberfire.annotations.processors.exceptions.GenerationException) GeneratorException(org.kie.workbench.common.stunner.svg.gen.exception.GeneratorException)

Example 9 with GeneratorException

use of org.kie.workbench.common.stunner.svg.gen.exception.GeneratorException in project kie-wb-common by kiegroup.

the class MultiPathDefinitionGenerator method doGenerate.

@Override
public StringBuffer doGenerate(final MultiPathDefinition input) throws GeneratorException {
    final Map<String, Object> root = new HashMap<String, Object>();
    root.put("className", MultiPath.class.getName());
    root.put("pathInstanceId", SVGViewFactoryGenerator.getStaticFieldValidId(input.getId()));
    // Generate the code using the given template.
    try {
        return writeTemplate(root);
    } catch (final GenerationException e) {
        throw new GeneratorException(e);
    }
}
Also used : MultiPath(com.ait.lienzo.client.core.shape.MultiPath) HashMap(java.util.HashMap) GenerationException(org.uberfire.annotations.processors.exceptions.GenerationException) GeneratorException(org.kie.workbench.common.stunner.svg.gen.exception.GeneratorException)

Example 10 with GeneratorException

use of org.kie.workbench.common.stunner.svg.gen.exception.GeneratorException in project kie-wb-common by kiegroup.

the class SVGViewDefinitionGenerator method generate.

@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<>();
        // Generate the children primitives.
        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));
            }
        }
        // SVG View children.
        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() + "]");
            }
        });
        // Look for the state shape view.
        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();
        // Generate the main shape.
        final PrimitiveDefinitionGenerator<PrimitiveDefinition<?>> mainGenerator = getGenerator(main);
        final StringBuffer mainBuffer = mainGenerator.generate(main);
        final LayoutDefinition mainLayoutDefinition = main.getLayoutDefinition();
        final String mainLayoutRaw = SVGPrimitiveGeneratorUtils.formatLayout(mainLayoutDefinition);
        // Generate the view's text styling stuff.
        final StyleSheetDefinition globalStyleSheetDefinition = ((ViewDefinitionImpl) viewDefinition).getGlobalStyleSheetDefinition();
        final String viewTextRaw = null != globalStyleSheetDefinition ? SVGShapeTextCodeBuilder.generate("view", viewId, globalStyleSheetDefinition) : "";
        // Populate the context and generate using the template.
        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;
}
Also used : ViewDefinitionImpl(org.kie.workbench.common.stunner.svg.gen.model.impl.ViewDefinitionImpl) ShapeDefinition(org.kie.workbench.common.stunner.svg.gen.model.ShapeDefinition) HashMap(java.util.HashMap) PrimitiveDefinition(org.kie.workbench.common.stunner.svg.gen.model.PrimitiveDefinition) LinkedList(java.util.LinkedList) ViewRefDefinition(org.kie.workbench.common.stunner.svg.gen.model.ViewRefDefinition) StyleSheetDefinition(org.kie.workbench.common.stunner.svg.gen.model.StyleSheetDefinition) GenerationException(org.uberfire.annotations.processors.exceptions.GenerationException) LayoutDefinition(org.kie.workbench.common.stunner.svg.gen.model.LayoutDefinition) GeneratorException(org.kie.workbench.common.stunner.svg.gen.exception.GeneratorException)

Aggregations

GeneratorException (org.kie.workbench.common.stunner.svg.gen.exception.GeneratorException)10 HashMap (java.util.HashMap)7 GenerationException (org.uberfire.annotations.processors.exceptions.GenerationException)7 ViewDefinitionImpl (org.kie.workbench.common.stunner.svg.gen.model.impl.ViewDefinitionImpl)4 LinkedList (java.util.LinkedList)3 List (java.util.List)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 LayoutDefinition (org.kie.workbench.common.stunner.svg.gen.model.LayoutDefinition)2 PrimitiveDefinition (org.kie.workbench.common.stunner.svg.gen.model.PrimitiveDefinition)2 ShapeDefinition (org.kie.workbench.common.stunner.svg.gen.model.ShapeDefinition)2 StyleSheetDefinition (org.kie.workbench.common.stunner.svg.gen.model.StyleSheetDefinition)2 ViewDefinition (org.kie.workbench.common.stunner.svg.gen.model.ViewDefinition)2 Document (org.w3c.dom.Document)2 Circle (com.ait.lienzo.client.core.shape.Circle)1 MultiPath (com.ait.lienzo.client.core.shape.MultiPath)1 Picture (com.ait.lienzo.client.core.shape.Picture)1 Rectangle (com.ait.lienzo.client.core.shape.Rectangle)1 InputStream (java.io.InputStream)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1