Skip to content

Commit d4b4eb2

Browse files
committed
Use adaptive serialization for util:log-*() functions
1 parent 12eaa06 commit d4b4eb2

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

src/org/exist/xquery/functions/util/LogFunction.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,16 @@
2727
import org.exist.dom.QName;
2828
import org.exist.source.StringSource;
2929
import org.exist.storage.serializers.Serializer;
30+
import org.exist.util.serializer.XQuerySerializer;
3031
import org.exist.xquery.*;
3132
import org.exist.xquery.value.*;
3233
import org.xml.sax.SAXException;
3334

35+
import javax.xml.transform.OutputKeys;
36+
import java.io.IOException;
37+
import java.io.StringWriter;
38+
import java.util.Properties;
39+
3440

3541
/**
3642
* @author Wolfgang Meier ([email protected])
@@ -84,6 +90,10 @@ public LogFunction(XQueryContext context, FunctionSignature signature) {
8490

8591
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
8692

93+
// Force adaptive serialization
94+
final Properties props = new Properties();
95+
props.setProperty(OutputKeys.METHOD, "adaptive");
96+
8797
SequenceIterator i;
8898

8999
final String calledAs = getName().getLocalPart();
@@ -137,17 +147,15 @@ public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathExce
137147
// Iterate over all input
138148
while (i.hasNext()) {
139149
final Item next = i.nextItem();
140-
if (Type.subTypeOf(next.getType(), Type.NODE)) {
141-
final Serializer serializer = context.getBroker().getSerializer();
142-
serializer.reset();
143-
try {
144-
buf.append(serializer.serialize((NodeValue) next));
145-
} catch (final SAXException e) {
146-
throw (new XPathException(this, "An exception occurred while serializing node to log: " + e.getMessage(), e));
147-
}
148-
} else {
149-
buf.append(next.getStringValue());
150+
try (StringWriter writer = new StringWriter()) {
151+
XQuerySerializer xqs = new XQuerySerializer(context.getBroker(), props, writer);
152+
xqs.serialize(next.toSequence());
153+
buf.append(writer.toString());
154+
155+
} catch (IOException | SAXException e) {
156+
throw new XPathException(this, e.getMessage());
150157
}
158+
151159
}
152160

153161
// Finally write the log

0 commit comments

Comments
 (0)