| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.serialize; |
| 9 |
|
| 10 |
import net.sf.saxon.event.ProxyReceiver; |
| 11 |
import net.sf.saxon.event.Receiver; |
| 12 |
import net.sf.saxon.expr.parser.Location; |
| 13 |
import net.sf.saxon.om.Item; |
| 14 |
import net.sf.saxon.om.NodeName; |
| 15 |
import net.sf.saxon.s9api.Destination; |
| 16 |
import net.sf.saxon.s9api.SaxonApiException; |
| 17 |
import net.sf.saxon.trans.XPathException; |
| 18 |
import net.sf.saxon.trans.XsltController; |
| 19 |
import net.sf.saxon.type.SchemaType; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class PrincipalOutputGatekeeper extends ProxyReceiver { |
| 39 |
|
| 40 |
private XsltController controller; |
| 41 |
private boolean usedAsPrimaryResult = false; |
| 42 |
private boolean usedAsSecondaryResult = false; |
| 43 |
private boolean open = false; |
| 44 |
private boolean closed = false; |
| 45 |
|
| 46 |
|
| 47 |
public PrincipalOutputGatekeeper(XsltController controller, Receiver next) { |
| 48 |
super(next); |
| 49 |
this.controller = controller; |
| 50 |
} |
| 51 |
|
| 52 |
public void open() throws XPathException { |
| 53 |
if (closed) { |
| 54 |
String uri = getSystemId().equals(XsltController.ANONYMOUS_PRINCIPAL_OUTPUT_URI) ? "(no URI supplied)" : getSystemId(); |
| 55 |
XPathException err = new XPathException( |
| 56 |
"Cannot write more than one result document to the principal output destination: " + uri); |
| 57 |
err.setErrorCode("XTDE1490"); |
| 58 |
throw err; |
| 59 |
} |
| 60 |
super.open(); |
| 61 |
open = true; |
| 62 |
} |
| 63 |
|
| 64 |
public synchronized void startDocument(int properties) throws XPathException { |
| 65 |
if (!open) { |
| 66 |
open(); |
| 67 |
} |
| 68 |
|
| 69 |
nextReceiver.startDocument(properties); |
| 70 |
} |
| 71 |
|
| 72 |
public void startElement(NodeName nameCode, SchemaType typeCode, Location location, int properties) throws XPathException { |
| 73 |
useAsPrimary(); |
| 74 |
nextReceiver.startElement(nameCode, typeCode, location, properties); |
| 75 |
} |
| 76 |
|
| 77 |
public synchronized void characters(CharSequence chars, Location locationId, int properties) throws XPathException { |
| 78 |
useAsPrimary(); |
| 79 |
nextReceiver.characters(chars, locationId, properties); |
| 80 |
} |
| 81 |
|
| 82 |
public void processingInstruction(String target, CharSequence data, Location locationId, int properties) throws XPathException { |
| 83 |
useAsPrimary(); |
| 84 |
nextReceiver.processingInstruction(target, data, locationId, properties); |
| 85 |
} |
| 86 |
|
| 87 |
public void comment(CharSequence chars, Location locationId, int properties) throws XPathException { |
| 88 |
useAsPrimary(); |
| 89 |
nextReceiver.comment(chars, locationId, properties); |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public void append(Item item, Location locationId, int copyNamespaces) throws XPathException { |
| 94 |
useAsPrimary(); |
| 95 |
nextReceiver.append(item, locationId, copyNamespaces); |
| 96 |
} |
| 97 |
|
| 98 |
private synchronized void useAsPrimary() throws XPathException { |
| 99 |
if (closed) { |
| 100 |
XPathException err = new XPathException( |
| 101 |
"Cannot write to the principal output destination as it has already been closed: " + identifySystemId()); |
| 102 |
err.setErrorCode("XTDE1490"); |
| 103 |
throw err; |
| 104 |
} |
| 105 |
if (usedAsSecondaryResult) { |
| 106 |
XPathException err = new XPathException( |
| 107 |
"Cannot write to the principal output destination as it has already been used by xsl:result-document: " + identifySystemId()); |
| 108 |
err.setErrorCode("XTDE1490"); |
| 109 |
throw err; |
| 110 |
} |
| 111 |
usedAsPrimaryResult = true; |
| 112 |
} |
| 113 |
|
| 114 |
public synchronized void useAsSecondary() throws XPathException { |
| 115 |
if (usedAsPrimaryResult) { |
| 116 |
XPathException err = new XPathException( |
| 117 |
"Cannot use xsl:result-document to write to a destination already used for the principal output: " + identifySystemId()); |
| 118 |
err.setErrorCode("XTDE1490"); |
| 119 |
throw err; |
| 120 |
} |
| 121 |
if (usedAsSecondaryResult) { |
| 122 |
XPathException err = new XPathException( |
| 123 |
"Cannot write more than one xsl:result-document to the principal output destination: " + identifySystemId()); |
| 124 |
err.setErrorCode("XTDE1490"); |
| 125 |
throw err; |
| 126 |
} |
| 127 |
usedAsSecondaryResult = true; |
| 128 |
} |
| 129 |
|
| 130 |
public Receiver makeReceiver(SerializationProperties params) { |
| 131 |
try { |
| 132 |
Destination dest = controller.getPrincipalDestination(); |
| 133 |
if (dest != null) { |
| 134 |
return dest.getReceiver(controller.makePipelineConfiguration(), params); |
| 135 |
} |
| 136 |
} catch (SaxonApiException e) { |
| 137 |
return null; |
| 138 |
} |
| 139 |
return null; |
| 140 |
} |
| 141 |
|
| 142 |
private String identifySystemId() { |
| 143 |
String uri = getSystemId(); |
| 144 |
return uri==null ? "(no URI supplied)" : uri; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
public void close() throws XPathException { |
| 149 |
closed = true; |
| 150 |
if (usedAsPrimaryResult) { |
| 151 |
nextReceiver.close(); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
} |
| 156 |
|