package net.sourceforge.fenixedu.integration.server; import java.util.Iterator; import org.apache.oro.text.perl.Perl5Util; import org.dspace.search.DSAnalyzer; import org.dspace.search.QueryArgs; import pt.utl.ist.fenix.tools.file.FilesetMetadataQuery; import pt.utl.ist.fenix.tools.file.FilesetMetadataQuery.ConjunctionType; import pt.utl.ist.fenix.tools.file.FilesetMetadataQuery.MetadataQuery; import pt.utl.ist.fenix.tools.file.FilesetMetadataQuery.MetadataQueryComponent; /** * Query Parser for Dublin Core Queries as used in DSpaceRemoteManager */ public class DCQueryParser extends QueryArgs { // the query string private String query; /** * set the query string * * @param newQuery */ public void setQuery(String newQuery) { query = newQuery; } /** * retrieve the query string * * @return the current query string */ public String getQuery() { return query; } /** * Builds an advanced-query description string. * * @return the query description string built */ public String buildQuery(FilesetMetadataQuery query) { StringBuffer newquery = new StringBuffer("("); ConjunctionType previousConjunction = null; int orCounts=0; for(Iterator queryComponentIter=query.iterator();queryComponentIter.hasNext();) { MetadataQueryComponent queryComponent=queryComponentIter.next(); String field=queryComponent.getQuery().getField(); String searchString=queryComponent.getQuery().getSearchString(); /* * If lucene finds a stop word it aborts the search. * So we remove all stop words from query. */ if(DSAnalyzer.isStopWord(searchString)) { continue; } ConjunctionType currentConjunctionType = queryComponent.getNextQueryConjunction(); if(ConjunctionType.OR.equals(currentConjunctionType)) { newquery.append("("); orCounts++; } newquery.append(buildQueryPart(searchString, field)); if(queryComponentIter.hasNext()) { if((ConjunctionType.OR.equals(previousConjunction) && (currentConjunctionType==null || ConjunctionType.AND.equals(currentConjunctionType)))) { for(;orCounts>0;orCounts--) newquery.append(")"); } if(currentConjunctionType!=null) newquery.append(" ").append(queryComponent.getNextQueryConjunction().name()).append(" "); else newquery.append(" ").append(ConjunctionType.AND.name()).append(" "); } else { for(;orCounts>0;orCounts--) newquery.append(")"); } previousConjunction = currentConjunctionType; } newquery.append(")"); String returnQuery = newquery.toString(); return returnQuery.contains(" AND )") ? returnQuery.substring(0,returnQuery.length()-6) +")" : returnQuery ; } /** * Builds a query-part using the field and value passed in * with ' --> " (single to double quote) translation. * * @param myquery the value the query will look for * @param myfield the field myquery will be looked for in * * @return the query created */ private String buildQueryPart(String myquery, String myfield) { Perl5Util util = new Perl5Util(); StringBuffer newquery = new StringBuffer("("); if (!myfield.equals(MetadataQuery.ANY_FIELD)) { newquery.append(myfield).append(":"); myquery = util.substitute("s/\'(.*)\'/\"$1\"/g", myquery); if (!util.match("/\".*\"/", myquery)) { myquery = util.substitute("s/ / " + myfield + ":/g", myquery); } } newquery.append(myquery ).append(")"); return newquery.toString(); } /** * Constructs a HashMap with the keys field{1,2,3}, query{1,2,3} and * conjunction{1,2} taking the values from the passed-in argument * defaulting to "". * * @param request the request-describing object to take the values from * * @return the created HashMap */ /* public HashMap buildQueryHash(HttpServletRequest request) { HashMap queryHash = new HashMap(); queryHash.put("query1", (request.getParameter("query1") == null) ? "" : request.getParameter("query1")); queryHash.put("query2", (request.getParameter("query2") == null) ? "" : request.getParameter("query2")); queryHash.put("query3", (request.getParameter("query3") == null) ? "" : request.getParameter("query3")); queryHash.put("field1", (request.getParameter("field1") == null) ? "ANY" : request .getParameter("field1")); queryHash.put("field2", (request.getParameter("field2") == null) ? "ANY" : request .getParameter("field2")); queryHash.put("field3", (request.getParameter("field3") == null) ? "ANY" : request .getParameter("field3")); queryHash.put("conjunction1", (request.getParameter("conjunction1") == null) ? "AND" : request.getParameter("conjunction1")); queryHash.put("conjunction2", (request.getParameter("conjunction2") == null) ? "AND" : request.getParameter("conjunction1")); return (queryHash); } */ /** * Builds an HTTP query string for some parameters with the value * taken from the request context passed in. * * The returned string includes key/value pairs in the HTTP query string * format (key1=value1&key2=value2...) for the keys query{1,2,3}, * field{1,2,3} and conjunction{1,2} with values taken from request * and defaulting to "". *

* Note, that the values are url-encoded using the UTF-8 encoding scheme * as the corresponding W3C recommendation states. *

* Also note that neither leading ? (question mark) * nor leading & (ampersand mark) is included. * Take this into account when appending to a real URL. * * @param request the request object to take the values from * * @return the query string that can be used without further * transformationin URLs * */ /* public String buildHTTPQuery(HttpServletRequest request) throws UnsupportedEncodingException { String querystring = ""; HashMap queryHash = buildQueryHash(request); Iterator i = queryHash.keySet().iterator(); while (i.hasNext()) { String key = (String) i.next(); String value = (String) queryHash.get(key); querystring = querystring + "&" + key + "=" + URLEncoder.encode(value, Constants.DEFAULT_ENCODING); } // return the result with the leading "&" removed return (querystring.substring(1)); }*/ }