www.pudn.com > gvSIG-1_1-rc1-src.zip > FilterEncoding.java



/* gvSIG. Sistema de Información Geográfica de la Generalitat Valenciana
*
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
*
* For more information, contact:
*
*  Generalitat Valenciana
*   Conselleria d'Infraestructures i Transport
*   Av. Blasco Ibáñez, 50
*   46010 VALENCIA
*   SPAIN
*
*      +34 963862235
*   gvsig@gva.es
*      www.gvsig.gva.es
*
*    or
*
*   IVER T.I. S.A
*   Salamanca 50
*   46005 Valencia
*   Spain
*
*   +34 963163400
*   dac@iver.es
*/
package es.gva.cit.catalogClient.languages;
import es.gva.cit.catalogClient.querys.Coordinates;
import java.util.Iterator;

/**
 * This class implements the Filter Encoding Language. It is used to
 * create querys in this language
 * 
 * 
 * @author Jorge Piera Llodra (piera_jor@gva.es)
 * @see http://portal.opengeospatial.org/files/?artifact_id=8340
 */
public class FilterEncoding extends AbstractGeneralLanguage {

/**
 * 
 * 
 */
    private String prefix = null;

/**
 * 
 * 
 */
    private String wildCard = null;

/**
 * 
 * 
 */
    private String singleChar = null;

/**
 * 
 * 
 */
    private String escape = null;

/**
 * 
 * 
 */
    private String filterAttribute = null;

/**
 * Create a new Filter Encoding Parser
 * 
 * 
 * @param prefix Prefix of the labels (if its necessary). Typically "ogc".
 * @param wildCard It Depends of the server
 * @param singleChar It Depends of the server
 * @param escape It Depends of the server
 * @param filterAttribute Sometimes, "Field" label needs an attribute.
 */
    public  FilterEncoding(String prefix, String wildCard, String singleChar, String escape, String filterAttribute) {        
        this.prefix = prefix;
        this.wildCard = wildCard;
        this.singleChar = singleChar;
        this.escape = escape;
        this.filterAttribute = filterAttribute;
    } 

/**
 * It Adds a new clause of the query
 * 
 * 
 * @param parameter Parameter name
 * @param value Parameter value
 * @param concordancia "E" (Exact phrase), "A" (All words) or "Y" (anY word).
 * @param relationship PropertyIsLike, PropertyIsLess, PropertyIsGreater,... See the File encoding
 * Documentation.
 * @param type Values: "P" (to comparate two propertyes) or "L" (to comparate one property
 * and one literal value)
 * @param operator "And" or "Or". Operator between fields
 */
    public void addClauses(String parameter, String value, String concordancia, String relationship, String type, String operator) {        
        currentClause = null;
        //Seperating the words
        Iterator values = parseValues(value, concordancia);
        //Filling the words
        addClauses(parameter, values, concordancia, relationship, type, operator);
    } 

/**
 * 
 * 
 * 
 * @param parameter 
 * @param values 
 * @param concordancia 
 * @param relationship 
 * @param type 
 * @param operator 
 */
    public void addClauses(String parameter, Iterator values, String concordancia, String relationship, String type, String operator) {        
        while (values.hasNext())
            addTerm(parameter, (String) values.next(), concordancia,
                relationship, type);
        addCurrentClauseQuery(operator);
    } 

/**
 * It adds a new term to the full query
 * 
 * 
 * @param parameter Parameter name
 * @param value Parameter Value
 * @param concordancia "E" (Exact phrase), "A" (All words) or "Y" (anY word).
 * @param relationship PropertyIsLike, PropertyIsLess, PropertyIsGreater,... See the File encoding
 * Documentation.
 * @param type Values: "P" (to comparate two propertyes) or "L" (to comparate one property
 * and one literal value)
 */
    private void addTerm(String parameter, String value, String concordancia, String relationship, String type) {        
        StringBuffer term = new StringBuffer();
        term.append(propertyIsXXX(relationship, parameter, value, type));
        if (currentClause == null) {
            currentClause = term.toString();
        } else {
            currentClause = currentClause + term.toString();
            currentClause = enterLabel(currentClause, getOperator(concordancia));
        }
    } 

/**
 * It adds the "and" label to join different operations
 * 
 * 
 * @param operator 
 */
    protected void addCurrentClauseQuery(String operator) {        
        if (currentClause != null) {
            if (currentQuery == null) {
                currentQuery = currentClause;
            } else {
                currentQuery = currentQuery + currentClause;
                currentQuery = enterLabel(currentQuery, operator);
            }
        }
    } 

/**
 * It returns the encoded query
 * 
 * 
 * @return 
 */
    public String toString() {        
        return enterLabel(currentQuery, "Filter");
    } 

/**
 * Envuelve a una pregunta con una etiqueta
 * 
 * 
 * @return String : parte de la query en el lenguaje soportado
 * @param pregunta Pregunta a envolver
 * @param etiqueta Nombre de la etiqueta
 */
    public String enterLabel(String pregunta, String etiqueta) {        
        if (etiqueta.equals("Filter") && (this.filterAttribute != null)) {
            return "<" + prefix + etiqueta + " " + this.filterAttribute + ">" +
            pregunta + "";
        } else {
            return "<" + prefix + etiqueta + ">" + pregunta + "";
        }
    } 

/**
 * It writes a "PropertyIsXXX" part of a filtyer encoding query
 * 
 * 
 * @return The part of the query
 * @param property Possible Values: PropertIsLike, PropertyIsLess, PropertyIsGreater,... See
 * the Filter Encoding documentation
 * @param parameter Parameter name
 * @param value Parameter value
 * @param type Values: "P" (to comparate two propertyes) or "L" (to comparate one property
 * and one literal value)
 */
    public String propertyIsXXX(String property, String parameter, String value, String type) {        
        String cadena = "";
        cadena = "<" + prefix + property;
        if (property.equals("PropertyIsLike")) {
            if (this.wildCard != null) {
                cadena = cadena + " wildCard=\"" + this.wildCard + "\"";
            }
            if (this.singleChar != null) {
                cadena = cadena + " singleChar=\"" + this.singleChar + "\"";
            }
            if (this.escape != null) {
                cadena = cadena + " escape=\"" + this.escape + "\"";
            }
        }
        cadena = cadena + ">" + enterLabel(parameter, "PropertyName");
        if (type.equals("P")) {
            cadena = cadena + enterLabel(value, "PropertyName");
        } else {
            cadena = cadena + enterLabel(value, "Literal");
        }
        return cadena + "";
    } 

/**
 * It Adds a Bounding Box query
 * 
 * 
 * @param coordinates Coordinates to find
 * @param propertyName Property that contains the geom field
 * @param not If we have to envolve the query with the "NOT" tag.
 */
    public void addBoundingBox(Coordinates coordinates, String propertyName, boolean not) {        
        // sNorth -> Uly();
        // sWest -> Ulx();
        // sSouth -> Bry();
        // sEast -> Brx();
        String bbox = "" + "" + propertyName +
            "" + "" + "" + "" +
            coordinates.ulx + "" + "" + coordinates.bry +
            "" + "" + "" + "" +
            coordinates.brx + "" + "" + coordinates.uly +
            "" + "" + "" + "";
        if (not){
            bbox = "" + bbox + ""; 
        }
        if (currentQuery == null) {
            currentQuery = bbox;
        } else {
            currentQuery = currentQuery + bbox;
            currentQuery = enterLabel(currentQuery, "And");
        }
    } 
 }