Added more helper methods to DOMUtils and cleaned up error handling in builders - thanks Chris.

Moved FilteredQueryBuilder.java to "builders" package with all other builders

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@382170 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mark Harwood 2006-03-01 21:49:17 +00:00
parent 738103d83d
commit 7dab9545be
13 changed files with 159 additions and 185 deletions

View File

@ -10,6 +10,7 @@ import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Query;
import org.apache.lucene.xmlparser.builders.BooleanQueryBuilder;
import org.apache.lucene.xmlparser.builders.ConstantScoreQueryBuilder;
import org.apache.lucene.xmlparser.builders.FilteredQueryBuilder;
import org.apache.lucene.xmlparser.builders.RangeFilterBuilder;
import org.apache.lucene.xmlparser.builders.SpanFirstBuilder;
import org.apache.lucene.xmlparser.builders.SpanNearBuilder;

View File

@ -11,6 +11,65 @@ import org.xml.sax.InputSource;
public class DOMUtils
{
public static Element getChildByTagOrFail(Element e, String name) throws ParserException
{
Element kid = getChildByTagName(e, name);
if (null == kid)
{
throw new ParserException(e.getTagName() + " missing \"" + name
+ "\" child element");
}
return kid;
}
public static Element getFirstChildOrFail(Element e) throws ParserException
{
Element kid = getFirstChildElement(e);
if (null == kid)
{
throw new ParserException(e.getTagName()
+ " does not contain a child element");
}
return kid;
}
public static String getAttributeOrFail(Element e, String name) throws ParserException
{
String v = e.getAttribute(name);
if (null == v)
{
throw new ParserException(e.getTagName() + " missing \"" + name
+ "\" attribute");
}
return v;
}
public static String getAttributeWithInheritanceOrFail(Element e, String name) throws ParserException
{
String v = getAttributeWithInheritance(e, name);
if (null == v)
{
throw new ParserException(e.getTagName() + " missing \"" + name
+ "\" attribute");
}
return v;
}
public static String getNonBlankTextOrFail(Element e) throws ParserException
{
String v = getText(e);
if (null != v)
v = v.trim();
if (null == v || 0 == v.length())
{
throw new ParserException(e.getTagName() + " has no text");
}
return v;
}
/* Convenience method where there is only one child Element of a given name */
public static Element getChildByTagName(Element e, String name)
{

View File

@ -1,71 +0,0 @@
/*
* Created on 25-Jan-2006
*/
package org.apache.lucene.xmlparser;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.FilteredQuery;
import org.apache.lucene.search.Query;
import org.w3c.dom.Element;
/**
* @author maharwood
*/
public class FilteredQueryBuilder implements QueryBuilder {
private FilterBuilder filterFactory;
private QueryBuilder queryFactory;
public FilteredQueryBuilder(FilterBuilder filterFactory, QueryBuilder queryFactory)
{
this.filterFactory=filterFactory;
this.queryFactory=queryFactory;
}
/* (non-Javadoc)
* @see org.apache.lucene.xmlparser.QueryObjectBuilder#process(org.w3c.dom.Element)
*/
public Query getQuery(Element e) throws ParserException {
Element filterElement=DOMUtils.getChildByTagName(e,"Filter");
if(filterElement==null)
{
throw new ParserException("FilteredQuery missing \"Filter\" child element");
}
filterElement=DOMUtils.getFirstChildElement(filterElement);
Filter f=null;
if(filterElement!=null)
{
f=filterFactory.getFilter(filterElement);
}
else
{
throw new ParserException("FilteredQuery \"Filter\" element missing child query element ");
}
Element queryElement=DOMUtils.getChildByTagName(e,"Query");
if(queryElement==null)
{
throw new ParserException("FilteredQuery missing \"Query\" child element");
}
queryElement=DOMUtils.getFirstChildElement(queryElement);
Query q=null;
if(queryElement!=null)
{
q=queryFactory.getQuery(queryElement);
}
else
{
throw new ParserException("FilteredQuery \"Query\" element missing child query element ");
}
FilteredQuery fq = new FilteredQuery(q,f);
fq.setBoost(DOMUtils.getAttribute(e,"boost",1.0f));
return fq;
}
}

View File

@ -37,18 +37,9 @@ public class BooleanQueryBuilder implements QueryBuilder {
Element clauseElem=(Element) nl.item(i);
BooleanClause.Occur occurs=getOccursValue(clauseElem);
//find the first element child which should contain a Query
Element clauseQuery=DOMUtils.getFirstChildElement(clauseElem);
if(clauseQuery!=null)
{
Element clauseQuery=DOMUtils.getFirstChildOrFail(clauseElem);
Query q=factory.getQuery(clauseQuery);
bq.add(new BooleanClause(q,occurs));
}
else
{
throw new ParserException("BooleanClause missing child query element ");
}
}
return bq;

View File

@ -22,33 +22,17 @@ public class BoostingQueryBuilder implements QueryBuilder
public Query getQuery(Element e) throws ParserException
{
Element mainQueryElem=DOMUtils.getChildByTagName(e,"Query");
if(mainQueryElem==null)
{
throw new ParserException("BoostingQuery missing a \"Query\" child element");
}
mainQueryElem=DOMUtils.getFirstChildElement(mainQueryElem);
if(mainQueryElem==null)
{
throw new ParserException("BoostingQuery \"Query\" element missing a child element");
}
Element mainQueryElem=DOMUtils.getChildByTagOrFail(e,"Query");
mainQueryElem=DOMUtils.getFirstChildOrFail(mainQueryElem);
Query mainQuery=factory.getQuery(mainQueryElem);
Element boostQueryElem=DOMUtils.getChildByTagName(e,"BoostQuery");
Element boostQueryElem=DOMUtils.getChildByTagOrFail(e,"BoostQuery");
float boost=DOMUtils.getAttribute(boostQueryElem,"boost",defaultBoost);
if(boostQueryElem==null)
{
throw new ParserException("BoostingQuery missing a \"BoostQuery\" child element");
}
boostQueryElem=DOMUtils.getFirstChildElement(boostQueryElem);
if(boostQueryElem==null)
{
throw new ParserException("BoostingQuery \"BoostQuery\" element missing a child element");
}
boostQueryElem=DOMUtils.getFirstChildOrFail(boostQueryElem);
Query boostQuery=factory.getQuery(boostQueryElem);
BoostingQuery bq = new BoostingQuery(mainQuery,boostQuery,boost);
bq.setBoost(DOMUtils.getAttribute(e,"boost",1.0f));
return bq;

View File

@ -19,14 +19,13 @@ public class ConstantScoreQueryBuilder implements QueryBuilder
public Query getQuery(Element e) throws ParserException
{
Element filterElem=DOMUtils.getFirstChildElement(e);
if(filterElem==null)
{
throw new ParserException("ConstantScoreQuery missing child element with filter");
}
Element filterElem=DOMUtils.getFirstChildOrFail(e);
Query q=new ConstantScoreQuery(filterFactory.getFilter(filterElem));
q.setBoost(DOMUtils.getAttribute(e,"boost",1.0f));
return q;
}
}

View File

@ -0,0 +1,48 @@
/*
* Created on 25-Jan-2006
*/
package org.apache.lucene.xmlparser.builders;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.FilteredQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.xmlparser.DOMUtils;
import org.apache.lucene.xmlparser.FilterBuilder;
import org.apache.lucene.xmlparser.ParserException;
import org.apache.lucene.xmlparser.QueryBuilder;
import org.w3c.dom.Element;
/**
* @author maharwood
*/
public class FilteredQueryBuilder implements QueryBuilder {
private FilterBuilder filterFactory;
private QueryBuilder queryFactory;
public FilteredQueryBuilder(FilterBuilder filterFactory, QueryBuilder queryFactory)
{
this.filterFactory=filterFactory;
this.queryFactory=queryFactory;
}
/* (non-Javadoc)
* @see org.apache.lucene.xmlparser.QueryObjectBuilder#process(org.w3c.dom.Element)
*/
public Query getQuery(Element e) throws ParserException {
Element filterElement=DOMUtils.getChildByTagOrFail(e,"Filter");
filterElement=DOMUtils.getFirstChildOrFail(filterElement);
Filter f=filterFactory.getFilter(filterElement);
Element queryElement=DOMUtils.getChildByTagOrFail(e,"Query");
queryElement=DOMUtils.getFirstChildOrFail(queryElement);
Query q=queryFactory.getQuery(queryElement);
FilteredQuery fq = new FilteredQuery(q,f);
fq.setBoost(DOMUtils.getAttribute(e,"boost",1.0f));
return fq;
}
}

View File

@ -19,11 +19,7 @@ public class SpanNearBuilder extends SpanBuilderBase
public SpanQuery getSpanQuery(Element e) throws ParserException
{
String slopString=e.getAttribute("slop");
if((slopString==null)||(slopString.length()==0))
{
throw new ParserException("SpanTermQuery missing slop property ");
}
String slopString=DOMUtils.getAttributeOrFail(e,"slop");
int slop=Integer.parseInt(slopString);
boolean inOrder=DOMUtils.getAttribute(e,"inOrder",false);
ArrayList spans=new ArrayList();

View File

@ -21,24 +21,12 @@ public class SpanNotBuilder extends SpanBuilderBase
}
public SpanQuery getSpanQuery(Element e) throws ParserException
{
Element includeElem=DOMUtils.getChildByTagName(e,"Include");
if(includeElem!=null)
{
includeElem=DOMUtils.getFirstChildElement(includeElem);
}
if(includeElem==null)
{
throw new ParserException("SpanNotQuery missing Include child Element");
}
Element excludeElem=DOMUtils.getChildByTagName(e,"Exclude");
if(excludeElem!=null)
{
excludeElem=DOMUtils.getFirstChildElement(excludeElem);
}
if(excludeElem==null)
{
throw new ParserException("SpanNotQuery missing Exclude child Element");
}
Element includeElem=DOMUtils.getChildByTagOrFail(e,"Include");
includeElem=DOMUtils.getFirstChildOrFail(includeElem);
Element excludeElem=DOMUtils.getChildByTagOrFail(e,"Exclude");
excludeElem=DOMUtils.getFirstChildOrFail(excludeElem);
SpanQuery include=factory.getSpanQuery(includeElem);
SpanQuery exclude=factory.getSpanQuery(excludeElem);

View File

@ -30,13 +30,8 @@ public class SpanOrTermsBuilder extends SpanBuilderBase
}
public SpanQuery getSpanQuery(Element e) throws ParserException
{
String fieldName=DOMUtils.getAttributeWithInheritance(e,"fieldName");
if(fieldName==null)
{
throw new ParserException("Error: SpanOrTermsBuilder missing \"fieldName\" property");
}
String value=DOMUtils.getText(e);
String fieldName=DOMUtils.getAttributeWithInheritanceOrFail(e,"fieldName");
String value=DOMUtils.getNonBlankTextOrFail(e);
try
{

View File

@ -12,20 +12,13 @@ public class SpanTermBuilder extends SpanBuilderBase
public SpanQuery getSpanQuery(Element e) throws ParserException
{
String fieldName=DOMUtils.getAttributeWithInheritance(e,"fieldName");
String value=DOMUtils.getText(e);
if((fieldName==null)||(fieldName.length()==0))
{
throw new ParserException("SpanTermQuery missing fieldName property ");
}
if((value==null)||(value.length()==0))
{
throw new ParserException("TermQuery missing value property ");
}
String fieldName=DOMUtils.getAttributeWithInheritanceOrFail(e,"fieldName");
String value=DOMUtils.getNonBlankTextOrFail(e);
SpanTermQuery stq = new SpanTermQuery(new Term(fieldName,value));
stq.setBoost(DOMUtils.getAttribute(e,"boost",1.0f));
return stq;
}
}

View File

@ -18,20 +18,14 @@ import org.w3c.dom.Element;
public class TermQueryBuilder implements QueryBuilder {
public Query getQuery(Element e) throws ParserException {
String field=DOMUtils.getAttributeWithInheritance(e,"fieldName");
String value=DOMUtils.getText(e);
if((field==null)||(field.length()==0))
{
throw new ParserException("TermQuery element missing fieldName attribute");
}
if((value==null)||(value.length()==0))
{
throw new ParserException("TermQuery element missing child text property ");
}
TermQuery tq = new TermQuery(new Term(field,value));
String field=DOMUtils.getAttributeWithInheritanceOrFail(e,"fieldName");
String value=DOMUtils.getNonBlankTextOrFail(e);
TermQuery tq = new TermQuery(new Term(field,value));
tq.setBoost(DOMUtils.getAttribute(e,"boost",1.0f));
return tq;
}
}

View File

@ -42,15 +42,12 @@ public class TermsFilterBuilder implements FilterBuilder
NodeList nl = e.getElementsByTagName("Field");
for(int i=0;i<nl.getLength();i++)
{
Element fieldElem=(Element) nl.item(i);
String fieldName=DOMUtils.getAttributeWithInheritance(fieldElem,"fieldName");
if(fieldName==null)
{
throw new ParserException("TermsFilter missing \"fieldName\" element");
}
String text=DOMUtils.getText(fieldElem).trim();
Element fieldElem=(Element) nl.item(i);
String fieldName=DOMUtils.getAttributeWithInheritanceOrFail(fieldElem,"fieldName");
String text=DOMUtils.getNonBlankTextOrFail(fieldElem);
TokenStream ts = analyzer.tokenStream(fieldName, new StringReader(text));
try
{
Token token=ts.next();