Project

Profile

Help

Bug #4618 » Main.java

Norman Tovey-Walsh, 2020-06-28 19:06

 
1
package com.nwalsh.drivers;
2

    
3
import net.sf.saxon.event.PipelineConfiguration;
4
import net.sf.saxon.event.Receiver;
5
import net.sf.saxon.expr.parser.Loc;
6
import net.sf.saxon.om.EmptyAttributeMap;
7
import net.sf.saxon.om.FingerprintedQName;
8
import net.sf.saxon.s9api.Axis;
9
import net.sf.saxon.s9api.DocumentBuilder;
10
import net.sf.saxon.s9api.Location;
11
import net.sf.saxon.s9api.Processor;
12
import net.sf.saxon.s9api.SaxonApiException;
13
import net.sf.saxon.s9api.XdmDestination;
14
import net.sf.saxon.s9api.XdmNode;
15
import net.sf.saxon.s9api.XdmNodeKind;
16
import net.sf.saxon.s9api.XdmSequenceIterator;
17
import net.sf.saxon.serialize.SerializationProperties;
18
import net.sf.saxon.trans.XPathException;
19
import org.xml.sax.InputSource;
20

    
21
import javax.xml.transform.sax.SAXSource;
22
import java.io.ByteArrayInputStream;
23
import java.net.URI;
24
import java.nio.charset.StandardCharsets;
25

    
26
public class Main {
27
    public static void main(String[] argv) throws SaxonApiException {
28
        Processor processor = new Processor(false);
29
        String docString = "<doc><p1/>text<p2 xml:base='http://base.com/'><p4/></p2><p3><p4/></p3></doc>";
30
        DocumentBuilder builder = processor.newDocumentBuilder();
31
        builder.setBaseURI(URI.create("http://foo.com/"));
32
        XdmNode document = builder.build(new SAXSource(new InputSource(new ByteArrayInputStream(docString.getBytes(StandardCharsets.UTF_8)))));
33

    
34
        System.err.println("==============");
35
        System.err.println("document:");
36
        dump(document);
37

    
38
        XdmSequenceIterator<XdmNode> iter = document.axisIterator(Axis.CHILD);
39
        XdmNode doc = iter.next();
40
        iter = doc.axisIterator(Axis.CHILD);
41

    
42
        XdmNode newdoc = null;
43
        XdmDestination destination = new XdmDestination();
44
        PipelineConfiguration pipe = processor.getUnderlyingConfiguration().makePipelineConfiguration();
45
        Receiver receiver = destination.getReceiver(pipe,  new SerializationProperties());
46
        try {
47
            //receiver.setSystemId("http://receiver.com/");
48
            receiver.open();
49
            receiver.startDocument(0);
50

    
51
            FingerprintedQName wrapper = new FingerprintedQName("", "", "wrapper2");
52
            Location loc = new Loc("http://example.com/", -1, -1);
53
            receiver.startElement(wrapper, doc.getUnderlyingNode().getSchemaType(), EmptyAttributeMap.getInstance(),
54
                    doc.getUnderlyingNode().getAllNamespaces(), loc, 0);
55

    
56
            loc = new Loc("http://bar.com/", -1, -1);
57
            while (iter.hasNext()) {
58
                XdmNode node = iter.next();
59
                if (node.getNodeKind() == XdmNodeKind.ELEMENT) {
60
                    receiver.append(node.getUnderlyingNode(), loc, 0);
61
                } else {
62
                    receiver.append(node.getUnderlyingNode(), loc, 0);
63
                }
64
            }
65

    
66
            receiver.endElement();
67
            receiver.endDocument();
68
            receiver.close();
69
            newdoc = destination.getXdmNode();
70
        } catch (XPathException e) {
71
            throw new RuntimeException(e);
72
        }
73

    
74
        System.err.println("==============");
75
        System.err.println("newdoc:");
76
        dump(newdoc);
77
    }
78

    
79
    private static void dump(XdmNode node) {
80
        switch (node.getNodeKind()) {
81
            case ELEMENT:
82
                System.err.println("E:" + node.getNodeName() + ": " + node.getBaseURI());
83
                break;
84
            case TEXT:
85
                System.err.println("T:" + node.getStringValue() + ": " + node.getBaseURI());
86
                break;
87
            default:
88
                break;
89
        }
90

    
91
        XdmSequenceIterator<XdmNode> iter = node.axisIterator(Axis.CHILD);
92
        while (iter.hasNext()) {
93
            XdmNode child = iter.next();
94
            dump(child);
95
        }
96
    }
97
}
(2-2/3)