Project

Profile

Help

Bug #4788 » SaxonThreads.java

Eryk Rzeźnik, 2020-10-08 11:42

 
1
import javax.xml.transform.Transformer;
2
import javax.xml.transform.stream.StreamResult;
3
import javax.xml.transform.stream.StreamSource;
4

    
5
import com.saxonica.config.EnterpriseTransformerFactory;
6
import net.sf.saxon.Configuration;
7
import net.sf.saxon.expr.XPathContext;
8
import net.sf.saxon.lib.ExtensionFunctionCall;
9
import net.sf.saxon.lib.ExtensionFunctionDefinition;
10
import net.sf.saxon.om.Sequence;
11
import net.sf.saxon.om.StructuredQName;
12
import net.sf.saxon.value.AtomicValue;
13
import net.sf.saxon.value.SequenceType;
14
import net.sf.saxon.value.StringValue;
15

    
16
public class SaxonThreads {
17

    
18
    public static void main(String[] args) throws Exception {
19
        for (int i = 0; i < 10; i++) {
20
            System.out.println("START " + i);
21
            try {
22
                test(i > 4);
23
            } catch (Exception e) {
24
                System.out.println(e.getMessage());
25
            }
26
            System.out.println("STOP " + i);
27
            Thread.sleep(15000);
28
        }
29
    }
30

    
31
    private static void test(boolean fail) throws Exception {
32
        EnterpriseTransformerFactory transformerFactory = new EnterpriseTransformerFactory();
33

    
34
        Configuration configuration = Configuration.newConfiguration();
35
        configuration.registerExtensionFunction(new SimpleExtension(fail));
36

    
37
        transformerFactory.setConfiguration(configuration);
38

    
39
        StreamSource xslt = new StreamSource("test.xslt");
40

    
41
        Transformer transformer = transformerFactory.newTransformer(xslt);
42

    
43
        StreamSource input = new StreamSource("input.xml");
44
        StreamResult output = new StreamResult("output.xml");
45

    
46
        transformer.transform(input, output);
47
    }
48

    
49
    static class SimpleExtension extends ExtensionFunctionDefinition {
50

    
51
        private final boolean fail;
52

    
53
        public SimpleExtension(boolean fail) {
54
            this.fail = fail;
55
        }
56

    
57
        @Override
58
        public StructuredQName getFunctionQName() {
59
            return StructuredQName.fromClarkName("{http://test.com/extension}test");
60
        }
61

    
62
        @Override
63
        public SequenceType[] getArgumentTypes() {
64
            return new SequenceType[] { SequenceType.SINGLE_INTEGER };
65
        }
66

    
67
        @Override
68
        public SequenceType getResultType(SequenceType[] sequenceTypes) {
69
            return SequenceType.SINGLE_STRING;
70
        }
71

    
72
        @Override
73
        public ExtensionFunctionCall makeCallExpression() {
74
            return new ExtensionFunctionCall() {
75

    
76
                @Override
77
                public Sequence call(XPathContext xPathContext, Sequence[] sequences) {
78
                    System.out.println(Thread.currentThread().getName());
79
                    try {
80
                        Thread.sleep(5000);
81
                    } catch (InterruptedException e) {
82
                        e.printStackTrace();
83
                    }
84

    
85
                    if (fail)
86
                        throw new RuntimeException("TEST");
87
                    else
88
                        return new StringValue(sequences.toString());
89
                }
90
            };
91
        }
92

    
93
    }
94

    
95
}
(2-2/4)