Package com.asakusafw.dmdl.java

Source Code of com.asakusafw.dmdl.java.GenerateTask

/**
* Copyright 2011-2014 Asakusa Framework Team.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.asakusafw.dmdl.java;

import java.io.IOException;
import java.util.Collection;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.asakusafw.dmdl.java.emitter.CompositeDataModelDriver;
import com.asakusafw.dmdl.java.emitter.JavaModelClassGenerator;
import com.asakusafw.dmdl.java.spi.JavaDataModelDriver;
import com.asakusafw.dmdl.semantics.DmdlSemantics;
import com.asakusafw.dmdl.semantics.ModelDeclaration;
import com.asakusafw.dmdl.util.AnalyzeTask;

/**
* Generates Java model classes from input DMDL scripts.
*/
public class GenerateTask {

    static final Logger LOG = LoggerFactory.getLogger(GenerateTask.class);

    private final Configuration conf;

    /**
     * Creates and returns a new instance.
     * @param conf emitter configuration
     * @throws IllegalArgumentException if some parameters were {@code null}
     */
    public GenerateTask(Configuration conf) {
        if (conf == null) {
            throw new IllegalArgumentException("conf must not be null"); //$NON-NLS-1$
        }
        this.conf = conf;
    }

    /**
     * Generates all models from source repository in the current configuration.
     * @throws IOException if failed to process DMDL scripts
     */
    public void process() throws IOException {
        JavaDataModelDriver driver = new CompositeDataModelDriver(conf.getServiceClassLoader());
        process(driver);
    }

    /**
     * Generates all models from source repository in the current configuration.
     * @param driver the Java program generator driver
     * @throws IOException if failed to process DMDL scripts
     */
    public void process(JavaDataModelDriver driver) throws IOException {
        if (driver == null) {
            throw new IllegalArgumentException("driver must not be null"); //$NON-NLS-1$
        }
        DmdlSemantics semantics = analyze();
        JavaModelClassGenerator generator = new JavaModelClassGenerator(semantics, conf, driver);
        Collection<ModelDeclaration> models = semantics.getDeclaredModels();
        LOG.info(Messages.getString("GenerateTask.monitorGenerateStarting"), models.size()); //$NON-NLS-1$
        for (ModelDeclaration model : models) {
            LOG.info(Messages.getString("GenerateTask.monitorGenerateModel"), model.getName()); //$NON-NLS-1$
            generator.emit(model);
        }
        LOG.info(Messages.getString("GenerateTask.monitorGenerateFinishing")); //$NON-NLS-1$
    }

    private DmdlSemantics analyze() throws IOException {
        AnalyzeTask analyzer =
                new AnalyzeTask(Messages.getString("GenerateTask.name"), conf.getServiceClassLoader()); //$NON-NLS-1$
        return analyzer.process(conf.getSource());
    }
}
TOP

Related Classes of com.asakusafw.dmdl.java.GenerateTask

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.