⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 feed.java

📁 esri的ArcGIS Server超级学习模板程序(for java)
💻 JAVA
字号:
/**
 * 
 */
package com.esri.solutions.jitk.web.tasks.feed;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.esri.adf.web.data.GraphicElement;
import com.esri.adf.web.data.WebContext;
import com.esri.adf.web.data.WebContextInitialize;
import com.esri.adf.web.data.WebContextObserver;
import com.esri.adf.web.data.geometry.WebExtent;
import com.esri.adf.web.data.geometry.WebGeometry;
import com.esri.adf.web.data.geometry.WebPoint;
import com.esri.adf.web.data.geometry.WebSpatialReference;
import com.esri.adf.web.data.symbol.WebSymbol;
import com.sun.syndication.feed.module.georss.GeoRSSModule;
import com.sun.syndication.feed.module.georss.GeoRSSUtils;
import com.sun.syndication.feed.module.georss.geometries.AbstractGeometry;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;

/**
 * @author vlad2928
 * 
 */
public class Feed implements WebContextInitialize, WebContextObserver {

	private static final Logger log = LogManager.getLogger(Feed.class);
	private WebContext context = null;
	private String name = null;
	private String url = null;
	private SyndFeed feed = null;
	private boolean visible = true;
	private WebSymbol symbol = null; 
	
	private Map<Integer,GraphicElement> graphics = new HashMap<Integer, GraphicElement>();

	public Feed() {}

	public Feed(String url, WebContext context) {
		setUrl(url);
		setContext(context);
	}

	public void setContext(WebContext context) {
		init(context);
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = (url != null && url.length() > 0) ? url : "";
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = (this.name == null && name != null && name.length() > 0) ? name : "";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.esri.adf.web.data.WebContextInitialize#init(com.esri.adf.web.data.WebContext)
	 */
	public void init(WebContext context) {
		this.context = context;
		this.context.addObserver(this);
		log.debug("WebContextObserver added in class Feed");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.esri.adf.web.data.WebContextInitialize#destroy()
	 */
	@SuppressWarnings("unchecked")
	public void destroy() {
		if(isPopulated())
			for(SyndEntry entry : (List<SyndEntry>) feed.getEntries()) {
				removeGraphicElement(entry);
			}
		feed = null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.esri.adf.web.data.WebContextObserver#update(com.esri.adf.web.data.WebContext,
	 *      java.lang.Object)
	 */
	public void update(WebContext context, Object arg) {
		if (this.context == null) init(context);
		refreshWebGraphics();
	}

	private GraphicElement newGraphicElement(SyndEntry entry, boolean isSelected) {
		GraphicElement graphicElement = null;
		
		GeoRSSModule module = GeoRSSUtils.getGeoRSS(entry);
		if (module != null) {
			AbstractGeometry geometry = module.getGeometry();	
			
			if (geometry instanceof com.sun.syndication.feed.module.georss.geometries.Point) {
				graphicElement = new GraphicElement();
				graphicElement.setGeometry(toWebGeometry(geometry));
			
				if(isSelected)
					graphicElement.setSymbol(GeorssTaskUtil.selectedWebPictureMarkerSymbol);
				else {
					if(this.symbol == null)
						symbol = GeorssTaskUtil.newWebPictureMarkerSymbol();
					
					graphicElement.setSymbol(symbol);
				}
			}
		}
		return graphicElement;
	}
	
	@SuppressWarnings("unchecked")
	public void read() throws FeedException, IOException {
		
		if(isPopulated()) return;
		
		this.graphics.clear();
		feed = new SyndFeedInput().build(new XmlReader(new URL(url).openStream()));
		setName(feed.getTitle());

		for (SyndEntry entry : (List<SyndEntry>) feed.getEntries()) {
			GraphicElement element = newGraphicElement(entry, false);
			if(element != null)
				this.graphics.put(new Integer(entry.hashCode()), element);	
		}
		
		refreshWebGraphics();
	}
	
	public WebGeometry toWebGeometry(AbstractGeometry feedGeometry) {
		WebGeometry webGeometry = null;
		if (feedGeometry instanceof com.sun.syndication.feed.module.georss.geometries.Point) {
			com.sun.syndication.feed.module.georss.geometries.Point pt = (com.sun.syndication.feed.module.georss.geometries.Point) feedGeometry;
			webGeometry = new WebPoint(pt.getPosition().getLongitude(), pt.getPosition().getLatitude());
			webGeometry.setSpatialReference(WebSpatialReference.getWebSpatialReference(4326));
		}
		return webGeometry;
	}
	
	public WebExtent toWebExtent(AbstractGeometry feedGeometry) {
		WebExtent webExtent = null;
		if (feedGeometry instanceof com.sun.syndication.feed.module.georss.geometries.Point) {
			com.sun.syndication.feed.module.georss.geometries.Point pt = (com.sun.syndication.feed.module.georss.geometries.Point) feedGeometry;
			webExtent = new WebExtent(pt.getPosition().getLongitude(), pt.getPosition().getLatitude(), pt.getPosition().getLongitude(), pt.getPosition().getLatitude(), WebSpatialReference.getWebSpatialReference(4326));
		}
		return webExtent;
	}
	
	@SuppressWarnings("unchecked")
	public void refreshWebGraphics() {
		if(isPopulated())
			for(SyndEntry entry : (List<SyndEntry>) feed.getEntries()) {
				GraphicElement element = this.graphics.get(new Integer(entry.hashCode()));
				if (element != null) {
					if(this.context.getWebGraphics().isGraphicsExists(element)) {
						if(!isVisible())
							this.context.getWebGraphics().removeGraphics(element); 
					} else this.context.getWebGraphics().addGraphics(element);
				}
			}
	}
	
	/*
	public void clearAllGraphicElements() {
		if(isPopulated())
			for(SyndEntry entry : (List<SyndEntry>) feed.getEntries()) {
				GraphicElement element = this.graphics.get(new Integer(entry.hashCode()));
				if (element != null && this.context.getWebGraphics().isGraphicsExists(element))
					this.context.getWebGraphics().removeGraphics(element);
			}
	}
	*/
	
	/*
	public void addAllGraphicElements() {
		if(isPopulated())
			for(SyndEntry entry : (List<SyndEntry>) feed.getEntries()) {
				GraphicElement element = this.graphics.get(new Integer(entry.hashCode()));
				if(element != null && !this.context.getWebGraphics().isGraphicsExists(element))
					this.context.getWebGraphics().addGraphics(element);
			}
	}
	*/
	
	public boolean isVisible() {
		return visible;
	}

	public void setVisible(boolean visible) {
		this.visible = visible;
	}

	public boolean isPopulated() {
		return (feed != null);
	}
	
	private void removeGraphicElement(SyndEntry entry) {
		GraphicElement element = this.graphics.get(new Integer(entry.hashCode()));
		
		if (element != null) {
			if(this.context.getWebGraphics().isGraphicsExists(element))
				this.context.getWebGraphics().removeGraphics(element);
			
			this.graphics.remove(new Integer(entry.hashCode()));
		}
	}
	
	private Map<String, String> getEntryDetails(SyndEntry entry) {
		Map<String, String> details = new HashMap<String, String>();
		details.put("Summary", entry.getDescription().getValue());
		details.put("Entry", "<a href=\"" + entry.getLink() + "\" target=\"_blank\">" + entry.getTitle() + "</a>");
		return details;
	}
	
	@SuppressWarnings("unchecked")
	public List<GeorssQueryResult> identifyEntries(WebExtent extent) {
		if(extent == null) return null;
		List<GeorssQueryResult> entries = new ArrayList<GeorssQueryResult>();
		if(isPopulated()) {
			for(SyndEntry entry : (List<SyndEntry>) feed.getEntries()) {
				GraphicElement element = this.graphics.get(new Integer(entry.hashCode()));
				if(element != null) {
					WebGeometry geometry = element.getGeometry();
					if(geometry instanceof WebPoint) {
						WebPoint point = (WebPoint) geometry;
						
						if(extent.contains(point))
							entries.add(new GeorssQueryResult(entry.getTitle(), getEntryDetails(entry), this.context, point));
					}	
				}
			}
			
			refreshWebGraphics();
		}
		return entries;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -