Search in sources :

Example 41 with JCodeModel

use of com.sun.codemodel.JCodeModel in project raml-for-jax-rs by mulesoft-labs.

the class SchemaTypeFactory method createXmlType.

public static TypeGenerator createXmlType(CurrentBuild currentBuild, GType type) {
    File schemaFile = null;
    try {
        schemaFile = JAXBHelper.saveSchema(type.schema(), currentBuild.getSchemaRepository());
        final JCodeModel codeModel = new JCodeModel();
        Map<String, JClass> generated = JAXBHelper.generateClassesFromXmlSchemas(currentBuild.getModelPackage(), schemaFile, codeModel);
        XmlSchemaTypeGenerator gen = new XmlSchemaTypeGenerator(type, codeModel, currentBuild.getModelPackage(), generated.values().iterator().next());
        type.setJavaType(gen.getGeneratedJavaType());
        currentBuild.newGenerator(type.name(), gen);
        return gen;
    } catch (Exception e) {
        throw new GenerationException(e);
    }
}
Also used : JClass(com.sun.codemodel.JClass) File(java.io.File) JCodeModel(com.sun.codemodel.JCodeModel)

Example 42 with JCodeModel

use of com.sun.codemodel.JCodeModel in project raml-for-jax-rs by mulesoft-labs.

the class JsonSchemaTypeGenerator method output.

@Override
public void output(CodeContainer<JCodeModel> container, BuildPhase buildPhase) throws IOException {
    GenerationConfig config = build.getJsonMapperConfig();
    final SchemaMapper mapper = new SchemaMapper(new RuleFactory(config, new Jackson2Annotator(), new SchemaStore()), new SchemaGenerator());
    final JCodeModel codeModel = new JCodeModel();
    File schemaFile = File.createTempFile("schema", "json", build.getSchemaRepository());
    Files.write(schema, schemaFile, Charset.defaultCharset());
    try {
        mapper.generate(codeModel, name.simpleName(), pack, schemaFile.toURL());
    } catch (IOException e) {
        throw new GenerationException(e);
    }
    container.into(codeModel);
}
Also used : RuleFactory(org.jsonschema2pojo.rules.RuleFactory) IOException(java.io.IOException) JCodeModel(com.sun.codemodel.JCodeModel) File(java.io.File)

Example 43 with JCodeModel

use of com.sun.codemodel.JCodeModel 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)

Example 44 with JCodeModel

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

the class CyclicalRefIT method recursiveTreeNodeWithNoParentFileIsReadCorrectly.

@Test
public void recursiveTreeNodeWithNoParentFileIsReadCorrectly() throws ClassNotFoundException, NoSuchMethodException, IOException {
    JCodeModel codeModel = new JCodeModel();
    String content = IOUtils.toString(getClass().getResourceAsStream("/schema/ref/recursiveTreeNode.json"));
    JsonNode schema = new ObjectMapper().readTree(content);
    JPackage p = codeModel._package("com.example");
    new RuleFactory().getSchemaRule().apply("Example", schema, null, p, new Schema(null, schema, null));
}
Also used : RuleFactory(org.jsonschema2pojo.rules.RuleFactory) Schema(org.jsonschema2pojo.Schema) JPackage(com.sun.codemodel.JPackage) JsonNode(com.fasterxml.jackson.databind.JsonNode) JCodeModel(com.sun.codemodel.JCodeModel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 45 with JCodeModel

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

the class CyclicalRefIT method recursiveEtcdTreeNodeWithNoParentFileIsReadCorrectly.

@Test
public void recursiveEtcdTreeNodeWithNoParentFileIsReadCorrectly() throws ClassNotFoundException, NoSuchMethodException, IOException {
    JCodeModel codeModel = new JCodeModel();
    String content = IOUtils.toString(getClass().getResourceAsStream("/schema/ref/recursiveEtcdTreeNode.json"));
    JsonNode schema = new ObjectMapper().readTree(content);
    JPackage p = codeModel._package("com.example");
    new RuleFactory().getSchemaRule().apply("Example", schema, null, p, new Schema(null, schema, null));
}
Also used : RuleFactory(org.jsonschema2pojo.rules.RuleFactory) Schema(org.jsonschema2pojo.Schema) JPackage(com.sun.codemodel.JPackage) JsonNode(com.fasterxml.jackson.databind.JsonNode) JCodeModel(com.sun.codemodel.JCodeModel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

JCodeModel (com.sun.codemodel.JCodeModel)89 Test (org.junit.Test)70 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)58 JPackage (com.sun.codemodel.JPackage)50 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)45 JType (com.sun.codemodel.JType)43 JDefinedClass (com.sun.codemodel.JDefinedClass)18 JClass (com.sun.codemodel.JClass)14 RuleFactory (org.jsonschema2pojo.rules.RuleFactory)14 Schema (org.jsonschema2pojo.Schema)13 JsonNode (com.fasterxml.jackson.databind.JsonNode)9 TextNode (com.fasterxml.jackson.databind.node.TextNode)7 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)5 File (java.io.File)5 IOException (java.io.IOException)5 URL (java.net.URL)5 JBlock (com.sun.codemodel.JBlock)4 JCatchBlock (com.sun.codemodel.JCatchBlock)4 JDocComment (com.sun.codemodel.JDocComment)4 JMethod (com.sun.codemodel.JMethod)4