Project

Profile

Help

Bug #4618 » Main.java

Norman Tovey-Walsh, 2020-06-27 16:52

 
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.om.EmptyAttributeMap;
6
import net.sf.saxon.om.FingerprintedQName;
7
import net.sf.saxon.s9api.Axis;
8
import net.sf.saxon.s9api.DocumentBuilder;
9
import net.sf.saxon.s9api.Processor;
10
import net.sf.saxon.s9api.SaxonApiException;
11
import net.sf.saxon.s9api.XdmDestination;
12
import net.sf.saxon.s9api.XdmNode;
13
import net.sf.saxon.s9api.XdmNodeKind;
14
import net.sf.saxon.s9api.XdmSequenceIterator;
15
import net.sf.saxon.serialize.SerializationProperties;
16
import net.sf.saxon.trans.XPathException;
17
import org.xml.sax.InputSource;
18

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

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

    
32
        XdmSequenceIterator<XdmNode> iter = document.axisIterator(Axis.CHILD);
33
        XdmNode doc = iter.next();
34

    
35
        System.err.println("Original wrapper: " + doc.getBaseURI());
36
        iter = doc.axisIterator(Axis.CHILD);
37
        while (iter.hasNext()) {
38
            XdmNode child = iter.next();
39
            if (child.getNodeKind() == XdmNodeKind.ELEMENT) {
40
                System.err.println("Original E:" + child.getNodeName() + ": " + child.getBaseURI());
41
            } else {
42
                System.err.println("Original T:" + child.getStringValue() + ": " + child.getBaseURI());
43
            }
44
        }
45

    
46
        iter = doc.axisIterator(Axis.CHILD);
47
        XdmNode newdoc = null;
48
        XdmDestination destination = new XdmDestination();
49
        //destination.setBaseURI(URI.create("http://destination.com/"));
50
        PipelineConfiguration pipe = processor.getUnderlyingConfiguration().makePipelineConfiguration();
51
        Receiver receiver = destination.getReceiver(pipe,  new SerializationProperties());
52
        try {
53
            receiver.open();
54
            receiver.startDocument(0);
55

    
56
            FingerprintedQName wrapper = new FingerprintedQName("", "", "wrapper");
57
            receiver.setSystemId("http://example.com/");
58
            receiver.startElement(wrapper, doc.getUnderlyingNode().getSchemaType(), EmptyAttributeMap.getInstance(),
59
                    doc.getUnderlyingNode().getAllNamespaces(), doc.getUnderlyingNode().saveLocation(), 0);
60

    
61
            while (iter.hasNext()) {
62
                XdmNode node = iter.next();
63
                if (node.getNodeKind() == XdmNodeKind.ELEMENT) {
64
                    receiver.setSystemId("http://bar.com/");
65
                    receiver.append(node.getUnderlyingNode());
66
                } else {
67
                    receiver.append(node.getUnderlyingNode());
68
                }
69
            }
70

    
71
            receiver.endElement();
72
            receiver.endDocument();
73
            receiver.close();
74
            newdoc = destination.getXdmNode();
75
        } catch (XPathException e) {
76
            throw new RuntimeException(e);
77
        }
78

    
79
        iter = newdoc.axisIterator(Axis.CHILD);
80
        doc = iter.next();
81

    
82
        System.err.println("Inserted wrapper: " + doc.getBaseURI());
83
        iter = doc.axisIterator(Axis.CHILD);
84
        while (iter.hasNext()) {
85
            XdmNode child = iter.next();
86
            if (child.getNodeKind() == XdmNodeKind.ELEMENT) {
87
                System.err.println("Inserted E:" + child.getNodeName() + ": " + child.getBaseURI());
88
            } else {
89
                System.err.println("Inserted T:" + child.getStringValue() + ": " + child.getBaseURI());
90
            }
91
        }
92
    }
93
}
(1-1/3)