1 package net.sf.saxon.query; 2 3 import net.sf.saxon.Configuration; 4 import net.sf.saxon.event.*; 5 import net.sf.saxon.om.DocumentInfo; 6 import net.sf.saxon.om.Item; 7 import net.sf.saxon.om.NodeInfo; 8 import net.sf.saxon.om.SequenceIterator; 9 import net.sf.saxon.tinytree.TinyBuilder; 10 import net.sf.saxon.trans.DynamicError; 11 import net.sf.saxon.trans.XPathException; 12 import net.sf.saxon.type.Type; 13 import net.sf.saxon.value.QNameValue; 14 15 import javax.xml.transform.OutputKeys ; 16 import javax.xml.transform.Result ; 17 import javax.xml.transform.stream.StreamResult ; 18 import java.io.OutputStream ; 19 import java.io.OutputStreamWriter ; 20 import java.io.PrintWriter ; 21 import java.io.UnsupportedEncodingException ; 22 import java.util.Properties ; 23 24 28 public class QueryResult { 29 30 public static String RESULT_NS = "http://saxon.sf.net/xquery-results"; 31 32 private QueryResult() { 33 } 34 35 43 44 public static DocumentInfo wrap(SequenceIterator iterator, Configuration config) throws XPathException { 45 PipelineConfiguration pipe = config.makePipelineConfiguration(); 46 TinyBuilder builder = new TinyBuilder(); 47 48 NamespaceReducer reducer = new NamespaceReducer(); 49 reducer.setUnderlyingReceiver(builder); 50 Receiver tree = reducer; 51 52 tree.setPipelineConfiguration(pipe); 53 builder.setPipelineConfiguration(pipe); 54 sendWrappedSequence(iterator, tree); 55 return (DocumentInfo)builder.getCurrentRoot(); 56 } 57 58 65 66 public static void sendWrappedSequence(SequenceIterator iterator, Receiver destination) throws XPathException { 67 SequenceCopier.copySequence(iterator, new SequenceWrapper(destination)); 68 } 69 70 79 80 public static void serialize(NodeInfo node, Result destination, Properties outputProperties, Configuration config) 81 throws XPathException { 82 PipelineConfiguration pipe = config.makePipelineConfiguration(); 83 int type = node.getNodeKind(); 84 if (type==Type.DOCUMENT || type==Type.ELEMENT) { 85 DocumentSender sender = new DocumentSender(node); 86 Receiver receiver = 87 ResultWrapper.getReceiver(destination, 88 pipe, 89 outputProperties); 90 NamespaceReducer reducer = new NamespaceReducer(); 91 reducer.setUnderlyingReceiver(receiver); 92 reducer.setPipelineConfiguration(pipe); 93 sender.send(reducer); 94 } else { 95 throw new DynamicError("Node to be serialized must be a Document or Element node"); 96 } 97 } 98 99 107 108 public static void serializeSequence(SequenceIterator results, Configuration config, 109 OutputStream destination, Properties outputProps) 110 throws XPathException { 111 String encoding = outputProps.getProperty(OutputKeys.ENCODING); 112 if (encoding==null) { 113 encoding = "UTF-8"; 114 } 115 PrintWriter writer; 116 try { 117 writer = new PrintWriter ( 118 new OutputStreamWriter (destination, encoding)); 119 } catch (UnsupportedEncodingException err) { 120 throw new DynamicError(err); 121 } 122 while (true) { 123 Item item = results.next(); 124 if (item == null) break; 125 if (item instanceof NodeInfo) { 126 switch (((NodeInfo)item).getNodeKind()) { 127 case Type.DOCUMENT: 128 case Type.ELEMENT: 129 serialize((NodeInfo)item, 130 new StreamResult (writer), 131 outputProps, 132 config); 133 writer.println(""); 134 break; 135 case Type.ATTRIBUTE: 136 writer.println(((NodeInfo)item).getLocalPart() + 137 "=\"" + 138 item.getStringValue() + 139 '\"'); 140 break; 141 case Type.COMMENT: 142 writer.println("<!--" + item.getStringValue() + "-->"); 143 break; 144 case Type.PROCESSING_INSTRUCTION: 145 writer.println("<?" + 146 ((NodeInfo)item).getLocalPart() + 147 ' ' + 148 item.getStringValue() + 149 "?>"); 150 break; 151 default: 152 writer.println(item.getStringValue()); 153 } 154 } else if (item instanceof QNameValue) { 155 writer.println(((QNameValue)item).getClarkName()); 156 } else { 157 writer.println(item.getStringValue()); 158 } 159 } 160 writer.flush(); 161 } 162 163 } 173 174 | Popular Tags |