📄 mapservicemetadataexporter.java
字号:
package com.esri.solutions.jitk.common.metadata;
import com.esri.solutions.jitk.common.enums.ServiceType;
import com.esri.solutions.jitk.datasources.ogc.csw.CswRecord;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Map;
import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
/**
* Extends the {@link AbstractMapServiceMetadataExporter} abstract class
* so that map service information can be parsed/exported.
*/
public class MapServiceMetadataExporter
extends AbstractMapServiceMetadataExporter {
/**
* {@link Logger} used to log messages for this class.
*/
private static final Logger _logger = Logger.getLogger(MapServiceMetadataExporter.class);
/**
* Key that can be used to configure the XPATH value for parsing the
* map service Url.
*/
private static final String SERVICE_URL_XPATH_KEY = "serviceUrl";
/**
* Key that can be used to configure the XPATH value for parsing the
* map service name.
*/
private static final String SERVICE_NAME_XPATH_KEY = "serviceName";
/**
* Key that can be used to configure the XPATH value for parsing the
* map service type.
*/
private static final String SERVICE_TYPE_XPATH_KEY = "serviceType";
/**
* {@link String} ID of the exporter.
*/
private String _exporterID = null;
/**
* Reference to the {@link CswRecord} containing the metadata
* document.
*/
private CswRecord _record = null;
/**
* {@link MetadataExporterState} state of the exporter.
*/
private MetadataExporterState _state = MetadataExporterState.NEEDS_RESET;
/**
* A {@link Map} of service type mappings for customized CS/W metadata
* documents.
*/
private Map<String, String> _serviceTypeMappings = null;
/**
* A {@link Map} of XPATH values for customized CS/W metadata
* documents.
*/
private Map<String, String> _xpathMappings = null;
/**
* {@link NamespaceContext} used by the XPath-processing DOM parser to
* identify XML Namespaces embedded in the metadata document. This value
* must be set properly or else the parser will not function correctly.
*/
private NamespaceContext _namespaceContext = null;
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.common.metadata.AbstractMapServiceMetadataExporter#begin()
*/
public void begin() {
if (_state != MetadataExporterState.NEEDS_RESET) {
throw new IllegalStateException(
"Exporter is not ready to begin exporting.");
}
_serviceUrl = null;
_serviceName = null;
_serviceType = null;
_state = MetadataExporterState.READY_TO_EXPORT;
}
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.common.metadata.AbstractMapServiceMetadataExporter#end()
*/
public void end() {
if (_state != MetadataExporterState.EXPORT_FINISHED) {
throw new IllegalStateException(
"Exporter is not finished exporting; end() cannot be called.");
}
// Intentionally does nothing else. There is no cleanup required.
}
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.common.metadata.AbstractMapServiceMetadataExporter#export()
*/
public void export() {
InputSource inSource = null;
XPathFactory xpathFactory = null;
XPath xpath = null;
XPathExpression expr = null;
DocumentBuilderFactory factory = null;
DocumentBuilder builder = null;
Document doc = null;
String serviceType = null;
String serviceUrlXpath = null;
String serviceNameXpath = null;
String serviceTypeXpath = null;
if (_state != MetadataExporterState.READY_TO_EXPORT) {
throw new IllegalStateException(
"Exporter is not yet ready to export.");
}
_state = MetadataExporterState.EXPORTING;
serviceUrlXpath = _xpathMappings.get(SERVICE_URL_XPATH_KEY);
serviceNameXpath = _xpathMappings.get(SERVICE_NAME_XPATH_KEY);
serviceTypeXpath = _xpathMappings.get(SERVICE_TYPE_XPATH_KEY);
try {
inSource = new InputSource(new ByteArrayInputStream(
_record.getFullMetadata().getBytes()));
factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(Boolean.TRUE);
builder = factory.newDocumentBuilder();
doc = builder.parse(inSource);
xpathFactory = XPathFactory.newInstance();
xpath = xpathFactory.newXPath();
if (getNamespaceContext() != null) {
xpath.setNamespaceContext(getNamespaceContext());
}
if (serviceUrlXpath != null) {
expr = xpath.compile(serviceUrlXpath);
_serviceUrl = (String) expr.evaluate(doc, XPathConstants.STRING);
}
if (serviceNameXpath != null) {
expr = xpath.compile(serviceNameXpath);
_serviceName = (String) expr.evaluate(doc, XPathConstants.STRING);
}
if (serviceTypeXpath != null) {
expr = xpath.compile(serviceTypeXpath);
serviceType = (String) expr.evaluate(doc, XPathConstants.STRING);
_serviceType = getServiceTypeMapping(serviceType);
}
} catch (XPathExpressionException ex) {
_logger.warn("XPathExpressionException occurred exporting service information.",
ex);
} catch (IOException ex) {
_logger.warn("IOException occurred exporting service information.",
ex);
} catch (ParserConfigurationException ex) {
_logger.warn("ParserConfigurationException occurred exporting service information.",
ex);
} catch (SAXException ex) {
_logger.warn("SAXException occurred exporting service information.",
ex);
}
_state = MetadataExporterState.EXPORT_FINISHED;
}
/**
* Retrieves the {@link NamespaceContext} that is used when the XPath engine
* parses the metadata to extract the Service connection information.
* <p>
* The {@link NamespaceContext} used by the XPath-processing DOM parser to
* identify XML Namespaces that are used in the metadata document. This
* value must be set to a properly-configured in order for the parser to
* function correctly.
* </p>
*
* @return The {@link NamespaceContext} that is being used, if any, when
* parsing the metadata.
*/
public NamespaceContext getNamespaceContext() {
return _namespaceContext;
}
/**
* Sets the {@link NamespaceContext} that is used when the XPath engine
* parses the metadata to extract the Service information information.
* <p>
* The {@link NamespaceContext} used by the XPath-processing DOM parser to
* identify XML Namespaces that are used in the metadata document. This
* value must be set to a properly-configured in order for the parser to
* function correctly.
* </p>
*
* @param namespaceContext
* The {@link NamespaceContext} to use; cannot be {@code null}.
*/
public void setNamespaceContext(NamespaceContext namespaceContext) {
if (namespaceContext == null) {
throw new NullPointerException("namespaceContext cannot be null.");
}
_namespaceContext = namespaceContext;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -