Java XML String Transform getStringFromStreamSource(StreamSource src)

Here you can find the source of getStringFromStreamSource(StreamSource src)

Description

Convert the input from the provided Reader into a String .

License

Open Source License

Parameter

Parameter Description
inputStream the Reader to copy from.

Exception

Parameter Description
IOException in case something bad happens.

Return

a that contains the content of the provided .

Declaration

public static String getStringFromStreamSource(StreamSource src)
        throws IOException 

Method Source Code

//package com.java2s;
/* (c) 2015 Open Source Geospatial Foundation - all rights reserved
 * (c) 2001 - 2015 OpenPlans//w w  w  . j  a v a  2 s  .c om
 * This code is licensed under the GPL 2.0 license, available at the root
 * application directory.
 */

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.io.Reader;

import javax.xml.transform.stream.StreamSource;

public class Main {
    /**
     * Convert the input from the provided {@link Reader} into a {@link String}.
     * 
     * @param inputStream the {@link Reader} to copy from.
     * @return a {@link String} that contains the content of the provided {@link Reader}.
     * @throws IOException in case something bad happens.
     */
    public static String getStringFromStreamSource(StreamSource src)
            throws IOException {

        inputNotNull(src);
        InputStream inputStream = src.getInputStream();
        if (inputStream != null) {
            return getStringFromStream(inputStream);
        } else {

            final Reader r = src.getReader();
            return getStringFromReader(r);
        }
    }

    /**
     * Checks if the input is not null.
     * @param oList list of elements to check for null.
     */
    private static void inputNotNull(Object... oList) {
        for (Object o : oList)
            if (o == null)
                throw new NullPointerException(
                        "Input objects cannot be null");

    }

    /**
     * Convert the input from the provided {@link InputStream} into a {@link String}.
     * 
     * @param inputStream the {@link InputStream} to copy from.
     * @return a {@link String} that contains the content of the provided {@link InputStream}.
     * @throws IOException in case something bad happens.
     */
    public static String getStringFromStream(InputStream inputStream)
            throws IOException {
        inputNotNull(inputStream);
        final Reader inReq = new InputStreamReader(inputStream);
        return getStringFromReader(inReq);
    }

    /**
     * Convert the input from the provided {@link Reader} into a {@link String}.
     * 
     * @param inputStream the {@link Reader} to copy from.
     * @return a {@link String} that contains the content of the provided {@link Reader}.
     * @throws IOException in case something bad happens.
     */
    public static String getStringFromReader(final Reader inputReader)
            throws IOException {
        inputNotNull(inputReader);
        final StringBuilder sb = new StringBuilder();
        final char[] buffer = new char[1024];
        int len;
        while ((len = inputReader.read(buffer)) >= 0) {
            char[] read = new char[len];
            System.arraycopy(buffer, 0, read, 0, len);
            sb.append(read);
        }
        return sb.toString();
    }
}

Related

  1. getSchema(String schemaString)
  2. getSchemaFromResource(String... files)
  3. getSourceAsString(Source source)
  4. getStreamResultForFile(String filename)
  5. getString(Node node, boolean indent)
  6. getTemplates(String name)
  7. getValidator(String path)
  8. getXmlStringFromSource(Source payload)
  9. identity(String xml)