EcogridQueryParser.java,1.3 | . | Disk File |
Skipping to line 52 | . | |
* A class to parse ecogrid xml query document to java object |
. |
* A class to parse ecogrid xml query document to java object |
*/ |
. |
*/ |
public class EcogridQueryParser |
. |
public class EcogridQueryParser |
{ |
. |
{ |
//fields |
. |
//fields |
private QueryType ecogridQuery; |
. |
private QueryType _ecogridQuery; |
private Document doc; |
. |
private Node _queryNode; |
  |
. |
|
//Constant |
. |
//Constant |
private static final String QUERYIDATTRIBUTE = "/query/@queryId"; |
. |
private static final String QUERYIDATTRIBUTE = "@queryId"; |
private static final String SYSTEMATTRIBUTE = "/query/@system"; |
. |
private static final String SYSTEMATTRIBUTE = "@system"; |
private static final String NAMESPACE = "/query/namespace"; |
. |
private static final String NAMESPACE = "namespace"; |
private static final String PREFIX = "prefix"; |
. |
private static final String PREFIX = "prefix"; |
private static final String RETURNFIELD = "/query/returnfield"; |
. |
private static final String RETURNFIELD = "returnField"; |
private static final String TITLE = "/query/title"; |
. |
private static final String TITLE = "title"; |
private static final String FIRSTAND = "/query/AND"; |
. |
private static final String FIRSTAND = "AND"; |
private static final String FIRSTOR = "/query/OR"; |
. |
private static final String FIRSTOR = "OR"; |
private static final String FIRSTCONDITION = "/query/condition"; |
. |
private static final String FIRSTCONDITION = "condition"; |
private static final String AND = "AND"; |
. |
private static final String AND = "AND"; |
private static final String OR = "OR"; |
. |
private static final String OR = "OR"; |
private static final String CON = "condition"; |
. |
private static final String CON = "condition"; |
private static final String OPERATOR = "operator"; |
. |
private static final String OPERATOR = "operator"; |
private static final String CONCEPT = "concept"; |
. |
private static final String CONCEPT = "concept"; |
  |
. |
|
/** |
. |
/** |
* default constructor |
. |
* Constructor with reader for parsing XML |
* @param xmlInput xml source |
. |
* @param xmlInput xml source |
*/ |
. |
*/ |
public EcogridQueryParser (Reader xmlInput) throws Exception |
. |
public EcogridQueryParser (Reader xmlInput) throws Exception |
{ |
. |
{ |
//create new ecogrid query java object |
. |
//create new ecogrid query java object |
ecogridQuery = new QueryType(); |
. |
_ecogridQuery = new QueryType(); |
Skipping to line 84 | . | |
//build dom tree |
. |
//build dom tree |
InputSource in = new InputSource(xmlInput); |
. |
InputSource in = new InputSource(xmlInput); |
DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); |
. |
DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); |
dfactory.setNamespaceAware(false); |
. |
dfactory.setNamespaceAware(false); |
doc = dfactory.newDocumentBuilder().parse(in); |
. |
Document doc = dfactory.newDocumentBuilder().parse(in); |
}//EcogridQueryParser |
. |
|
  |
. |
// Find the query node, all searches are relative to it. |
  |
. |
NodeList queryIdList = XPathAPI.selectNodeList(doc, "/query"); |
  |
. |
_queryNode = queryIdList.item(0); |
  |
. |
|
  |
. |
} //EcogridQueryParser |
  |
. |
|
  |
. |
/** |
  |
. |
* Constructor wityh DOM node point to Query node |
  |
. |
* @param aQueryDOMNode DOM Node |
  |
. |
*/ |
  |
. |
public EcogridQueryParser (Node aQueryDOMNode) |
  |
. |
{ |
  |
. |
//create new ecogrid query java object |
  |
. |
_ecogridQuery = new QueryType(); |
  |
. |
_queryNode = aQueryDOMNode; |
  |
. |
} //EcogridQueryParser |
|
. |
|
/** |
. |
/** |
* Method to get ecogridquery |
. |
* Method to get ecogridquery |
*/ |
. |
*/ |
public QueryType getEcogridQuery() |
. |
public QueryType getEcogridQuery() |
Skipping to line 112 | . | |
return ecogridQuery; |
. |
return _ecogridQuery; |
} |
. |
} |
|
. |
|
/** |
. |
/** |
* Method to set ecogrid query |
. |
* Method to set ecogrid query |
*/ |
. |
*/ |
Skipping to line 119 | . | |
{ |
. |
{ |
ecogridQuery = query; |
. |
_ecogridQuery = query; |
} |
. |
} |
|
. |
|
/** |
. |
/** |
*Method set up ecogrid query object by parsing xml |
. |
*Method set up ecogrid query object by parsing xml |
*/ |
. |
*/ |
Skipping to line 130 | . | |
setupReturnField(); |
. |
setupReturnField(); |
setupTitle(); |
. |
setupTitle(); |
setupFirstRelation(); |
. |
setupFirstRelation(); |
}//parseXML |
. |
}//parseXML |
|
. |
|
|
. |
private Node getAttrNode(String aDocXPath, String aNodeXPath) |
  |
. |
{ |
  |
. |
Node node = _queryNode; |
  |
. |
try |
  |
. |
{ |
  |
. |
if (_queryNode.getNodeType() == Node.DOCUMENT_NODE) |
  |
. |
{ |
 
|
. |
NodeList queryIdList = XPathAPI.selectNodeList(_queryNode, aDocXP
ath); |
  |
. |
node = queryIdList.item(0); |
  |
. |
} |
  |
. |
else |
  |
. |
{ |
 
|
. |
NodeList queryIdList = XPathAPI.selectNodeList(_queryNode, aNod
eXPath); |
  |
. |
node = queryIdList.item(0); |
  |
. |
} |
  |
. |
} catch (Exception e) |
  |
. |
{ |
  |
. |
System.err.println(e); |
  |
. |
} |
  |
. |
return node; |
  |
. |
} |
/* setup queryid and system */ |
. |
/* setup queryid and system */ |
private void setupQueryIdAndSystem() throws Exception |
. |
private void setupQueryIdAndSystem() throws Exception |
{ |
. |
{ |
 
|
. |
// The _queryNode may be a document node qparsed in from an XML document
or may be |
  |
. |
// a generic DOM node (a subtree) of an existing tree |
// get query id |
. |
// get query id |
NodeList queryIdList = XPathAPI.selectNodeList(doc, QUERYIDATTRIBUTE);
|
. |
NodeList queryIdList = XPathAPI.selectNodeList(_queryNode, QUERYIDATTRIBU
TE); |
String queryId = (queryIdList.item(0)).getNodeValue(); |
. |
String queryId = ((Node)queryIdList.item(0)).getNodeValue(); |
  |
. |
|
//System.out.println("queryId is: "+ queryId); |
. |
//System.out.println("queryId is: "+ queryId); |
// set query id |
. |
// set query id |
ecogridQuery.setQueryId(queryId); |
. |
_ecogridQuery.setQueryId(queryId); |
// get system |
. |
// get system |
NodeList systemList = XPathAPI.selectNodeList(doc, SYSTEMATTRIBUTE);
|
. |
NodeList systemList = XPathAPI.selectNodeList(_queryNode, SYSTEMATTRIBUTE
); |
String system = (systemList.item(0)).getNodeValue(); |
. |
String system = (systemList.item(0)).getNodeValue(); |
//System.out.println("system is: "+ system); |
. |
//System.out.println("system is: "+ system); |
URI systemUri = new URI(system); |
. |
URI systemUri = new URI(system); |
//set system |
. |
//set system |
ecogridQuery.setSystem(systemUri); |
. |
_ecogridQuery.setSystem(systemUri); |
}//setupQueryIdAndSystem |
. |
|
  |
. |
} // setupQueryIdAndSystem |
|
. |
|
  |
. |
|
/* setup namepsace */ |
. |
/* setup namepsace */ |
private void setupNamespace() throws Exception |
. |
private void setupNamespace() throws Exception |
{ |
. |
{ |
// get namespace |
. |
// get namespace |
NodeList namespaceList = XPathAPI.selectNodeList(doc, NAMESPACE); |
. |
NodeList namespaceList = XPathAPI.selectNodeList(_queryNode, NAMESPACE); |
int length = namespaceList.getLength(); |
. |
int length = namespaceList.getLength(); |
QueryType_namespace [] namespaceArray; |
. |
QueryType_namespace [] namespaceArray; |
if ( length != 0 ) |
. |
if ( length != 0 ) |
{ |
. |
{ |
namespaceArray = new QueryType_namespace [length]; |
. |
namespaceArray = new QueryType_namespace [length]; |
Skipping to line 200 | . | |
queryNamespace.setPrefix(prefix); |
. |
queryNamespace.setPrefix(prefix); |
// put the object into array |
. |
// put the object into array |
namespaceArray[i]=queryNamespace; |
. |
namespaceArray[i]=queryNamespace; |
} |
. |
} |
// set up the QueryType_namespace object to ecogrid query object |
. |
// set up the QueryType_namespace object to ecogrid query object |
ecogridQuery.setNamespace(namespaceArray); |
. |
_ecogridQuery.setNamespace(namespaceArray); |
} |
. |
} |
}//setupNamespace |
. |
}//setupNamespace |
|
. |
|
/* setup returnfield */ |
. |
/* setup returnfield */ |
private void setupReturnField() throws Exception |
. |
private void setupReturnField() throws Exception |
Skipping to line 212 | . | |
// get retrunfield |
. |
// get retrunfield |
NodeList returnFieldList = XPathAPI.selectNodeList(doc, RETURNFIELD);
|
. |
NodeList returnFieldList = XPathAPI.selectNodeList(_queryNode, RETURNFIEL
D); |
String [] returnFieldArray ; |
. |
String [] returnFieldArray ; |
int length = returnFieldList.getLength(); |
. |
int length = returnFieldList.getLength(); |
if (length != 0) |
. |
if (length != 0) |
{ |
. |
{ |
returnFieldArray = new String [length]; |
. |
returnFieldArray = new String [length]; |
Skipping to line 222 | . | |
Node returnfield = returnFieldList.item(i); |
. |
Node returnfield = returnFieldList.item(i); |
String returnfieldString = returnfield.getFirstChild().getNodeValue()
; |
. |
String returnfieldString = returnfield.getFirstChild().getNodeValue()
; |
returnFieldArray[i] = returnfieldString; |
. |
returnFieldArray[i] = returnfieldString; |
} |
. |
} |
// set up the QueryType_namespace object to ecogrid query object |
. |
// set up the QueryType_namespace object to ecogrid query object |
ecogridQuery.setReturnfield(returnFieldArray); |
. |
_ecogridQuery.setReturnfield(returnFieldArray); |
} |
. |
} |
}//setupReturnField |
. |
}//setupReturnField |
|
. |
|
/* setup title */ |
. |
/* setup title */ |
private void setupTitle() throws Exception |
. |
private void setupTitle() throws Exception |
Skipping to line 234 | . | |
// get retrunfield |
. |
// get retrunfield |
NodeList titleList = XPathAPI.selectNodeList(doc, TITLE); |
. |
NodeList titleList = XPathAPI.selectNodeList(_queryNode, TITLE); |
String [] titleArray ; |
. |
String [] titleArray ; |
int length = titleList.getLength(); |
. |
int length = titleList.getLength(); |
if (length != 0) |
. |
if (length != 0) |
{ |
. |
{ |
titleArray = new String [length]; |
. |
titleArray = new String [length]; |
Skipping to line 245 | . | |
String titleString = title.getFirstChild().getNodeValue(); |
. |
String titleString = title.getFirstChild().getNodeValue(); |
//System.out.println("title is: " + titleString); |
. |
//System.out.println("title is: " + titleString); |
titleArray[i] = titleString; |
. |
titleArray[i] = titleString; |
} |
. |
} |
// set up the QueryType_namespace object to ecogrid query object |
. |
// set up the QueryType_namespace object to ecogrid query object |
ecogridQuery.setTitle(titleArray); |
. |
_ecogridQuery.setTitle(titleArray); |
} |
. |
} |
}//setupReturnField |
. |
}//setupReturnField |
|
. |
|
/* setup first relation type */ |
. |
/* setup first relation type */ |
private void setupFirstRelation() throws Exception |
. |
private void setupFirstRelation() throws Exception |
Skipping to line 257 | . | |
NodeList firstAND = XPathAPI.selectNodeList(doc, FIRSTAND); |
. |
NodeList firstAND = XPathAPI.selectNodeList(_queryNode, FIRSTAND); |
NodeList firstOR = XPathAPI.selectNodeList(doc, FIRSTOR); |
. |
NodeList firstOR = XPathAPI.selectNodeList(_queryNode, FIRSTOR); |
NodeList firstCondition = XPathAPI.selectNodeList(doc, FIRSTCONDITION);
|
. |
NodeList firstCondition = XPathAPI.selectNodeList(_queryNode, FIRSTCONDIT
ION); |
int andLength = firstAND.getLength(); |
. |
int andLength = firstAND.getLength(); |
int orLength = firstOR.getLength(); |
. |
int orLength = firstOR.getLength(); |
int conLength = firstCondition.getLength(); |
. |
int conLength = firstCondition.getLength(); |
if ( andLength != 0 && orLength ==0 && conLength == 0) |
. |
if ( andLength != 0 && orLength ==0 && conLength == 0) |
{ |
. |
{ |
Skipping to line 266 | . | |
ANDType and = new ANDType(); |
. |
ANDType and = new ANDType(); |
handleANDType(and, firstAND.item(0)); |
. |
handleANDType(and, firstAND.item(0)); |
ecogridQuery.setAND(and); |
. |
_ecogridQuery.setAND(and); |
|
. |
|
} |
. |
} |
else if (orLength != 0 && andLength ==0 && conLength == 0) |
. |
else if (orLength != 0 && andLength ==0 && conLength == 0) |
{ |
. |
{ |
// first is or |
. |
// first is or |
Skipping to line 275 | . | |
handleORType(or, firstOR.item(0)); |
. |
handleORType(or, firstOR.item(0)); |
ecogridQuery.setOR(or); |
. |
_ecogridQuery.setOR(or); |
} |
. |
} |
else if (conLength != 0 &&orLength == 0 && andLength == 0 ) |
. |
else if (conLength != 0 &&orLength == 0 && andLength == 0 ) |
{ |
. |
{ |
// first is condition |
. |
// first is condition |
ConditionType condition = handleConditionType(firstCondition.item(0)); |
. |
ConditionType condition = handleConditionType(firstCondition.item(0)); |
ecogridQuery.setCondition(condition); |
. |
_ecogridQuery.setCondition(condition); |