| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.serialize; |
| 9 |
|
| 10 |
import net.sf.saxon.event.EventBuffer; |
| 11 |
import net.sf.saxon.event.ProxyReceiver; |
| 12 |
import net.sf.saxon.event.Receiver; |
| 13 |
import net.sf.saxon.event.ReceiverOptions; |
| 14 |
import net.sf.saxon.expr.parser.Location; |
| 15 |
import net.sf.saxon.lib.NamespaceConstant; |
| 16 |
import net.sf.saxon.lib.SaxonOutputKeys; |
| 17 |
import net.sf.saxon.lib.SerializerFactory; |
| 18 |
import net.sf.saxon.om.CopyOptions; |
| 19 |
import net.sf.saxon.om.Item; |
| 20 |
import net.sf.saxon.om.NodeInfo; |
| 21 |
import net.sf.saxon.om.NodeName; |
| 22 |
import net.sf.saxon.trans.XPathException; |
| 23 |
import net.sf.saxon.type.SchemaType; |
| 24 |
import net.sf.saxon.value.Whitespace; |
| 25 |
|
| 26 |
import javax.xml.transform.OutputKeys; |
| 27 |
import javax.xml.transform.Result; |
| 28 |
import java.util.Properties; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class UncommittedSerializer extends ProxyReceiver { |
| 39 |
|
| 40 |
private boolean committed = false; |
| 41 |
private EventBuffer pending = null; |
| 42 |
private Result finalResult; |
| 43 |
private SerializationProperties properties; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public UncommittedSerializer(Result finalResult, Receiver next, SerializationProperties params) { |
| 54 |
super(next); |
| 55 |
this.finalResult = finalResult; |
| 56 |
this.properties = params; |
| 57 |
} |
| 58 |
|
| 59 |
public void open() throws XPathException { |
| 60 |
committed = false; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public void close() throws XPathException { |
| 68 |
|
| 69 |
if (!committed) { |
| 70 |
switchToMethod("xml"); |
| 71 |
} |
| 72 |
getNextReceiver().close(); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public void characters(CharSequence chars, Location locationId, int properties) throws XPathException { |
| 80 |
if (committed) { |
| 81 |
getNextReceiver().characters(chars, locationId, properties); |
| 82 |
} else { |
| 83 |
if (pending == null) { |
| 84 |
pending = new EventBuffer(getPipelineConfiguration()); |
| 85 |
} |
| 86 |
pending.characters(chars, locationId, properties); |
| 87 |
if (!Whitespace.isWhite(chars)) { |
| 88 |
switchToMethod("xml"); |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public void processingInstruction(String target, CharSequence data, Location locationId, int properties) throws XPathException { |
| 98 |
if (committed) { |
| 99 |
getNextReceiver().processingInstruction(target, data, locationId, properties); |
| 100 |
} else { |
| 101 |
if (pending == null) { |
| 102 |
pending = new EventBuffer(getPipelineConfiguration()); |
| 103 |
} |
| 104 |
pending.processingInstruction(target, data, locationId, properties); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public void comment(CharSequence chars, Location locationId, int properties) throws XPathException { |
| 113 |
if (committed) { |
| 114 |
getNextReceiver().comment(chars, locationId, properties); |
| 115 |
} else { |
| 116 |
if (pending == null) { |
| 117 |
pending = new EventBuffer(getPipelineConfiguration()); |
| 118 |
} |
| 119 |
pending.comment(chars, locationId, properties); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public void startElement(NodeName elemName, SchemaType typeCode, Location location, int properties) throws XPathException { |
| 134 |
if (!committed) { |
| 135 |
String name = elemName.getLocalPart(); |
| 136 |
String uri = elemName.getURI(); |
| 137 |
if (name.equalsIgnoreCase("html") && uri.isEmpty()) { |
| 138 |
switchToMethod("html"); |
| 139 |
} else if (name.equals("html") && uri.equals(NamespaceConstant.XHTML)) { |
| 140 |
String version = this.properties.getProperties().getProperty(SaxonOutputKeys.STYLESHEET_VERSION); |
| 141 |
if ("10".equals(version)) { |
| 142 |
switchToMethod("xml"); |
| 143 |
} else { |
| 144 |
switchToMethod("xhtml"); |
| 145 |
} |
| 146 |
} else { |
| 147 |
switchToMethod("xml"); |
| 148 |
} |
| 149 |
} |
| 150 |
getNextReceiver().startElement(elemName, typeCode, location, properties); |
| 151 |
} |
| 152 |
|
| 153 |
@Override |
| 154 |
public void startDocument(int properties) throws XPathException { |
| 155 |
if (committed) { |
| 156 |
getNextReceiver().startDocument(properties); |
| 157 |
} else { |
| 158 |
if (pending == null) { |
| 159 |
pending = new EventBuffer(getPipelineConfiguration()); |
| 160 |
} |
| 161 |
pending.startDocument(properties); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
@Override |
| 166 |
public void endDocument() throws XPathException { |
| 167 |
|
| 168 |
if (!committed) { |
| 169 |
switchToMethod("xml"); |
| 170 |
} |
| 171 |
getNextReceiver().endDocument(); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
private void switchToMethod(String method) throws XPathException { |
| 181 |
Properties newProperties = new Properties(properties.getProperties()); |
| 182 |
newProperties.setProperty(OutputKeys.METHOD, method); |
| 183 |
SerializerFactory sf = getConfiguration().getSerializerFactory(); |
| 184 |
SerializationProperties newParams = new SerializationProperties(newProperties, properties.getCharacterMapIndex()); |
| 185 |
newParams.setValidationFactory(properties.getValidationFactory()); |
| 186 |
Receiver target = sf.getReceiver(finalResult, newParams, getPipelineConfiguration()); |
| 187 |
committed = true; |
| 188 |
target.open(); |
| 189 |
if (pending != null) { |
| 190 |
pending.replay(target); |
| 191 |
pending = null; |
| 192 |
} |
| 193 |
setUnderlyingReceiver(target); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
@Override |
| 207 |
public void append(Item item, Location locationId, int copyNamespaces) throws XPathException { |
| 208 |
if (item instanceof NodeInfo) { |
| 209 |
((NodeInfo) item).copy(this, CopyOptions.ALL_NAMESPACES, locationId); |
| 210 |
} else { |
| 211 |
if (!committed) { |
| 212 |
switchToMethod("xml"); |
| 213 |
} |
| 214 |
getNextReceiver().append(item); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
} |
| 219 |
|