| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.event; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.parser.Location; |
| 11 |
import net.sf.saxon.om.NamespaceBindingSet; |
| 12 |
import net.sf.saxon.om.NodeName; |
| 13 |
import net.sf.saxon.s9api.Action; |
| 14 |
import net.sf.saxon.s9api.Destination; |
| 15 |
import net.sf.saxon.s9api.SaxonApiException; |
| 16 |
import net.sf.saxon.trans.XPathException; |
| 17 |
import net.sf.saxon.type.SchemaType; |
| 18 |
import net.sf.saxon.type.SimpleType; |
| 19 |
|
| 20 |
import java.util.ArrayList; |
| 21 |
import java.util.List; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public abstract class SequenceNormalizer extends ProxyReceiver { |
| 37 |
|
| 38 |
protected int level = 0; |
| 39 |
protected boolean inStartTag = false; |
| 40 |
private List<Action> actionList; |
| 41 |
private boolean failed = false; |
| 42 |
|
| 43 |
public SequenceNormalizer(Receiver next) { |
| 44 |
super(next); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
@Override |
| 51 |
public void open() throws XPathException { |
| 52 |
level = 0; |
| 53 |
previousAtomic = false; |
| 54 |
super.open(); |
| 55 |
getNextReceiver().startDocument(0); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
@Override |
| 62 |
public void startDocument(int properties) throws XPathException { |
| 63 |
level++; |
| 64 |
previousAtomic = false; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
@Override |
| 71 |
public void endDocument() throws XPathException { |
| 72 |
level--; |
| 73 |
previousAtomic = false; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
@Override |
| 84 |
public void startElement(NodeName elemName, SchemaType typeCode, Location location, int properties) throws XPathException { |
| 85 |
try { |
| 86 |
maybeStartContent(); |
| 87 |
level++; |
| 88 |
super.startElement(elemName, typeCode, location, properties); |
| 89 |
inStartTag = true; |
| 90 |
previousAtomic = false; |
| 91 |
} catch (XPathException e) { |
| 92 |
failed = true; |
| 93 |
e.maybeSetLocation(location); |
| 94 |
throw e; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
@Override |
| 114 |
public void attribute(NodeName name, SimpleType typeCode, CharSequence value, Location locationId, int properties) throws XPathException { |
| 115 |
if (level == 0) { |
| 116 |
failed = true; |
| 117 |
XPathException err = new XPathException("Cannot serialize a free-standing attribute node (" + name + ')', "SENR0001"); |
| 118 |
err.setLocation(locationId); |
| 119 |
throw err; |
| 120 |
} |
| 121 |
try { |
| 122 |
super.attribute(name, typeCode, value, locationId, properties); |
| 123 |
} catch (XPathException e) { |
| 124 |
failed = true; |
| 125 |
throw e; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
@Override |
| 141 |
public void namespace(NamespaceBindingSet namespaceBindings, int properties) throws XPathException { |
| 142 |
if (level == 0) { |
| 143 |
failed = true; |
| 144 |
throw new XPathException("Cannot serialize a free-standing namespace node", "SENR0001"); |
| 145 |
} |
| 146 |
try { |
| 147 |
super.namespace(namespaceBindings, properties); |
| 148 |
} catch (XPathException e) { |
| 149 |
failed = true; |
| 150 |
throw e; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
@Override |
| 161 |
public void startContent() throws XPathException { |
| 162 |
try { |
| 163 |
super.startContent(); |
| 164 |
inStartTag = false; |
| 165 |
} catch (XPathException e) { |
| 166 |
failed = true; |
| 167 |
throw e; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
private void maybeStartContent() throws XPathException { |
| 172 |
if (inStartTag) { |
| 173 |
startContent(); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
@Override |
| 181 |
public void characters(CharSequence chars, Location locationId, int properties) throws XPathException { |
| 182 |
try { |
| 183 |
maybeStartContent(); |
| 184 |
super.characters(chars, locationId, properties); |
| 185 |
previousAtomic = false; |
| 186 |
} catch (XPathException e) { |
| 187 |
failed = true; |
| 188 |
throw e; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
@Override |
| 196 |
public void processingInstruction(String target, CharSequence data, Location locationId, int properties) throws XPathException { |
| 197 |
try { |
| 198 |
maybeStartContent(); |
| 199 |
super.processingInstruction(target, data, locationId, properties); |
| 200 |
previousAtomic = false; |
| 201 |
} catch (XPathException e) { |
| 202 |
failed = true; |
| 203 |
throw e; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
@Override |
| 211 |
public void comment(CharSequence chars, Location locationId, int properties) throws XPathException { |
| 212 |
try { |
| 213 |
maybeStartContent(); |
| 214 |
super.comment(chars, locationId, properties); |
| 215 |
previousAtomic = false; |
| 216 |
} catch (XPathException e) { |
| 217 |
failed = true; |
| 218 |
throw e; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
@Override |
| 226 |
public void endElement() throws XPathException { |
| 227 |
try { |
| 228 |
maybeStartContent(); |
| 229 |
level--; |
| 230 |
super.endElement(); |
| 231 |
previousAtomic = false; |
| 232 |
} catch (XPathException e) { |
| 233 |
failed = true; |
| 234 |
throw e; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
@Override |
| 243 |
public void close() throws XPathException { |
| 244 |
if (failed) { |
| 245 |
super.close(); |
| 246 |
} else { |
| 247 |
getNextReceiver().endDocument(); |
| 248 |
super.close(); |
| 249 |
try { |
| 250 |
if (actionList != null) { |
| 251 |
for (Action action : actionList) { |
| 252 |
action.act(); |
| 253 |
} |
| 254 |
} |
| 255 |
} catch (SaxonApiException e) { |
| 256 |
throw XPathException.makeXPathException(e); |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
public void onClose(List<Action> actionList) { |
| 267 |
this.actionList = actionList; |
| 268 |
} |
| 269 |
|
| 270 |
public void onClose(Action action) { |
| 271 |
if (actionList == null) { |
| 272 |
actionList = new ArrayList<>(); |
| 273 |
} |
| 274 |
actionList.add(action); |
| 275 |
} |
| 276 |
|
| 277 |
} |
| 278 |
|