get Soap From File - Java javax.xml.soap

Java examples for javax.xml.soap:SOAPMessage

Description

get Soap From File

Demo Code


import java.io.*;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class Main{
    private static Map<String, String> fileCache = new HashMap<String, String>();
    public static String getSoapFromFile(String filenameXml,
            String filenameData, int row) throws IOException {
        return parsePlaceholders(filenameXml,
                CsvUtils.read(filenameData, row));
    }/*ww w .j  a  va  2 s .c om*/
    protected static String parsePlaceholders(String filename,
            Map<String, String> params) throws IOException {
        URL url = SoapUtils.class.getResource(filename);
        if (url == null) {
            throw new FileNotFoundException(filename);
        }

        String request;
        if (fileCache.get(url.getFile()) != null) {
            request = fileCache.get(url.getFile());
        } else {
            request = readFileAsString(SoapUtils.class
                    .getResourceAsStream(filename));
            fileCache.put(url.getFile(), request);
        }

        for (Map.Entry<String, String> entry : params.entrySet()) {
            request = request.replaceAll(placeholder(entry.getKey()), entry
                    .getValue().trim());
        }

        return request;
    }
    private static String readFileAsString(InputStream is)
            throws java.io.IOException {
        final char[] buffer = new char[0x1000];
        StringBuilder out = new StringBuilder();
        Reader in = new InputStreamReader(is, "UTF-8");
        int read;
        do {
            read = in.read(buffer, 0, buffer.length);
            if (read > 0) {
                out.append(buffer, 0, read);
            }
        } while (read >= 0);

        return out.toString();
    }
    private static String placeholder(String placeholder) {
        return new StringBuffer().append("\\$\\{").append(placeholder)
                .append("\\}").toString();
    }
}

Related Tutorials