summaryrefslogtreecommitdiff
path: root/test/java/src/org/apache/qetest/xalanj2/XalanDumper.java
blob: 504602597b38c7781639c848c179e942d3edb230 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the  "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
 * $Id$
 */

package org.apache.qetest.xalanj2;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.util.Properties;

import javax.xml.transform.Transformer;

import org.apache.xalan.templates.ElemLiteralResult;
import org.apache.xalan.templates.ElemTemplate;
import org.apache.xalan.templates.ElemTemplateElement;
import org.apache.xalan.templates.ElemTextLiteral;
import org.apache.xalan.transformer.TransformerImpl;
import org.apache.xml.dtm.ref.DTMNodeProxy;
import org.apache.xpath.XPath;
import org.w3c.dom.Attr;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * Static utility for dumping info about common Xalan objects.
 * Cheap-o string representations of some common properties 
 * of various objects; supports some formatting and encapsulation 
 * but could use improvements.
 * Note: currently purposefully outputs plain strings, not 
 * any XML-like elements, so it's easier for other XML-like 
 * logging utilities to output our data without escaping, etc.
 * 
 * @author shane_curcuru@lotus.com
 * @version $Id$
 */
public abstract class XalanDumper 
{
    // abstract class cannot be instantiated

    /** Simple text constants: for items that are null.  */
    public static final String NULL = "NULL";
    /** Simple text constants: separator between items.  */
    public static final String SEP = ";";
    /** Simple text constants: beginning a block of items.  */
    public static final String LBRACKET = "[";
    /** Simple text constants: ending a block of items.  */
    public static final String RBRACKET = "]";
    /** Simple text constants: line number.  */
    public static final String LNUM = "L";
    /** Simple text constants: column number.  */
    public static final String CNUM = "C";

    /** Simple output formats: default behavior.  */
    public static final int DUMP_DEFAULT = 0;
    /** Simple output formats: verbose: extra output.  */
    public static final int DUMP_VERBOSE = 1;
    /** Simple output formats: a contained object.  */
    public static final int DUMP_CONTAINED = 2;
    /** Simple output formats: don't close block.  */
    public static final int DUMP_NOCLOSE = 4;
    /** Simple output formats: don't include id's or other items likely to change.  */
    public static final int DUMP_NOIDS = 8;

    /** Cheap-o recursion marker: already recursing in Nodes/NodeLists.  */
    public static final int DUMP_NODE_RECURSION = 16;

    /**
     * Return String describing an ElemTemplateElement.
     *
     * @param elem the ElemTemplateElement to print info of
     * @param dumpLevel what format/how much to dump
     */
    public static String dump(ElemTemplateElement elem, int dumpLevel)
    {
        StringBuffer buf = new StringBuffer("ElemTemplateElement" + LBRACKET);
        if (null == elem)
            return buf.toString() + NULL + RBRACKET;

        // Note for user if it's an LRE or an xsl element
        if(elem instanceof ElemLiteralResult)
            buf.append("LRE:");
        else
            buf.append("xsl:");

        buf.append(elem.getNodeName());
        buf.append(SEP + LNUM + elem.getLineNumber());
        buf.append(SEP + CNUM + elem.getColumnNumber());
        buf.append(SEP + "getLength=" + elem.getLength());
        if (DUMP_VERBOSE == (dumpLevel & DUMP_VERBOSE))
        {
            // Only include systemIds (which are long) if verbose
            buf.append(SEP + "getSystemId=" + elem.getSystemId());
            buf.append(SEP + "getStylesheet=" + elem.getStylesheet().getSystemId());
        }
        try
        {
            Class cl = ((Object)elem).getClass();
            Method getSelect = cl.getMethod("getSelect", null);
            if(null != getSelect)
            {
                buf.append(SEP + "select=");
                XPath xpath = (XPath)getSelect.invoke(elem, null);
                buf.append(xpath.getPatternString());
            }
        }
        catch(Exception e)
        {
            // no-op: just don't put in the select info for these items
        }
        if (DUMP_NOCLOSE == (dumpLevel & DUMP_NOCLOSE))
            return buf.toString();
        else
            return buf.toString() + RBRACKET;
    }


    /**
     * Return String describing an ElemTextLiteral.
     *
     * @param elem the ElemTextLiteral to print info of
     * @param dumpLevel what format/how much to dump
     */
    public static String dump(ElemTextLiteral elem, int dumpLevel)
    {
        StringBuffer buf = new StringBuffer("ElemTextLiteral" + LBRACKET);
        if (null == elem)
            return buf.toString() + NULL + RBRACKET;

        buf.append(elem.getNodeName()); // I don't think this ever changes from #Text?
        buf.append(SEP + LNUM + elem.getLineNumber());
        buf.append(SEP + CNUM + elem.getColumnNumber());

        String chars = new String(elem.getChars(), 0, elem.getChars().length);
        buf.append(SEP + "chars=" + chars.trim());

        if (DUMP_NOCLOSE == (dumpLevel & DUMP_NOCLOSE))
            return buf.toString();
        else
            return buf.toString() + RBRACKET;
    }

    /**
     * Return String describing an ElemTemplate.
     *
     * @param elem the ElemTemplate to print info of
     * @param dumpLevel what format/how much to dump
     */
    public static String dump(ElemTemplate elem, int dumpLevel)
    {
        StringBuffer buf = new StringBuffer("ElemTemplate" + LBRACKET);
        if (null == elem)
            return buf.toString() + NULL + RBRACKET;

        buf.append("xsl:" + elem.getNodeName());
        buf.append(SEP + LNUM + elem.getLineNumber());
        buf.append(SEP + CNUM + elem.getColumnNumber());
        if (DUMP_VERBOSE == (dumpLevel & DUMP_VERBOSE))
        {
            // Only include systemIds (which are long) if verbose
            buf.append(SEP + "getSystemId=" + elem.getSystemId());
            buf.append(SEP + "getStylesheet=" + elem.getStylesheet().getSystemId());
        }
        try
        {
            Class cl = ((Object)elem).getClass();
            Method getSelect = cl.getMethod("getSelect", null);
            if(null != getSelect)
            {
                buf.append(SEP + "select=");
                XPath xpath = (XPath)getSelect.invoke(elem, null);
                buf.append(xpath.getPatternString());
            }
        }
        catch(Exception e)
        {
            // no-op: just don't put in the select info for these items
        }
        if (null != elem.getMatch())
            buf.append(SEP + "match=" + elem.getMatch().getPatternString());

        if (null != elem.getName())
            buf.append(SEP + "name=" + elem.getName());

        if (null != elem.getMode())
            buf.append(SEP + "mode=" + elem.getMode());

        buf.append(SEP + "priority=" + elem.getPriority());

        if (DUMP_NOCLOSE == (dumpLevel & DUMP_NOCLOSE))
            return buf.toString();
        else
            return buf.toString() + RBRACKET;
    }


    /**
     * Return String describing a Transformer.
     * Currently just returns info about a get selected public 
     * getter methods from a Transformer.
     * Only really useful when it can do instanceof TransformerImpl 
     * to return custom info about Xalan
     *
     * @param t the Transformer to print info of
     * @param dumpLevel what format/how much to dump
     */
    public static String dump(Transformer trans, int dumpLevel)
    {
        if (null == trans)
            return "Transformer" + LBRACKET + NULL + RBRACKET;

        StringBuffer buf = new StringBuffer();

        StringWriter sw = new StringWriter();
        Properties p = trans.getOutputProperties();
        if (null != p)
        {
            p.list(new PrintWriter(sw));
            buf.append("getOutputProperties{" + sw.toString() + "}");
        }

        if (trans instanceof TransformerImpl)
        {
            final TransformerImpl timpl = (TransformerImpl)trans;
            // We have a Xalan-J 2.x basic transformer

            // Android-changed: TransformerImpl in 2.7.1 doesn't have getBaseURLOfSource() method.
            // buf.append("getBaseURLOfSource=" + timpl.getBaseURLOfSource() + SEP);
            // Result getOutputTarget()
            // ContentHandler getInputContentHandler(boolean doDocFrag)
            // DeclHandler getInputDeclHandler()
            // LexicalHandler getInputLexicalHandler()
            // OutputProperties getOutputFormat()
            // Serializer getSerializer()
            // ElemTemplateElement getCurrentElement()
            // int getCurrentNode()
            // ElemTemplate getCurrentTemplate()
            // ElemTemplate getMatchedTemplate()
            // int getMatchedNode()
            // DTMIterator getContextNodeList()
            // StylesheetRoot getStylesheet()
            // int getRecursionLimit()
            buf.append("getMode=" + timpl.getMode() + SEP);
        }

        return "Transformer" + LBRACKET 
            + buf.toString() + RBRACKET;
    }


    /**
     * Return String describing a Node.
     * Currently just returns TracerEvent.printNode(n)
     *
     * @param n the Node to print info of
     * @param dumpLevel what format/how much to dump
     */
    public static String dump(Node n, int dumpLevel)
    {
        if (null == n)
            return "Node" + LBRACKET + NULL + RBRACKET;

        // Copied but modified from TracerEvent; ditch hashCode
        StringBuffer buf = new StringBuffer();

        if (n instanceof Element)
        {
            buf.append(n.getNodeName());

            Node c = n.getFirstChild();

            while (null != c)
            {
                if (c instanceof Attr)
                {
                    buf.append(dump(c, dumpLevel | DUMP_NODE_RECURSION) + " ");
                }
                c = c.getNextSibling();
            }
        }
        else
        {
            if (n instanceof Attr)
            {
                buf.append(n.getNodeName() + "=" + n.getNodeValue());
            }
            else
            {
                buf.append(n.getNodeName());
            }
        }

            
        // If we're already recursing, don't bother printing out 'Node' again
        if (DUMP_NODE_RECURSION == (dumpLevel & DUMP_NODE_RECURSION))
            return LBRACKET + buf.toString() + RBRACKET;
        else
            return "Node" + LBRACKET + buf.toString() + RBRACKET;
    }

    /**
     * Return String describing a DTMNodeProxy.
     * This is the Xalan-J 2.x internal wrapper for Nodes.
     *
     * @param n the DTMNodeProxy to print info of
     * @param dumpLevel what format/how much to dump
     */
    public static String dump(DTMNodeProxy n, int dumpLevel)
    {
        if (null == n)
            return "DTMNodeProxy" + LBRACKET + NULL + RBRACKET;

        // Copied but modified from TracerEvent; ditch hashCode
        StringBuffer buf = new StringBuffer();

        if (DUMP_NOIDS != (dumpLevel & DUMP_NOIDS))
        {
            // Only include the DTM node number if asked
            buf.append(n.getDTMNodeNumber());
        }
        
        if (n instanceof Element)
        {
            buf.append(n.getNodeName());
            // Also output first x chars of value
            buf.append(substr(n.getNodeValue()));

            DTMNodeProxy c = (DTMNodeProxy)n.getFirstChild();

            while (null != c)
            {
                buf.append(dump(c, dumpLevel | DUMP_NODE_RECURSION) + " ");
                c = (DTMNodeProxy)c.getNextSibling();
            }
        }
        else
        {
            if (n instanceof Attr)
            {
                buf.append(n.getNodeName() + "=" + n.getNodeValue());
            }
            else
            {
                buf.append(n.getNodeName());
                // Also output first x chars of value
                buf.append(substr(n.getNodeValue()));
            }
        }

            
        // If we're already recursing, don't bother printing out 'Node' again
        if (DUMP_NODE_RECURSION == (dumpLevel & DUMP_NODE_RECURSION))
            return LBRACKET + buf.toString() + RBRACKET;
        else
            return "DTMNodeProxy" + LBRACKET + buf.toString() + RBRACKET;
    }

    /** Cheap-o worker method to substring a string.  */
    public static int MAX_SUBSTR = 8;

    /** Cheap-o worker method to substring a string.  */
    public static String SUBSTR_PREFIX = ":";

    /** Cheap-o worker method to substring a string.  */
    protected static String substr(String s)
    {
        if (null == s)
            return "";
        return SUBSTR_PREFIX + s.substring(0, Math.min(s.length(), MAX_SUBSTR));
    }

    /**
     * Return String describing a NodeList.
     * Currently just returns TracerEvent.printNode(n)
     *
     * @param nl the NodeList to print info of
     * @param dumpLevel what format/how much to dump
     */
    public static String dump(NodeList nl, int dumpLevel)
    {
        if (null == nl)
            return "NodeList" + LBRACKET + NULL + RBRACKET;

        StringBuffer buf = new StringBuffer();

        int len = nl.getLength() - 1;
        int i = 0;
        while (i < len)
        {
            Node n = nl.item(i);
            if (null != n)
            {
                buf.append(dump(n, dumpLevel) + ", ");
            }
            ++i;
        }

        if (i == len)
        {
            Node n = nl.item(len);
            if (null != n)
            {
                buf.append(dump(n, dumpLevel));
            }
        }
        return "NodeList" + LBRACKET 
            + buf.toString() + RBRACKET;
    }


    /**
     * Print String type of node.  
     * @param n Node to report type of 
     * @return String type name
     */
    public static String dumpNodeType(Node n)
    {
        if (null == n)
            return NULL;
        switch (n.getNodeType())
        {
        case Node.DOCUMENT_NODE :
            return "DOCUMENT_NODE";

        case Node.ELEMENT_NODE :
            return "ELEMENT_NODE";

        case Node.CDATA_SECTION_NODE :
            return "CDATA_SECTION_NODE";

        case Node.ENTITY_REFERENCE_NODE :
            return "ENTITY_REFERENCE_NODE";

        case Node.ATTRIBUTE_NODE :
            return "ATTRIBUTE_NODE";

        case Node.COMMENT_NODE :
            return "COMMENT_NODE";

        case Node.ENTITY_NODE :
            return "ENTITY_NODE";

        case Node.NOTATION_NODE :
            return "NOTATION_NODE";

        case Node.PROCESSING_INSTRUCTION_NODE :
            return "PROCESSING_INSTRUCTION_NODE";

        case Node.TEXT_NODE :
            return "TEXT_NODE";

        default :
            return "UNKNOWN_NODE";
        }
    }  // end of dumpNodeType()

}