Search in sources :

Example 1 with CodeWriter

use of com.sun.codemodel.CodeWriter in project jsonschema2pojo by joelittlejohn.

the class Jsonschema2Pojo method generate.

/**
     * Reads the contents of the given source and initiates schema generation.
     *
     * @param config
     *            the configuration options (including source and target paths,
     *            and other behavioural options) that will control code
     *            generation
     * @throws FileNotFoundException
     *             if the source path is not found
     * @throws IOException
     *             if the application is unable to read data from the source
     */
public static void generate(GenerationConfig config) throws IOException {
    Annotator annotator = getAnnotator(config);
    RuleFactory ruleFactory = createRuleFactory(config);
    ruleFactory.setAnnotator(annotator);
    ruleFactory.setGenerationConfig(config);
    SchemaMapper mapper = new SchemaMapper(ruleFactory, new SchemaGenerator());
    JCodeModel codeModel = new JCodeModel();
    if (config.isRemoveOldOutput()) {
        removeOldOutput(config.getTargetDirectory());
    }
    for (Iterator<URL> sources = config.getSource(); sources.hasNext(); ) {
        URL source = sources.next();
        if (URLUtil.parseProtocol(source.toString()) == URLProtocol.FILE && URLUtil.getFileFromURL(source).isDirectory()) {
            generateRecursive(config, mapper, codeModel, defaultString(config.getTargetPackage()), Arrays.asList(URLUtil.getFileFromURL(source).listFiles(config.getFileFilter())));
        } else {
            mapper.generate(codeModel, getNodeName(source, config), defaultString(config.getTargetPackage()), source);
        }
    }
    if (config.getTargetDirectory().exists() || config.getTargetDirectory().mkdirs()) {
        CodeWriter sourcesWriter = new FileCodeWriterWithEncoding(config.getTargetDirectory(), config.getOutputEncoding());
        CodeWriter resourcesWriter = new FileCodeWriterWithEncoding(config.getTargetDirectory(), config.getOutputEncoding());
        codeModel.build(sourcesWriter, resourcesWriter);
    } else {
        throw new GenerationException("Could not create or access target directory " + config.getTargetDirectory().getAbsolutePath());
    }
}
Also used : RuleFactory(org.jsonschema2pojo.rules.RuleFactory) GenerationException(org.jsonschema2pojo.exception.GenerationException) JCodeModel(com.sun.codemodel.JCodeModel) CodeWriter(com.sun.codemodel.CodeWriter) URL(java.net.URL)

Example 2 with CodeWriter

use of com.sun.codemodel.CodeWriter in project jsonschema2pojo by joelittlejohn.

the class Jsonschema2Pojo method generate.

/**
 * Reads the contents of the given source and initiates schema generation.
 *
 * @param config
 *            the configuration options (including source and target paths,
 *            and other behavioural options) that will control code
 *            generation
 * @throws FileNotFoundException
 *             if the source path is not found
 * @throws IOException
 *             if the application is unable to read data from the source
 */
public static void generate(GenerationConfig config, RuleLogger logger) throws IOException {
    Annotator annotator = getAnnotator(config);
    RuleFactory ruleFactory = createRuleFactory(config);
    ruleFactory.setAnnotator(annotator);
    ruleFactory.setGenerationConfig(config);
    ruleFactory.setLogger(logger);
    ruleFactory.setSchemaStore(new SchemaStore(createContentResolver(config)));
    SchemaMapper mapper = new SchemaMapper(ruleFactory, createSchemaGenerator(config));
    JCodeModel codeModel = new JCodeModel();
    if (config.isRemoveOldOutput()) {
        removeOldOutput(config.getTargetDirectory());
    }
    for (Iterator<URL> sources = config.getSource(); sources.hasNext(); ) {
        URL source = sources.next();
        if (URLUtil.parseProtocol(source.toString()) == URLProtocol.FILE && URLUtil.getFileFromURL(source).isDirectory()) {
            generateRecursive(config, mapper, codeModel, defaultString(config.getTargetPackage()), Arrays.asList(URLUtil.getFileFromURL(source).listFiles(config.getFileFilter())));
        } else {
            mapper.generate(codeModel, getNodeName(source, config), defaultString(config.getTargetPackage()), source);
        }
    }
    if (config.getTargetDirectory().exists() || config.getTargetDirectory().mkdirs()) {
        CodeWriter sourcesWriter = new FileCodeWriterWithEncoding(config.getTargetDirectory(), config.getOutputEncoding());
        CodeWriter resourcesWriter = new FileCodeWriterWithEncoding(config.getTargetDirectory(), config.getOutputEncoding());
        codeModel.build(sourcesWriter, resourcesWriter);
    } else {
        throw new GenerationException("Could not create or access target directory " + config.getTargetDirectory().getAbsolutePath());
    }
}
Also used : RuleFactory(org.jsonschema2pojo.rules.RuleFactory) GenerationException(org.jsonschema2pojo.exception.GenerationException) JCodeModel(com.sun.codemodel.JCodeModel) CodeWriter(com.sun.codemodel.CodeWriter) URL(java.net.URL)

Aggregations

CodeWriter (com.sun.codemodel.CodeWriter)2 JCodeModel (com.sun.codemodel.JCodeModel)2 URL (java.net.URL)2 GenerationException (org.jsonschema2pojo.exception.GenerationException)2 RuleFactory (org.jsonschema2pojo.rules.RuleFactory)2