📄 agxexporter.java
字号:
package com.esri.solutions.jitk.common.export.agx;
import com.esri.adf.web.ags.data.AGSLocalMapResource;
import com.esri.adf.web.ags.data.AGSMapResource;
import com.esri.adf.web.aims.data.AIMSMapResource;
import com.esri.adf.web.data.GISResource;
import com.esri.adf.web.data.TocNode;
import com.esri.adf.web.data.WebContext;
import com.esri.adf.web.data.WebToc;
import com.esri.adf.web.data.geometry.WebExtent;
import com.esri.adf.web.wms.data.WMSMapResource;
import org.apache.log4j.Logger;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Iterator;
import java.util.Map;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
/**
* This class will handle extracting map resources from the {@link WebContext}
* object and put them into an ArcGIS Explorer document (.nmf).
*/
public class AGXExporter {
/**
* {@link Logger} used to log messages for this class.
*/
private static final Logger _logger = Logger.getLogger(AGXExporter.class);
/**
* Reference to the ADF {@link WebContext} object.
*/
private WebContext m_context = null;
/**
* Constructor that initializes the object with the ADF's
* {@link WebContext} reference.
*
* @param context Reference to the ADF {@link WebContext} object.
* @throws NullPointerException Thrown if the <code>context</code> argument is <code>null</code>.
*/
public AGXExporter(WebContext context) {
if (context == null) {
throw new NullPointerException();
}
m_context = context;
}
/**
* Retrieves all map resources from the {@link WebContext} via
* {@link WebContext#getResources()} and will then create an ArcGIS
* Explorer document that can be returned to the client.
*
* @return {@link String} representation of the xml ArcGIS Explorer document.
*/
public String export() {
Map<String, GISResource> resources = null;
GISResource resource = null;
Iterator<String> iter = null;
Object test = null;
StringBuilder sb = null;
StreamResult streamResult = null;
StringReader reader = null;
StringWriter writer = null;
WebExtent extent = null;
double xmin = 0D;
double ymin = 0D;
double xmax = 0D;
double ymax = 0D;
resources = m_context.getResources();
iter = resources.keySet().iterator();
if (iter.hasNext()) {
sb = new StringBuilder();
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
sb.append("<export>");
while (iter.hasNext()) {
test = resources.get(iter.next());
if (test instanceof GISResource &&
!(test instanceof AGSLocalMapResource)) {
resource = (GISResource) test;
if (resource instanceof WMSMapResource) {
sb.append(generateXmlForWMS((WMSMapResource) resource));
} else if (resource instanceof AIMSMapResource) {
sb.append(generateXmlForAIMS((AIMSMapResource) resource));
} else if (resource instanceof AGSMapResource) {
sb.append(generateXmlForAGSMapResource(
(AGSMapResource) resource));
}
}
}
extent = m_context.getWebMap().getCurrentExtent();
// get the x/y min/max extent values
xmin = extent.getMinX();
ymin = extent.getMinY();
xmax = extent.getMaxX();
ymax = extent.getMaxY();
sb.append("<extent>");
sb.append("<xmin>").append(xmin).append("</xmin>");
sb.append("<ymin>").append(ymin).append("</ymin>");
sb.append("<xmax>").append(xmax).append("</xmax>");
sb.append("<ymax>").append(ymax).append("</ymax>");
sb.append("</extent>");
sb.append("</export>");
reader = new StringReader(sb.toString());
writer = new StringWriter();
try {
Transformer transformer = TransformerFactory.newInstance()
.newTransformer(new StreamSource(
getClass().getClassLoader()
.getResourceAsStream("xsl/viewWith_nmf.xsl")));
streamResult = new StreamResult(writer);
transformer.transform(new StreamSource(reader), streamResult);
return streamResult.getWriter().toString();
} catch (TransformerConfigurationException ex) {
_logger.warn("TransformerConfigurationException occurred trying to create new Transformer.",
ex);
return null;
} catch (TransformerException ex) {
_logger.warn("TransformerException occurred while transformer AGX xml.",
ex);
return null;
}
} else {
return null;
}
}
/**
* Takes the given {@link AIMSMapResource} and puts the essential
* properties into xml format that is an ArcGIS Explorer 'mapservice'
* element which can then be added to the AGX document.
*
* @param resource Reference to the {@link AIMSMapResource} to be exported.
* @return {@link String} representation of the <code>resource</code> in
* ArcGIS Explorer document format.
*/
private String generateXmlForAIMS(AIMSMapResource resource) {
StringBuilder sb = null;
sb = new StringBuilder();
sb.append("<mapservice>");
sb.append("<name>").append(resource.getAlias()).append("</name>");
sb.append("<server>").append(resource.getHostName()).append("</server>");
sb.append("<service>").append(resource.getServiceName())
.append("</service>");
sb.append("<visible>").append(getResourceVisibility(resource))
.append("</visible>");
sb.append("<nmftype>").append("0").append("</nmftype>");
sb.append("</mapservice>");
return sb.toString();
}
/**
* Takes the given {@link WMSMapResource} and puts the essential
* properties into xml format that is an ArcGIS Explorer 'mapservice'
* element which can then be added to the AGX document.
*
* @param resource Reference to the {@link WMSMapResource} to be exported.
* @return {@link String} representation of the <code>resource</code> in
* ArcGIS Explorer document format.
*/
private String generateXmlForWMS(WMSMapResource resource) {
StringBuilder sb = null;
String wmsUrl = null;
wmsUrl = resource.getWmsURL();
// check that the last character in the url is a '?', if it's not
// AGX will choke and not add the WMS service
wmsUrl = (wmsUrl.charAt(wmsUrl.length() - 1) == '?') ? wmsUrl
: wmsUrl.concat(
"?");
sb = new StringBuilder();
sb.append("<mapservice>");
sb.append("<name>").append(resource.getAlias()).append("</name>");
sb.append("<server>").append(wmsUrl).append("</server>");
sb.append("<service></service>");
sb.append("<visible>").append(getResourceVisibility(resource))
.append("</visible>");
sb.append("<nmftype>6</nmftype>");
sb.append("</mapservice>");
return sb.toString();
}
/**
* Takes the given {@link AGSMapResource} and puts the essential
* properties into xml format that is an ArcGIS Explorer 'mapservice'
* element which can then be added to the AGX document.
*
* @param resource Reference to the {@link AGSMapResource} to be exported.
* @return {@link String} representation of the <code>resource</code> in
* ArcGIS Explorer document format.
*/
private String generateXmlForAGSMapResource(AGSMapResource resource) {
StringBuilder sb = null;
String url = null;
String serviceName = null;
url = resource.getEndPointURL();
serviceName = url.substring(0, url.indexOf("/MapServer"));
url = url.substring(0, serviceName.lastIndexOf("/"));
serviceName = serviceName.substring(serviceName.lastIndexOf("/") + 1);
sb = new StringBuilder();
sb.append("<mapservice>");
sb.append("<name>").append(resource.getAlias()).append("</name>");
sb.append("<server>").append(url).append("</server>");
sb.append("<service>").append(serviceName).append("</service>");
sb.append("<visible>").append(getResourceVisibility(resource))
.append("</visible>");
sb.append("<nmftype>2</nmftype>");
sb.append("</mapservice>");
return sb.toString();
}
/**
* Gets the visibility state of the given {@link GISResource}
* from the {@link WebToc}.
*
* @param resource The {@link GISResource} in question.
* @return true if <code>resource</code> is checked in the {@link WebToc},
* false otherwise.
*/
private boolean getResourceVisibility(GISResource resource) {
WebToc toc = null;
TocNode node = null;
Iterator<TocNode> iter = null;
String name = null;
boolean visible = Boolean.TRUE;
name = resource.getAlias();
toc = m_context.getWebToc();
iter = toc.getRootNodes().iterator();
while (iter.hasNext()) {
node = (TocNode) iter.next();
if (node.getContent().getText().equals(name)) {
visible = node.getContent().isChecked();
break;
}
}
return visible;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -