| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.query; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.event.*; |
| 12 |
import net.sf.saxon.expr.parser.ExplicitLocation; |
| 13 |
import net.sf.saxon.lib.SerializerFactory; |
| 14 |
import net.sf.saxon.om.CopyOptions; |
| 15 |
import net.sf.saxon.om.DocumentInfo; |
| 16 |
import net.sf.saxon.om.NodeInfo; |
| 17 |
import net.sf.saxon.om.SequenceIterator; |
| 18 |
import net.sf.saxon.serialize.SerializationProperties; |
| 19 |
import net.sf.saxon.trans.XPathException; |
| 20 |
import net.sf.saxon.tree.iter.SingletonIterator; |
| 21 |
import net.sf.saxon.tree.tiny.TinyBuilder; |
| 22 |
import net.sf.saxon.type.Type; |
| 23 |
|
| 24 |
import javax.xml.transform.Result; |
| 25 |
import javax.xml.transform.stream.StreamResult; |
| 26 |
import java.io.*; |
| 27 |
import java.net.URI; |
| 28 |
import java.net.URISyntaxException; |
| 29 |
import java.util.Properties; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class QueryResult { |
| 36 |
|
| 37 |
public static String RESULT_NS = "http://saxon.sf.net/xquery-results"; |
| 38 |
|
| 39 |
private QueryResult() { |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public static String serialize( NodeInfo nodeInfo) throws XPathException { |
| 54 |
StringWriter sw = new StringWriter(); |
| 55 |
Properties props = new Properties(); |
| 56 |
props.setProperty("method", "xml"); |
| 57 |
props.setProperty("indent", "yes"); |
| 58 |
props.setProperty("omit-xml-declaration", "yes"); |
| 59 |
serialize(nodeInfo, new StreamResult(sw), props); |
| 60 |
return sw.toString(); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public static DocumentInfo wrap(SequenceIterator iterator, Configuration config) throws XPathException { |
| 77 |
PipelineConfiguration pipe = config.makePipelineConfiguration(); |
| 78 |
TinyBuilder builder = new TinyBuilder(pipe); |
| 79 |
builder.setStatistics(config.getTreeStatistics().SOURCE_DOCUMENT_STATISTICS); |
| 80 |
NamespaceReducer reducer = new NamespaceReducer(builder); |
| 81 |
ComplexContentOutputter outputter = new ComplexContentOutputter(reducer); |
| 82 |
sendWrappedSequence(iterator, outputter); |
| 83 |
return new DocumentInfo(builder.getCurrentRoot()); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public static void sendWrappedSequence(SequenceIterator iterator, Receiver destination) throws XPathException { |
| 99 |
SerializerFactory sf = destination.getPipelineConfiguration().getConfiguration().getSerializerFactory(); |
| 100 |
SequenceCopier.copySequence(iterator, sf.newSequenceWrapper(destination)); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public static void serialize( NodeInfo node, Result destination, Properties outputProperties) |
| 116 |
throws XPathException { |
| 117 |
Configuration config = node.getConfiguration(); |
| 118 |
serializeSequence(SingletonIterator.makeIterator(node), config, destination, outputProperties); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public static void serialize(NodeInfo node, Result destination, SerializationProperties properties) |
| 133 |
throws XPathException { |
| 134 |
Configuration config = node.getConfiguration(); |
| 135 |
serializeSequence(SingletonIterator.makeIterator(node), config, destination, properties); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public static void serializeSequence( |
| 151 |
SequenceIterator<?> iterator, Configuration config, OutputStream destination, Properties outputProps) |
| 152 |
throws XPathException { |
| 153 |
serializeSequence(iterator, config, new StreamResult(destination), outputProps); |
| 154 |
try { |
| 155 |
destination.flush(); |
| 156 |
} catch (IOException err) { |
| 157 |
throw new XPathException(err); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public static void serializeSequence( |
| 174 |
SequenceIterator<?> iterator, Configuration config, Writer writer, Properties outputProps) |
| 175 |
throws XPathException { |
| 176 |
serializeSequence(iterator, config, new StreamResult(writer), outputProps); |
| 177 |
try { |
| 178 |
writer.flush(); |
| 179 |
} catch (IOException err) { |
| 180 |
throw new XPathException(err); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public static void serializeSequence( |
| 197 |
SequenceIterator<?> iterator, Configuration config, Result result, Properties outputProperties) |
| 198 |
throws XPathException { |
| 199 |
SerializerFactory sf = config.getSerializerFactory(); |
| 200 |
Receiver tr = sf.getReceiver(result, new SerializationProperties(outputProperties)); |
| 201 |
SequenceCopier.copySequence(iterator, tr); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
public static void serializeSequence( |
| 217 |
SequenceIterator<?> iterator, Configuration config, Result result, SerializationProperties properties) |
| 218 |
throws XPathException { |
| 219 |
SerializerFactory sf = config.getSerializerFactory(); |
| 220 |
Receiver tr = sf.getReceiver(result, properties); |
| 221 |
SequenceCopier.copySequence(iterator, tr); |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
public static void rewriteToDisk(NodeInfo doc, Properties outputProperties, boolean backup, PrintStream log) |
| 238 |
throws XPathException { |
| 239 |
switch (doc.getNodeKind()) { |
| 240 |
case Type.DOCUMENT: |
| 241 |
|
| 242 |
break; |
| 243 |
case Type.ELEMENT: |
| 244 |
NodeInfo parent = doc.getParent(); |
| 245 |
if (parent != null && parent.getNodeKind() != Type.DOCUMENT) { |
| 246 |
throw new XPathException("Cannot rewrite an element node unless it is top-level"); |
| 247 |
} |
| 248 |
break; |
| 249 |
default: |
| 250 |
throw new XPathException("Node to be rewritten must be a document or element node"); |
| 251 |
} |
| 252 |
String uri = doc.getSystemId(); |
| 253 |
if (uri == null || uri.isEmpty()) { |
| 254 |
throw new XPathException("Cannot rewrite a document with no known URI"); |
| 255 |
} |
| 256 |
URI u; |
| 257 |
try { |
| 258 |
u = new URI(uri); |
| 259 |
} catch (URISyntaxException e) { |
| 260 |
throw new XPathException("SystemId of updated document is not a valid URI: " + uri); |
| 261 |
} |
| 262 |
File existingFile = new File(u); |
| 263 |
File dir = existingFile.getParentFile(); |
| 264 |
if (backup && existingFile.exists()) { |
| 265 |
File backupFile = new File(dir, existingFile.getName() + ".bak"); |
| 266 |
if (log != null) { |
| 267 |
log.println("Creating backup file " + backupFile); |
| 268 |
} |
| 269 |
boolean success = existingFile.renameTo(backupFile); |
| 270 |
if (!success) { |
| 271 |
throw new XPathException("Failed to create backup file of " + backupFile); |
| 272 |
} |
| 273 |
} |
| 274 |
if (!existingFile.exists()) { |
| 275 |
if (log != null) { |
| 276 |
log.println("Creating file " + existingFile); |
| 277 |
} |
| 278 |
try { |
| 279 |
|
| 280 |
existingFile.createNewFile(); |
| 281 |
} catch (IOException e) { |
| 282 |
throw new XPathException("Failed to create new file " + existingFile); |
| 283 |
} |
| 284 |
} else { |
| 285 |
if (log != null) { |
| 286 |
log.println("Overwriting file " + existingFile); |
| 287 |
} |
| 288 |
} |
| 289 |
Configuration config = doc.getConfiguration(); |
| 290 |
SerializerFactory factory = config.getSerializerFactory(); |
| 291 |
Receiver r = factory.getReceiver(new StreamResult(existingFile), new SerializationProperties(outputProperties)); |
| 292 |
doc.copy(r, CopyOptions.ALL_NAMESPACES, ExplicitLocation.UNKNOWN_LOCATION); |
| 293 |
r.close(); |
| 294 |
} |
| 295 |
} |