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

com.github.dreamhead.moco.parser.model.FileContainer Maven / Gradle / Ivy

Go to download

Moco is an easy setup stub framework, mainly focusing on testing and integration.

There is a newer version: 1.5.0
Show newest version
package com.github.dreamhead.moco.parser.model;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.github.dreamhead.moco.parser.deserializer.FileContainerDeserializer;
import com.github.dreamhead.moco.resource.ContentResource;
import com.google.common.base.MoreObjects;

import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Map;
import java.util.Optional;

import static java.util.Optional.empty;
import static java.util.Optional.of;

@JsonDeserialize(using = FileContainerDeserializer.class)
public final class FileContainer extends TextContainer {
    private TextContainer name;
    private Charset charset;
    private TextContainer content;

    private FileContainer(final TextContainer container) {
        this.name = container;
        this.content = container;
    }

    public TextContainer getName() {
        return name;
    }

    public Optional getCharset() {
        return Optional.ofNullable(charset);
    }

    @Override
    public ContentResource asResource() {
        if (this.content == null) {
            return null;
        }

        return this.content.asResource();
    }

    @Override
    public ContentResource asTemplateResource() {
        if (this.content == null) {
            return null;
        }

        return this.content.asTemplateResource();
    }

    @Override
    public ContentResource asTemplateResource(final String resourceName) {
        if (this.content == null) {
            return null;
        }

        return this.content.asTemplateResource(resourceName);
    }

    @Override
    public boolean isRawText() {
        return this.content != null && this.content.isRawText();

    }

    @Override
    public String getText() {
        if (this.content == null) {
            return null;
        }

        return this.content.getText();
    }

    @Override
    public String getOperation() {
        if (this.content == null) {
            return null;
        }

        return this.content.getOperation();
    }

    @Override
    public boolean hasProperties() {
        return this.content != null && this.content.hasProperties();
    }

    @Override
    public Map getProps() {
        if (this.content == null) {
            return null;
        }

        return this.content.getProps();
    }

    @Override
    public boolean isForTemplate() {
        return this.content != null && this.content.isForTemplate();
    }

    protected MoreObjects.ToStringHelper toStringHelper() {
        return super.toStringHelper()
                .add("name", name)
                .add("charset", charset);
    }

    @Override
    public boolean isFileContainer() {
        return true;
    }

    public static FileContainer asFileContainer(final TextContainer container) {
        return new FileContainer(container);
    }

    public static FileContainerBuilder aFileContainer() {
        return new FileContainerBuilder();
    }

    public static final class FileContainerBuilder {
        private TextContainer name;
        private String charset;

        public FileContainerBuilder withName(final TextContainer name) {
            this.name = name;
            return this;
        }

        public FileContainerBuilder withCharset(final String charset) {
            this.charset = charset;
            return this;
        }

        public FileContainer build() {
            FileContainer container = new FileContainer(name);
            container.charset = toCharset(charset).orElse(null);
            if (charset == null) {
                container.content = null;
            }
            return container;
        }

        private Optional toCharset(final String charset) {
            if (charset == null) {
                return empty();
            }

            try {
                return of(Charset.forName(charset));
            } catch (UnsupportedCharsetException e) {
                return empty();
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy