Search in sources :

Example 1 with ProgressCodeWriter

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);
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) ArrayList(java.util.ArrayList) JSONObjectAdapterException(org.sagebionetworks.schema.adapter.JSONObjectAdapterException) JSONException(org.json.JSONException) JCodeModel(com.sun.codemodel.JCodeModel) JSONObjectAdapterImpl(org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl) FileCodeWriter(com.sun.codemodel.writer.FileCodeWriter) ProgressCodeWriter(com.sun.codemodel.writer.ProgressCodeWriter) ObjectSchema(org.sagebionetworks.schema.ObjectSchema) FileFilter(java.io.FileFilter) File(java.io.File) FileCodeWriter(com.sun.codemodel.writer.FileCodeWriter) CodeWriter(com.sun.codemodel.CodeWriter) ProgressCodeWriter(com.sun.codemodel.writer.ProgressCodeWriter)

Example 2 with ProgressCodeWriter

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);
}
Also used : FileCodeWriter(com.sun.codemodel.writer.FileCodeWriter) ProgressCodeWriter(com.sun.codemodel.writer.ProgressCodeWriter) FileCodeWriter(com.sun.codemodel.writer.FileCodeWriter) ProgressCodeWriter(com.sun.codemodel.writer.ProgressCodeWriter)

Example 3 with ProgressCodeWriter

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();
        }
    }
}
Also used : WsgenOptions(com.sun.tools.ws.wscompile.WsgenOptions) ProgressCodeWriter(com.sun.codemodel.writer.ProgressCodeWriter) IOException(java.io.IOException) FilerCodeWriter(com.sun.tools.ws.wscompile.FilerCodeWriter) ProgressCodeWriter(com.sun.codemodel.writer.ProgressCodeWriter) CodeWriter(com.sun.codemodel.CodeWriter) FilerCodeWriter(com.sun.tools.ws.wscompile.FilerCodeWriter)

Example 4 with ProgressCodeWriter

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;
}
Also used : ProgressCodeWriter(com.sun.codemodel.writer.ProgressCodeWriter) TJavaGeneratorExtension(com.sun.tools.ws.api.TJavaGeneratorExtension) GeneratorBase(com.sun.tools.ws.processor.generator.GeneratorBase) CodeWriter(com.sun.codemodel.CodeWriter) ProgressCodeWriter(com.sun.codemodel.writer.ProgressCodeWriter) SAXException(org.xml.sax.SAXException)

Aggregations

ProgressCodeWriter (com.sun.codemodel.writer.ProgressCodeWriter)4 CodeWriter (com.sun.codemodel.CodeWriter)3 FileCodeWriter (com.sun.codemodel.writer.FileCodeWriter)2 JCodeModel (com.sun.codemodel.JCodeModel)1 JDefinedClass (com.sun.codemodel.JDefinedClass)1 TJavaGeneratorExtension (com.sun.tools.ws.api.TJavaGeneratorExtension)1 GeneratorBase (com.sun.tools.ws.processor.generator.GeneratorBase)1 FilerCodeWriter (com.sun.tools.ws.wscompile.FilerCodeWriter)1 WsgenOptions (com.sun.tools.ws.wscompile.WsgenOptions)1 File (java.io.File)1 FileFilter (java.io.FileFilter)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 JSONException (org.json.JSONException)1 ObjectSchema (org.sagebionetworks.schema.ObjectSchema)1 JSONObjectAdapterException (org.sagebionetworks.schema.adapter.JSONObjectAdapterException)1 JSONObjectAdapterImpl (org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl)1 SAXException (org.xml.sax.SAXException)1