From 7dab9545be865ae5f67561642452a9014defc3c6 Mon Sep 17 00:00:00 2001 From: Mark Harwood Date: Wed, 1 Mar 2006 21:49:17 +0000 Subject: [PATCH] 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 --- .../apache/lucene/xmlparser/CoreParser.java | 1 + .../org/apache/lucene/xmlparser/DOMUtils.java | 59 +++++++++++++++ .../xmlparser/FilteredQueryBuilder.java | 71 ------------------- .../builders/BooleanQueryBuilder.java | 15 +--- .../builders/BoostingQueryBuilder.java | 38 +++------- .../builders/ConstantScoreQueryBuilder.java | 15 ++-- .../builders/FilteredQueryBuilder.java | 48 +++++++++++++ .../xmlparser/builders/SpanNearBuilder.java | 8 +-- .../xmlparser/builders/SpanNotBuilder.java | 28 +++----- .../builders/SpanOrTermsBuilder.java | 9 +-- .../xmlparser/builders/SpanTermBuilder.java | 19 ++--- .../xmlparser/builders/TermQueryBuilder.java | 20 ++---- .../builders/TermsFilterBuilder.java | 13 ++-- 13 files changed, 159 insertions(+), 185 deletions(-) delete mode 100644 contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/FilteredQueryBuilder.java create mode 100644 contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/FilteredQueryBuilder.java diff --git a/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/CoreParser.java b/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/CoreParser.java index 48fec961c7d..62087cde649 100644 --- a/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/CoreParser.java +++ b/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/CoreParser.java @@ -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; diff --git a/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/DOMUtils.java b/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/DOMUtils.java index e40bbdf5391..eef786f05f0 100644 --- a/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/DOMUtils.java +++ b/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/DOMUtils.java @@ -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) { diff --git a/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/FilteredQueryBuilder.java b/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/FilteredQueryBuilder.java deleted file mode 100644 index 269c62f8862..00000000000 --- a/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/FilteredQueryBuilder.java +++ /dev/null @@ -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; - - } - -} diff --git a/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/BooleanQueryBuilder.java b/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/BooleanQueryBuilder.java index 1ad2cda16c4..b88853fb257 100644 --- a/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/BooleanQueryBuilder.java +++ b/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/BooleanQueryBuilder.java @@ -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) - { - Query q=factory.getQuery(clauseQuery); - bq.add(new BooleanClause(q,occurs)); - - } - else - { - throw new ParserException("BooleanClause missing child query element "); - } + Element clauseQuery=DOMUtils.getFirstChildOrFail(clauseElem); + Query q=factory.getQuery(clauseQuery); + bq.add(new BooleanClause(q,occurs)); } return bq; diff --git a/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/BoostingQueryBuilder.java b/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/BoostingQueryBuilder.java index 7367772c3ff..90f311bb91b 100644 --- a/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/BoostingQueryBuilder.java +++ b/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/BoostingQueryBuilder.java @@ -22,34 +22,18 @@ 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"); - } - Query mainQuery=factory.getQuery(mainQueryElem); - + Element mainQueryElem=DOMUtils.getChildByTagOrFail(e,"Query"); + mainQueryElem=DOMUtils.getFirstChildOrFail(mainQueryElem); + Query mainQuery=factory.getQuery(mainQueryElem); - Element boostQueryElem=DOMUtils.getChildByTagName(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"); - } - Query boostQuery=factory.getQuery(boostQueryElem); - - BoostingQuery bq = new BoostingQuery(mainQuery,boostQuery,boost); - bq.setBoost(DOMUtils.getAttribute(e,"boost",1.0f)); + Element boostQueryElem=DOMUtils.getChildByTagOrFail(e,"BoostQuery"); + float boost=DOMUtils.getAttribute(boostQueryElem,"boost",defaultBoost); + 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; } diff --git a/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/ConstantScoreQueryBuilder.java b/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/ConstantScoreQueryBuilder.java index 62094877f08..b3a3dcc1b24 100644 --- a/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/ConstantScoreQueryBuilder.java +++ b/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/ConstantScoreQueryBuilder.java @@ -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"); - } - Query q=new ConstantScoreQuery(filterFactory.getFilter(filterElem)); - q.setBoost(DOMUtils.getAttribute(e,"boost",1.0f)); - return q; + Element filterElem=DOMUtils.getFirstChildOrFail(e); + Query q=new ConstantScoreQuery(filterFactory.getFilter(filterElem)); + + q.setBoost(DOMUtils.getAttribute(e,"boost",1.0f)); + + return q; + } } diff --git a/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/FilteredQueryBuilder.java b/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/FilteredQueryBuilder.java new file mode 100644 index 00000000000..f71821e60e9 --- /dev/null +++ b/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/FilteredQueryBuilder.java @@ -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; + } + +} diff --git a/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/SpanNearBuilder.java b/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/SpanNearBuilder.java index b18f3bc452d..1a823343923 100644 --- a/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/SpanNearBuilder.java +++ b/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/SpanNearBuilder.java @@ -19,12 +19,8 @@ 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 "); - } - int slop=Integer.parseInt(slopString); + String slopString=DOMUtils.getAttributeOrFail(e,"slop"); + int slop=Integer.parseInt(slopString); boolean inOrder=DOMUtils.getAttribute(e,"inOrder",false); ArrayList spans=new ArrayList(); for (Node kid = e.getFirstChild(); kid != null; kid = kid.getNextSibling()) diff --git a/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/SpanNotBuilder.java b/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/SpanNotBuilder.java index 26eacb093ec..554348ede25 100644 --- a/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/SpanNotBuilder.java +++ b/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/SpanNotBuilder.java @@ -21,26 +21,14 @@ 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"); - } - SpanQuery include=factory.getSpanQuery(includeElem); - SpanQuery exclude=factory.getSpanQuery(excludeElem); + 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); SpanNotQuery snq = new SpanNotQuery(include,exclude); diff --git a/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/SpanOrTermsBuilder.java b/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/SpanOrTermsBuilder.java index 12fff01e6c7..9b4e251e38f 100644 --- a/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/SpanOrTermsBuilder.java +++ b/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/SpanOrTermsBuilder.java @@ -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 { diff --git a/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/SpanTermBuilder.java b/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/SpanTermBuilder.java index ab53d9eed3e..18e19f81fde 100644 --- a/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/SpanTermBuilder.java +++ b/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/SpanTermBuilder.java @@ -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 "); - } - SpanTermQuery stq = new SpanTermQuery(new Term(fieldName,value)); + 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; - stq.setBoost(DOMUtils.getAttribute(e,"boost",1.0f)); - return stq; } } diff --git a/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/TermQueryBuilder.java b/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/TermQueryBuilder.java index 133fb223180..7a1dd8d9f1f 100644 --- a/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/TermQueryBuilder.java +++ b/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/TermQueryBuilder.java @@ -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)); - tq.setBoost(DOMUtils.getAttribute(e,"boost",1.0f)); - return tq; + 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; + } } diff --git a/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/TermsFilterBuilder.java b/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/TermsFilterBuilder.java index 2d430e79ab0..fc4a4201103 100644 --- a/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/TermsFilterBuilder.java +++ b/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/TermsFilterBuilder.java @@ -42,15 +42,12 @@ public class TermsFilterBuilder implements FilterBuilder NodeList nl = e.getElementsByTagName("Field"); for(int i=0;i