/*
* 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.OGCCapabilityInformation;
import gov.nasa.worldwind.ogc.OGCServiceInformation;
import gov.nasa.worldwind.util.Logging;
import gov.nasa.worldwind.util.xml.AbstractXMLEventParser;
import gov.nasa.worldwind.util.xml.BasicXMLEventParserContext;
import gov.nasa.worldwind.util.xml.XMLEventParser;
import gov.nasa.worldwind.util.xml.XMLEventParserContext;
import java.io.IOException;
import javax.xml.stream.events.Attribute;
import java.util.Iterator;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.XMLEvent;
import org.geoforge.worldwind.util.GfrWWXML;
/**
*
* @author bantchao
*/
abstract public class GfrOGCCapabilities extends AbstractXMLEventParser
{
abstract public String getDefaultNamespaceURI();
abstract public boolean isRootElementName(QName name); // implement to test name of root element
// Element names, constructed when the namespaceURI is known
protected QName SERVICE;
protected QName CAPABILITY;
protected QName VERSION;
protected QName UPDATE_SEQUENCE;
protected String version;
protected String updateSequence;
protected OGCServiceInformation serviceInformation;
protected OGCCapabilityInformation capabilityInformation;
protected XMLEventReader eventReader;
protected XMLEventParserContext parserContext;
public String getVersion()
{
return version;
}
protected XMLEventParserContext getParserContext()
{
return this.parserContext;
}
public OGCCapabilityInformation getCapabilityInformation()
{
return capabilityInformation;
}
public OGCServiceInformation getServiceInformation()
{
return serviceInformation;
}
public String getUpdateSequence()
{
return updateSequence;
}
protected void setServiceInformation(OGCServiceInformation serviceInformation)
{
this.serviceInformation = serviceInformation;
}
protected void setCapabilityInformation(OGCCapabilityInformation capabilityInformation)
{
this.capabilityInformation = capabilityInformation;
}
protected void setVersion(String version)
{
this.version = version;
}
protected void setUpdateSequence(String updateSequence)
{
this.updateSequence = updateSequence;
}
protected GfrOGCCapabilities(String namespaceURI, Object docSource) throws
IOException,
XMLStreamException,
IllegalArgumentException
{
super(namespaceURI);
this.eventReader = this.createReader(docSource);
this.initialize();
}
public GfrOGCCapabilities parse(Object... args) throws XMLStreamException
{
XMLEventParserContext ctx = this.parserContext;
for (XMLEvent event = ctx.nextEvent(); ctx.hasNext(); event = ctx.nextEvent())
{
if (event == null)
continue;
if (event.isStartElement() && this.isRootElementName(event.asStartElement().getName()))
{
super.parse(ctx, event, args);
return this;
}
}
return null;
}
protected XMLEventReader createReader(Object docSource) throws
IOException,
XMLStreamException,
IllegalArgumentException
{
return GfrWWXML.openEventReader(docSource);
}
@Override
protected void doParseEventContent(XMLEventParserContext ctx, XMLEvent event, Object... args)
throws XMLStreamException
{
if (ctx.isStartElement(event, SERVICE))
{
XMLEventParser parser = this.allocate(ctx, event);
if (parser != null)
{
Object o = parser.parse(ctx, event, args);
if (o != null && o instanceof OGCServiceInformation)
this.setServiceInformation((OGCServiceInformation) o);
}
}
else if (ctx.isStartElement(event, CAPABILITY))
{
XMLEventParser parser = this.allocate(ctx, event);
if (parser != null)
{
Object o = parser.parse(ctx, event, args);
if (o != null && o instanceof OGCCapabilityInformation)
this.setCapabilityInformation((OGCCapabilityInformation) o);
}
}
}
@Override
protected void doParseEventAttributes(XMLEventParserContext ctx, XMLEvent event, Object... args)
{
Iterator iter = event.asStartElement().getAttributes();
if (iter == null)
return;
while (iter.hasNext())
{
Attribute attr = (Attribute) iter.next();
if (ctx.isSameAttributeName(attr.getName(), VERSION))
this.setVersion(attr.getValue());
else if (ctx.isSameAttributeName(attr.getName(), UPDATE_SEQUENCE))
this.setUpdateSequence(attr.getValue());
}
}
protected XMLEventParserContext createParserContext(XMLEventReader reader)
{
this.parserContext = new BasicXMLEventParserContext(reader);
this.parserContext.setDefaultNamespaceURI(this.getDefaultNamespaceURI());
return this.parserContext;
}
@Override
public XMLEventParser allocate(XMLEventParserContext ctx, XMLEvent event)
{
if (ctx == null)
{
String message = Logging.getMessage("nullValue.ParserContextIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
XMLEventParser defaultParser = null;
if (ctx.isStartElement(event, SERVICE))
defaultParser = new OGCServiceInformation(this.getNamespaceURI());
return ctx.allocate(event, defaultParser);
}
@Override
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append("Version: ").
append(this.getVersion() != null ? this.getVersion() : "none").append("\n");
sb.append("UpdateSequence: ").
append(this.getUpdateSequence() != null ? this.getUpdateSequence() : "none");
sb.append("\n");
sb.append(this.getServiceInformation() != null ? this.getServiceInformation() : "Service Information: none");
sb.append("\n");
sb.append(this.getCapabilityInformation() != null
? this.getCapabilityInformation() : "Capability Information: none");
sb.append("\n");
return sb.toString();
}
private void initialize()
{
this.parserContext = this.createParserContext(this.eventReader);
SERVICE = new QName(this.getNamespaceURI(), "Service");
CAPABILITY = new QName(this.getNamespaceURI(), "Capability");
VERSION = new QName(this.getNamespaceURI(), "version");
UPDATE_SEQUENCE = new QName(this.getNamespaceURI(), "updateSequence");
this.getParserContext().registerParser(SERVICE, new OGCServiceInformation(this.getNamespaceURI()));
// Capability parser is registered by subclass.
}
}