All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.talend.sdk.component.tools.SvgValidator Maven / Gradle / Ivy

There is a newer version: 10.57.0
Show newest version
/**
 * Copyright (C) 2006-2020 Talend Inc. - www.talend.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.talend.sdk.component.tools;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Locale;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Stream;

import org.apache.batik.anim.dom.SAXSVGDocumentFactory;
import org.apache.batik.anim.dom.SVGOMSVGElement;
import org.apache.batik.util.XMLResourceDescriptor;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.svg.SVGAnimatedRect;
import org.w3c.dom.svg.SVGRect;

public class SvgValidator {

    private final SAXSVGDocumentFactory factory =
            new SAXSVGDocumentFactory(XMLResourceDescriptor.getXMLParserClassName());

    public Stream validate(final Path path) {
        final String prefix = "[" + path.getFileName() + "] ";

        final SVGOMSVGElement icon;
        try {
            icon = loadSvg(path);
        } catch (final IllegalStateException e) {
            return Stream.of(prefix + "Invalid SVG: " + e.getMessage());
        }
        return Stream
                .> of(this::noEmbedStyle, this::pathsAreClosed, this::noDisplayNone,
                        this::viewportSize)
                .map(fn -> fn.apply(icon))
                .filter(Objects::nonNull)
                .map(error -> prefix + error);
    }

    private SVGOMSVGElement loadSvg(final Path path) {
        try {
            final Document document = factory.createDocument(path.toUri().toASCIIString());
            return SVGOMSVGElement.class.cast(document.getDocumentElement());
        } catch (final IOException e) {
            throw new IllegalStateException(e);
        }
    }

    private String viewportSize(final SVGOMSVGElement icon) {
        final SVGAnimatedRect viewBox = icon.getViewBox();
        if (viewBox == null) {
            return "No viewBox, need one with '0 0 16 16'";
        }
        final SVGRect baseVal = viewBox.getBaseVal();
        if (baseVal.getX() != 0 || baseVal.getY() != 0 || baseVal.getHeight() != 16 || baseVal.getWidth() != 16) {
            return "viewBox must be '0 0 16 16' found '" + (int) baseVal.getX() + ' ' + (int) baseVal.getY() + ' '
                    + (int) baseVal.getWidth() + ' ' + (int) baseVal.getHeight() + '\'';
        }
        return null;
    }

    private String pathsAreClosed(final SVGOMSVGElement icon) {
        return browseDom(icon, node -> {
            if ("path".equals(node.getNodeName())) {
                final Node d = node.getAttributes() == null ? null : node.getAttributes().getNamedItem("d");
                if (d == null || d.getNodeValue() == null) {
                    return "Missing 'd' in a path";
                }
                if (!d.getNodeValue().toLowerCase(Locale.ROOT).endsWith("z")) {
                    return "All path must be closed so end with 'z', found value: '" + d.getNodeValue() + '\'';
                }
            }
            return null;
        });
    }

    private String noEmbedStyle(final SVGOMSVGElement icon) {
        return browseDom(icon, node -> node.getNodeName().equals("style") ? "Forbidden