Project

Profile

Help

Support #5608 » Xslt30CacheServiceImpl.java

fouad MOUTASSIM, 2022-07-22 15:37

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

import com.loreal.hip.fwk.common.config.EnvConfig;
import com.loreal.hip.fwk.common.services.Xslt30CacheService;
import com.loreal.hip.fwk.processing.services.extension.ByteUtils;
import com.loreal.hip.fwk.processing.services.extension.EncodeBase64Extension;
import lombok.extern.slf4j.Slf4j;
import net.sf.saxon.Configuration;
import net.sf.saxon.lib.Feature;
import net.sf.saxon.s9api.*;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.xml.transform.stream.StreamSource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

@Slf4j
@Service
public class Xslt30CacheServiceImpl implements Xslt30CacheService {
//private CacheManager cacheManager;

@Cacheable(value = "xslt30Cache", key = "#xsltFileName")
public Xslt30Transformer buildCacheable(EnvConfig envConfig, Processor processor, String xsltFileName) throws SaxonApiException, FileNotFoundException {

XsltCompiler compiler = processor.newXsltCompiler();

/** Import CSV Package to the compiler */
File csvFile = new File(envConfig.getCsvXslPackagePath());
XsltPackage csvPackage = compiler.compilePackage(new StreamSource(new FileInputStream(csvFile)));
compiler.importPackage(csvPackage);

compiler.setGenerateByteCode(false);
File xsltFile = new File(xsltFileName);
if (!xsltFile.exists()) {
throw new FileNotFoundException(xsltFileName + " does not exist");
}
return compiler.compile(new StreamSource(xsltFile)).load30();
}


@Cacheable(value = "saxonLicenseProcessorCache", key = "#saxonlicensePath")
public Processor getProcessor(String saxonlicensePath) {
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());

processor.setConfigurationProperty(Feature.LICENSE_FILE_LOCATION, saxonlicensePath);
// 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());
return processor;
}


@CacheEvict(value = "xslt30Cache", key = "#key")
public void removeFromCache(String key , String mapName) {
log.info("The MAP " + mapName + " was successfully removed from cache ");
}




}
(4-4/5)