| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.functions; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.Controller; |
| 12 |
import net.sf.saxon.event.Builder; |
| 13 |
import net.sf.saxon.event.Receiver; |
| 14 |
import net.sf.saxon.event.Sender; |
| 15 |
import net.sf.saxon.expr.Callable; |
| 16 |
import net.sf.saxon.expr.PackageData; |
| 17 |
import net.sf.saxon.expr.XPathContext; |
| 18 |
import net.sf.saxon.lib.ParseOptions; |
| 19 |
import net.sf.saxon.om.*; |
| 20 |
import net.sf.saxon.style.StylesheetPackage; |
| 21 |
import net.sf.saxon.trans.XPathException; |
| 22 |
import net.sf.saxon.tree.tiny.TinyDocumentImpl; |
| 23 |
import net.sf.saxon.value.ObjectValue; |
| 24 |
import net.sf.saxon.value.SequenceExtent; |
| 25 |
import net.sf.saxon.value.StringValue; |
| 26 |
import org.xml.sax.ErrorHandler; |
| 27 |
import org.xml.sax.InputSource; |
| 28 |
import org.xml.sax.SAXParseException; |
| 29 |
|
| 30 |
import javax.xml.transform.Source; |
| 31 |
import javax.xml.transform.sax.SAXSource; |
| 32 |
import java.io.StringReader; |
| 33 |
import java.util.ArrayList; |
| 34 |
import java.util.List; |
| 35 |
|
| 36 |
public class ParseXml extends SystemFunction implements Callable { |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public ZeroOrOne<NodeInfo> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 48 |
StringValue input = (StringValue) arguments[0].head(); |
| 49 |
return input == null ? ZeroOrOne.empty() : new ZeroOrOne<>(evalParseXml(input, context)); |
| 50 |
} |
| 51 |
|
| 52 |
private NodeInfo evalParseXml(StringValue inputArg, XPathContext context) throws XPathException { |
| 53 |
String baseURI = getRetainedStaticContext().getStaticBaseUriString(); |
| 54 |
|
| 55 |
RetentiveErrorHandler errorHandler = new RetentiveErrorHandler(); |
| 56 |
try { |
| 57 |
Controller controller = context.getController(); |
| 58 |
if (controller == null) { |
| 59 |
throw new XPathException("parse-xml() function is not available in this environment"); |
| 60 |
} |
| 61 |
Configuration config = controller.getConfiguration(); |
| 62 |
|
| 63 |
StringReader sr = new StringReader(inputArg.getStringValue()); |
| 64 |
InputSource is = new InputSource(sr); |
| 65 |
is.setSystemId(baseURI); |
| 66 |
Source source = new SAXSource(is); |
| 67 |
source.setSystemId(baseURI); |
| 68 |
|
| 69 |
Builder b = TreeModel.TINY_TREE.makeBuilder(controller.makePipelineConfiguration()); |
| 70 |
Receiver s = b; |
| 71 |
ParseOptions options = new ParseOptions(config.getParseOptions()); |
| 72 |
PackageData pd = getRetainedStaticContext().getPackageData(); |
| 73 |
if (pd instanceof StylesheetPackage) { |
| 74 |
options.setSpaceStrippingRule(((StylesheetPackage) pd).getSpaceStrippingRule()); |
| 75 |
if (((StylesheetPackage)pd).isStripsTypeAnnotations()) { |
| 76 |
s = config.getAnnotationStripper(s); |
| 77 |
} |
| 78 |
} else { |
| 79 |
options.setSpaceStrippingRule(IgnorableSpaceStrippingRule.getInstance()); |
| 80 |
} |
| 81 |
options.setErrorHandler(errorHandler); |
| 82 |
|
| 83 |
s.setPipelineConfiguration(b.getPipelineConfiguration()); |
| 84 |
|
| 85 |
Sender.send(source, s, options); |
| 86 |
|
| 87 |
TinyDocumentImpl node = (TinyDocumentImpl) b.getCurrentRoot(); |
| 88 |
node.setBaseURI(baseURI); |
| 89 |
node.getTreeInfo().setUserData("saxon:document-uri", ""); |
| 90 |
b.reset(); |
| 91 |
return node; |
| 92 |
} catch (XPathException err) { |
| 93 |
XPathException xe = new XPathException("First argument to parse-xml() is not a well-formed and namespace-well-formed XML document. XML parser reported: " + |
| 94 |
err.getMessage(), "FODC0006"); |
| 95 |
errorHandler.captureRetainedErrors(xe); |
| 96 |
xe.maybeSetContext(context); |
| 97 |
throw xe; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
public static class RetentiveErrorHandler implements ErrorHandler { |
| 102 |
|
| 103 |
public List<SAXParseException> errors = new ArrayList<>(); |
| 104 |
public boolean failed = false; |
| 105 |
|
| 106 |
public void error(SAXParseException exception) { |
| 107 |
errors.add(exception); |
| 108 |
} |
| 109 |
|
| 110 |
public void warning(SAXParseException exception) { |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
public void fatalError(SAXParseException exception) { |
| 115 |
errors.add(exception); |
| 116 |
failed = true; |
| 117 |
} |
| 118 |
|
| 119 |
public void captureRetainedErrors(XPathException xe) { |
| 120 |
List<SAXParseException> retainedErrors = errors; |
| 121 |
if (!retainedErrors.isEmpty()) { |
| 122 |
List<ObjectValue<SAXParseException>> wrappedErrors = new ArrayList<>(); |
| 123 |
for (SAXParseException e : retainedErrors) { |
| 124 |
wrappedErrors.add(new ObjectValue<>(e)); |
| 125 |
} |
| 126 |
xe.setErrorObject(SequenceExtent.makeSequenceExtent(wrappedErrors)); |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
|