21
21
*/
22
22
package org .exist .xquery .functions .fn ;
23
23
24
- import java .io .StringWriter ;
25
- import java .io .Writer ;
26
24
import org .exist .dom .QName ;
27
- import org .exist .storage .serializers .Serializer ;
28
- import org .exist .xquery .BasicFunction ;
29
- import org .exist .xquery .Cardinality ;
30
- import org .exist .xquery .Function ;
31
- import org .exist .xquery .FunctionSignature ;
32
- import org .exist .xquery .XPathException ;
33
- import org .exist .xquery .XQueryContext ;
34
- import org .exist .xquery .value .FunctionParameterSequenceType ;
35
- import org .exist .xquery .value .FunctionReturnSequenceType ;
36
- import org .exist .xquery .value .Item ;
37
- import org .exist .xquery .value .NodeValue ;
38
- import org .exist .xquery .value .Sequence ;
39
- import org .exist .xquery .value .SequenceIterator ;
40
- import org .exist .xquery .value .SequenceType ;
41
- import org .exist .xquery .value .Type ;
42
- import org .exist .xquery .value .ValueSequence ;
25
+ import org .exist .util .serializer .XQuerySerializer ;
26
+ import org .exist .xquery .*;
27
+ import org .exist .xquery .value .*;
43
28
import org .xml .sax .SAXException ;
44
29
30
+ import javax .xml .transform .OutputKeys ;
31
+ import java .io .IOException ;
32
+ import java .io .StringWriter ;
33
+ import java .util .Properties ;
34
+
45
35
/**
46
36
* @author Dannes Wessels
47
37
* @author Wolfgang Meier ([email protected] )
@@ -72,18 +62,12 @@ public FunTrace(XQueryContext context) {
72
62
* @see org.exist.xquery.BasicFunction#eval(Sequence[], Sequence)
73
63
*/
74
64
public Sequence eval (Sequence [] args , Sequence contextSequence ) throws XPathException {
75
-
76
- // TODO Add TRACE log statements using log4j
77
- // TODO Figure out why XQTS still does not work
78
- // TODO Remove unneeded comments
79
-
65
+
80
66
String label = args [1 ].getStringValue ();
81
67
if (label ==null ){
82
68
label ="" ;
83
69
}
84
-
85
- final Serializer serializer = context .getBroker ().getSerializer ();
86
-
70
+
87
71
Sequence result ;
88
72
89
73
if (args [0 ].isEmpty ()){
@@ -95,38 +79,31 @@ public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathExce
95
79
96
80
int position = 0 ;
97
81
82
+ // Force adaptive serialization
83
+ final Properties props = new Properties ();
84
+ props .setProperty (OutputKeys .METHOD , "adaptive" );
85
+
98
86
for (final SequenceIterator i = args [0 ].iterate (); i .hasNext ();) {
99
87
100
88
// Get item
101
89
final Item next = i .nextItem ();
102
90
103
- // Only write if debug mode
91
+ // Only write if logger is set to debug mode
104
92
if (LOG .isDebugEnabled ()) {
105
-
106
- String value = null ;
107
- position ++;
108
93
109
- final int type = next .getType ();
110
-
111
- // Serialize an element type
112
- if (Type .ELEMENT == type ) {
113
- final Writer sw = new StringWriter ();
114
- try {
115
- serializer .serialize ((NodeValue ) next , sw );
94
+ position ++;
116
95
117
- } catch (final SAXException ex ) {
118
- LOG .error (ex .getMessage ());
119
- }
120
- value = sw .toString ();
96
+ try (StringWriter writer = new StringWriter ()) {
97
+ XQuerySerializer xqs = new XQuerySerializer (context .getBroker (), props , writer );
98
+ xqs .serialize (next .toSequence ());
121
99
122
- // Get string value for other types
123
- } else {
124
- value = next .getStringValue ();
100
+ // Write to log
101
+ LOG .debug ("{} [{}] [{}]: {}" , label , position , Type .getTypeName (next .getType ()), writer .toString ());
125
102
103
+ } catch (IOException | SAXException e ) {
104
+ throw new XPathException (this , e .getMessage ());
126
105
}
127
-
128
- // Write to log
129
- LOG .debug (label + " [" + position + "]: " + Type .getTypeName (type ) + ": " + value );
106
+
130
107
}
131
108
132
109
// Add to result
0 commit comments