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.search.Query;
import org.apache.lucene.xmlparser.builders.BooleanQueryBuilder; import org.apache.lucene.xmlparser.builders.BooleanQueryBuilder;
import org.apache.lucene.xmlparser.builders.ConstantScoreQueryBuilder; 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.RangeFilterBuilder;
import org.apache.lucene.xmlparser.builders.SpanFirstBuilder; import org.apache.lucene.xmlparser.builders.SpanFirstBuilder;
import org.apache.lucene.xmlparser.builders.SpanNearBuilder; import org.apache.lucene.xmlparser.builders.SpanNearBuilder;

View File

@ -11,6 +11,65 @@ import org.xml.sax.InputSource;
public class DOMUtils 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 */ /* Convenience method where there is only one child Element of a given name */
public static Element getChildByTagName(Element e, String 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); Element clauseElem=(Element) nl.item(i);
BooleanClause.Occur occurs=getOccursValue(clauseElem); BooleanClause.Occur occurs=getOccursValue(clauseElem);
//find the first element child which should contain a Query Element clauseQuery=DOMUtils.getFirstChildOrFail(clauseElem);
Element clauseQuery=DOMUtils.getFirstChildElement(clauseElem); Query q=factory.getQuery(clauseQuery);
if(clauseQuery!=null) bq.add(new BooleanClause(q,occurs));
{
Query q=factory.getQuery(clauseQuery);
bq.add(new BooleanClause(q,occurs));
}
else
{
throw new ParserException("BooleanClause missing child query element ");
}
} }
return bq; return bq;

View File

@ -22,34 +22,18 @@ public class BoostingQueryBuilder implements QueryBuilder
public Query getQuery(Element e) throws ParserException public Query getQuery(Element e) throws ParserException
{ {
Element mainQueryElem=DOMUtils.getChildByTagName(e,"Query"); Element mainQueryElem=DOMUtils.getChildByTagOrFail(e,"Query");
if(mainQueryElem==null) mainQueryElem=DOMUtils.getFirstChildOrFail(mainQueryElem);
{ Query mainQuery=factory.getQuery(mainQueryElem);
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");
}
Query mainQuery=factory.getQuery(mainQueryElem);
Element boostQueryElem=DOMUtils.getChildByTagOrFail(e,"BoostQuery");
float boost=DOMUtils.getAttribute(boostQueryElem,"boost",defaultBoost);
boostQueryElem=DOMUtils.getFirstChildOrFail(boostQueryElem);
Query boostQuery=factory.getQuery(boostQueryElem);
Element boostQueryElem=DOMUtils.getChildByTagName(e,"BoostQuery"); BoostingQuery bq = new BoostingQuery(mainQuery,boostQuery,boost);
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");
}
Query boostQuery=factory.getQuery(boostQueryElem);
BoostingQuery bq = new BoostingQuery(mainQuery,boostQuery,boost); bq.setBoost(DOMUtils.getAttribute(e,"boost",1.0f));
bq.setBoost(DOMUtils.getAttribute(e,"boost",1.0f));
return bq; return bq;
} }

View File

@ -19,14 +19,13 @@ public class ConstantScoreQueryBuilder implements QueryBuilder
public Query getQuery(Element e) throws ParserException public Query getQuery(Element e) throws ParserException
{ {
Element filterElem=DOMUtils.getFirstChildElement(e); Element filterElem=DOMUtils.getFirstChildOrFail(e);
if(filterElem==null) Query q=new ConstantScoreQuery(filterFactory.getFilter(filterElem));
{
throw new ParserException("ConstantScoreQuery missing child element with filter"); q.setBoost(DOMUtils.getAttribute(e,"boost",1.0f));
}
Query q=new ConstantScoreQuery(filterFactory.getFilter(filterElem)); return q;
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,12 +19,8 @@ public class SpanNearBuilder extends SpanBuilderBase
public SpanQuery getSpanQuery(Element e) throws ParserException public SpanQuery getSpanQuery(Element e) throws ParserException
{ {
String slopString=e.getAttribute("slop"); String slopString=DOMUtils.getAttributeOrFail(e,"slop");
if((slopString==null)||(slopString.length()==0)) int slop=Integer.parseInt(slopString);
{
throw new ParserException("SpanTermQuery missing slop property ");
}
int slop=Integer.parseInt(slopString);
boolean inOrder=DOMUtils.getAttribute(e,"inOrder",false); boolean inOrder=DOMUtils.getAttribute(e,"inOrder",false);
ArrayList spans=new ArrayList(); ArrayList spans=new ArrayList();
for (Node kid = e.getFirstChild(); kid != null; kid = kid.getNextSibling()) for (Node kid = e.getFirstChild(); kid != null; kid = kid.getNextSibling())

View File

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

View File

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

View File

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

View File

@ -18,20 +18,14 @@ import org.w3c.dom.Element;
public class TermQueryBuilder implements QueryBuilder { public class TermQueryBuilder implements QueryBuilder {
public Query getQuery(Element e) throws ParserException { 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));
tq.setBoost(DOMUtils.getAttribute(e,"boost",1.0f)); String field=DOMUtils.getAttributeWithInheritanceOrFail(e,"fieldName");
return tq; 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"); NodeList nl = e.getElementsByTagName("Field");
for(int i=0;i<nl.getLength();i++) for(int i=0;i<nl.getLength();i++)
{ {
Element fieldElem=(Element) nl.item(i);
String fieldName=DOMUtils.getAttributeWithInheritance(fieldElem,"fieldName");
if(fieldName==null) Element fieldElem=(Element) nl.item(i);
{ String fieldName=DOMUtils.getAttributeWithInheritanceOrFail(fieldElem,"fieldName");
throw new ParserException("TermsFilter missing \"fieldName\" element"); String text=DOMUtils.getNonBlankTextOrFail(fieldElem);
} TokenStream ts = analyzer.tokenStream(fieldName, new StringReader(text));
String text=DOMUtils.getText(fieldElem).trim();
TokenStream ts = analyzer.tokenStream(fieldName, new StringReader(text));
try try
{ {
Token token=ts.next(); Token token=ts.next();