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);
}
}
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);
}
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());
}
}
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));
}
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));
}
Aggregations