Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/eXist-db/exist into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfgangmm committed Mar 2, 2014
2 parents 5b8a5e6 + 2aa3748 commit 1ae8b49
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/org/exist/xquery/functions/fn/FnModule.java
Expand Up @@ -221,7 +221,9 @@ public class FnModule extends AbstractInternalModule {
new FunctionDef(FunHigherOrderFun.signatures[3], FunHigherOrderFun.class),
new FunctionDef(FunHigherOrderFun.signatures[4], FunHigherOrderFun.class),
new FunctionDef(FunEnvironment.signature[0], FunEnvironment.class),
new FunctionDef(FunEnvironment.signature[1], FunEnvironment.class),
new FunctionDef(FunEnvironment.signature[1], FunEnvironment.class),
new FunctionDef(ParsingFunctions.signatures[0], ParsingFunctions.class),
new FunctionDef(ParsingFunctions.signatures[1], ParsingFunctions.class)
};

static {
Expand Down
145 changes: 145 additions & 0 deletions src/org/exist/xquery/functions/fn/ParsingFunctions.java
@@ -0,0 +1,145 @@
/*
* eXist Open Source Native XML Database
* Copyright (C) 2001-09 The eXist Project
* http://exist-db.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* $Id$
*/
package org.exist.xquery.functions.fn;

import org.apache.log4j.Logger;
import org.exist.Namespaces;
import org.exist.dom.QName;
import org.exist.memtree.DocumentImpl;
import org.exist.memtree.MemTreeBuilder;
import org.exist.memtree.NodeImpl;
import org.exist.memtree.SAXAdapter;
import org.exist.validation.ValidationReport;
import org.exist.xquery.*;
import org.exist.xquery.functions.validation.Shared;
import org.exist.xquery.value.FunctionParameterSequenceType;
import org.exist.xquery.value.FunctionReturnSequenceType;
import org.exist.xquery.value.NodeValue;
import org.exist.xquery.value.Sequence;
import org.exist.xquery.value.SequenceType;
import org.exist.xquery.value.Type;
import org.exist.xquery.value.ValueSequence;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;
import java.io.StringReader;

public class ParsingFunctions extends BasicFunction {

protected static final FunctionReturnSequenceType RESULT_TYPE_FOR_PARSE_XML = new FunctionReturnSequenceType(Type.DOCUMENT,
Cardinality.ZERO_OR_ONE, "the parsed document");
protected static final FunctionReturnSequenceType RESULT_TYPE_FOR_PARSE_XML_FRAGMENT = new FunctionReturnSequenceType(Type.ELEMENT,
Cardinality.ZERO_OR_MORE, "the parsed document fragment");

protected static final FunctionParameterSequenceType TO_BE_PARSED_PARAMETER = new FunctionParameterSequenceType(
"arg", Type.STRING, Cardinality.ZERO_OR_ONE, "The string to be parsed");

protected static final Logger logger = Logger.getLogger(ParsingFunctions.class);

public final static FunctionSignature signatures[] = {
new FunctionSignature(
new QName("parse-xml", Function.BUILTIN_FUNCTION_NS),
"This function takes as input an XML document represented as a string,"
+ " and returns the document node at the root of an XDM tree representing the parsed document.",
new SequenceType[] { TO_BE_PARSED_PARAMETER }, RESULT_TYPE_FOR_PARSE_XML),
new FunctionSignature(
new QName("parse-xml-fragment", Function.BUILTIN_FUNCTION_NS),
"This function takes as input an XML external entity represented as a string," +
"and returns the document node at the root of an XDM tree representing the parsed document fragment.",
new SequenceType[] { TO_BE_PARSED_PARAMETER }, RESULT_TYPE_FOR_PARSE_XML_FRAGMENT) };

public ParsingFunctions(XQueryContext context, FunctionSignature signature) {
super(context, signature);
}

public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {

Sequence resultSequence;

if (args[0].getItemCount() == 0) {
return Sequence.EMPTY_SEQUENCE;
}
String xmlContent = args[0].itemAt(0).getStringValue();
if (xmlContent.length() == 0) {
return Sequence.EMPTY_SEQUENCE;
}

if (isCalledAs("parse-xml-fragment")) {
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.EXXQDY0002, "Error while parsing XML: " + 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.EXXQDY0002, report.toString(), result);
}
}
}

0 comments on commit 1ae8b49

Please sign in to comment.