001/*******************************************************************************
002 * Copyright (c) 2017 Red Hat Inc
003 * All rights reserved. This program and the accompanying materials
004 * are made available under the terms of the Eclipse Public License v1.0
005 * which accompanies this distribution, and is available at
006 * http://www.eclipse.org/legal/epl-v10.html
007 *
008 * Contributors:
009 *     Jens Reimann - initial API and implementation
010 *******************************************************************************/
011package de.dentrassi.varlink.generator;
012
013import java.nio.charset.Charset;
014import java.nio.charset.StandardCharsets;
015import java.nio.file.Path;
016import java.util.Collection;
017import java.util.LinkedList;
018import java.util.List;
019
020import de.dentrassi.varlink.idl.varlinkIdl.Interface;
021import de.dentrassi.varlink.maven.Loader;
022
023public interface Generator {
024    public void generate(Interface varlinkInterface);
025
026    public default void generateAll(final Collection<Interface> interfaces) {
027        interfaces.stream().forEach(this::generate);
028    }
029
030    public default void generateAll(final Loader loader) {
031        generateAll(loader.getInterfaces());
032    }
033
034    public static final class Options {
035        private Path targetPath;
036        private Charset characterSet = StandardCharsets.UTF_8;
037
038        public Options() {
039        }
040
041        public Options(final Options other) {
042            this.targetPath = other.targetPath;
043            this.characterSet = other.characterSet;
044        }
045
046        public void setCharacterSet(final Charset characterSet) {
047            this.characterSet = characterSet != null ? characterSet : StandardCharsets.UTF_8;
048        }
049
050        public Charset getCharacterSet() {
051            return this.characterSet;
052        }
053
054        public void setTargetPath(final Path targetPath) {
055            this.targetPath = targetPath;
056        }
057
058        public Path getTargetPath() {
059            return this.targetPath;
060        }
061
062        public void validate(final List<Exception> errors) {
063            if (this.targetPath == null) {
064                errors.add(new IllegalStateException("'targetPath' is not set"));
065            }
066        }
067
068        public void validate() {
069            final LinkedList<Exception> errors = new LinkedList<>();
070            validate(errors);
071
072            if (!errors.isEmpty()) {
073                final RuntimeException e = new RuntimeException("Invalid generator settings", errors.pollFirst());
074                errors.stream().forEach(e::addSuppressed);
075                throw e;
076            }
077        }
078    }
079}