Clover coverage report -
Coverage timestamp: Sun Nov 1 2009 23:08:24 UTC
file stats: LOC: 94   Methods: 1
NCLOC: 47   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
FuncFTContains.java 50% 57.1% 100% 57.1%
coverage coverage
 1    /*
 2    * Licensed to the Apache Software Foundation (ASF) under one or more
 3    * contributor license agreements. See the NOTICE file distributed with
 4    * this work for additional information regarding copyright ownership.
 5    * The ASF licenses this file to You under the Apache License, Version 2.0
 6    * (the "License"); you may not use this file except in compliance with
 7    * the License. You may obtain a copy of the License at
 8    *
 9    * http://www.apache.org/licenses/LICENSE-2.0
 10    *
 11    * Unless required by applicable law or agreed to in writing, software
 12    * distributed under the License is distributed on an "AS IS" BASIS,
 13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14    * See the License for the specific language governing permissions and
 15    * limitations under the License.
 16    *
 17    * $Id: FuncFTContains.java 586647 2007-10-20 00:32:43Z natalia $
 18    */
 19   
 20    package org.apache.xindice.core.query;
 21   
 22    import org.apache.xpath.XPathContext;
 23    import org.apache.xpath.functions.FunctionOneArg;
 24    import org.apache.xpath.objects.XObject;
 25    import org.apache.xpath.objects.XBoolean;
 26    import org.apache.lucene.queryParser.ParseException;
 27    import org.apache.lucene.analysis.Analyzer;
 28    import org.apache.xindice.core.query.ftsearch.Searcher;
 29    import org.apache.xindice.core.data.NodeSet;
 30    import org.apache.xindice.core.Collection;
 31    import org.apache.xindice.core.indexer.Indexer;
 32    import org.apache.xindice.core.indexer.LuceneIndexer;
 33    import org.apache.xindice.xml.dom.NodeListImpl;
 34    import org.w3c.dom.Node;
 35   
 36    import javax.xml.transform.TransformerException;
 37   
 38    /**
 39    * Full text search extention function for XPath.
 40    *
 41    * @version $Revision: 586647 $, $Date: 2007-10-20 00:32:43 +0000 (Sat, 20 Oct 2007) $
 42    */
 43    public class FuncFTContains extends FunctionOneArg {
 44   
 45    /**
 46    * Execute text search function. The function returns XBoolean.S_TRUE
 47    * if the current node text matches the query, XBoolean.S_FALSE otherwise.
 48    * @param xctxt The current execution context.
 49    * @return XBoolean.S_TRUE or XBoolean.S_FALSE.
 50    *
 51    * @throws javax.xml.transform.TransformerException
 52    */
 53  44 public XObject execute(XPathContext xctxt) throws TransformerException {
 54   
 55  44 XPathQueryResolver.XPathResolverContext ctxt = (XPathQueryResolver.XPathResolverContext) xctxt;
 56   
 57    // it is not guaranteed that analyzer will be set at this time, even if there is
 58    // a suitable index in the collection, for various reasons XPathQueryResolver may
 59    // decide not to run optimization for the function
 60  44 Analyzer analyzer = (Analyzer) ctxt.getParameter(XPathQueryResolver.PARAM_ANALYZER);
 61  44 if (analyzer == null) {
 62  0 try {
 63  0 Collection collection = (Collection) ctxt.getParameter(XPathQueryResolver.PARAM_COLLECTION);
 64  0 Indexer idx = collection.getIndexManager().getBestIndexer(Indexer.STYLE_FULLTEXT, null);
 65  0 if (idx instanceof LuceneIndexer) {
 66  0 analyzer = ((LuceneIndexer) idx).getAnalyzer();
 67    } else {
 68  0 analyzer = (Analyzer) Class.forName(LuceneIndexer.DEFANALYZER).newInstance();
 69    }
 70   
 71    // parameters are intentionally altered so next node or next document won't need
 72    // to do an analyzer lookup
 73  0 ctxt.setParameter(XPathQueryResolver.PARAM_ANALYZER, analyzer);
 74    } catch (Exception e) {
 75  0 throw new TransformerException("Could not get text analyzer");
 76    }
 77    }
 78   
 79  44 String query = getArg0().execute(xctxt).str();
 80  44 try {
 81  44 int ctxtNode = xctxt.getCurrentNode();
 82  44 Node node = xctxt.getDTM(ctxtNode).getNode(ctxtNode);
 83  44 NodeListImpl list = new NodeListImpl(null);
 84  44 list.add(node);
 85   
 86  44 Searcher searcher = new Searcher(list, analyzer);
 87  44 NodeSet nodes = searcher.search(query);
 88   
 89  44 return nodes.hasMoreNodes() ? XBoolean.S_TRUE : XBoolean.S_FALSE;
 90    } catch (ParseException e) {
 91  0 throw new TransformerException("Error in text query", e);
 92    }
 93    }
 94    }