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

com.crabshue.commons.xml.inputsource.InputSourceBuilder Maven / Gradle / Ivy

package com.crabshue.commons.xml.inputsource;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Collection;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;

import com.crabshue.commons.exceptions.SystemException;
import com.crabshue.commons.file.FileIOUtils;
import com.crabshue.commons.xml.exceptions.XmlErrorType;
import com.crabshue.commons.xml.node.NodeConverter;
import lombok.NonNull;

/**
 * Utility class for {@link InputSource}.
 */
public class InputSourceBuilder {

    /**
     * Build a new {@link InputSource} from a byte array.
     *
     * @param content the byte array
     * @return the input source
     */
    public static InputSource newInputSource(@NonNull final byte[] content) {

        try (InputStream is = new ByteArrayInputStream(content)) {
            return new InputSource(is);
        } catch (IOException e) {
            throw new SystemException(XmlErrorType.ERROR_BUILDING_INPUT_SOURCE, e);
        }
    }

    /**
     * Build a new {@link InputSource} from a {@link File}.
     *
     * @param file the file
     * @return the input source
     */
    public static InputSource newInputSource(@NonNull final File file) {

        try (InputStream is = FileIOUtils.openInputStream(file)) {
            final InputSource ret = new InputSource(is);
            ret.setSystemId(file.getAbsolutePath());
            ret.setPublicId(file.getAbsolutePath());
            return ret;
        } catch (IOException e) {
            throw new SystemException(XmlErrorType.ERROR_BUILDING_INPUT_SOURCE, e);
        }
    }

    /**
     * Build a new {@link InputSource} from a {@link String}.
     *
     * @param content the content as {@link String}
     * @return the input source
     */
    public static InputSource newInputSource(@NonNull final String content) {

        try (InputStream is = IOUtils.toInputStream(content, StandardCharsets.UTF_8)) {
            return new InputSource(is);
        } catch (IOException e) {
            throw new SystemException(XmlErrorType.ERROR_BUILDING_INPUT_SOURCE, e);
        }
    }

    /**
     * Build a new {@link InputSource} from a collection of{@link String}.
     *
     * @param content the content as a collection {@link String}
     * @return the input source
     */
    public static InputSource newInputSource(@NonNull final Collection content) {

        try (InputStream is = IOUtils.toInputStream(StringUtils.join(content, " "), StandardCharsets.UTF_8)) {
            return new InputSource(is);
        } catch (IOException e) {
            throw new SystemException(XmlErrorType.ERROR_BUILDING_INPUT_SOURCE, e);
        }
    }

    /**
     * Build a new {@link InputSource} from a {@link Node}.
     *
     * @param content the content as a {@link Node}
     * @return the input source
     */
    public static InputSource newInputSource(@NonNull final Node content) {

        try (InputStream is = NodeConverter.toInputStream(content)) {
            return new InputSource(is);
        } catch (IOException | SystemException e) {
            throw new SystemException(XmlErrorType.ERROR_BUILDING_INPUT_SOURCE, e);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy