|
27 | 27 | import org.exist.dom.QName;
|
28 | 28 | import org.exist.source.StringSource;
|
29 | 29 | import org.exist.storage.serializers.Serializer;
|
| 30 | +import org.exist.util.serializer.XQuerySerializer; |
30 | 31 | import org.exist.xquery.*;
|
31 | 32 | import org.exist.xquery.value.*;
|
32 | 33 | import org.xml.sax.SAXException;
|
33 | 34 |
|
| 35 | +import javax.xml.transform.OutputKeys; |
| 36 | +import java.io.IOException; |
| 37 | +import java.io.StringWriter; |
| 38 | +import java.util.Properties; |
| 39 | + |
34 | 40 |
|
35 | 41 | /**
|
36 | 42 | * @author Wolfgang Meier ([email protected])
|
@@ -84,6 +90,10 @@ public LogFunction(XQueryContext context, FunctionSignature signature) {
|
84 | 90 |
|
85 | 91 | public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
|
86 | 92 |
|
| 93 | + // Force adaptive serialization |
| 94 | + final Properties props = new Properties(); |
| 95 | + props.setProperty(OutputKeys.METHOD, "adaptive"); |
| 96 | + |
87 | 97 | SequenceIterator i;
|
88 | 98 |
|
89 | 99 | final String calledAs = getName().getLocalPart();
|
@@ -137,17 +147,15 @@ public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathExce
|
137 | 147 | // Iterate over all input
|
138 | 148 | while (i.hasNext()) {
|
139 | 149 | 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()); |
150 | 157 | }
|
| 158 | + |
151 | 159 | }
|
152 | 160 |
|
153 | 161 | // Finally write the log
|
|
0 commit comments