使用Jaxb从Kotlin Enum生成XML模式

时间:2018-03-21 12:50:45

标签: jaxb kotlin jaxb2-maven-plugin

我想使用Kotlin枚举类中的一些XML模式文件。

当我从xml架构生成类时,这是输出:

import javax.xml.bind.annotation.XmlEnum;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>Java class for LogLevelType.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * <p>
 * <pre>
 * &lt;simpleType name="LogLevelType"&gt;
 *   &lt;restriction base="{http://www.w3.org/2001/XMLSchema}string"&gt;
 *     &lt;enumeration value="ERROR"/&gt;
 *     &lt;enumeration value="WARNING"/&gt;
 *     &lt;enumeration value="INFO"/&gt;
 *     &lt;enumeration value="DEBUG"/&gt;
 *   &lt;/restriction&gt;
 * &lt;/simpleType&gt;
 * </pre>
 * 
 */
@XmlType(name = "LogLevelType", namespace = "http://www.brabantia.com/XMLSchema/Logging")
@XmlEnum
public enum LogLevelType {

    ERROR,
    WARNING,
    INFO,
    DEBUG;

    public String value() {
        return name();
    }

    public static LogLevelType fromValue(String v) {
        return valueOf(v);
    }    
}

所以我认为当我创建一个枚举类并添加相同的注释时,我可以反过来做同样的事情。

import javax.xml.bind.annotation.XmlEnum
import javax.xml.bind.annotation.XmlType

@XmlType(name = "ErrorCategoryType", namespace = "http://www.brabantia.com/XMLSchema/Enum")
@XmlEnum
enum class ErrorCategoryType {
    FUNCTIONAL,
    TECHNICAL;

    fun value(): String = name
    fun fromValue(v: String) = valueOf(v)
}

在我的pom文件中,我有以下配置:

<plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>jaxb2-maven-plugin</artifactId>
        <version>2.3.1</version>
        <executions>
            <execution>
                <id>xjc</id>
                <goals>
                    <goal>xjc</goal>
                </goals>
                <configuration>
                    <outputDirectory>${project.build.directory}/generated-sources/jaxb</outputDirectory>

                    <!-- The package of your generated sources -->
                    <packageName>com.brabantia.common.schema</packageName>
                    <sources>
                        <source>${project.basedir}/src/main/resources/BrabantiaMain.xsd</source>
                    </sources>
                </configuration>
            </execution>
            <execution>
                <id>schemagen</id>
                <goals>
                    <goal>schemagen</goal>
                </goals>
                <configuration>
                    <outputDirectory>${project.basedir}/src/main/resources/Generated</outputDirectory>
                    <sources>
                        <source>${project.basedir}/src/main/kotlin/com/brabantia/common/xmlschemas/enums</source>
                    </sources>
                </configuration>
            </execution>
        </executions>
    </plugin>

使用xjc生成Java类,但是当我尝试使用schemagen生成xml架构时,会显示以下错误:

Failed to execute goal org.codehaus.mojo:jaxb2-maven-plugin:2.3.1:schemagen (default-cli) on project xml-schemas: Execution default-cli of goal org.codehaus.mojo:jaxb2-maven-plugin:2.3.1:schemagen failed: syntax error @[1,41] in file:/C:/projects/INTEGRATION_FACTORY/XML_SCHEMAS/src/main/kotlin/com/brabantia/common/xmlschemas/enum/ErrorCategoryType.kt -> [Help 1]

如何从Kotlin枚举类生成XML模式?

0 个答案:

没有答案