Java XML String Transform convertValidatorResult(Result result, StringWriter writer)

Here you can find the source of convertValidatorResult(Result result, StringWriter writer)

Description

Writes the content of the given Result into a StringWriter

License

Open Source License

Parameter

Parameter Description
result Result from SchematronValidator validation
writer StringWriter to write report to

Declaration

public static void convertValidatorResult(Result result, StringWriter writer) 

Method Source Code


//package com.java2s;
/*//from   w w w .  j ava 2  s .c  om
 * Copyright (c) 2016 wetransform GmbH
 * 
 * All rights reserved. This program and the accompanying materials are made
 * available under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution. If not, see <http://www.gnu.org/licenses/>.
 * 
 * Contributors:
 *     wetransform GmbH <http://www.wetransform.to>
 */

import java.io.ByteArrayOutputStream;
import java.io.StringWriter;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

public class Main {
    /**
     * Writes the content of the given {@link Result} into a
     * {@link StringWriter}
     * 
     * @param result {@link Result} from {@link SchematronValidator} validation
     * @param writer {@link StringWriter} to write report to
     */
    public static void convertValidatorResult(Result result, StringWriter writer) {
        if (result instanceof DOMResult) {
            convertResult((DOMResult) result, writer);
        } else if (result instanceof StreamResult) {
            convertResult((StreamResult) result, writer);
        } else {
            throw new RuntimeException(String.format("Could not evaluate Schematron validation result of type '%s'",
                    result.getClass().getCanonicalName()));
        }
    }

    private static void convertResult(StreamResult result, StringWriter writer) {
        ByteArrayOutputStream baos = (ByteArrayOutputStream) result.getOutputStream();
        writer.write(baos.toString());
    }

    private static void convertResult(DOMResult result, StringWriter writer)
            throws TransformerFactoryConfigurationError {
        TransformerFactory txfFactory = TransformerFactory.newInstance();
        Transformer transformer;
        try {
            transformer = txfFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            DOMSource resultSource = new DOMSource(result.getNode(), result.getSystemId());
            StreamResult printer = new StreamResult(writer);
            transformer.transform(resultSource, printer);
        } catch (TransformerException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
}

Related

  1. addNode(String nodeType, String idField, String nodeID, File destFile, ArrayList attributes)
  2. applyXSL(File xmlFile, File xslFile, String outputFilename)
  3. base64encode(String decodedString)
  4. convertPlist(File info_plist_file, String script_url, Map script_parameters)
  5. convertResult(StreamResult result, StringWriter writer)
  6. createElement(Node node, String name, String value, Map attributes)
  7. createFullReportLog(String sessionLogDir)
  8. createObject(String classname)
  9. createOrUpdateConnection(String fileName, String directory, String communityId, String serviceName, String serviceUrl, String defaultVersion, Logger log)