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