/*
* 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: FuncFTContains.java 586647 2007-10-20 00:32:43Z natalia $
*/
package org.apache.xindice.core.query;
import org.apache.xpath.XPathContext;
import org.apache.xpath.functions.FunctionOneArg;
import org.apache.xpath.objects.XObject;
import org.apache.xpath.objects.XBoolean;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.analysis.Analyzer;
import org.apache.xindice.core.query.ftsearch.Searcher;
import org.apache.xindice.core.data.NodeSet;
import org.apache.xindice.core.Collection;
import org.apache.xindice.core.indexer.Indexer;
import org.apache.xindice.core.indexer.LuceneIndexer;
import org.apache.xindice.xml.dom.NodeListImpl;
import org.w3c.dom.Node;
import javax.xml.transform.TransformerException;
/**
* Full text search extention function for XPath.
*
* @version $Revision: 586647 $, $Date: 2007-10-19 20:32:43 -0400 (Fri, 19 Oct 2007) $
*/
public class FuncFTContains extends FunctionOneArg {
/**
* Execute text search function. The function returns XBoolean.S_TRUE
* if the current node text matches the query, XBoolean.S_FALSE otherwise.
* @param xctxt The current execution context.
* @return XBoolean.S_TRUE or XBoolean.S_FALSE.
*
* @throws javax.xml.transform.TransformerException
*/
public XObject execute(XPathContext xctxt) throws TransformerException {
XPathQueryResolver.XPathResolverContext ctxt = (XPathQueryResolver.XPathResolverContext) xctxt;
// it is not guaranteed that analyzer will be set at this time, even if there is
// a suitable index in the collection, for various reasons XPathQueryResolver may
// decide not to run optimization for the function
Analyzer analyzer = (Analyzer) ctxt.getParameter(XPathQueryResolver.PARAM_ANALYZER);
if (analyzer == null) {
try {
Collection collection = (Collection) ctxt.getParameter(XPathQueryResolver.PARAM_COLLECTION);
Indexer idx = collection.getIndexManager().getBestIndexer(Indexer.STYLE_FULLTEXT, null);
if (idx instanceof LuceneIndexer) {
analyzer = ((LuceneIndexer) idx).getAnalyzer();
} else {
analyzer = (Analyzer) Class.forName(LuceneIndexer.DEFANALYZER).newInstance();
}
// parameters are intentionally altered so next node or next document won't need
// to do an analyzer lookup
ctxt.setParameter(XPathQueryResolver.PARAM_ANALYZER, analyzer);
} catch (Exception e) {
throw new TransformerException("Could not get text analyzer");
}
}
String query = getArg0().execute(xctxt).str();
try {
int ctxtNode = xctxt.getCurrentNode();
Node node = xctxt.getDTM(ctxtNode).getNode(ctxtNode);
NodeListImpl list = new NodeListImpl(null);
list.add(node);
Searcher searcher = new Searcher(list, analyzer);
NodeSet nodes = searcher.search(query);
return nodes.hasMoreNodes() ? XBoolean.S_TRUE : XBoolean.S_FALSE;
} catch (ParseException e) {
throw new TransformerException("Error in text query", e);
}
}
}