Debian Codegraph @

net/sf/saxon/event/SignificantItemDetector.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 package net.sf.saxon.event;
9
10 import net.sf.saxon.expr.parser.Location;
11 import net.sf.saxon.ma.arrays.ArrayItem;
12 import net.sf.saxon.om.*;
13 import net.sf.saxon.trans.XPathException;
14 import net.sf.saxon.type.SchemaType;
15 import net.sf.saxon.type.SimpleType;
16 import net.sf.saxon.type.Type;
17 import net.sf.saxon.value.AtomicValue;
18
19 /**
20 * This receiver is inserted into the output pipeline whenever on-empty or on-non-empty is used (XSLT 3.0).
21 * It passes all events to the underlying receiver unchanged, but invokes a callback action when the
22 * first item is written.
23 */
24 public class SignificantItemDetector extends ProxyReceiver {
25
26 private int level = 0;
27 private boolean empty = true;
28 private Action trigger;
29
30 public SignificantItemDetector(Receiver next, Action trigger) {
31 super(next);
32 this.trigger = trigger;
33 }
34
35 private void start() throws XPathException {
36 if (/*level==0 && */empty) {
37 trigger.doAction();
38 empty = false;
39 }
40 }
41
42 /**
43 * Start of a document node.
44 */
45 @Override
46 public void startDocument(int properties) throws XPathException {
47 if (level++ != 0) {
48 super.startDocument(properties);
49 }
50 }
51
52 @Override
53 public void startElement(NodeName elemName, SchemaType typeCode, Location location, int properties) throws XPathException {
54 start();
55 level++;
56 super.startElement(elemName, typeCode, location, properties);
57 }
58
59 @Override
60 public void namespace(NamespaceBindingSet namespaceBindings, int properties) throws XPathException {
61 start();
62 super.namespace(namespaceBindings, properties);
63 }
64
65 @Override
66 public void attribute(NodeName nameCode, SimpleType typeCode, CharSequence value, Location locationId, int properties) throws XPathException {
67 start();
68 super.attribute(nameCode, typeCode, value, locationId, properties);
69 }
70
71 @Override
72 public void characters(CharSequence chars, Location locationId, int properties) throws XPathException {
73 if (chars.length() > 0) {
74 start();
75 }
76 super.characters(chars, locationId, properties);
77 }
78
79 @Override
80 public void processingInstruction(String target, CharSequence data, Location locationId, int properties) throws XPathException {
81 start();
82 super.processingInstruction(target, data, locationId, properties);
83 }
84
85 @Override
86 public void comment(CharSequence chars, Location locationId, int properties) throws XPathException {
87 start();
88 super.comment(chars, locationId, properties);
89 }
90
91 public static boolean isSignificant(Item item) {
92 if (item instanceof NodeInfo) {
93 NodeInfo node = (NodeInfo) item;
94 return (node.getNodeKind() != Type.TEXT || !node.getStringValue().isEmpty())
95 && (node.getNodeKind() != Type.DOCUMENT || node.hasChildNodes());
96 } else if (item instanceof AtomicValue) {
97 return !item.getStringValue().isEmpty();
98 } else if (item instanceof ArrayItem) {
99 if (((ArrayItem) item).isEmpty()) {
100 return true;
101 } else {
102 for (Sequence<?> mem : ((ArrayItem) item).members()) {
103 try {
104 SequenceIterator<?> memIter = mem.iterate();
105 Item it;
106 while ((it = memIter.next()) != null) {
107 if (isSignificant(it)) {
108 return true;
109 }
110 }
111 } catch (XPathException e) {
112 return true;
113 }
114 }
115 return false;
116 }
117 }
118 return true;
119 }
120
121 @Override
122 public void append(Item item, Location locationId, int copyNamespaces) throws XPathException {
123 if (isSignificant(item)) {
124 start();
125 }
126 super.append(item, locationId, copyNamespaces);
127 }
128
129 /**
130 * Notify the end of a document node
131 */
132 @Override
133 public void endDocument() throws XPathException {
134 if (--level != 0) {
135 super.endDocument();
136 }
137 }
138
139 /**
140 * End of element
141 */
142 @Override
143 public void endElement() throws XPathException {
144 level--;
145 super.endElement();
146 }
147
148 /**
149 * Ask if the sequence that has been written so far is considered empty
150 * @return true if no significant items have been written (or started)
151 */
152
153 public boolean isEmpty() {
154 return empty;
155 }
156
157 }
158