| 1 |
package net.sf.saxon.jetonnet; |
| 2 |
|
| 3 |
import net.sf.saxon.Configuration; |
| 4 |
import net.sf.saxon.lib.ParseOptions; |
| 5 |
import net.sf.saxon.om.NodeInfo; |
| 6 |
import net.sf.saxon.pull.PullSource; |
| 7 |
import net.sf.saxon.s9api.Processor; |
| 8 |
import net.sf.saxon.s9api.XdmNode; |
| 9 |
import net.sf.saxon.s9api.XdmValue; |
| 10 |
import net.sf.saxon.trans.XPathException; |
| 11 |
|
| 12 |
import javax.xml.transform.Source; |
| 13 |
import javax.xml.transform.stream.StreamSource; |
| 14 |
import javax.xml.xpath.XPath; |
| 15 |
import java.io.IOException; |
| 16 |
import java.io.InputStream; |
| 17 |
import java.nio.ByteBuffer; |
| 18 |
import java.nio.ByteOrder; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public class JetStream extends InputStream { |
| 24 |
private Object stream; |
| 25 |
private String baseURI; |
| 26 |
long currentOffset; |
| 27 |
long markedOffset = 0; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public JetStream() { |
| 35 |
|
| 36 |
} |
| 37 |
|
| 38 |
public JetStream(Object stream, String baseUri) { |
| 39 |
this.stream = stream; |
| 40 |
this.baseURI = baseUri; |
| 41 |
} |
| 42 |
|
| 43 |
public Object getPointerObject() { |
| 44 |
return stream; |
| 45 |
} |
| 46 |
|
| 47 |
public native int Readc(); |
| 48 |
|
| 49 |
public native int ReadArray(byte b[], int off, int len); |
| 50 |
|
| 51 |
public native boolean get_CanSeek(); |
| 52 |
|
| 53 |
public native long Seek(long l, int seekOrigin); |
| 54 |
|
| 55 |
public native void Close(); |
| 56 |
|
| 57 |
@Override |
| 58 |
public int read() throws IOException { |
| 59 |
int i = Readc(); |
| 60 |
if (i != -1) { |
| 61 |
currentOffset++; |
| 62 |
return i; |
| 63 |
} else { |
| 64 |
return -1; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
private int tempLen; |
| 69 |
private int tempOff; |
| 70 |
private byte [] tempB; |
| 71 |
|
| 72 |
public int getLength(){ |
| 73 |
return tempLen; |
| 74 |
} |
| 75 |
|
| 76 |
public int getOffset(){ |
| 77 |
return tempOff; |
| 78 |
} |
| 79 |
|
| 80 |
public byte [] getTempB(){ |
| 81 |
return tempB; |
| 82 |
} |
| 83 |
|
| 84 |
public void setTempB(byte [] b){ |
| 85 |
tempB = b; |
| 86 |
} |
| 87 |
|
| 88 |
public int read(byte b[], int off, int len) throws IOException { |
| 89 |
|
| 90 |
if(len == 0) { |
| 91 |
return 0; |
| 92 |
|
| 93 |
} |
| 94 |
setTempB(new byte[len]); |
| 95 |
tempLen = len; |
| 96 |
int c = ReadArray(b,off,len); |
| 97 |
|
| 98 |
|
| 99 |
for(int i=0;i<c;i++ ){ |
| 100 |
b[off+i] = tempB[i]; |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
return c; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
public boolean markSupported() { |
| 145 |
return get_CanSeek(); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
public synchronized void mark(int readlimit) { |
| 150 |
markedOffset = currentOffset; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
public synchronized void reset() throws IOException { |
| 155 |
currentOffset = markedOffset; |
| 156 |
Seek(markedOffset, 0); |
| 157 |
} |
| 158 |
|
| 159 |
public void close() throws IOException { |
| 160 |
Close(); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
public static void BuildStreamV(Processor processor, Object stream, String baseUri) throws Exception { |
| 165 |
System.err.println("Java - JetStream.BuildStreamv - BaseUri = " + baseUri); |
| 166 |
if (baseUri != null) { |
| 167 |
System.err.println("Java - JetStream.BuildStreamv - BaseUri = " + baseUri); |
| 168 |
} else { |
| 169 |
System.err.println("Java - JetStream.BuildStreamv - baseUris is null"); |
| 170 |
|
| 171 |
} |
| 172 |
if (stream != null) { |
| 173 |
System.err.println("Java - JetStream.BuildStreamv - stream"); |
| 174 |
} else { |
| 175 |
System.err.println("Java - JetStream.BuildStreamv - stream is null"); |
| 176 |
|
| 177 |
} |
| 178 |
if (processor == null) { |
| 179 |
System.err.println("Java - JetStream.BuildStreamv - processor is null"); |
| 180 |
|
| 181 |
} |
| 182 |
|
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
public static String BuildStreamString(Processor processor, Object stream, String baseUri) throws Exception { |
| 187 |
if (baseUri != null) { |
| 188 |
System.err.println("Java - JetStream.BuildStream - BaseUri = " + baseUri); |
| 189 |
} else { |
| 190 |
System.err.println("Java - JetStream.BuildStream - baseUris is null"); |
| 191 |
return null; |
| 192 |
} |
| 193 |
if (stream != null) { |
| 194 |
System.err.println("Java - JetStream.BuildStream - stream"); |
| 195 |
} else { |
| 196 |
System.err.println("Java - JetStream.BuildStream - stream is null"); |
| 197 |
return null; |
| 198 |
} |
| 199 |
if (processor == null) { |
| 200 |
System.err.println("Java - JetStream.BuildStream - processor is null"); |
| 201 |
return null; |
| 202 |
} |
| 203 |
return "String value returned from BuildStream"; |
| 204 |
} |
| 205 |
|
| 206 |
public static XdmNode BuildStream(Processor processor, Object stream, String baseUri) throws Exception { |
| 207 |
|
| 208 |
Source source = null; |
| 209 |
ParseOptions options = null; |
| 210 |
Configuration config = null; |
| 211 |
if (baseUri != null) { |
| 212 |
System.err.println("Java - JetStream.BuildStream - BaseUri = " + baseUri); |
| 213 |
} else { |
| 214 |
System.err.println("Java - JetStream.BuildStream - baseUris is null"); |
| 215 |
return null; |
| 216 |
} |
| 217 |
if (processor == null) { |
| 218 |
System.err.println("Java - JetStream.BuildStream - processor is null"); |
| 219 |
return null; |
| 220 |
} |
| 221 |
try { |
| 222 |
config = processor.getUnderlyingConfiguration(); |
| 223 |
|
| 224 |
options = new ParseOptions(config.getParseOptions()); |
| 225 |
|
| 226 |
source = new StreamSource(new JetStream(stream, baseUri)); |
| 227 |
if (source == null) { |
| 228 |
System.err.println("Java - source is null"); |
| 229 |
} else { |
| 230 |
source.setSystemId(baseUri); |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
NodeInfo doc = config.buildDocumentTree(source, options).getRootNode(); |
| 235 |
return (XdmNode) XdmValue.wrap(doc); |
| 236 |
} catch (Exception ex) { |
| 237 |
ex.printStackTrace(); |
| 238 |
|
| 239 |
return null; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
} |