All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.n2oapp.framework.config.selective.CompileInfo Maven / Gradle / Ivy

The newest version!
package net.n2oapp.framework.config.selective;

import net.n2oapp.framework.api.metadata.Compiled;
import net.n2oapp.framework.api.metadata.SourceMetadata;
import net.n2oapp.framework.api.metadata.global.N2oMetadata;
import net.n2oapp.framework.api.metadata.io.NamespaceIO;
import net.n2oapp.framework.api.metadata.io.ProxyNamespaceIO;
import net.n2oapp.framework.api.metadata.reader.NamespaceReader;
import net.n2oapp.framework.api.reader.SourceLoader;
import net.n2oapp.framework.api.register.SourceInfo;
import net.n2oapp.framework.api.register.SourceTypeRegister;
import net.n2oapp.framework.api.register.scan.MetadataScanner;
import net.n2oapp.framework.config.N2oApplicationBuilder;
import net.n2oapp.framework.config.register.*;
import net.n2oapp.framework.config.register.scanner.FolderInfoScanner;
import net.n2oapp.framework.config.register.storage.PathUtil;
import net.n2oapp.framework.config.metadata.pack.N2oSourceTypesPack;


/**
 * Аналог Info для тестов
 */
public class CompileInfo extends SourceInfo {

    private static SourceTypeRegister sourceTypes = new N2oApplicationBuilder()
            .packs(new N2oSourceTypesPack())
            .getEnvironment().getSourceTypeRegister();

    protected Origin origin = Origin.xml;
    private String path;
    private NamespaceReader metadataReader;
    private Class compiledMetadataClass;
    private Class sourceMetadataClass;

    public CompileInfo(XmlInfo info) {
        origin = info.getOrigin();
        path = info.getLocalPath() != null && !info.getLocalPath().isEmpty() ? info.getLocalPath() : info.getId();
        sourceMetadataClass = info.getBaseSourceClass();
    }

    public CompileInfo(String path) {
        this.path = PathUtil.convertPathToClasspathUri(path);
    }

    public CompileInfo(String path, NamespaceReader metadataReader,
                       Class compiledMetadataClass) {
        this(path);
        this.metadataReader = metadataReader;
        this.compiledMetadataClass = compiledMetadataClass;
    }

    public CompileInfo(String path, NamespaceIO metadataIO,
                       Class compiledMetadataClass) {
        this(path);
        this.metadataReader = new ProxyNamespaceIO<>(metadataIO);
        this.compiledMetadataClass = compiledMetadataClass;
    }

    public CompileInfo(String id, Class metadataSourceClass) {
        this.id = id;
        this.sourceMetadataClass = metadataSourceClass;
    }

    public String getPath() {
        return path;
    }

    @Override
    public Class getBaseSourceClass() {
        if (sourceMetadataClass != null)
            return sourceMetadataClass;
        else if (path != null) {
            String type = RegisterUtil.getIdAndPostfix(path)[1];
            if (type == null || type.isEmpty())
                return null;
            return sourceTypes.get(type).getBaseSourceClass();
        } else
            return null;
    }

    @Deprecated
    public Class getSourceMetadataClass() {
        return getBaseSourceClass();
    }

    @Override
    public Class getReaderClass() {
        return SelectiveMetadataLoader.class;
    }

    @Override
    public Class getScannerClass() {
        return FolderInfoScanner.class;
    }

    public String getId() {
        if (id != null)
            return id;
        else if (path != null)
            return RegisterUtil.getIdAndPostfix(path)[0];
        else
            throw new IllegalStateException("Required id or path");
    }

    public String getPostfix() {
        return RegisterUtil.getIdAndPostfix(path)[1];
    }

    public void setCompiledMetadataClass(Class compiledMetadataClass) {
        this.compiledMetadataClass = compiledMetadataClass;
    }

    public void setSourceMetadataClass(Class sourceMetadataClass) {
        this.sourceMetadataClass = sourceMetadataClass;
    }

    public void setMetadataReader(NamespaceReader metadataReader) {
        this.metadataReader = metadataReader;
    }

    public void setOrigin(Origin origin) {
        this.origin = origin;
    }

    public Origin getOrigin() {
        return origin;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUri() {
        return path;
    }

    public static void setSourceTypes(SourceTypeRegister sourceTypes) {
        CompileInfo.sourceTypes = sourceTypes;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy