-
Notifications
You must be signed in to change notification settings - Fork 6
[bugfix] fn:transform() now logs xsl:message output #235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop-7.x.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,6 +53,7 @@ | |
| import net.sf.saxon.s9api.*; | ||
| import net.sf.saxon.serialize.SerializationProperties; | ||
| import net.sf.saxon.trans.UncheckedXPathException; | ||
| import org.apache.commons.io.output.StringBuilderWriter; | ||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
| import org.exist.dom.QName; | ||
|
|
@@ -162,6 +163,34 @@ public Sequence eval(final Sequence[] args, final Sequence contextSequence) thro | |
|
|
||
| final Xslt30Transformer xslt30Transformer = xsltExecutable.load30(); | ||
|
|
||
| xslt30Transformer.setMessageListener((content, terminate, locator) ->{ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please have this as a separate class at the bottom of the file rather than a large lambda here, e.g. private static class XsltMessageListener implements MessageListener {
...
} |
||
| try { | ||
| final StringBuilderWriter writer = new StringBuilderWriter(); | ||
| final Serializer serializer = context.getBroker().getBrokerPool().getSaxonProcessor().newSerializer(); | ||
| serializer.setOutputProperty(Serializer.Property.OMIT_XML_DECLARATION, "yes"); | ||
| serializer.setOutputWriter(writer); | ||
| serializer.serializeNode(content); | ||
|
|
||
| final String source; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Import the annotation from googlecode findbugs library please |
||
| final int sourceLine; | ||
| final int sourceColumn; | ||
| if (locator != null) { | ||
| source = locator.getSystemId(); | ||
| sourceLine = locator.getLineNumber(); | ||
| sourceColumn = locator.getColumnNumber(); | ||
| } else { | ||
| source = null; | ||
| sourceLine = -1; | ||
| sourceColumn = -1; | ||
| } | ||
|
|
||
| LOGGER.info("<xsl:message terminate=\"{}\" source=\"{}\" sourceLine=\"{}\" sourceColumn=\"{}\">{}</xsl:message>", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if |
||
| terminate, source, sourceLine, sourceColumn, writer.toString()); | ||
| } catch (final SaxonApiException e) { | ||
| LOGGER.error("Unable to serialize xsl:message content", e); | ||
| } | ||
| }); | ||
|
|
||
| options.initialMode.ifPresent(qNameValue -> xslt30Transformer.setInitialMode(Convert.ToSaxon.of(qNameValue.getQName()))); | ||
| xslt30Transformer.setInitialTemplateParameters(options.templateParams, false); | ||
| xslt30Transformer.setInitialTemplateParameters(options.tunnelParams, true); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,11 @@ | |
| package org.exist.xquery.functions.fn.transform; | ||
|
|
||
| import com.evolvedbinary.j8fu.tuple.Tuple2; | ||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.core.LogEvent; | ||
| import org.apache.logging.log4j.core.appender.AbstractAppender; | ||
| import org.apache.logging.log4j.core.config.Property; | ||
| import org.apache.logging.log4j.core.layout.PatternLayout; | ||
| import org.exist.EXistException; | ||
| import org.exist.collections.Collection; | ||
| import org.exist.security.PermissionDeniedException; | ||
|
|
@@ -49,7 +54,9 @@ | |
|
|
||
| import javax.xml.transform.Source; | ||
| import java.io.IOException; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.concurrent.CopyOnWriteArrayList; | ||
|
|
||
| import static com.evolvedbinary.j8fu.tuple.Tuple.Tuple; | ||
| import static org.junit.Assert.*; | ||
|
|
@@ -247,6 +254,46 @@ public void identityMixedMemoryAndPersistentDom() throws XPathException, Permiss | |
| expectQuery(IDENTITY_MIXED_XSLT_QUERY_5, expected); | ||
| } | ||
|
|
||
| @Test | ||
| public void xslMessageIsLogged() throws EXistException, PermissionDeniedException, IOException { | ||
| final CapturingAppender appender = new CapturingAppender(); | ||
| appender.start(); | ||
|
|
||
| final org.apache.logging.log4j.core.Logger transformLogger = | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fully qualified names hare are not needed |
||
| (org.apache.logging.log4j.core.Logger) LogManager.getLogger(Transform.class); | ||
| transformLogger.addAppender(appender); | ||
|
|
||
| try { | ||
| final String query = | ||
| "fn:transform(map {\n" + | ||
| " \"stylesheet-text\": '<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"3.0\">\n" + | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. version should be 2.0 not 3.0 |
||
| " <xsl:template match=\"/\">\n" + | ||
| " <xsl:message>Hello from XSLT</xsl:message>\n" + | ||
| " <out/>\n" + | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need this? |
||
| " </xsl:template>\n" + | ||
| " </xsl:stylesheet>',\n" + | ||
| " \"source-node\": document { <in/> }\n" + | ||
| "})?output"; | ||
|
|
||
| final BrokerPool pool = existEmbeddedServer.getBrokerPool(); | ||
| try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject())); | ||
| final XQueryUtil.QueryResult queryResult = XQueryUtil.query(broker, new StringSource(query), false, null, null, null, null, null)) { | ||
| assertNotNull(queryResult.result); | ||
| } catch (final XPathException e) { | ||
| fail("Transform should have succeeded: " + e.getMessage()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just throw the exception from tests |
||
| } | ||
|
|
||
| final Optional<String> logged = appender.getMessages().stream() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need a stream here. Please avoid streams where possible.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we don't need Optional. |
||
| .filter(message -> message.contains("<xsl:message")) | ||
| .findFirst(); | ||
|
|
||
| assertTrue("Expected an xsl:message log entry", logged.isPresent()); | ||
| assertTrue(logged.get().contains("Hello from XSLT")); | ||
| } finally { | ||
| transformLogger.removeAppender(appender); | ||
| } | ||
| } | ||
|
|
||
| private static void expectQuery(final String query, final Source expected) throws EXistException, XPathException, PermissionDeniedException, IOException { | ||
| final BrokerPool pool = existEmbeddedServer.getBrokerPool(); | ||
| try(final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject())); | ||
|
|
@@ -298,4 +345,26 @@ private static void createCollection(final DBBroker broker, final Txn transactio | |
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A minimal in-memory Log4j2 appender that just remembers every | ||
| * formatted message it receives, so a test can inspect what was logged. | ||
| */ | ||
| private static class CapturingAppender extends AbstractAppender { | ||
|
|
||
| private final List<String> messages = new CopyOnWriteArrayList<>(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should not be |
||
|
|
||
| CapturingAppender() { | ||
| super("capturing-appender", null, PatternLayout.createDefaultLayout(), false, Property.EMPTY_ARRAY); | ||
| } | ||
|
|
||
| @Override | ||
| public void append(final LogEvent event) { | ||
| messages.add(event.getMessage().getFormattedMessage()); | ||
| } | ||
|
|
||
| List<String> getMessages() { | ||
| return messages; | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just use
java.io.StringWriterhere please