Project

Profile

Help

Support #5608 » Xslt30TransfoServiceImpl.java

fouad MOUTASSIM, 2022-07-22 15:37

 
package com.loreal.hip.fwk.common.services.impl;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.transform.stream.StreamSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.loreal.hip.fwk.common.config.EnvConfig;
import com.loreal.hip.fwk.common.services.Xslt30CacheService;
import com.loreal.hip.fwk.common.services.Xslt30TransfoService;
import com.loreal.hip.fwk.core.mapping.FwkMappingIn;
import com.loreal.hip.fwk.core.mapping.FwkMappingInOut;
import com.loreal.hip.fwk.core.processing.FwkMessage;

import lombok.extern.slf4j.Slf4j;
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.Serializer;
import net.sf.saxon.s9api.Xslt30Transformer;

@Slf4j
@Service
public class Xslt30TransfoServiceImpl implements Xslt30TransfoService {


private final Xslt30CacheService xslt30CacheService;
private final EnvConfig envConfig;

@Autowired
public Xslt30TransfoServiceImpl(final EnvConfig envConfig, final Xslt30CacheService xslt30CacheService) {
this.xslt30CacheService = xslt30CacheService;
this.envConfig = envConfig;
}

public FwkMappingInOut getFwkMappingOut(FwkMessage in, FwkMappingIn inFwkMappingIn, String mapName, String xsltFileName) throws SaxonApiException, FileNotFoundException, JAXBException {

long startTimeInit = System.nanoTime();
OutputStream outputStream = new ByteArrayOutputStream();

// Processor processor = new Processor(true);
// //todo uncomment these 3 lines
// processor.registerExtensionFunction(new ByteUtils().new GetSize());
// processor.registerExtensionFunction(new EncodeBase64Extension());
// //processor.registerExtensionFunction(new RandomExtension());
//
// log.debug("#### SAXON LICENSE LOADING .... ");
// processor.setConfigurationProperty(Feature.LICENSE_FILE_LOCATION,
// envConfig.getSaxonLicensePath());
// log.debug("#### SAXON LICENSE LOADING [END] .... ");
//
// // processor.setConfigurationProperty(Feature.TRACE_EXTERNAL_FUNCTIONS, true);
// // processor.setConfigurationProperty(Feature.GENERATE_BYTE_CODE,false);
//
// /** Load Configuration.class.getClassLoader() */
// Configuration configuration = processor.getUnderlyingConfiguration();
// configuration.getDynamicLoader().setClassLoader(
// Configuration.class.getClassLoader());


//get cached Processor
Processor processor = xslt30CacheService.getProcessor(envConfig.getSaxonLicensePath());

// log.debug("############[ JAVA_LOGS TRANSFORM XSLT BUILD TRANSFORMER - STARTING ] #################");
long transformerTime = System.nanoTime();
Xslt30Transformer transformer = xslt30CacheService.buildCacheable(envConfig, processor, xsltFileName);
// log.debug("############[ JAVA_LOGS TRANSFORM XSLT BUILD TRANSFORMER - ENDING {} ] #################", (System.nanoTime() - transformerTime));


//Serialize FwkMappingIn
JAXBContext jaxbContext = JAXBContext.newInstance(FwkMappingIn.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

StringWriter sw = new StringWriter();
marshaller.marshal(inFwkMappingIn, sw);
String fwkMappingIn = sw.toString();

Serializer out = processor.newSerializer(outputStream);

long startTimeTransform = System.nanoTime();
InputStream inputStream = new ByteArrayInputStream(fwkMappingIn.getBytes());
transformer.transform(new StreamSource(inputStream), out);
String result = outputStream.toString();
long transformExecutionTime = System.nanoTime() - startTimeTransform;
// log.debug("############[ JAVA_LOGS TRANSFORM EXECUTION XSLT DONE - transformExecutionTime : {} ns] #################", transformExecutionTime);
// log.debug("############# fwkMappingOut : {}", result);

//handle response
FwkMappingInOut fwkMappingOut = (FwkMappingInOut) JAXBContext.newInstance(FwkMappingInOut.class).createUnmarshaller().unmarshal(new StringReader(result));
//fwkMappingOut.getFwkMapping().getHeader().setExecutionTime(System.nanoTime() - startTimeInit + transformExecutionTime);
fwkMappingOut.getFwkMapping().getHeader().setExecutionTime(System.nanoTime() - startTimeInit);

//set mapping data to empty list to avoid NPE
if (fwkMappingOut.getFwkMapping().getMappingData() == null) {
log.warn("Mapping returned FwkMappingInOut with null mapping data");
fwkMappingOut.getFwkMapping().setMappingData(new ArrayList<>());
}

// log.debug("############ MAP COMPLETION '{}' --> {} ns #################", mapName, fwkMappingOut.getFwkMapping().getHeader().getExecutionTime());

return fwkMappingOut;
}

}
(5-5/5)