Search in sources :

Example 1 with RuleLogger

use of org.jsonschema2pojo.RuleLogger in project jsonschema2pojo by joelittlejohn.

the class Jsonschema2PojoTask method execute.

/**
 * Execute this task (it's expected that all relevant setters will have been
 * called by Ant to provide task configuration <em>before</em> this method
 * is called).
 *
 * @throws BuildException
 *             if this task cannot be completed due to some error reading
 *             schemas, generating types or writing output .java files.
 */
@Override
public void execute() throws BuildException {
    if (skip) {
        return;
    }
    if (source == null) {
        log("source attribute is required but was not set");
        return;
    }
    // attempt to parse the url
    URL sourceURL;
    try {
        sourceURL = URLUtil.parseURL(source);
    } catch (IllegalArgumentException e) {
        log(String.format("Invalid schema source provided: %s", source));
        return;
    }
    // if url is a file, ensure it exists
    if (URLUtil.parseProtocol(sourceURL.toString()) == URLProtocol.FILE) {
        File sourceFile = new File(sourceURL.getFile());
        if (!sourceFile.exists()) {
            log(sourceFile.getAbsolutePath() + " cannot be found");
            return;
        }
    }
    if (targetDirectory == null) {
        log("targetDirectory attribute is required but was not set");
        return;
    }
    ClassLoader extendedClassloader = buildExtendedClassloader();
    Thread.currentThread().setContextClassLoader(extendedClassloader);
    RuleLogger ruleLogger = new AntRuleLogger(this);
    try {
        Jsonschema2Pojo.generate(this, ruleLogger);
    } catch (IOException e) {
        throw new BuildException("Error generating classes from JSON Schema file(s) " + source, e);
    }
}
Also used : RuleLogger(org.jsonschema2pojo.RuleLogger) URLClassLoader(java.net.URLClassLoader) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) URL(java.net.URL)

Example 2 with RuleLogger

use of org.jsonschema2pojo.RuleLogger in project jsonschema2pojo by joelittlejohn.

the class Jsonschema2PojoMojo method execute.

/**
 * Executes the plugin, to read the given source and behavioural properties
 * and generate POJOs. The current implementation acts as a wrapper around
 * the command line interface.
 */
@Override
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = { "NP_UNWRITTEN_FIELD", "UWF_UNWRITTEN_FIELD" }, justification = "Private fields set by Maven.")
@SuppressWarnings("PMD.UselessParentheses")
public void execute() throws MojoExecutionException {
    addProjectDependenciesToClasspath();
    try {
        getAnnotationStyle();
    } catch (IllegalArgumentException e) {
        throw new MojoExecutionException("Not a valid annotation style: " + annotationStyle);
    }
    try {
        new AnnotatorFactory(this).getAnnotator(getCustomAnnotator());
    } catch (IllegalArgumentException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
    if (skip) {
        return;
    }
    // verify source directories
    if (sourceDirectory != null) {
        sourceDirectory = FilenameUtils.normalize(sourceDirectory);
        // verify sourceDirectory
        try {
            URLUtil.parseURL(sourceDirectory);
        } catch (IllegalArgumentException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }
    } else if (!isEmpty(sourcePaths)) {
        // verify individual source paths
        for (int i = 0; i < sourcePaths.length; i++) {
            sourcePaths[i] = FilenameUtils.normalize(sourcePaths[i]);
            try {
                URLUtil.parseURL(sourcePaths[i]);
            } catch (IllegalArgumentException e) {
                throw new MojoExecutionException(e.getMessage(), e);
            }
        }
    } else {
        throw new MojoExecutionException("One of sourceDirectory or sourcePaths must be provided");
    }
    if (filteringEnabled() || (sourceDirectory != null && isEmpty(sourcePaths))) {
        if (sourceDirectory == null) {
            throw new MojoExecutionException("Source includes and excludes require the sourceDirectory property");
        }
        if (!isEmpty(sourcePaths)) {
            throw new MojoExecutionException("Source includes and excludes are incompatible with the sourcePaths property");
        }
        fileFilter = createFileFilter();
    }
    if (addCompileSourceRoot) {
        project.addCompileSourceRoot(outputDirectory.getPath());
    }
    if (useCommonsLang3) {
        getLog().warn("useCommonsLang3 is deprecated. Please remove it from your config.");
    }
    RuleLogger logger = new MojoRuleLogger(getLog());
    try {
        Jsonschema2Pojo.generate(this, logger);
    } catch (IOException e) {
        throw new MojoExecutionException("Error generating classes from JSON Schema file(s) " + sourceDirectory, e);
    }
}
Also used : RuleLogger(org.jsonschema2pojo.RuleLogger) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) IOException(java.io.IOException) AnnotatorFactory(org.jsonschema2pojo.AnnotatorFactory)

Example 3 with RuleLogger

use of org.jsonschema2pojo.RuleLogger in project jsonschema2pojo by joelittlejohn.

the class EnumRule method buildEnumDefinition.

/**
 * Builds the effective definition of an enumeration is based on what schema elements are provided.
 * <p/>
 * This function determines which method it should delegate creating of the definition to:
 *
 * For "enum" handled by {@link #buildEnumDefinitionWithNoExtensions(String, JsonNode, JsonNode, JType)}
 * For "enum" and "javaEnums" handled by {@link #buildEnumDefinitionWithJavaEnumsExtension(String, JsonNode, JsonNode, JsonNode, JType)}
 * For "enum" and "javaEnumNames" handled by {@link #buildEnumDefinitionWithJavaEnumNamesExtension(String, JsonNode, JsonNode, JsonNode, JType)}
 *
 * @param nodeName
 *            the name of the property which is an "enum"
 * @param node
 *            the enum node
 * @param backingType
 *            the object backing the value of enum, most commonly this is a string
 *
 * @return the effective definition for enumeration
 */
protected EnumDefinition buildEnumDefinition(String nodeName, JsonNode node, JType backingType) {
    JsonNode enums = node.path("enum");
    JsonNode javaEnumNames = node.path("javaEnumNames");
    JsonNode javaEnums = node.path("javaEnums");
    RuleLogger logger = ruleFactory.getLogger();
    if (!javaEnums.isMissingNode() && !javaEnumNames.isMissingNode()) {
        logger.error("Both javaEnums and javaEnumNames provided; the property javaEnumNames will be ignored when both javaEnums and javaEnumNames are provided.");
    }
    if (!javaEnumNames.isMissingNode()) {
        logger.error("javaEnumNames is deprecated; please migrate to javaEnums.");
    }
    EnumDefinition enumDefinition;
    if (!javaEnums.isMissingNode()) {
        enumDefinition = buildEnumDefinitionWithJavaEnumsExtension(nodeName, node, enums, javaEnums, backingType);
    } else if (!javaEnumNames.isMissingNode()) {
        enumDefinition = buildEnumDefinitionWithJavaEnumNamesExtension(nodeName, node, enums, javaEnumNames, backingType);
    } else {
        enumDefinition = buildEnumDefinitionWithNoExtensions(nodeName, node, enums, backingType);
    }
    return enumDefinition;
}
Also used : RuleLogger(org.jsonschema2pojo.RuleLogger) JsonNode(com.fasterxml.jackson.databind.JsonNode) EnumDefinition(org.jsonschema2pojo.model.EnumDefinition)

Example 4 with RuleLogger

use of org.jsonschema2pojo.RuleLogger in project jsonschema2pojo by joelittlejohn.

the class RuleFactoryImplTest method generationConfigIsReturned.

@Test
public void generationConfigIsReturned() {
    GenerationConfig mockGenerationConfig = mock(GenerationConfig.class);
    RuleLogger mockRuleLogger = mock(RuleLogger.class);
    RuleFactory ruleFactory = new RuleFactory(mockGenerationConfig, new NoopAnnotator(), new SchemaStore());
    ruleFactory.setLogger(mockRuleLogger);
    assertThat(ruleFactory.getGenerationConfig(), is(sameInstance(mockGenerationConfig)));
    assertThat(ruleFactory.getLogger(), is(sameInstance(mockRuleLogger)));
}
Also used : RuleLogger(org.jsonschema2pojo.RuleLogger) SchemaStore(org.jsonschema2pojo.SchemaStore) NoopAnnotator(org.jsonschema2pojo.NoopAnnotator) GenerationConfig(org.jsonschema2pojo.GenerationConfig) DefaultGenerationConfig(org.jsonschema2pojo.DefaultGenerationConfig) Test(org.junit.Test)

Example 5 with RuleLogger

use of org.jsonschema2pojo.RuleLogger in project jsonschema2pojo by joelittlejohn.

the class RuleFactoryImplTest method generationRuleLoggerIsReturned.

@Test
public void generationRuleLoggerIsReturned() {
    GenerationConfig mockGenerationConfig = mock(GenerationConfig.class);
    RuleLogger mockRuleLogger = mock(RuleLogger.class);
    RuleFactory ruleFactory = new RuleFactory(new DefaultGenerationConfig(), new NoopAnnotator(), new SchemaStore());
    ruleFactory.setLogger(mockRuleLogger);
    assertThat(ruleFactory.getLogger(), is(sameInstance(mockRuleLogger)));
}
Also used : DefaultGenerationConfig(org.jsonschema2pojo.DefaultGenerationConfig) RuleLogger(org.jsonschema2pojo.RuleLogger) SchemaStore(org.jsonschema2pojo.SchemaStore) NoopAnnotator(org.jsonschema2pojo.NoopAnnotator) GenerationConfig(org.jsonschema2pojo.GenerationConfig) DefaultGenerationConfig(org.jsonschema2pojo.DefaultGenerationConfig) Test(org.junit.Test)

Aggregations

RuleLogger (org.jsonschema2pojo.RuleLogger)5 IOException (java.io.IOException)2 DefaultGenerationConfig (org.jsonschema2pojo.DefaultGenerationConfig)2 GenerationConfig (org.jsonschema2pojo.GenerationConfig)2 NoopAnnotator (org.jsonschema2pojo.NoopAnnotator)2 SchemaStore (org.jsonschema2pojo.SchemaStore)2 Test (org.junit.Test)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 File (java.io.File)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 BuildException (org.apache.tools.ant.BuildException)1 AnnotatorFactory (org.jsonschema2pojo.AnnotatorFactory)1 EnumDefinition (org.jsonschema2pojo.model.EnumDefinition)1