www.pudn.com > gvSIG-1_1-rc1-src.zip > CswCapabilitiesParser.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.csw.parsers;
import es.gva.cit.catalogClient.csw.drivers.CSWCatalogServiceDriver;
import es.gva.cit.catalogClient.metadataXML.XMLNode;
import es.gva.cit.catalogClient.metadataXML.XMLTree;
import es.gva.cit.catalogClient.schemas.Schemas;
import es.gva.cit.catalogClient.schemas.discoverer.SOAPMessageParser;
import es.gva.cit.catalogClient.utils.Strings;
import java.net.URL;
import java.util.Vector;

/**
 * 
 * 
 * 
 * @author Jorge Piera Llodra (piera_jor@gva.es)
 */
public class CswCapabilitiesParser {
/**
 * 
 * 
 */
    private CSWCatalogServiceDriver driver;

/**
 * 
 * 
 */
    private URL url;
/**
 * 
 * 
 */
    private XMLNode rootNode;

/**
 * 
 * 
 * 
 * @param driver Protocol Driver
 * @param url Server Protocol
 */
    public  CswCapabilitiesParser(CSWCatalogServiceDriver driver, URL url) {        
        this.driver = driver;
        this.url = url;        
    } 

/**
 * 
 * 
 * 
 * @return 
 * @param node 
 */
    public boolean parse(XMLNode node) {        
        rootNode = node;
        
        String prefix = "";
        XMLNode[] operations = null;
        if (httpServerIsReady() == false){
            return false;
        }
        
        if (soapServerIsReady() == false){
            return false;
        }
       
        
        discoverServerProfile(node);
        
        if (driver.getServerProfile().equals(Schemas.EBRIM)){
            boolean b = new CswIDECCapabilitiesParser(driver).parse();
            if (b){
                setWelcomeMessage(node,"");
            }
            return b;
        }        
                        
        //Vamos a recorrer todas las operaciones. Para cada una de ellas
        //tomaremos las acciones correspondientes
        operations = XMLTree.searchMultipleNode(node,
                "ows:OperationsMetadata->ows:Operation");
        if (!(operations.length == 0)) {
            prefix = "ows:";
        } else {
            operations = XMLTree.searchMultipleNode(node,
                    "OperationsMetadata->Operation");
        }
        if ((operations == null) || (operations.length == 0)) {
            driver.setServerAnswerReady("errorNotCSWSupportedProtocol");
            
            return false;
        }
        if (this.driver.getCommunicationProtocol().equals("GET") &&
                getHTTPExceptions(node)) {
            return false;
        }
        if (this.driver.getCommunicationProtocol().equals("SOAP") &&
                getSOAPExceptions(node)) {
            return false;
        }
        
        //To save the protocols supported by each operation
        driver.setOperations(new CswSupportedProtocolOperations());
        
        //Operations name
        String sOperation;
        
        for (int i = 0; i < operations.length; i++) {
            sOperation = XMLTree.searchAtribute(operations[i], "name");
            //////////////////Get Capabilities////////////////////
            if (sOperation.equals("GetCapabilities") ||
                    sOperation.equals("CSW-Discovery.getCapabilities")) {
                parseCapabilities(operations[i], prefix);
            }
            ////////////////////DescribeRecord//////////////////
            if (sOperation.equals("DescribeRecord")) {
                parseDescribeRecord(operations[i], prefix);
            }
            ////////////////////GetDomain//////////////////
            if (sOperation.equals("GetDomain")) {
                parseGetDomain(operations[i], prefix);
            }
            ////////////////////GetRecords//////////////////
            if (sOperation.equals("GetRecords") ||
                    sOperation.equals("CSW-Discovery.getRecords")) {
                if (!(parseGetRecords(operations[i], prefix))) {
                    return false;
                }
            }
            ////////////////////GetRecordsById//////////////////
            if (sOperation.equals("GetRecordsById") ||
                    sOperation.equals("CSW-Discovery.getRecordById")) {
                parseGetRecordsByID(operations[i], prefix);
            }
            ////////////////////Transaction//////////////////
            if (sOperation.equals("Transaction") ||
                    sOperation.equals("CSW-Publication.transaction")) {
                parseTransaction(operations[i], prefix);
            }
            ////////////////////Harvest//////////////////
            if (sOperation.equals("Harvest") ||
                    sOperation.equals("CSW-Publication.harvest")) {
                parseHarvest(operations[i], prefix);
            }
        }      
        setWelcomeMessage(node,prefix);
        setConstantValues();
        return true;
    } 

/**
 * This method is used to detect some common problems when the
 * used protocol has been HTTP
 * 
 * 
 * @return true if all is OK
 */
    private boolean httpServerIsReady() {        
        if (rootNode == null){
            driver.setServerAnswerReady("errorNotSupportedProtocol");
            return false;
        }
        
        if (rootNode.getName().equals("SRW:explainResponse")){
            driver.setServerAnswerReady("errorIsASRWServer");
            return false;
        }  
        
        if (rootNode.getName().equals("ows:ServiceExceptionReport")){
            driver.setServerAnswerReady("errorServerException");
            return false;
        }  
        return true;
    } 

/**
 * This method is used to detect some common problems when the
 * used protocol has been SOAP
 * 
 * 
 * @return true if all is OK
 */
    private boolean soapServerIsReady() {        
        if (XMLTree.searchNode(rootNode,"SOAP-ENV:Body->SOAP-ENV:Fault") != null){
            driver.setServerAnswerReady("errorSOAPProtocol");
            return false;
        }  
        return true;
    } 

/**
 * This method is used to discover the CSW server profile. The value
 * can be ebRIM or ISO19115
 * 
 * 
 * @param node 
 */
    private void discoverServerProfile(XMLNode node) {        
    	if ((url.getHost().equals("delta.icc.es")) || 
                (url.getHost().equals("indicio.demo.galdosinc.com")) ||
                (url.getHost().equals("laits.gmu.edu"))){   
            driver.setServerProfile(Schemas.EBRIM);
        }else{
            driver.setServerProfile(Schemas.ISO19115);
        }
        
    } 

/**
 * Sets the welcome message
 * 
 * 
 * @param node 
 * @param prefix 
 */
    private void setWelcomeMessage(XMLNode node, String prefix) {        
        //Escribimos el mensaje de BienVenida
        driver.setServerAnswerReady(XMLTree.searchNodeValue(node,
                prefix + "ServiceIdentification->" + prefix + "Title") + "\n" +
            XMLTree.searchNodeValue(node,
                prefix + "ServiceIdentification->" + prefix + "Abstract"));
    } 

/**
 * Sets some constant values
 * 
 */
    private void setConstantValues() {        
        driver.setSortBy("true");
        driver.setStartPosition("1");
        driver.setMaxRecords("10");
    } 

/**
 * 
 * 
 * 
 * @param node 
 * @param prefix 
 */
    private void parseCapabilities(XMLNode node, String prefix) {        
    	driver.getOperations().setGetCapabilities(setSupportedProtocols(node,prefix));
    	
    } 

/**
 * 
 * 
 * 
 * @param node 
 * @param prefix 
 */
    private void parseDescribeRecord(XMLNode node, String prefix) {        
    	driver.getOperations().setDescribeRecords(setSupportedProtocols(node,prefix));
    } 

/**
 * 
 * 
 * 
 * @param node 
 * @param prefix 
 */
    private void parseGetRecordsByID(XMLNode node, String prefix) {        
    	driver.getOperations().setGetRecordsById(setSupportedProtocols(node,prefix));
    } 

/**
 * 
 * 
 * 
 * @param node 
 * @param prefix 
 */
    private void parseGetDomain(XMLNode node, String prefix) {        
    	driver.getOperations().setGetDomain(setSupportedProtocols(node,prefix));
    } 

/**
 * 
 * 
 * 
 * @return 
 * @param node 
 * @param prefix 
 */
    private boolean parseGetRecords(XMLNode node, String prefix) {        
    	driver.getOperations().setGetRecords(setSupportedProtocols(node,prefix));
    	
    	XMLNode[] parameters;
        //Alamacenamos los parametros y los escribimos
        parameters = XMLTree.searchMultipleNode(node, prefix + "Parameter");
        for (int j = 0; j < parameters.length; j++) {
            String sParameter = XMLTree.searchAtribute(parameters[j], "name");
            String[] values = XMLTree.searchMultipleNodeValue(parameters[j],
                    prefix + "Value");
            String[] defaultValue = XMLTree.searchMultipleNodeValue(parameters[j],
                    prefix + "DefaultValue");
            if (sParameter.equals("TypeName")) {
                driver.setTypeNames(Strings.join(defaultValue, values));
            }
            if (sParameter.equals("outputFormat")) {
                if (!(Strings.find("text/xml", defaultValue) ||
                        Strings.find("text/xml", values))) {
                    if (!(Strings.find("application/xml", defaultValue) ||
                            Strings.find("application/xml", values))) {
                        driver.setServerAnswerReady("El servidor no devuelve " +
                            "texto en formato XML");
                        return false;
                    }
                }
            }
            driver.setOutputFormat("text/xml");
            if (sParameter.equals("outputSchema")) {
                driver.setOutputSchema(Strings.join(defaultValue, values));
                driver.setServerProfile(Schemas.DUBLINCORE);
                for (int i=0 ; i