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

io.cloudslang.content.utils.ResourceLoader Maven / Gradle / Ivy

There is a newer version: 0.0.7
Show newest version
/*
 * (c) Copyright 2017 EntIT Software LLC, a Micro Focus company, L.P.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License v2.0 which accompany this distribution.
 *
 * The Apache License is available 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 io.cloudslang.content.utils;

import org.apache.commons.io.IOUtils;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;

/**
 * Created by giloan on 3/26/2016.
 */
public class ResourceLoader {

    /**
     * Loads the contents of a project resource file in a string.
     *
     * @param resourceFileName The name of the resource file.
     * @return A string value representing the entire content of the resource.
     * @throws IOException
     * @throws URISyntaxException
     */
    public static String loadAsString(String resourceFileName) throws IOException, URISyntaxException {
        try (InputStream is = ResourceLoader.class.getClassLoader().getResourceAsStream(resourceFileName)) {
            StringWriter stringWriter = new StringWriter();
            IOUtils.copy(is, stringWriter, StandardCharsets.UTF_8);
            return stringWriter.toString();
        }
    }

    public static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException {
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        builderFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
        builderFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        builderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        return builderFactory.newDocumentBuilder();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy