Changeset 1812 for TI01-discovery


Ignore:
Timestamp:
05/12/06 09:58:03 (13 years ago)
Author:
mpritcha
Message:

Used Matcher.quoteReplacement() to avoid problems inserting orderByField into xquery.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • TI01-discovery/trunk/ws-Discovery2/src/ndg/services/discovery/SearchAgent.java

    r1801 r1812  
    1515import java.text.DecimalFormat; 
    1616import java.text.SimpleDateFormat; 
    17  
     17import java.util.regex.Matcher; 
    1818import javax.xml.namespace.*; 
    1919import javax.xml.xpath.*; 
     
    5050        String term = ""; 
    5151        String orderBy;                                                 // representation used in WSDL e.g. "date", "dataCentre" 
    52         String orderByField = "";                               // xquery / xpath expression representing orderBy field 
     52        String orderByField = "document($name)/dgMetadata/dgMetadataRecord/dgDataEntity/dgDataSummary/dgDataCoverage/dgTemporalCoverage/DateRange/DateRangeStart";                              // xquery / xpath expression representing orderBy field 
    5353        String orderByDirection = "ascending";  //default orderBy direction 
    5454        // TODO ...add orderByDirection (and to WSDL) 
     
    120120                if ( s.equals("date") ) 
    121121                { 
    122                         this.orderByField = "{document($name)/dgMetadata/dgMetadataRecord/dgDataEntity/dgDataSummary/dgDataCoverage/dgTemporalCoverage/DateRange/DateRangeStart}"; 
     122                        this.orderByField = "document($name)/dgMetadata/dgMetadataRecord/dgDataEntity/dgDataSummary/dgDataCoverage/dgTemporalCoverage/DateRange/DateRangeStart"; 
    123123                } 
    124124 
     
    126126                { 
    127127                        // TODO ...not correct yet (should look up Organisation moles record) 
    128                         this.orderByField = "{document($name)/dgMetadata/dgMetadataRecord/dgDataEntity/dgDataRoles/dgDataCurator/dgRoleHolder/dgOrganisationID/repositoryIdentifier}"; 
     128                        this.orderByField = "document($name)/dgMetadata/dgMetadataRecord/dgDataEntity/dgDataRoles/dgDataCurator/dgRoleHolder/dgOrganisationID/repositoryIdentifier"; 
    129129                } 
    130130        } 
     
    419419                        xqueryStr = xqueryStr.replaceFirst("__subst_doTerm__", this.doTerm ? "fn:true()" : "fn:false()"); 
    420420                        xqueryStr = xqueryStr.replaceFirst("__subst_doSpatioTemp__", (this.doSpatio | this.doTemporal) ? "fn:true()" : "fn:false()"); 
    421                         xqueryStr = xqueryStr.replaceFirst("__subst_doOrderBy__", this.doOrderBy ? "fn:true()" : "fn:false()"); 
     421                        xqueryStr = xqueryStr.replaceFirst("__subst_doOrderBy__", this.doOrderBy ? "fn:true()" : "fn:false()"); //NB replaceAll cos more than 1!  
    422422                        xqueryStr = xqueryStr.replaceFirst("__subst_doScope__", this.doScope ? "fn:true()" : "fn:false()"); 
     423                         
     424                        // orderByField contains $ chars etc so needs special treatment to avoid "java.lang.IllegalArgumentException: Illegal group reference" when replacing 
     425                        String orderByFieldReplace = Matcher.quoteReplacement(this.orderByField); 
     426                        xqueryStr = xqueryStr.replaceAll("__subst_orderByField__", orderByFieldReplace); //NB replaceAll cos more than 1!  
    423427 
    424428                        System.out.println("Xquery was \n" + xqueryStr); 
     
    531535"   \"__subst_scope__\" \n"+ 
    532536"}; \n"+ 
    533 "declare function local:getOrderByField() as xs:string { \n"+ 
    534 "   \"__subst_orderByField__\" \n"+ 
    535 "}; \n"+ 
    536537"declare function local:getSpatioTempWhereClause() as xs:string { \n"+ 
    537538"   \"__subst_spatioTempWhereClause__\" \n"+ 
     
    575576"      for $i in $arg1 \n"+ 
    576577"       let $coll := '/db/discovery/moles' \n"+ 
    577 "       let $thisdoc := util:document-name($i) \n"+ 
     578"       let $thisdoc := $i/text() \n"+ 
    578579"       let $name := string-join( ($coll , $thisdoc), '/' ) \n"+ 
    579580"       let $molesdoc := document( $name ) \n"+ 
     
    587588"      if ( local:getOrderByDirection() = \"descending\" ) then ( \n"+ 
    588589"         for $i in $arg1 \n"+ 
    589 "         order by local:getOrderByField() descending \n"+ 
     590"         let $coll := '/db/discovery/moles' \n"+ 
     591"         let $thisdoc := $i/text() \n"+ 
     592"         let $name := string-join( ($coll , $thisdoc), '/' ) \n"+ 
     593"         order by document($name)/dgMetadata/dgMetadataRecord/dgDataEntity/dgDataSummary/dgDataCoverage/dgTemporalCoverage/DateRange/DateRangeStart descending \n"+ 
    590594"         return $i \n"+ 
    591595"      ) \n"+ 
    592596"      else ( \n"+ 
    593597"         for $i in $arg1 \n"+ 
    594 "         order by local:getOrderByField() ascending \n"+ 
     598"         let $coll := '/db/discovery/moles' \n"+ 
     599"         let $thisdoc := $i/text() \n"+ 
     600"         let $name := string-join( ($coll , $thisdoc), '/' ) \n"+ 
     601"         order by document($name)/dgMetadata/dgMetadataRecord/dgDataEntity/dgDataSummary/dgDataCoverage/dgTemporalCoverage/DateRange/DateRangeStart ascending \n"+ 
    595602"         return $i \n"+ 
    596603"      ) \n"+ 
Note: See TracChangeset for help on using the changeset viewer.