/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*
* $Id: XUpdateQueryResolver.java 511426 2007-02-25 03:25:02Z vgritsenko $
*/
package org.apache.xindice.core.xupdate;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.xindice.core.Collection;
import org.apache.xindice.core.data.Key;
import org.apache.xindice.core.data.NodeSet;
import org.apache.xindice.core.query.CompilationException;
import org.apache.xindice.core.query.NodeListSet;
import org.apache.xindice.core.query.ProcessingException;
import org.apache.xindice.core.query.Query;
import org.apache.xindice.core.query.QueryEngine;
import org.apache.xindice.core.query.QueryException;
import org.apache.xindice.core.query.QueryResolver;
import org.apache.xindice.util.Configuration;
import org.apache.xindice.util.SimpleConfigurable;
import org.apache.xindice.util.XindiceException;
import org.apache.xindice.xml.NamespaceMap;
import org.apache.xindice.xml.NodeSource;
import org.apache.xindice.xml.dom.DocumentImpl;
import org.apache.xindice.xml.dom.NodeImpl;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
/**
* XUpdateQueryResolver
*
* @version $Revision: 511426 $, $Date: 2007-02-24 22:25:02 -0500 (Sat, 24 Feb 2007) $
*/
public class XUpdateQueryResolver extends SimpleConfigurable implements QueryResolver {
private static final Log log = LogFactory.getLog(XUpdateQueryResolver.class);
private static final String XUPDATE_XPATH_PROP = "org.xmldb.common.xml.queries.XPathQueryFactory";
private static final String XUPDATE_XPATH_IMPL = "org.apache.xindice.core.xupdate.XPathQueryFactoryImpl";
static {
System.setProperty(XUPDATE_XPATH_PROP, XUPDATE_XPATH_IMPL);
}
public static final String STYLE_XUPDATE = "XUpdate";
public void setConfig(Configuration config) throws XindiceException {
super.setConfig(config);
}
public String getQueryStyle() {
return STYLE_XUPDATE;
}
public void setQueryEngine(QueryEngine engine) {
// FIXME Not used this.engine = engine;
}
public Query compileQuery(Collection context, String query, NamespaceMap nsMap, Key[] keys) throws QueryException {
return new XUpdateQuery(context, query, nsMap, keys);
}
public NodeSet query(Collection context, String query, NamespaceMap nsMap, Key[] keys) throws QueryException {
XUpdateQuery xq = new XUpdateQuery(context, query, nsMap, keys);
return xq.execute();
}
/**
* XUpdateQuery
*/
private class XUpdateQuery implements Query {
public Collection context;
public String query;
public NamespaceMap nsMap;
public XUpdateImpl xu;
public Key[] keys;
public XUpdateQuery(Collection context, String query, NamespaceMap nsMap, Key[] keys) throws QueryException {
this.context = context;
this.query = query;
this.nsMap = nsMap;
this.keys = keys;
try {
xu = new XUpdateImpl();
xu.setQString(query);
xu.setNamespaceMap(nsMap);
} catch (Exception e) {
if (e instanceof QueryException) {
throw (QueryException) e.fillInStackTrace();
}
throw new CompilationException("Error Compiling XUpdate Query", e);
}
}
public String getQueryStyle() {
return STYLE_XUPDATE;
}
public Collection getQueryContext() {
return context;
}
public String getQueryString() {
return query;
}
public NamespaceMap getNamespaceMap() {
return nsMap;
}
public Key[] getKeySet() {
return keys;
}
public NodeSet execute() throws QueryException {
try {
if (keys != null) {
try {
for (int i = 0; i < keys.length; i++) {
Document doc = context.getDocument(keys[i]);
xu.execute(doc.getDocumentElement());
context.setDocument(keys[i], doc);
}
} catch (Exception e) {
if (log.isWarnEnabled()) {
log.warn("ignored exception", e);
}
}
} else {
xu.execute(context);
}
DocumentImpl doc = new DocumentImpl();
Element elem = doc.createElementNS(NodeSource.SOURCE_NS, "src:" + NodeSource.SOURCE_MODIFIED);
elem.setAttribute(NodeImpl.XMLNS_PREFIX + ":src", NodeSource.SOURCE_NS);
doc.appendChild(elem);
Text count = doc.createTextNode(Integer.toString(xu.getModifiedCount()));
elem.appendChild(count);
return new NodeListSet(doc.getChildNodes());
} catch (Exception e) {
if (e instanceof QueryException) {
throw (QueryException) e.fillInStackTrace();
}
throw new ProcessingException("Error executing XUpdate query", e);
}
}
}
}