com.mkdoc.query
Class AbstractQueryBuilder

java.lang.Object
  |
  +--com.mkdoc.query.AbstractQueryBuilder
All Implemented Interfaces:
QueryBuilder
Direct Known Subclasses:
ServletQueryBuilder

public abstract class AbstractQueryBuilder
extends java.lang.Object
implements QueryBuilder

A base implementation of a query interpreter.

Version:
0.7 2005-09-14
Author:
Philip Shaw
To do:
Reduce the complexity of the getQueryTerms method

Field Summary
protected  boolean extendable
          Whether any properties have optional encoding schemes.
protected  boolean extended
          Whether to use extended bNode queries.
protected  ApplicationProfile profile
          The application profile to use for interpreting queries.
 
Fields inherited from interface com.mkdoc.query.QueryBuilder
APPEND_AND, APPEND_COMMA, APPEND_OR, CONSTRUCT, DELIMITERS, DISTINCT, FROM, LIKE, NOT_LIKE, QUOTE, SELECT, SPACE, USING, WHERE
 
Constructor Summary
AbstractQueryBuilder()
           
 
Method Summary
protected  void addCondition(SchemaProperty property, java.lang.String term)
          Add a condition to the query selection.
protected  void addExpression(SchemaProperty property)
          Add a path expression to the query.
protected  void addNamespaceDeclaration(SchemaProperty property)
          Add a namespace declaration to the query selection.
protected  void addSummaryExpression(SchemaProperty property)
          Add an optional summary expression to the query.
protected  void addTitleExpression(SchemaProperty property)
          Add an optional title expression to the query.
protected  void addVariable(SchemaProperty property)
          Add a variable name.
protected  void appendConditions(java.lang.StringBuffer query)
          Append conditions for the first selection in a union query.
protected  void appendPrimaryExpressions(java.lang.StringBuffer query)
          Append path expressions for the first selection in a union query.
protected  void appendSecondaryExpressions(java.lang.StringBuffer query)
          Append path expressions for the second selection in a union query.
 java.util.List getQueryTerms(java.lang.String query)
          Get query terms from a string input.
 java.lang.String getSeRQLQuery(boolean extended)
          Get a SeRQL query string.
protected  boolean hasExtendableProperties()
          Check whether the query has properties that may have encoded schema.
protected  boolean isDelimiter(java.lang.String token)
          Check whether a string token is a query delimiter.
protected  boolean isUriQuery(SchemaProperty property, java.lang.String term)
          Check whether this is a URI query.
 void setApplicationProfile(ApplicationProfile profile)
          Set the application profile to use for interpreting queries.
 void setDistinct(boolean distinct)
          Set whether the DISTINCT keyword is applied.
protected  void setQueryType(java.lang.String queryType)
          Set query type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.mkdoc.query.QueryBuilder
getQuery
 

Field Detail

extended

protected boolean extended
Whether to use extended bNode queries.

Since:
Version 0.6 2005-09-13

extendable

protected boolean extendable
Whether any properties have optional encoding schemes.

Since:
Version 0.6 2005-09-14

profile

protected ApplicationProfile profile
The application profile to use for interpreting queries.

Since:
Version 0.1 2005-07-05
Constructor Detail

AbstractQueryBuilder

public AbstractQueryBuilder()
Method Detail

setApplicationProfile

public final void setApplicationProfile(ApplicationProfile profile)
Set the application profile to use for interpreting queries.

Specified by:
setApplicationProfile in interface QueryBuilder
Parameters:
profile - The application profile to use to prepare the query.
Since:
Version 0.1 2005-07-05

getQueryTerms

public final java.util.List getQueryTerms(java.lang.String query)
Get query terms from a string input.

The parse rules for query parameters are as follows:

White space

White space includes the single space, tab, new line, carriage return, and form feed characters. Any white space after the first, and before the last, non-white space character is interpreted as a single space, which is a word separator.

Leading and trailing white space

All leading and trailing white space characters are trimmed and discarded.

Single word

A single word is interpreted as a match against the single word content of a metadata field. The SeRQL condition for the query "cat" is:

 WHERE label(varX) LIKE "cat"
      

This query would only match the literal word "cat", not a phrase that included the word, such as "the cat sat on the mat". See "Wildcard before and after".

Multiple words

Multiple words are interpreted as if each word is an alternative match against the single word content of a metadata field. The SeRQL condition for the query "cat dog" is:

 WHERE label(varX) LIKE "cat" OR label(varX) LIKE "dog"
      

This would only match the literal word "cat" or "dog", not a phrase that included either word, such as "fought like cat and dog". See "Wildcard before and after".

The wildcard schemes described below can be applied to multiple query terms. For example, the query "*cat* *dog*" would match "fought like cat and dog", "I like cats" and "I like dogs".

Wildcard before

A word with an asterisk before it is interpreted as the end of a phrase, or the suffix to a single word, where any number of characters may be matched before it, but not after it. The SeRQL condition for the query "*cat" is:

 WHERE label(varX) LIKE "*cat"
      

This would match "cat", "senses of a cat" and "polecat", but not "cats" or "fought like cat and dog".

Wildcard after

A word with an asterisk after it is interpreted as the start of a phrase, or the prefix to a single word, where any number of characters may be matched after it, but not before it. The SeRQL condition for the query "cat*" is:

 WHERE label(varX) LIKE "cat*"
      

This would match "cat", "cats" and "cats and dogs", but not "polecat" or "senses of a cat".

Wildcard before and after

A word with an asterisk before and after it is interpreted as the middle of a phrase, or part of a compound word, where any number of characters may be matched before and after it. The SeRQL condition for the query "*cat*" is:

 WHERE label(varX) LIKE "*cat*"
      

This would match "cat", "cats", "cats and dogs", "polecat" and "senses of a cat".

Quoted phrase

A phrase enclosed by double quote characters, ", is interpreted as a match against the whole content of a single metadatum field. The SeRQL condition for "cat litter" is:

 WHERE label(varX) LIKE "cat litter"
      

This would only match "cat litter", not "cat litter tray", "use cat litter", "cat" or "litter".

Quoted phrases can also be include wildcard asterisks at the beginning and end of the phrase. The query "*cat litter" would match "use cat litter" and "buy cat litter", for example.

Un-terminated quoted phrases are interpreted as everything following an opening double quote to the end of the query parameter.

Specified by:
getQueryTerms in interface QueryBuilder
Parameters:
query - The contents of a single metadata query field, which may include multiple search terms separated by delimiters.
Returns:
A Vector containing all phrase- and word-query tokens. If the query is the empty string or pure white space, the list will be empty, so clients should check its size before further processing.
Since:
Version 0.1 2005-07-05

isDelimiter

protected final boolean isDelimiter(java.lang.String token)
Check whether a string token is a query delimiter.

Parameters:
token - The string to test.
Returns:
true if the input is a single character from the set of query delimiters.
Since:
Version 0.4 2005-01-27

addVariable

protected final void addVariable(SchemaProperty property)
Add a variable name.

Parameters:
property - The schema property for which to add a query variable.
Since:
Version 0.2 2005-07-26

addExpression

protected final void addExpression(SchemaProperty property)
Add a path expression to the query.

Parameters:
property - The schema property for which to add a query expression.
Since:
Version 0.2 2005-07-26

addSummaryExpression

protected final void addSummaryExpression(SchemaProperty property)
Add an optional summary expression to the query.

Parameters:
property - The schema property for which to add an optional summary expression.
Since:
Version 0.3 2005-07-27

addTitleExpression

protected final void addTitleExpression(SchemaProperty property)
Add an optional title expression to the query.

Parameters:
property - The schema property for which to add an optional title expression.
Since:
Version 0.3 2005-07-27

addCondition

protected final void addCondition(SchemaProperty property,
                                  java.lang.String term)
Add a condition to the query selection.

Parameters:
property - The schema property for which to add a query condition.
term - The query term.
Since:
Version 0.2 2005-07-26

addNamespaceDeclaration

protected final void addNamespaceDeclaration(SchemaProperty property)
Add a namespace declaration to the query selection.

Parameters:
property - The schema property for which to add a query namespace declaration.
Since:
Version 0.2 2005-07-26

setDistinct

public final void setDistinct(boolean distinct)
Set whether the DISTINCT keyword is applied.

Specified by:
setDistinct in interface QueryBuilder
Parameters:
distinct - Whether the query interpreter should suppress duplicate results.
Since:
Version 0.2 2005-07-26

setQueryType

protected final void setQueryType(java.lang.String queryType)
Set query type.

Parameters:
queryType - SELECT or CONSTRUCT.
Since:
Version 0.2 2005-07-26

getSeRQLQuery

public final java.lang.String getSeRQLQuery(boolean extended)
Get a SeRQL query string.

Parameters:
extended - Whether an extended bNode union query is requested.
Returns:
An SeRQL query that may use a union join to combine simple and extended bNode path expressions. If none of the query terms support encoding schemes that would be expressed in a bNode graph, no union is applied.
Since:
Version 0.7 2005-09-14

hasExtendableProperties

protected final boolean hasExtendableProperties()
Check whether the query has properties that may have encoded schema.

Returns:
true if any property registered for the query has an optional encoding scheme.
Since:
Version 0.7 2005-09-14

appendPrimaryExpressions

protected final void appendPrimaryExpressions(java.lang.StringBuffer query)
Append path expressions for the first selection in a union query.

Parameters:
query - The query buffer to which to append the expressions.
Since:
0.7 2005-09-14

appendConditions

protected final void appendConditions(java.lang.StringBuffer query)
Append conditions for the first selection in a union query.

Parameters:
query - The query buffer to which to append the expressions.
Since:
0.7 2005-09-14

isUriQuery

protected final boolean isUriQuery(SchemaProperty property,
                                   java.lang.String term)
Check whether this is a URI query.

Parameters:
property - The schema property against which the query term is to be tested.
term - The user query term to check.
Returns:
true if the schema property has a URI encoding scheme and the query term is a URI.
Since:
Version

appendSecondaryExpressions

protected final void appendSecondaryExpressions(java.lang.StringBuffer query)
Append path expressions for the second selection in a union query.

Parameters:
query - The query buffer to which to append the expressions.
Since:
0.7 2005-09-14