Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.xmlbeans.impl.xpath.saxon;
import net.sf.saxon.Configuration;
import net.sf.saxon.dom.DocumentWrapper;
import net.sf.saxon.dom.NodeOverNodeInfo;
import net.sf.saxon.ma.map.HashTrieMap;
import net.sf.saxon.om.Item;
import net.sf.saxon.om.NodeInfo;
import net.sf.saxon.om.StructuredQName;
import net.sf.saxon.query.DynamicQueryContext;
import net.sf.saxon.query.StaticQueryContext;
import net.sf.saxon.query.XQueryExpression;
import net.sf.saxon.type.BuiltInAtomicType;
import net.sf.saxon.value.*;
import org.apache.xmlbeans.*;
import org.apache.xmlbeans.impl.store.Cur;
import org.apache.xmlbeans.impl.store.Cursor;
import org.apache.xmlbeans.impl.store.Locale;
import org.apache.xmlbeans.impl.xpath.XQuery;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.Duration;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.namespace.QName;
import javax.xml.transform.TransformerException;
import javax.xml.xpath.XPathException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URI;
import java.util.Date;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
public class SaxonXQuery implements XQuery {
private final XQueryExpression xquery;
private final String contextVar;
private final Configuration config;
private Cur _cur;
private long _version;
private XmlOptions _options;
/**
* Construct given an XQuery expression string.
*
* @param query The XQuery expression
* @param contextVar The name of the context variable
* @param boundary The offset of the end of the prolog
*/
public SaxonXQuery(final String query, String contextVar, Integer boundary, XmlOptions xmlOptions) {
assert !(contextVar.startsWith(".") || contextVar.startsWith(".."));
_options = xmlOptions;
config = new Configuration();
StaticQueryContext sc = config.newStaticQueryContext();
Map nsMap = xmlOptions.getLoadAdditionalNamespaces();
if (nsMap != null) {
nsMap.forEach(sc::declareNamespace);
}
this.contextVar = contextVar;
//Saxon requires external variables at the end of the prolog...
try {
xquery = sc.compileQuery(
query.substring(0, boundary) + " declare variable $" + contextVar + " external;" + query.substring(boundary)
);
} catch (TransformerException e) {
throw new XmlRuntimeException(e);
}
}
public XmlObject[] objectExecute(Cur c, XmlOptions options) {
_version = c.getLocale().version();
_cur = c.weakCur(this);
this._options = options;
return objectExecute();
}
public XmlCursor cursorExecute(Cur c, XmlOptions options) {
_version = c.getLocale().version();
_cur = c.weakCur(this);
this._options = options;
return cursorExecute();
}
public List execQuery(Object node, Map variableBindings) {
try {
Node contextNode = (Node) node;
Document dom = (contextNode.getNodeType() == Node.DOCUMENT_NODE)
? (Document) contextNode : contextNode.getOwnerDocument();
DocumentWrapper docWrapper = new DocumentWrapper(dom, null, config);
NodeInfo root = docWrapper.wrap(contextNode);
DynamicQueryContext dc = new DynamicQueryContext(config);
dc.setContextItem(root);
dc.setParameter(new StructuredQName("", null, contextVar), root);
// Set the other variables
if (variableBindings != null) {
for (Map.Entry me : ((Map) variableBindings).entrySet()) {
StructuredQName key = new StructuredQName("", null, me.getKey());
Object value = me.getValue();
if (value instanceof XmlTokenSource) {
Node paramObject = ((XmlTokenSource) value).getDomNode();
dc.setParameter(key, docWrapper.wrap(paramObject));
} else {
try {
dc.setParameter(key, objectToItem(value, config));
} catch (XPathException e) {
throw new RuntimeException(e);
}
}
}
}
List