Debian Codegraph @

net/sf/saxon/event/SequenceNormalizerWithItemSeparator.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.ExplicitLocation;
11 import net.sf.saxon.expr.parser.Location;
12 import net.sf.saxon.ma.arrays.ArrayItem;
13 import net.sf.saxon.om.Item;
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.value.AtomicValue;
18
19 /**
20 * Implement the "sequence normalization" logic as defined in the XSLT 3.0/XQuery 3.0
21 * serialization spec.
22 *
23 * <p>This class is used only if an ItemSeparator is specified. In the absence of an ItemSeparator,
24 * the insertion of a single space performed by the ComplexContentOutputter serves the purpose.</p>
25 */
26
27 public class SequenceNormalizerWithItemSeparator extends SequenceNormalizer {
28
29 private String separator;
30 private boolean first = true;
31
32 public SequenceNormalizerWithItemSeparator(Receiver next, String separator) {
33 super(next);
34 this.separator = separator;
35 }
36
37 /**
38 * Start of event stream
39 */
40 @Override
41 public void open() throws XPathException {
42 first = true;
43 super.open();
44 }
45
46 /**
47 * Start of a document node.
48 */
49 @Override
50 public void startDocument(int properties) throws XPathException {
51 sep();
52 super.startDocument(properties);
53 }
54
55
56 /**
57 * Notify the start of an element
58 * @param elemName integer code identifying the name of the element within the name pool.
59 * @param typeCode integer code identifying the element's type within the name pool.
60 * @param location
61 * @param properties properties of the element node
62 */
63 @Override
64 public void startElement(NodeName elemName, SchemaType typeCode, Location location, int properties) throws XPathException {
65 sep();
66 super.startElement(elemName, typeCode, location, properties);
67 }
68
69 /**
70 * Character data
71 */
72 @Override
73 public void characters(CharSequence chars, Location locationId, int properties) throws XPathException {
74 sep();
75 super.characters(chars, locationId, properties);
76 }
77
78 /**
79 * Processing Instruction
80 */
81 @Override
82 public void processingInstruction(String target, CharSequence data, Location locationId, int properties) throws XPathException {
83 sep();
84 super.processingInstruction(target, data, locationId, properties);
85 }
86
87 /**
88 * Output a comment
89 */
90 @Override
91 public void comment(CharSequence chars, Location locationId, int properties) throws XPathException {
92 sep();
93 super.comment(chars, locationId, properties);
94 }
95
96 /**
97 * Append an arbitrary item (node or atomic value) to the output
98 *
99 * @param item the item to be appended
100 * @param locationId the location of the calling instruction, for diagnostics
101 * @param copyNamespaces if the item is an element node, this indicates whether its namespaces
102 * need to be copied. Values are {@link ReceiverOptions#ALL_NAMESPACES},
103 * {@link ReceiverOptions#LOCAL_NAMESPACES}; the default (0) means
104 * no namespaces
105 */
106 @Override
107 public void append(Item item, Location locationId, int copyNamespaces) throws XPathException {
108 if (item instanceof ArrayItem) {
109 flatten((ArrayItem) item, locationId, copyNamespaces);
110 } else {
111 if (item instanceof AtomicValue) {
112 sep();
113 nextReceiver.characters(item.getStringValueCS(), locationId, 0);
114 } else {
115 decompose(item, locationId, copyNamespaces);
116 }
117 }
118 }
119
120 /**
121 * End of output. Note that closing this receiver also closes the rest of the
122 * pipeline.
123 */
124 @Override
125 public void close() throws XPathException {
126 //getNextReceiver().endDocument();
127 super.close();
128 }
129
130 /**
131 * Output the separator, assuming we are at the top level and not at the start
132 */
133
134 public void sep() throws XPathException {
135 if (level == 0 && !first) {
136 super.characters(separator, ExplicitLocation.UNKNOWN_LOCATION, 0);
137 } else {
138 first = false;
139 }
140 }
141 }
142