Skip to content
Browse files

Isolate code ab bit so it can be reused later

  • Loading branch information...
1 parent dca4b93 commit 710823dfbff3efca50088a020d26b9f80473178e @dizzzz dizzzz committed Aug 15, 2014
Showing with 53 additions and 47 deletions.
  1. +53 −47 src/org/exist/xquery/functions/fn/ParsingFunctions.java
View
100 src/org/exist/xquery/functions/fn/ParsingFunctions.java
@@ -94,52 +94,58 @@ public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathExce
xmlContent = "<root>" + xmlContent + "</root>";
}
- final StringReader reader = new StringReader(xmlContent);
- final ValidationReport report = new ValidationReport();
- final SAXAdapter adapter = new SAXAdapter(context);
- try {
- final SAXParserFactory factory = SAXParserFactory.newInstance();
- factory.setNamespaceAware(true);
- final InputSource src = new InputSource(reader);
-
- XMLReader xr = null;
-
- if (xr == null) {
- final SAXParser parser = factory.newSAXParser();
- xr = parser.getXMLReader();
- }
-
- xr.setErrorHandler(report);
- xr.setContentHandler(adapter);
- xr.setProperty(Namespaces.SAX_LEXICAL_HANDLER, adapter);
- xr.parse(src);
- } catch (final ParserConfigurationException e) {
- throw new XPathException(this, ErrorCodes.EXXQDY0002, "Error while constructing XML parser: "
- + e.getMessage(), args[0], e);
- } catch (final SAXException e) {
- logger.debug("Error while parsing XML: " + e.getMessage(), e);
- } catch (final IOException e) {
- throw new XPathException(this, ErrorCodes.FODC0006, ErrorCodes.FODC0006.getDescription() + ": " + e.getMessage(),
- args[0], e);
- }
-
- if (report.isValid()) {
- if (isCalledAs("parse-xml-fragment")) {
- resultSequence = new ValueSequence();
- NodeList children = adapter.getDocument().getDocumentElement().getChildNodes();
- for (int i = 0, il = children.getLength(); i < il; i++) {
- Node child = children.item(i);
- resultSequence.add((NodeValue)child);
- }
-
- return resultSequence;
- } else {
- return (DocumentImpl) adapter.getDocument();
- }
- } else {
- final MemTreeBuilder builder = context.getDocumentBuilder();
- final NodeImpl result = Shared.writeReport(report, builder);
- throw new XPathException(this, ErrorCodes.FODC0006, ErrorCodes.FODC0006.getDescription() + ": " + report.toString(), result);
- }
+ final StringReader reader = new StringReader(xmlContent);
+ final InputSource src = new InputSource(reader);
+
+ return parse(src, context, args);
}
+
+ private Sequence parse(final InputSource src, XQueryContext theContext, Sequence[] args) throws XPathException {
+ Sequence resultSequence;
+ final ValidationReport report = new ValidationReport();
+ final SAXAdapter adapter = new SAXAdapter(theContext);
+ try {
+ final SAXParserFactory factory = SAXParserFactory.newInstance();
+ factory.setNamespaceAware(true);
+
+ XMLReader xr = null;
+
+ if (xr == null) {
+ final SAXParser parser = factory.newSAXParser();
+ xr = parser.getXMLReader();
+ }
+
+ xr.setErrorHandler(report);
+ xr.setContentHandler(adapter);
+ xr.setProperty(Namespaces.SAX_LEXICAL_HANDLER, adapter);
+ xr.parse(src);
+
+ } catch (final ParserConfigurationException e) {
+ throw new XPathException(this, ErrorCodes.EXXQDY0002, "Error while constructing XML parser: " + e.getMessage(), args[0], e);
+ } catch (final SAXException e) {
+ logger.debug("Error while parsing XML: " + e.getMessage(), e);
+ } catch (final IOException e) {
+ throw new XPathException(this, ErrorCodes.FODC0006, ErrorCodes.FODC0006.getDescription() + ": " + e.getMessage(),
+ args[0], e);
+ }
+
+ if (report.isValid()) {
+ if (isCalledAs("parse-xml-fragment")) {
+ resultSequence = new ValueSequence();
+ NodeList children = adapter.getDocument().getDocumentElement().getChildNodes();
+ for (int i = 0, il = children.getLength(); i < il; i++) {
+ Node child = children.item(i);
+ resultSequence.add((NodeValue)child);
+ }
+
+ return resultSequence;
+ } else {
+ return (DocumentImpl) adapter.getDocument();
+ }
+ } else {
+ final MemTreeBuilder builder = theContext.getDocumentBuilder();
+ final NodeImpl result = Shared.writeReport(report, builder);
+ throw new XPathException(this, ErrorCodes.FODC0006, ErrorCodes.FODC0006.getDescription() + ": " + report.toString(), result);
+ }
+ }
}

0 comments on commit 710823d

Please sign in to comment.
Something went wrong with that request. Please try again.