Debian Codegraph @

net/sf/saxon/event/EventBuffer.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8
9 package net.sf.saxon.event;
10
11 import net.sf.saxon.expr.parser.Location;
12 import net.sf.saxon.om.Item;
13 import net.sf.saxon.om.NamespaceBindingSet;
14 import net.sf.saxon.om.NodeName;
15 import net.sf.saxon.trans.XPathException;
16 import net.sf.saxon.type.SchemaType;
17 import net.sf.saxon.type.SimpleType;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 /**
23 * An <tt>EventBuffer</tt> is a receiver of events that records the events in memory
24 * for subsequent replay. It is used, for example, in the implementation of try/catch,
25 * where events cannot be written directly to the final serializer in case an error
26 * occurs and is caught.
27 * <p>Note that events are retained along with their properties, so the class implements
28 * "sticky disable-output-escaping" - text nodes can have selected characters marked
29 * with the disable-escaping property.</p>
30 * @since 9.9
31 */
32
33 public class EventBuffer extends SequenceReceiver {
34
35 List<Event> buffer = new ArrayList<>();
36
37 public EventBuffer(PipelineConfiguration pipe){
38 super(pipe);
39 }
40
41 @Override
42 public void startDocument(int properties) throws XPathException {
43 buffer.add(new Event.StartDocument(properties));
44 }
45
46 @Override
47 public void endDocument() throws XPathException {
48 buffer.add(new Event.EndDocument());
49 }
50
51 @Override
52 public void startElement(NodeName elemName, SchemaType typeCode, Location location, int properties) throws XPathException {
53 buffer.add(new Event.StartElement(elemName, typeCode, location, properties));
54 }
55
56 @Override
57 public void namespace(NamespaceBindingSet namespaceBindings, int properties) throws XPathException {
58 buffer.add(new Event.Namespace(namespaceBindings, properties));
59 }
60
61 @Override
62 public void attribute(NodeName attName, SimpleType typeCode, CharSequence value, Location location, int properties) throws XPathException {
63 buffer.add(new Event.Attribute(attName, typeCode, location, value, properties));
64 }
65
66 @Override
67 public void startContent() throws XPathException {
68 buffer.add(new Event.StartContent());
69 }
70
71 @Override
72 public void endElement() throws XPathException {
73 buffer.add(new Event.EndElement());
74 }
75
76 @Override
77 public void characters(CharSequence chars, Location location, int properties) throws XPathException {
78 buffer.add(new Event.Text(chars, location, properties));
79 }
80
81 @Override
82 public void processingInstruction(String name, CharSequence data, Location location, int properties) throws XPathException {
83 buffer.add(new Event.ProcessingInstruction(name, data, location, properties));
84 }
85
86 @Override
87 public void comment(CharSequence content, Location location, int properties) throws XPathException {
88 buffer.add(new Event.Comment(content, location, properties));
89 }
90
91 @Override
92 public void append(Item item, Location location, int properties) throws XPathException {
93 buffer.add(new Event.Append(item, location, properties));
94 }
95
96 @Override
97 public void close() throws XPathException {
98 // no action
99 }
100
101 /**
102 * Replay the captured events to a supplied destination
103 * @param out the destination {@code Receiver} to receive the events
104 * @throws XPathException if any error occurs
105 */
106
107 public void replay(Receiver out) throws XPathException {
108 for (Event event : buffer) {
109 event.replay(out);
110 }
111 }
112 }
113