Skip to content

Commit

Permalink
Post-processing works
Browse files Browse the repository at this point in the history
  • Loading branch information
utas-raymondng committed Oct 25, 2024
1 parent dfc516c commit 1e5fa29
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import org.aspectj.lang.annotation.Pointcut;
import org.fao.geonet.api.records.formatters.FormatterParams;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.lang.reflect.Method;
Expand All @@ -31,13 +29,16 @@
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

/**
* This class is used to add AspectJ support to the geonetwork. Mainly override or intercept function call
* that is not possible without alter the code of geonetwork.
*/
@Aspect
@Configuration
@EnableAspectJAutoProxy
public class AspectConfig {

protected Logger logger = LoggerFactory.getLogger(AspectConfig.class);
protected TransformerFactory factory = TransformerFactory.newInstance();

@Pointcut("execution(public * org.fao.geonet.api.records.formatters.XsltFormatter.format(org.fao.geonet.api.records.formatters.FormatterParams))")
public void interceptFormatter() {}
Expand Down Expand Up @@ -73,7 +74,14 @@ public ResponseEntity<?> aroundAdvice(ProceedingJoinPoint joinPoint) throws Thro
return (ResponseEntity<?>)joinPoint.proceed();
}
}

/**
* Use to intercept the formatter to provide a post-processing transformation. That is after the original transform
* you got a chance to do additional transformation based on the output content. By doing so you just need to
* add a post-processing.xsl in the same folder, and it will work without duplicate or create new view.xsl
* @param joinPoint - Aspect call param for @Around
* @return - A post-processed string
* @throws Throwable - Not expect to have this throw
*/
@Around("interceptFormatter()")
public Object afterProcessingXslt(ProceedingJoinPoint joinPoint) throws Throwable {
if(joinPoint.getArgs()[0] instanceof FormatterParams) {
Expand All @@ -82,25 +90,31 @@ public Object afterProcessingXslt(ProceedingJoinPoint joinPoint) throws Throwabl
// Expect a string of html after processing
Object value = joinPoint.proceed();

Path p = Paths.get(params.formatDir + "post-processing.xsl");
Path p = Paths.get(params.formatDir + "/post-processing.xsl");
if(Files.exists(p)) {
try(InputStream inputStream = new FileInputStream(p.toFile())) {
Source xslt = new StreamSource(inputStream);
try {
Source xslt = new StreamSource(p.toFile());
// Input source (XHTML), but need to remove the extra tag as this is not valid during transformation
Source text = new StreamSource(
new StringReader(value.toString().replaceAll("<!DOCTYPE[^>]*>", ""))
);

TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(xslt);

// Set the parameter value
transformer.setParameter("xml", params.metadataInfo.asXml());

// Input source (XHTML)
Source text = new StreamSource(new StringReader(value.toString()));

// Output destination
StringWriter outputWriter = new StringWriter();
StreamResult result = new StreamResult(outputWriter);

// Perform the transformation
transformer.transform(text, result);
return result.toString();
transformer.transform(text, new StreamResult(outputWriter));
// Add back the tag
return "<!DOCTYPE div\n SYSTEM \"html\">" + outputWriter;
}
catch(Exception e) {
logger.error("Error in post-processing", e);
}
}
return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
Expand Down Expand Up @@ -72,6 +70,12 @@ public class Config {
@Autowired
protected XsltFormatter aspectJXsltFormatter;

/**
* This is used to swap a bean in the real context that operated which is not current parent context!
*
* @param beanName - The name of the bean you want to swap
* @param bean - Replace by this bean
*/
public <T> void swapBean(String beanName, T bean) {
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) ApplicationContextHolder.get().getBeanFactory();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>
<xsl:param name='xml' select=''/>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output method="html" indent="yes"/>
<!-- Identity template to copy all content, important to preserve the html tags -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- Template to match the <section class="abc"> -->
<xsl:template match="//section[@class='gn-md-side-providedby']">
<section class="gn-md-side-providedby-custom">
<section class="gn-md-side-providedby">
<h2>
<i class="fa fa-fw fa-cog"></i>
<span>Custom Provided By Section</span>
</h2>
<img class="gn-source-logo"
alt="Custom Logo"
src="custom-logo.png" />
src="custom-logo.png"></img>
</section>
</xsl:template>
</xsl:stylesheet>

0 comments on commit 1e5fa29

Please sign in to comment.