use of com.sun.codemodel.writer.ProgressCodeWriter in project study by bage2014.
the class SchemaToPojo method generatePojos.
/**
* Will read all schemas in the passed directory (or file) and create java files for each in the output directory.
* All classes will be created using the provided package name.
* @param schemaSource
* @param outputDir
* @param createRegister
* @param factory
* @param log
* @throws IOException
* @throws JSONObjectAdapterException
* @throws ClassNotFoundException
*/
public static void generatePojos(File schemaSource, File outputDir, String createRegister, HandlerFactory factory, StringBuilder log) throws IOException, JSONObjectAdapterException, ClassNotFoundException {
if (schemaSource == null)
throw new IllegalArgumentException("schemaSource cannot be null");
if (outputDir == null)
throw new IllegalArgumentException("outputDir cannot be null");
if (factory == null)
throw new IllegalArgumentException("The HandlerFactory cannot be null");
// process each file
Iterator<File> iterator = FileUtils.getRecursiveIterator(schemaSource, new FileFilter() {
@Override
public boolean accept(File pathname) {
// Only include .json files
return pathname.getName().toLowerCase().endsWith(".json");
}
});
// Build up the list of schemas from the files
List<ObjectSchema> schemaList = new ArrayList<ObjectSchema>();
while (iterator.hasNext()) {
File file = iterator.next();
String string = FileUtils.readToString(file);
// Create a new schema
ObjectSchema schema;
try {
schema = new ObjectSchema(new JSONObjectAdapterImpl(string));
} catch (JSONObjectAdapterException e) {
if (e.getCause() instanceof JSONException) {
JSONException e2 = (JSONException) e.getCause();
throw new JSONObjectAdapterException(file.getAbsolutePath() + ": " + e2.getMessage(), e2);
}
throw e;
}
// Now if the schema does not have a name use the file name
if (schema.getName() == null) {
schema.setName(extractSchemaNameFromFileName(file));
}
// Set the id
String packageName = getPackageNameFromFiles(schemaSource, file);
schema.setId(packageName + schema.getName());
// Each base schema must be an object even if it is not set
if (schema.getType() == null) {
schema.setType(TYPE.OBJECT);
}
// Add it to the list
schemaList.add(schema);
}
// JCodeModel is used as the document model for the classes.
JCodeModel codeModel = new JCodeModel();
// Create the register class if it is provided
JDefinedClass registerClass = null;
if (createRegister != null) {
registerClass = RegisterGenerator.createClassFromFullName(codeModel, createRegister);
}
// The drive does the recursive work and drives the handlers
PojoGeneratorDriver driver = new PojoGeneratorDriver(factory);
driver.createAllClasses(codeModel, schemaList);
// When provided, create a register for all of the classes in the list.
if (createRegister != null) {
RegisterGenerator.createRegister(codeModel, schemaList, registerClass, null);
}
// The final step is to generate the classes
if (!outputDir.exists()) {
outputDir.mkdirs();
}
if (createRegister != null) {
// create an effective schema file for each schema.
EffectiveSchemaUtil.generateEffectiveSchemaFiles(outputDir, schemaList);
}
CodeWriter sources = new ChangeFileCodeWriter(outputDir, log);
CodeWriter resources = new FileCodeWriter(outputDir);
sources = new ProgressCodeWriter(sources, System.out);
resources = new ProgressCodeWriter(resources, System.out);
codeModel.build(sources, resources);
}
use of com.sun.codemodel.writer.ProgressCodeWriter in project jaxb-ri by eclipse-ee4j.
the class JCodeModel method build.
/**
* Generates Java source code.
* A convenience method that calls {@link #build(CodeWriter,CodeWriter)}.
*
* @param srcDir
* Java source files are generated into this directory.
* @param resourceDir
* Other resource files are generated into this directory.
* @param status
* if non-null, progress indication will be sent to this stream.
*/
public void build(File srcDir, File resourceDir, PrintStream status) throws IOException {
CodeWriter src = new FileCodeWriter(srcDir);
CodeWriter res = new FileCodeWriter(resourceDir);
if (status != null) {
src = new ProgressCodeWriter(src, status);
res = new ProgressCodeWriter(res, status);
}
build(src, res);
}
use of com.sun.codemodel.writer.ProgressCodeWriter in project metro-jax-ws by eclipse-ee4j.
the class WebServiceWrapperGenerator method doPostProcessWebService.
@SuppressWarnings("CallToThreadDumpStack")
protected void doPostProcessWebService(WebService webService, TypeElement d) {
if (cm != null) {
WsgenOptions options = builder.getOptions();
assert options.filer != null;
try {
CodeWriter cw = new FilerCodeWriter(options);
if (options.verbose)
cw = new ProgressCodeWriter(cw, System.out);
cm.build(cw);
} catch (IOException e) {
e.printStackTrace();
}
}
}
use of com.sun.codemodel.writer.ProgressCodeWriter in project metro-jax-ws by eclipse-ee4j.
the class WsimportTool method generateCode.
protected boolean generateCode(Listener listener, Receiver receiver, Model wsdlModel, boolean generateService) throws IOException {
// generated code
if (!options.quiet)
listener.message(WscompileMessages.WSIMPORT_GENERATING_CODE());
TJavaGeneratorExtension[] genExtn = ServiceFinder.find(TJavaGeneratorExtension.class, ServiceLoader.load(TJavaGeneratorExtension.class)).toArray();
CustomExceptionGenerator.generate(wsdlModel, options, receiver);
SeiGenerator.generate(wsdlModel, options, receiver, genExtn);
if (receiver.hadError()) {
throw new AbortException();
}
if (generateService) {
ServiceGenerator.generate(wsdlModel, options, receiver);
}
for (GeneratorBase g : ServiceFinder.find(GeneratorBase.class, ServiceLoader.load(GeneratorBase.class))) {
g.init(wsdlModel, options, receiver);
g.doGeneration();
}
List<String> implFiles = null;
if (options.isGenerateJWS) {
implFiles = JwsImplGenerator.generate(wsdlModel, options, receiver);
}
for (Plugin plugin : options.activePlugins) {
try {
plugin.run(wsdlModel, options, receiver);
} catch (SAXException sex) {
// fatal error. error should have been reported
return false;
}
}
if (options.getModuleName() != null) {
options.getCodeModel()._prepareModuleInfo(options.getModuleName(), JAXWS_MODULE);
}
CodeWriter cw;
if (options.filer != null) {
cw = new FilerCodeWriter(options);
} else {
cw = new WSCodeWriter(options.sourceDir, options);
}
if (options.verbose)
cw = new ProgressCodeWriter(cw, out);
options.getCodeModel().build(cw);
if (options.isGenerateJWS) {
// move Impl files to implDestDir
return JwsImplGenerator.moveToImplDestDir(implFiles, options, receiver);
}
return true;
}
Aggregations