org.jafer.query
Class QueryBuilder

java.lang.Object
  extended by org.jafer.query.QueryBuilder
All Implemented Interfaces:
QueryBuilder

public class QueryBuilder
extends java.lang.Object
implements QueryBuilder

Class for building XML representations of Z39.50 Bib1 queries.

A default search profile is used if one has been set using an appropriate constructor.

Some getNode() methods allow the default search profile to be altered, or ignored for that query.

See Also:
of XML structure available at www.jafer.org .

Field Summary
(package private)  int[] defaultAttributes
           
private  org.w3c.dom.Document document
           
 
Constructor Summary
QueryBuilder()
          Constructs a new QueryBuilder without any default search profile.
QueryBuilder(int[] defaultSearchProfile)
          Constructs a new QueryBuilder with a default Bib1 search profile which will be used in creating queries.
QueryBuilder(java.lang.String defaultSearchProfile)
          Constructs a new QueryBuilder with a default Bib1 search profile which will be used in creating queries.
 
Method Summary
 org.w3c.dom.Node and(org.w3c.dom.Node leftNode, org.w3c.dom.Node rightNode)
          Produces an AND node from 2 nodes, which can be query or Boolean nodes.
 org.w3c.dom.Node and(java.util.Vector useAttributes, java.util.Vector terms)
          Produces an AND node from a pair of attributes, or a tree of AND nodes if more than 2 attributes are given.
private  org.w3c.dom.Node buildConstraintModelNode(int[] attributes, java.lang.String term)
          Builds an XML representation of a query, using Bib1 attributes.
private  java.lang.String[] getConstraintModelData(org.w3c.dom.Node cM)
           
 java.lang.String[][] getContent(org.w3c.dom.Node queryNode)
          Method returns a representation of an XML query as a 2 dimensional String[].
 org.w3c.dom.Node getNode(int[][] attTypesValues, java.lang.String term)
          Creates a basic query node incorporating the term and attributes supplied.
 org.w3c.dom.Node getNode(int[] attributes, java.lang.String term)
          Creates a basic query node incorporating the attributes and term supplied.
 org.w3c.dom.Node getNode(int useAttribute, java.lang.String term)
          Creates a basic query node incorporating the Use attribute and term supplied.
 org.w3c.dom.Node getNode(java.lang.String queryExp)
          Create a Node from a query string
 org.w3c.dom.Node getNode(java.lang.String[] termAndAttributes)
          Creates a basic query node incorporating the term and attributes supplied.
 org.w3c.dom.Node getNode(java.lang.String useAttribute, java.lang.String term)
          Creates a basic query node incorporating the Use attribute and term supplied.
private  void getNodeData(org.w3c.dom.Node node, java.util.Vector nodeData)
           
private  java.lang.String getNodeValue(org.w3c.dom.Node node)
           
private  int lookUpUseAttribute(java.lang.String attributeString)
           
 org.w3c.dom.Node not(org.w3c.dom.Node inputNode)
          Produces a NOT node from the supplied node, which can be a query or Boolean node.
 org.w3c.dom.Node or(org.w3c.dom.Node leftNode, org.w3c.dom.Node rightNode)
          Produces an OR node from 2 nodes, which can be query or Boolean nodes.
 org.w3c.dom.Node or(java.util.Vector useAttributes, java.util.Vector terms)
          Produces an OR node from a pair of attributes, or a tree of OR nodes if more than 2 attributes are given.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

document

private org.w3c.dom.Document document

defaultAttributes

int[] defaultAttributes
Constructor Detail

QueryBuilder

public QueryBuilder()
Constructs a new QueryBuilder without any default search profile.


QueryBuilder

public QueryBuilder(int[] defaultSearchProfile)
Constructs a new QueryBuilder with a default Bib1 search profile which will be used in creating queries.

The profile parameter is interpreted in the following order:

[1] Use, [2] Relation, [3] Position, [4] Structure, [5] Truncation, [6] Completeness

A value of zero in the array will be ignored, and can be used as a spacer.

(Default search profile can be overidden when using certain getNode() methods.)

Parameters:
defaultSearchProfile - int array (0 < length < 7)

QueryBuilder

public QueryBuilder(java.lang.String defaultSearchProfile)
Constructs a new QueryBuilder with a default Bib1 search profile which will be used in creating queries.

The profile parameter consists of a dot delimited String (eg: 4.3.3.101.100.1), with values interpreted in the following order:

Use, Relation, Position, Structure, Truncation, Completeness.

A value of zero will be ignored, and can be used as a spacer.

(Default search profile can be overidden when using certain getNode() methods.)

Parameters:
defaultSearchProfile - dot delimited String with at least 1, and a maximum of 6 items.
Method Detail

getNode

public org.w3c.dom.Node getNode(java.lang.String useAttribute,
                                java.lang.String term)
                         throws QueryException
Creates a basic query node incorporating the Use attribute and term supplied.

Attribute supplied must be the name of a Use attribute (eg: "Title"), or a String representation of its corresponding numeric value (eg: "4").

Note: If an attribute name is supplied, it is checked against lookup table via org.jafer.conf.Config.getAttributeValue(), but not if a corresponding numeric value is used, in which case any positive value within the int range is accepted.

Specified by:
getNode in interface QueryBuilder
Parameters:
useAttribute - represents the Use attribute to be used in the query, e.g. "title".
term - the term to be used in the query.
Returns:
a query node.
Throws:
org.jafer.zclient.QueryException - if useAttribute parameter is not valid, and term is empty..
QueryException

getNode

public org.w3c.dom.Node getNode(int useAttribute,
                                java.lang.String term)
                         throws QueryException
Creates a basic query node incorporating the Use attribute and term supplied.

Attribute value is not checked, any positive int value is accepted.

Specified by:
getNode in interface QueryBuilder
Parameters:
useAttribute - represents the Use attribute to be used in the query.
term - the term to be used in the query.
Returns:
a query node.
Throws:
QueryException - if parameters are not valid or empty.

getNode

public org.w3c.dom.Node getNode(int[] attributes,
                                java.lang.String term)
                         throws QueryException
Creates a basic query node incorporating the attributes and term supplied.

The int[] parameter values are used in order for:

{Use, Relation, Position, Structure, Truncation, Completeness}

e.g. { 4, 3, 3, 2, 100, 1 }

A smaller sized array can be used, in which case the values given will be interpreted in order, following the pattern above.

A parameter value of 0 will cause the corresponding attribute type to be excluded from the query.

(If a default search profile has been set, it will be overridden for this query: any values supplied in the parameter will replace corresponding default values.)

Specified by:
getNode in interface QueryBuilder
Parameters:
attributes - represents the attribute values to be used in the query.
term - the term to be used in the query.
Returns:
a query node.
Throws:
org.jafer.zclient.QueryException - if attributes array size is smaller than 1, or larger than 6.
QueryException

getNode

public org.w3c.dom.Node getNode(int[][] attTypesValues,
                                java.lang.String term)
                         throws QueryException
Creates a basic query node incorporating the term and attributes supplied.

The int[][] parameter holds the attribute types and corresponding values.

e.g. {{1,1003}, {2,3}, {4,2}} would set:

Use = 1003, Relation = 3 and Structure = 2.

A parameter value of 0 will result in the corresponding attribute type to be excluded from the query.

(If a default search profile has been set, it will be altered for this query: any values given in the parameter will replace corresponding default values.)

Specified by:
getNode in interface QueryBuilder
Parameters:
attTypesValues - represents the attribute types and their corresponding values to be used in the query.
term - the term to be used in the query.
Returns:
a query node.
Throws:
org.jafer.zclient.QueryException - if attTypesValues array size is smaller than 1, or larger than 6. Also if the first attribute type is missing, or not a positive value.
QueryException

getNode

public org.w3c.dom.Node getNode(java.lang.String[] termAndAttributes)
                         throws QueryException
Creates a basic query node incorporating the term and attributes supplied.

The String[] parameter holds the term to be used in position [0], and the remaining attributes thereafter, eg:

{"Shakespeare", "1003", "3", "3", "2", "100", "1"}

Shorter arrays can be used, and values of "0" will cause that attribute to be omitted from the query.

(If a default search profile has been set, it will be ignored for this query)

Specified by:
getNode in interface QueryBuilder
Parameters:
termAndAttributes - the term (position [0]) and attributes to be used in the query.
Returns:
a query node.
Throws:
QueryException

buildConstraintModelNode

private org.w3c.dom.Node buildConstraintModelNode(int[] attributes,
                                                  java.lang.String term)
                                           throws QueryException
Builds an XML representation of a query, using Bib1 attributes.

Parameters:
attributes - represents the attribute values to be used in the query.
term - the term to be used in the query.
Returns:
a query node.
Throws:
org.jafer.zclient.QueryException - if there is an error in the attribute lookup, or the term is empty.
QueryException
See Also:
of XML structure available at www.jafer.org .

and

public org.w3c.dom.Node and(org.w3c.dom.Node leftNode,
                            org.w3c.dom.Node rightNode)
                     throws QueryException
Produces an AND node from 2 nodes, which can be query or Boolean nodes.

Specified by:
and in interface QueryBuilder
Parameters:
leftNode - a query, AND, OR or NOT node.
rightNode - a query, AND, OR or NOT node.
Returns:
an AND node.
Throws:
QueryException - if either Node parameter is null.

and

public org.w3c.dom.Node and(java.util.Vector useAttributes,
                            java.util.Vector terms)
                     throws QueryException
Produces an AND node from a pair of attributes, or a tree of AND nodes if more than 2 attributes are given.

Vector parameters: Minimum size is 2, and must contain only String objects.

Attributes supplied must be the name of a Use attribute (eg: "Title"), or a String representation of its corresponding numeric value (eg: "4").

Note: If an attribute name is supplied, it is checked against lookup table via org.jafer.conf.Config.getAttributeValue(), but not if a corresponding numeric value is used, in which case any positive value within the int range is accepted.

Parameters:
useAttributes - the attribute values to be used in building the query.
terms - the corresponding terms to be used in building the query.
Returns:
an AND node or tree.
Throws:
QueryException - if a problem is encountered with the input Vectors.

or

public org.w3c.dom.Node or(org.w3c.dom.Node leftNode,
                           org.w3c.dom.Node rightNode)
                    throws QueryException
Produces an OR node from 2 nodes, which can be query or Boolean nodes.

Specified by:
or in interface QueryBuilder
Parameters:
leftNode - a query, AND, OR or NOT node.
rightNode - a query, AND, OR or NOT node.
Returns:
an OR node.
Throws:
QueryException - if either Node parameter is null.

or

public org.w3c.dom.Node or(java.util.Vector useAttributes,
                           java.util.Vector terms)
                    throws QueryException
Produces an OR node from a pair of attributes, or a tree of OR nodes if more than 2 attributes are given.

Vector parameters: minimum size is 2, and must contain only String objects.

Attributes supplied must be the name of a Use attribute (eg: "Title"), or a String representation of its corresponding numeric value (eg: "4").

Note: If an attribute name is supplied, it is checked against lookup table via org.jafer.conf.Config.getAttributeValue(), but not if a corresponding numeric value is used, in which case any positive value within the int range is accepted.

Parameters:
useAttributes - the attribute values to be used in building the query.
terms - the corresponding terms to be used in building the query.
Returns:
an OR node or tree.
Throws:
QueryException - if a problem is encountered with the input Vectors.

not

public org.w3c.dom.Node not(org.w3c.dom.Node inputNode)
                     throws QueryException
Produces a NOT node from the supplied node, which can be a query or Boolean node. i.e. produces a negated version of the supplied node.

Specified by:
not in interface QueryBuilder
Parameters:
inputNode - a query, AND or OR node.
Returns:
a NOT node.
Throws:
QueryException - if inputNode parameter is null..

getNode

public org.w3c.dom.Node getNode(java.lang.String queryExp)
                         throws QueryException
Create a Node from a query string

Throws:
QueryException

getContent

public java.lang.String[][] getContent(org.w3c.dom.Node queryNode)
                                throws QueryException
Method returns a representation of an XML query as a 2 dimensional String[].

Operands follow operators.

An operator is a String[] containing 1 String, eg: {"and"}

An operand is a String[] containing a term, followed by up to 6 attributes. eg:

{"Shakespeare", "1003", "3", "3", "2", "100", "1"}

Parameters:
queryNode - the XML query node.
Returns:
A String[][] representation of a query.
Throws:
QueryException - if a problem is found in the query structure.

getNodeData

private void getNodeData(org.w3c.dom.Node node,
                         java.util.Vector nodeData)
                  throws QueryException
Throws:
QueryException

getConstraintModelData

private java.lang.String[] getConstraintModelData(org.w3c.dom.Node cM)
                                           throws QueryException
Throws:
QueryException

getNodeValue

private java.lang.String getNodeValue(org.w3c.dom.Node node)

lookUpUseAttribute

private int lookUpUseAttribute(java.lang.String attributeString)
                        throws QueryException
Throws:
QueryException