| 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.pull.PullProvider; |
| 10 |
import net.sf.saxon.pull.PullSource; |
| 11 |
import net.sf.saxon.pull.UnparsedEntity; |
| 12 |
import net.sf.saxon.s9api.Processor; |
| 13 |
import net.sf.saxon.s9api.XdmNode; |
| 14 |
import net.sf.saxon.s9api.XdmValue; |
| 15 |
import net.sf.saxon.trans.XPathException; |
| 16 |
import net.sf.saxon.tree.tiny.CompressedWhitespace; |
| 17 |
import net.sf.saxon.tree.util.AttributeCollectionImpl; |
| 18 |
import net.sf.saxon.type.BuiltInAtomicType; |
| 19 |
import net.sf.saxon.type.SchemaType; |
| 20 |
import net.sf.saxon.type.Untyped; |
| 21 |
import net.sf.saxon.value.AtomicValue; |
| 22 |
|
| 23 |
import javax.xml.transform.Source; |
| 24 |
import java.util.List; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public class JetPullProvider implements PullProvider { |
| 30 |
|
| 31 |
private PipelineConfiguration pipe; |
| 32 |
private long parserPtr; |
| 33 |
private int parserPtrInt; |
| 34 |
private Object parser; |
| 35 |
private String baseURI; |
| 36 |
private boolean isEmptyElement = false; |
| 37 |
private int current = START_OF_INPUT; |
| 38 |
private boolean expandDefaults = true; |
| 39 |
|
| 40 |
|
| 41 |
public JetPullProvider(Object parser, long parserPtr, String bURI) { |
| 42 |
this.parser = parser; |
| 43 |
this.parserPtr = parserPtr; |
| 44 |
this.parserPtrInt = (int)parserPtr; |
| 45 |
baseURI = bURI; |
| 46 |
} |
| 47 |
|
| 48 |
public static XdmNode Build(Processor processor, Object parser, long parserPtr, String baseUri) throws XPathException { |
| 49 |
|
| 50 |
|
| 51 |
Configuration config = processor.getUnderlyingConfiguration(); |
| 52 |
|
| 53 |
Source source; |
| 54 |
ParseOptions options = new ParseOptions(config.getParseOptions()); |
| 55 |
|
| 56 |
source = new PullSource(new JetPullProvider(parser, parserPtr, baseUri)); |
| 57 |
source.setSystemId(baseUri); |
| 58 |
|
| 59 |
|
| 60 |
NodeInfo doc = config.buildDocumentTree(source, options).getRootNode(); |
| 61 |
return (XdmNode) XdmValue.wrap(doc); |
| 62 |
|
| 63 |
|
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
public long getParserPointerValue(){ |
| 68 |
return parserPtr; |
| 69 |
} |
| 70 |
|
| 71 |
public Object getPointerObject(){ |
| 72 |
return parser; |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public void setPipelineConfiguration(PipelineConfiguration pipe) { |
| 77 |
this.pipe = pipe; |
| 78 |
final Configuration config = pipe.getConfiguration(); |
| 79 |
expandDefaults = config.isExpandAttributeDefaults(); |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public PipelineConfiguration getPipelineConfiguration() { |
| 84 |
return pipe; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public native boolean Read(Object parseri); |
| 90 |
|
| 91 |
public native int get_NodeType(long parserPtri); |
| 92 |
|
| 93 |
public native boolean get_IsEmptyElement(long parserPtri); |
| 94 |
|
| 95 |
public native boolean get_EOF(long parserPtri); |
| 96 |
|
| 97 |
public native int get_Depth(long parserPtri); |
| 98 |
|
| 99 |
public native void parser_close(long parserPtri); |
| 100 |
|
| 101 |
public native String[] get_Attributes(long parserPtri, boolean expandDefault); |
| 102 |
|
| 103 |
public native String get_Value(long parserPtri); |
| 104 |
|
| 105 |
public native String get_NodeName(long parserPtr); |
| 106 |
|
| 107 |
@Override |
| 108 |
public int next() throws XPathException { |
| 109 |
|
| 110 |
if (current == START_OF_INPUT) { |
| 111 |
current = START_DOCUMENT; |
| 112 |
return current; |
| 113 |
} else if (current == END_DOCUMENT || current == END_OF_INPUT) { |
| 114 |
current = END_OF_INPUT; |
| 115 |
return current; |
| 116 |
} else if (current == START_ELEMENT && isEmptyElement) { |
| 117 |
current = END_ELEMENT; |
| 118 |
return current; |
| 119 |
} |
| 120 |
|
| 121 |
do { |
| 122 |
|
| 123 |
Read(parser); |
| 124 |
|
| 125 |
|
| 126 |
if (false) { |
| 127 |
XPathException de = new XPathException("Error reported by XML parser: dummy"); |
| 128 |
throw de; |
| 129 |
|
| 130 |
} |
| 131 |
|
| 132 |
if (false) { |
| 133 |
XPathException de = new XPathException("Validation error reported by XML parser: "); |
| 134 |
|
| 135 |
throw de; |
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
int intype = get_NodeType(parserPtr); |
| 140 |
isEmptyElement = get_IsEmptyElement(parserPtr); |
| 141 |
if (get_EOF(parserPtr)) { |
| 142 |
current = END_DOCUMENT; |
| 143 |
return current; |
| 144 |
} |
| 145 |
if (intype == JetXmlNodeType.EntityReference) { |
| 146 |
|
| 147 |
current = -1; |
| 148 |
} else { |
| 149 |
current = mapInputKindToOutputKind(intype); |
| 150 |
if (current == TEXT && get_Depth(parserPtr) == 0) { |
| 151 |
current = -1; |
| 152 |
} |
| 153 |
} |
| 154 |
} while (current == -1); |
| 155 |
return current; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
private int mapInputKindToOutputKind(int in) { |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
switch (in) { |
| 172 |
case JetXmlNodeType.Attribute: |
| 173 |
return PullProvider.ATTRIBUTE; |
| 174 |
case JetXmlNodeType.CDATA: |
| 175 |
return PullProvider.TEXT; |
| 176 |
case JetXmlNodeType.Comment: |
| 177 |
return PullProvider.COMMENT; |
| 178 |
case JetXmlNodeType.Document: |
| 179 |
return PullProvider.START_DOCUMENT; |
| 180 |
case JetXmlNodeType.DocumentFragment: |
| 181 |
return PullProvider.START_DOCUMENT; |
| 182 |
case JetXmlNodeType.Element: |
| 183 |
return PullProvider.START_ELEMENT; |
| 184 |
case JetXmlNodeType.EndElement: |
| 185 |
return PullProvider.END_ELEMENT; |
| 186 |
case JetXmlNodeType.ProcessingInstruction: |
| 187 |
return PullProvider.PROCESSING_INSTRUCTION; |
| 188 |
case JetXmlNodeType.SignificantWhitespace: |
| 189 |
|
| 190 |
return PullProvider.TEXT; |
| 191 |
case JetXmlNodeType.Text: |
| 192 |
return PullProvider.TEXT; |
| 193 |
case JetXmlNodeType.Whitespace: |
| 194 |
|
| 195 |
return PullProvider.TEXT; |
| 196 |
|
| 197 |
default: |
| 198 |
return -1; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
@Override |
| 203 |
public int current() { |
| 204 |
return current; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
public String getSystemId() { |
| 219 |
String base = null; |
| 220 |
if (base == null || base.isEmpty()) { |
| 221 |
return baseURI; |
| 222 |
} else { |
| 223 |
return base; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
@Override |
| 228 |
public AttributeCollection getAttributes() throws XPathException { |
| 229 |
|
| 230 |
String[] attributes = get_Attributes(parserPtr, expandDefaults); |
| 231 |
|
| 232 |
if (attributes != null && attributes.length > 0) { |
| 233 |
int attrLen = attributes.length; |
| 234 |
AttributeCollectionImpl atts = new AttributeCollectionImpl(pipe.getConfiguration()); |
| 235 |
for (int i = 0; i < attrLen; i++) { |
| 236 |
String result = attributes[i]; |
| 237 |
int openingBracket = result.indexOf("{"); |
| 238 |
int closingBracket = result.indexOf("}"); |
| 239 |
int valueIndex = result.indexOf("="); |
| 240 |
String prefix = result.substring(0, openingBracket); |
| 241 |
String namespaceURI = result.substring(result.indexOf("{") + 1, closingBracket); |
| 242 |
String localName = result.substring(closingBracket + 1, valueIndex); |
| 243 |
String value = result.substring(valueIndex + 1); |
| 244 |
NodeName nc = new FingerprintedQName(prefix, namespaceURI, localName); |
| 245 |
|
| 246 |
atts.addAttribute(nc, BuiltInAtomicType.UNTYPED_ATOMIC, value, ExplicitLocation.UNKNOWN_LOCATION, 0); |
| 247 |
} |
| 248 |
return atts; |
| 249 |
} else { |
| 250 |
return AttributeCollectionImpl.EMPTY_ATTRIBUTE_COLLECTION; |
| 251 |
} |
| 252 |
|
| 253 |
} |
| 254 |
|
| 255 |
@Override |
| 256 |
public NamespaceBinding[] getNamespaceDeclarations() throws XPathException { |
| 257 |
return new NamespaceBinding[0]; |
| 258 |
} |
| 259 |
|
| 260 |
@Override |
| 261 |
public int skipToMatchingEnd() throws XPathException { |
| 262 |
return 0; |
| 263 |
} |
| 264 |
|
| 265 |
@Override |
| 266 |
public void close() { |
| 267 |
parser_close(parserPtr); |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
@Override |
| 272 |
public NodeName getNodeName() { |
| 273 |
String result = get_NodeName(parserPtr); |
| 274 |
int openingBracket = result.indexOf("{"); |
| 275 |
int closingBracket = result.indexOf("}"); |
| 276 |
String prefix = result.substring(0, openingBracket); |
| 277 |
String namespaceURI = result.substring(result.indexOf("{") + 1, closingBracket); |
| 278 |
String localName = result.substring(closingBracket + 1); |
| 279 |
return new FingerprintedQName(prefix, namespaceURI, localName); |
| 280 |
} |
| 281 |
|
| 282 |
@Override |
| 283 |
public CharSequence getStringValue() throws XPathException { |
| 284 |
if (current == TEXT) { |
| 285 |
return CompressedWhitespace.compress(get_Value(parserPtr)); |
| 286 |
} else { |
| 287 |
return get_Value(parserPtr); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
@Override |
| 292 |
public SchemaType getSchemaType() { |
| 293 |
return Untyped.getInstance(); |
| 294 |
} |
| 295 |
|
| 296 |
@Override |
| 297 |
public AtomicValue getAtomicValue() { |
| 298 |
return null; |
| 299 |
} |
| 300 |
|
| 301 |
@Override |
| 302 |
public Location getSourceLocator() { |
| 303 |
return null; |
| 304 |
} |
| 305 |
|
| 306 |
@Override |
| 307 |
public List<UnparsedEntity> getUnparsedEntities() { |
| 308 |
return null; |
| 309 |
} |
| 310 |
} |