Java XML Transform transformLogViaXSLT(String logFilePath, String xslFilePath)

Here you can find the source of transformLogViaXSLT(String logFilePath, String xslFilePath)

Description

transform Log Via XSLT

License

Apache License

Declaration

public static String transformLogViaXSLT(String logFilePath, String xslFilePath)
            throws TransformerConfigurationException, TransformerException, IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2013 Khepry Software/*  ww  w  . j a  v  a2s  .c o m*/
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License 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.
 ******************************************************************************/

import java.io.ByteArrayOutputStream;
import java.io.File;

import java.io.IOException;

import java.nio.file.Files;
import java.nio.file.Paths;

import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class Main {
    public static String transformLogViaXSLT(String logFilePath, String xslFilePath)
            throws TransformerConfigurationException, TransformerException, IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        File logFile = new File(logFilePath);
        if (logFile.exists()) {
            File xslFile = new File(xslFilePath);
            if (xslFile.exists()) {
                TransformerFactory factory = TransformerFactory.newInstance();
                Source xslt = new StreamSource(new File(xslFilePath));
                Transformer transformer = factory.newTransformer(xslt);
                Source logXmlText = new StreamSource(new File(logFilePath));
                transformer.transform(logXmlText, new StreamResult(baos));
                return baos.toString();
            } else {
                return new String(Files.readAllBytes(Paths.get(logFilePath)));
            }
        } else {
            return baos.toString();
        }
    }
}

Related

  1. transformar(String xmlOrigen, String xslOrigen)
  2. transformer()
  3. transformerExceptionMsg(TransformerException ex, Node contextNode, String str)
  4. transformerFactory()
  5. transformFile(Source stylesheetFile, File input, File output)
  6. transformNonTextNodeToOutputStream(Node node, OutputStream os, boolean omitXmlDeclaration)
  7. transformStringToDoc(String input)
  8. transformStringToXMLGregorianCalendar(String dateTime, String pattern)
  9. transformToHTML(StreamSource xmlStreamSource)