Java 类org.eclipse.xtext.service.AbstractGenericModule 实例源码

项目:SensIDL    文件:GenerationHandler.java   
/**
 * Start the Code generator
 * 
 * @param path
 *            the path where the files will be saves
 * @param modelPath
 *            the path to the model form with the code will be generated
 * @param language
 *            the language in which the Code will be generated
 * @return true if the code was generated
 * @throws NoSidlFileException
 *             Throws this Exception when the given file is not a sidl file
 * @throws FileNotFoundException
 *             Throws this Exception when there is no file at the given path
 * 
 */
public static boolean generate(String path, String modelPath, String language, Resource sensidlmodel)
        throws NoSidlFileException, FileNotFoundException {
    setGenerationLanguage(language);
    Injector injector = new SensidlStandaloneSetup().createInjectorAndDoEMFRegistration();

    // get resource
    Resource resource = null;
    if (sensidlmodel != null) {
        resource = sensidlmodel;
    } else {
        ResourceSet rs = new ResourceSetImpl();
        File file = new File(modelPath);

        // Exception handling for not existing input files
        if (!file.exists()) {
            throw new FileNotFoundException("File not found");
        }
        // Exception handling for input files in the wrong format
        if (!FilenameUtils.getExtension(modelPath).equals("sidl")) {
            throw new NoSidlFileException("No SIDL file found");
        }

        resource = rs.getResource(URI.createURI(file.toURI().toString()), true);
    }
    // Use the JavaIoFileSystemAccess and set the path
    final JavaIoFileSystemAccess fsa = new JavaIoFileSystemAccess();
    fsa.setOutputPath(path);

    generator = injector.getInstance(SensidlGenerator.class); // set up the
    // generator
    generator.setGenerationLanguage(generationLanguage);

    // inject fsa
    Guice.createInjector(new AbstractGenericModule() {
        @SuppressWarnings("unused")
        public Class<? extends IEncodingProvider> bindIEncodingProvider() {
            return IEncodingProvider.Runtime.class;
        }
    }).injectMembers(fsa);

    // generate
    generator.doGenerate(resource, fsa);

    return true;

}