Use plugin-generator-api in yang-maven-plugin
[yangtools.git] / plugin / yang-maven-plugin / src / main / java / org / opendaylight / yangtools / yang2sources / plugin / GeneratorTaskFactory.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang2sources.plugin;
9
10 import com.google.common.base.MoreObjects;
11 import com.google.common.base.MoreObjects.ToStringHelper;
12 import org.apache.maven.project.MavenProject;
13 import org.eclipse.jdt.annotation.NonNullByDefault;
14 import org.opendaylight.yangtools.plugin.generator.api.FileGenerator.ImportResolutionMode;
15 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
16
17 @NonNullByDefault
18 abstract class GeneratorTaskFactory extends ParserModeAware {
19     private final StatementParserMode parserMode;
20
21     GeneratorTaskFactory(final ImportResolutionMode importMode) {
22         switch (importMode) {
23             case REVISION_EXACT_OR_LATEST:
24                 parserMode = StatementParserMode.DEFAULT_MODE;
25                 break;
26             case SEMVER_LATEST:
27                 parserMode = StatementParserMode.SEMVER_MODE;
28                 break;
29             default:
30                 throw new LinkageError("Unhandled import mode " + importMode);
31         }
32     }
33
34     @Override
35     final StatementParserMode parserMode() {
36         return parserMode;
37     }
38
39     final String generatorName() {
40         return generator().getClass().getName();
41     }
42
43     /**
44      * Create a new {@link GeneratorTask} which will work in scope of specified {@link MavenProject} with the effective
45      * model held in specified {@link ContextHolder}.
46      *
47      * @param project current Maven Project
48      * @param context model generation context
49      */
50     abstract GeneratorTask<?> createTask(MavenProject project, ContextHolder context);
51
52     abstract Object generator();
53
54     ToStringHelper addToStringProperties(final ToStringHelper helper) {
55         return helper.add("generator", generatorName());
56     }
57
58     @Override
59     public final String toString() {
60         return addToStringProperties(MoreObjects.toStringHelper(this).omitNullValues()).toString();
61     }
62 }