Package org.geoforge.worldwindogc.capabilities

Source Code of org.geoforge.worldwindogc.capabilities.GfrWMSCapabilities

/*
*  Copyright (C) 2011-2014 GeoForge Project
*
*  This program is free software; you can redistribute it and/or
*  modify it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
*
*  You should have received a copy of the GNU Lesser 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.
*/
package org.geoforge.worldwindogc.capabilities;

import gov.nasa.worldwind.ogc.OGCConstants;
import gov.nasa.worldwind.ogc.OGCOnlineResource;
import gov.nasa.worldwind.ogc.OGCRequestDescription;
import gov.nasa.worldwind.ogc.wms.WMSCapabilityInformation;
import gov.nasa.worldwind.ogc.wms.WMSLayerCapabilities;
import gov.nasa.worldwind.ogc.wms.WMSServiceInformation;
import gov.nasa.worldwind.util.Logging;
import gov.nasa.worldwind.util.WWUtil;
import gov.nasa.worldwind.util.xml.XMLEventParser;
import gov.nasa.worldwind.util.xml.XMLEventParserContext;
import gov.nasa.worldwind.wms.CapabilitiesRequest;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.XMLEvent;


/**
*
* @author bantchao
*/
public class GfrWMSCapabilities extends GfrOGCCapabilities
{
   protected static final QName ROOT_ELEMENT_NAME_1_1_1 = new QName("WMT_MS_Capabilities");
    protected static final QName ROOT_ELEMENT_NAME_1_3_0 = new QName("WMS_Capabilities");
   
   @Override
    public String getDefaultNamespaceURI()
    {
        return OGCConstants.WMS_NAMESPACE_URI;
    }
  
   public static GfrWMSCapabilities retrieve(URI uri) throws
           IOException,
           XMLStreamException,
           IllegalArgumentException,
           URISyntaxException
    {
        CapabilitiesRequest request = new CapabilitiesRequest(uri);
        return new GfrWMSCapabilities(request);
    }
  
   public GfrWMSCapabilities(Object docSource) throws
           IOException,
           XMLStreamException
    {
        super(OGCConstants.WMS_NAMESPACE_URI, docSource);

        this._initialize_();
    }
  
   public String getRequestURL(String requestName, String protocol, String requestMethod)
    {
        OGCRequestDescription rd = this.getRequestDescription(requestName);
       
        if (rd == null)
           return null;
       
        OGCOnlineResource ol = rd.getOnlineResouce(protocol, requestMethod);
       
        if (ol == null)
               return null;
       
        String strRef = ol.getHref();
       
        if (strRef == null)
           return null;
       
        return strRef.trim(); // caution, encountered XML files contain string with space at the end
    }
  
   public OGCRequestDescription getRequestDescription(String requestName)
    {
        for (OGCRequestDescription rd : this.getCapabilityInformation().getRequestDescriptions())
        {
            if (rd.getRequestName().equalsIgnoreCase(requestName))
                return rd;
        }

        return null;
    }
  
   public Set<String> getImageFormats()
    {
        Set<OGCRequestDescription> requestDescriptions = this.getCapabilityInformation().getRequestDescriptions();
       
        for (OGCRequestDescription rd : requestDescriptions)
        {
            if (rd.getRequestName().equals("GetMap"))
                return rd.getFormats();
        }

        return null;
    }
  
   public Double[] getLayerExtremeElevations(GfrWMSCapabilities caps, String[] layerNames)
    {
        if (caps == null)
        {
            String message = Logging.getMessage("nullValue.WMSCapabilities");
            Logging.logger().severe(message);
            throw new IllegalArgumentException(message);
        }

        if (layerNames == null)
        {
            String message = Logging.getMessage("nullValue.WMSLayerNames");
            Logging.logger().severe(message);
            throw new IllegalArgumentException(message);
        }

        Double extremeMin = null;
        Double extremeMax = null;

        for (String name : layerNames)
        {
            WMSLayerCapabilities layer = caps.getLayerByName(name);
           
            if (layer == null)
                continue;

            Double min = layer.getExtremeElevationMin();
           
            if (min != null && (extremeMin == null || min.compareTo(min) > 0))
                extremeMin = min;

            Double max = layer.getExtremeElevationMax();
           
            if (max != null && (extremeMax == null || max.compareTo(max) > 0))
                extremeMax = max;
        }

        if (extremeMin != null || extremeMax != null)
        {
            Double[] extremes = new Double[] {null, null};

            if (extremeMin != null)
                extremes[0] = extremeMin;
           
            if (extremeMax != null)
                extremes[1] = extremeMax;

            return extremes;
        }

        return null;
    }
  
   public Long getLayerLatestLastUpdateTime(GfrWMSCapabilities caps, String[] layerNames)
    {
        if (caps == null)
        {
            String message = Logging.getMessage("nullValue.WMSCapabilities");
            Logging.logger().severe(message);
            throw new IllegalArgumentException(message);
        }

        if (layerNames == null)
        {
            String message = Logging.getMessage("nullValue.WMSLayerNames");
            Logging.logger().severe(message);
            throw new IllegalArgumentException(message);
        }

        String lastUpdate = null;

        for (String name : layerNames)
        {
            WMSLayerCapabilities layer = this.getLayerByName(name);
            if (layer == null)
                continue;

            String update = layer.getLastUpdate();
            if (update != null && update.length() > 0 && (lastUpdate == null || update.compareTo(lastUpdate) > 0))
                lastUpdate = update;
        }

        if (lastUpdate != null)
        {
            try
            {
                return Long.parseLong(lastUpdate);
            }
            catch (NumberFormatException e)
            {
                String message = Logging.getMessage("generic.ConversionError", lastUpdate);
                Logging.logger().warning(message);
            }
        }

        return null;
    }
  
   public WMSLayerCapabilities getLayerByName(String name)
    {
        if (WWUtil.isEmpty(name))
            return null;

        List<WMSLayerCapabilities> namedLayers = this.getNamedLayers();
       
        for (WMSLayerCapabilities layer : namedLayers)
        {
            if (layer.getName().equals(name))
                return layer;
        }

        return null;
    }
  
   // beg added bantchao (recursive)
  
   public List<WMSLayerCapabilities> getAllLayersFolders()
    {
        if (this.getCapabilityInformation() == null || this.getCapabilityInformation().getLayerCapabilities() == null)
            return null;

        List<WMSLayerCapabilities> lstCapLyrsAll = new ArrayList<WMSLayerCapabilities>();

        for (WMSLayerCapabilities capLayerCur : this.getCapabilityInformation().getLayerCapabilities())
        {
           lstCapLyrsAll.addAll(capLayerCur.getAllLayersFolders());
        }

        return lstCapLyrsAll;
    }
   
    public List<WMSLayerCapabilities> getAllLayers()
    {
        if (this.getCapabilityInformation() == null || this.getCapabilityInformation().getLayerCapabilities() == null)
            return null;

        List<WMSLayerCapabilities> lstCapLyrsAll = new ArrayList<WMSLayerCapabilities>();

        for (WMSLayerCapabilities capLayerCur : this.getCapabilityInformation().getLayerCapabilities())
        {
           lstCapLyrsAll.add(capLayerCur);
            lstCapLyrsAll.addAll(capLayerCur.getAllLayers());
        }

        return lstCapLyrsAll;
    }
   
    // end added bantchao
  
   public List<WMSLayerCapabilities> getNamedLayersLeaves()
   {
      if (this.getCapabilityInformation() == null || this.getCapabilityInformation().getLayerCapabilities() == null)
            return null;
     
      List<WMSLayerCapabilities> lstCapLyrsLeaf = new ArrayList<WMSLayerCapabilities>();
     
      for (WMSLayerCapabilities layer : this.getCapabilityInformation().getLayerCapabilities())
      {
         lstCapLyrsLeaf.addAll(layer.getNamedLayersLeaves());
      }
     
      return lstCapLyrsLeaf;
   }
  
   public List<WMSLayerCapabilities> getNamedLayers()
    {
        if (this.getCapabilityInformation() == null || this.getCapabilityInformation().getLayerCapabilities() == null)
            return null;

        List<WMSLayerCapabilities> namedLayers = new ArrayList<WMSLayerCapabilities>();

        for (WMSLayerCapabilities layer : this.getCapabilityInformation().getLayerCapabilities())
        {
            namedLayers.addAll(layer.getNamedLayers());
        }

        return namedLayers;
    }
  
   @Override
   public WMSCapabilityInformation getCapabilityInformation()
    {
        return (WMSCapabilityInformation) super.getCapabilityInformation();
    }
  
   public GfrWMSCapabilities(CapabilitiesRequest docSource) throws
           IOException,
           XMLStreamException,
           IllegalArgumentException,
           URISyntaxException
    {
        super(OGCConstants.WMS_NAMESPACE_URI, docSource.getUri().toURL());

        this._initialize_();
    }
  
   @Override
   public boolean isRootElementName(QName candidate)
    {
        return this.getParserContext().isSameName(candidate, ROOT_ELEMENT_NAME_1_1_1)
            || this.getParserContext().isSameName(candidate, ROOT_ELEMENT_NAME_1_3_0);
    }
  
   @Override
   public XMLEventParser allocate(XMLEventParserContext ctx, XMLEvent event)
    {
        if (ctx.isStartElement(event, CAPABILITY))
            return ctx.allocate(event, new WMSCapabilityInformation(this.getNamespaceURI()));
        else
            return super.allocate(ctx, event);
    }
  
   @Override
    public GfrWMSCapabilities parse(Object... args) throws XMLStreamException
    {
        return (GfrWMSCapabilities) super.parse(args);
    }
  
    @Override
    public String toString() // TODO: Complete this method
    {
        StringBuilder sb = new StringBuilder(super.toString());

        sb.append("LAYERS\n");

        for (WMSLayerCapabilities layerCaps : this.getNamedLayers())
        {
            sb.append(layerCaps.toString()).append("\n");
        }

        return sb.toString();
    }

    private void _initialize_()
    {
        this.getParserContext().registerParser(new QName(this.getDefaultNamespaceURI(), "Service"),
            new WMSServiceInformation(this.getNamespaceURI()));
       
        this.getParserContext().registerParser(new QName("Capability"),
            new WMSCapabilityInformation(this.getNamespaceURI()));
    }
}
TOP

Related Classes of org.geoforge.worldwindogc.capabilities.GfrWMSCapabilities

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.