| 1 |
package net.sf.saxon.jetonnet; |
| 2 |
|
| 3 |
import net.sf.saxon.Configuration; |
| 4 |
import net.sf.saxon.event.PipelineConfiguration; |
| 5 |
import net.sf.saxon.expr.parser.ExplicitLocation; |
| 6 |
import net.sf.saxon.expr.parser.Location; |
| 7 |
import net.sf.saxon.lib.ParseOptions; |
| 8 |
import net.sf.saxon.om.*; |
| 9 |
import net.sf.saxon.option.cpp.SaxonCAPI; |
| 10 |
import net.sf.saxon.pull.PullProvider; |
| 11 |
import net.sf.saxon.pull.PullSource; |
| 12 |
import net.sf.saxon.pull.UnparsedEntity; |
| 13 |
import net.sf.saxon.s9api.Processor; |
| 14 |
import net.sf.saxon.s9api.XdmNode; |
| 15 |
import net.sf.saxon.s9api.XdmValue; |
| 16 |
import net.sf.saxon.trans.XPathException; |
| 17 |
import net.sf.saxon.tree.tiny.CompressedWhitespace; |
| 18 |
import net.sf.saxon.tree.util.AttributeCollectionImpl; |
| 19 |
import net.sf.saxon.type.BuiltInAtomicType; |
| 20 |
import net.sf.saxon.type.SchemaType; |
| 21 |
import net.sf.saxon.type.Untyped; |
| 22 |
import net.sf.saxon.value.AtomicValue; |
| 23 |
|
| 24 |
import javax.xml.transform.Source; |
| 25 |
import java.util.List; |
| 26 |
|
| 27 |
/** |
| 28 |
* Created by ond1 on 09/03/2018. |
| 29 |
*/ |
| 30 |
public class JetPullProvider implements PullProvider { |
| 31 |
|
| 32 |
private PipelineConfiguration pipe; |
| 33 |
private long parserPtr; |
| 34 |
private int parserPtrInt; |
| 35 |
private Object parser; |
| 36 |
private String baseURI; |
| 37 |
private boolean isEmptyElement = false; |
| 38 |
private int current = START_OF_INPUT; |
| 39 |
private boolean expandDefaults = true; |
| 40 |
|
| 41 |
|
| 42 |
public JetPullProvider(Object parser, long parserPtr, String bURI) { |
| 43 |
if (SaxonCAPI.debug) { |
| 44 |
System.err.println("Java: Parser pointer long value="+parserPtr); |
| 45 |
} |
| 46 |
this.parser = parser; |
| 47 |
this.parserPtr = parserPtr; |
| 48 |
this.parserPtrInt = (int)parserPtr; |
| 49 |
baseURI = bURI; |
| 50 |
} |
| 51 |
|
| 52 |
public static XdmNode Build(Processor processor, Object parser, long parserPtr, String baseUri) throws XPathException { |
| 53 |
|
| 54 |
|
| 55 |
Configuration config = processor.getUnderlyingConfiguration(); |
| 56 |
|
| 57 |
Source source; |
| 58 |
ParseOptions options = new ParseOptions(config.getParseOptions()); |
| 59 |
|
| 60 |
source = new PullSource(new JetPullProvider(parser, parserPtr, baseUri)); |
| 61 |
source.setSystemId(baseUri); |
| 62 |
|
| 63 |
|
| 64 |
NodeInfo doc = config.buildDocumentTree(source, options).getRootNode(); |
| 65 |
return (XdmNode) XdmValue.wrap(doc); |
| 66 |
|
| 67 |
|
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
public long getParserPointerValue(){ |
| 72 |
return parserPtr; |
| 73 |
} |
| 74 |
|
| 75 |
public Object getPointerObject(){ |
| 76 |
return parser; |
| 77 |
} |
| 78 |
|
| 79 |
@Override |
| 80 |
public void setPipelineConfiguration(PipelineConfiguration pipe) { |
| 81 |
this.pipe = pipe; |
| 82 |
final Configuration config = pipe.getConfiguration(); |
| 83 |
expandDefaults = config.isExpandAttributeDefaults(); |
| 84 |
} |
| 85 |
|
| 86 |
@Override |
| 87 |
public PipelineConfiguration getPipelineConfiguration() { |
| 88 |
return pipe; |
| 89 |
} |
| 90 |
|
| 91 |
// public native void next(Object parser); |
| 92 |
|
| 93 |
public native boolean Read(Object parseri); |
| 94 |
|
| 95 |
public native int get_NodeType(long parserPtri); |
| 96 |
|
| 97 |
public native boolean get_IsEmptyElement(long parserPtri); |
| 98 |
|
| 99 |
public native boolean get_EOF(long parserPtri); |
| 100 |
|
| 101 |
public native int get_Depth(long parserPtri); |
| 102 |
|
| 103 |
public native void parser_close(long parserPtri); |
| 104 |
|
| 105 |
public native String[] get_Attributes(long parserPtri, boolean expandDefault); |
| 106 |
|
| 107 |
public native String get_Value(long parserPtri); |
| 108 |
|
| 109 |
public native String get_NodeName(long parserPtr); |
| 110 |
|
| 111 |
@Override |
| 112 |
public int next() throws XPathException { |
| 113 |
|
| 114 |
if (SaxonCAPI.debug) { |
| 115 |
System.err.println("Java: next(), current = " + current + " empty: " + isEmptyElement); |
| 116 |
} |
| 117 |
if (current == START_OF_INPUT) { |
| 118 |
current = START_DOCUMENT; |
| 119 |
return current; |
| 120 |
} else if (current == END_DOCUMENT || current == END_OF_INPUT) { |
| 121 |
current = END_OF_INPUT; |
| 122 |
return current; |
| 123 |
} else if (current == START_ELEMENT && isEmptyElement) { |
| 124 |
current = END_ELEMENT; |
| 125 |
return current; |
| 126 |
} |
| 127 |
|
| 128 |
do { |
| 129 |
|
| 130 |
Read(parser); |
| 131 |
|
| 132 |
//noinspection ConstantIfStatement |
| 133 |
if (false) { |
| 134 |
XPathException de = new XPathException("Error reported by XML parser: dummy"); |
| 135 |
throw de; |
| 136 |
|
| 137 |
} |
| 138 |
//noinspection ConstantIfStatement |
| 139 |
if (false) { |
| 140 |
XPathException de = new XPathException("Validation error reported by XML parser: "); |
| 141 |
|
| 142 |
throw de; |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
int intype = get_NodeType(parserPtr); |
| 147 |
isEmptyElement = get_IsEmptyElement(parserPtr); |
| 148 |
if (SaxonCAPI.debug) { |
| 149 |
System.err.println("Java: Next event: " + intype + " at depth " + get_Depth(parserPtr) + " empty: " + isEmptyElement + "," + get_IsEmptyElement(parserPtr)); |
| 150 |
} |
| 151 |
if (get_EOF(parserPtr)) { |
| 152 |
current = END_DOCUMENT; |
| 153 |
return current; |
| 154 |
} |
| 155 |
if (intype == JetXmlNodeType.EntityReference) { |
| 156 |
//parser.ResolveEntity(); |
| 157 |
current = -1; |
| 158 |
} else { |
| 159 |
current = mapInputKindToOutputKind(intype); |
| 160 |
if (current == TEXT && get_Depth(parserPtr) == 0) { |
| 161 |
current = -1; |
| 162 |
} |
| 163 |
} |
| 164 |
} while (current == -1); |
| 165 |
return current; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
/** |
| 170 |
* Map the numbers used to identify events in the .NET XMLReader interface to the numbers used |
| 171 |
* by the Saxon PullProvider interface |
| 172 |
* |
| 173 |
* @param in the XMLReader event number |
| 174 |
* @return the Saxon PullProvider event number |
| 175 |
*/ |
| 176 |
|
| 177 |
private int mapInputKindToOutputKind(int in) { |
| 178 |
// Note: we are losing unparsedEntities - see test expr02. Apparently unparsed entities are not |
| 179 |
// available via an XMLValidatingReader. We would have to build a DOM to get them, and that's too high |
| 180 |
// a price to pay. |
| 181 |
switch (in) { |
| 182 |
case JetXmlNodeType.Attribute: |
| 183 |
return PullProvider.ATTRIBUTE; |
| 184 |
case JetXmlNodeType.CDATA: |
| 185 |
return PullProvider.TEXT; |
| 186 |
case JetXmlNodeType.Comment: |
| 187 |
return PullProvider.COMMENT; |
| 188 |
case JetXmlNodeType.Document: |
| 189 |
return PullProvider.START_DOCUMENT; |
| 190 |
case JetXmlNodeType.DocumentFragment: |
| 191 |
return PullProvider.START_DOCUMENT; |
| 192 |
case JetXmlNodeType.Element: |
| 193 |
return PullProvider.START_ELEMENT; |
| 194 |
case JetXmlNodeType.EndElement: |
| 195 |
return PullProvider.END_ELEMENT; |
| 196 |
case JetXmlNodeType.ProcessingInstruction: |
| 197 |
return PullProvider.PROCESSING_INSTRUCTION; |
| 198 |
case JetXmlNodeType.SignificantWhitespace: |
| 199 |
//System.err.println("Significant whitespace"); |
| 200 |
return PullProvider.TEXT; |
| 201 |
case JetXmlNodeType.Text: |
| 202 |
return PullProvider.TEXT; |
| 203 |
case JetXmlNodeType.Whitespace: |
| 204 |
//System.err.println("Plain whitespace"); |
| 205 |
return PullProvider.TEXT; |
| 206 |
//return -1; |
| 207 |
default: |
| 208 |
return -1; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
@Override |
| 213 |
public int current() { |
| 214 |
return current; |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Return the system identifier for the current document event. |
| 219 |
* <p>The return value is the system identifier of the document |
| 220 |
* entity or of the external parsed entity in which the markup that |
| 221 |
* triggered the event appears.</p> |
| 222 |
* <p>If the system identifier is a URL, the parser must resolve it |
| 223 |
* fully before passing it to the application.</p> |
| 224 |
* |
| 225 |
* @return A string containing the system identifier, or null |
| 226 |
* if none is available. |
| 227 |
*/ |
| 228 |
public String getSystemId() { |
| 229 |
String base = null;//parser.get_BaseURI(); |
| 230 |
if (base == null || base.isEmpty()) { |
| 231 |
return baseURI; |
| 232 |
} else { |
| 233 |
return base; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
@Override |
| 238 |
public AttributeCollection getAttributes() throws XPathException { |
| 239 |
|
| 240 |
String[] attributes = get_Attributes(parserPtr, expandDefaults); |
| 241 |
|
| 242 |
if (attributes != null && attributes.length > 0) { |
| 243 |
int attrLen = attributes.length; |
| 244 |
AttributeCollectionImpl atts = new AttributeCollectionImpl(pipe.getConfiguration()); |
| 245 |
for (int i = 0; i < attrLen; i++) { |
| 246 |
String result = attributes[i]; |
| 247 |
int openingBracket = result.indexOf("{"); |
| 248 |
int closingBracket = result.indexOf("}"); |
| 249 |
int valueIndex = result.indexOf("="); |
| 250 |
String prefix = result.substring(0, openingBracket); |
| 251 |
String namespaceURI = result.substring(result.indexOf("{") + 1, closingBracket); |
| 252 |
String localName = result.substring(closingBracket + 1, valueIndex); |
| 253 |
String value = result.substring(valueIndex + 1); |
| 254 |
if (SaxonCAPI.debug) { |
| 255 |
System.err.println("Java: getNodeName prefix= " + prefix + ", namespace=" + namespaceURI + ", localName=" + localName + " value=" + value); |
| 256 |
} |
| 257 |
NodeName nc = new FingerprintedQName(prefix, namespaceURI, localName); |
| 258 |
// .NET does not report the attribute type (even if it's an ID...) |
| 259 |
atts.addAttribute(nc, BuiltInAtomicType.UNTYPED_ATOMIC, value, ExplicitLocation.UNKNOWN_LOCATION, 0); |
| 260 |
} |
| 261 |
return atts; |
| 262 |
} else { |
| 263 |
return AttributeCollectionImpl.EMPTY_ATTRIBUTE_COLLECTION; |
| 264 |
} |
| 265 |
|
| 266 |
} |
| 267 |
|
| 268 |
@Override |
| 269 |
public NamespaceBinding[] getNamespaceDeclarations() throws XPathException { |
| 270 |
return new NamespaceBinding[0]; |
| 271 |
} |
| 272 |
|
| 273 |
@Override |
| 274 |
public int skipToMatchingEnd() throws XPathException { |
| 275 |
return 0; |
| 276 |
} |
| 277 |
|
| 278 |
@Override |
| 279 |
public void close() { |
| 280 |
parser_close(parserPtr); |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
@Override |
| 285 |
public NodeName getNodeName() { |
| 286 |
String result = get_NodeName(parserPtr); |
| 287 |
int openingBracket = result.indexOf("{"); |
| 288 |
int closingBracket = result.indexOf("}"); |
| 289 |
String prefix = result.substring(0, openingBracket); |
| 290 |
String namespaceURI = result.substring(result.indexOf("{") + 1, closingBracket); |
| 291 |
String localName = result.substring(closingBracket + 1); |
| 292 |
if (SaxonCAPI.debug) { |
| 293 |
System.err.println("Java: getNodeName prefix= " + prefix + ", namespace=" + namespaceURI + ", localName=" + localName); |
| 294 |
} |
| 295 |
return new FingerprintedQName(prefix, namespaceURI, localName); |
| 296 |
} |
| 297 |
|
| 298 |
@Override |
| 299 |
public CharSequence getStringValue() throws XPathException { |
| 300 |
if (current == TEXT) { |
| 301 |
return CompressedWhitespace.compress(get_Value(parserPtr)); |
| 302 |
} else { |
| 303 |
return get_Value(parserPtr); |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
@Override |
| 308 |
public SchemaType getSchemaType() { |
| 309 |
return Untyped.getInstance(); |
| 310 |
} |
| 311 |
|
| 312 |
@Override |
| 313 |
public AtomicValue getAtomicValue() { |
| 314 |
return null; |
| 315 |
} |
| 316 |
|
| 317 |
@Override |
| 318 |
public Location getSourceLocator() { |
| 319 |
return null; |
| 320 |
} |
| 321 |
|
| 322 |
@Override |
| 323 |
public List<UnparsedEntity> getUnparsedEntities() { |
| 324 |
return null; |
| 325 |
} |
| 326 |
} |