📄 catalogexchangequery.java
字号:
/* * Created on 27-nov-2005 * *//** * @author magowiz * */package catalog_exchange;import java.io.InputStream;import java.io.StringWriter;import java.net.URL;import java.util.Date;import java.util.Enumeration;import net.jxta.document.Document;import net.jxta.document.Element;import net.jxta.document.MimeMediaType;import net.jxta.document.StructuredDocument;import net.jxta.document.StructuredDocumentFactory;import net.jxta.document.StructuredTextDocument;import net.jxta.document.TextElement;/** * Object that implements a Catalog Exchange Query, a XML JXTA Message that * will be sent by a peer who wants to download from another a catalog. * */public class CatalogExchangeQuery { private Date dateLastUpdate; /**The date of the last update of the owned catalog, if present*/ private URL catalogUrl;/**The url from which the catalog can be downloaded*/ private String catalogName; /**The name of the catalog, identifies the catalog*/ /** * Returns the last update date of the catalog if the querying peer * has got one. */ public long getDateUpdate() { long n=-1; if (dateLastUpdate!=null) n=dateLastUpdate.getTime(); return n; } /** * Returns the url of the catalog. */ public URL getCatalogUrl() { return catalogUrl; } /** * Catalog Name getter * * @return Returns the name of the catalog. */ public String getCatalogName() { return catalogName; } /** * Empty constructor, doesn't do anything. * @deprecated */ public CatalogExchangeQuery() { } /** * Constructor needed if a peer has got the catalog and wants to check * if any other peer has got a newer version. * @param cn the name of the catalog * @param curl the url which the catalog can be downloaded from * @param du the date of the last update of local catalog, in long format(milliseconds) */ public CatalogExchangeQuery(String cn,URL curl,long du) { catalogName=cn; dateLastUpdate=new Date(du); catalogUrl=curl; } /** * Constructor needed if a peer hasn't got the catalog and wants to download * it from another peer. * @param cn the name of the catalog * @param curl the url which the catalog can be downloaded from */ public CatalogExchangeQuery(String cn,URL curl) { catalogName=cn; catalogUrl=curl; dateLastUpdate=null; } /** * Method that creates and return the xml document which represents * the query. * @param asMimeType MimeType of the output document. * @return The query in xml document format. */ public Document getDocument(MimeMediaType asMimeType) { StructuredDocument document = (StructuredTextDocument) StructuredDocumentFactory.newStructuredDocument( asMimeType, "example:ExampleQuery"); Element element; element = document.createElement("catalogName",catalogName); document.appendChild(element); element=document.createElement("catalogUrl",catalogUrl.toString()); document.appendChild(element); if(dateLastUpdate!=null) {element=document.createElement("dateLastUpdate",String.valueOf(dateLastUpdate.getTime())); document.appendChild(element);} return document; } /** * Constructor needed for the peer who receive the query to easily extract * info from query. * @param stream is the query xml document received */ public CatalogExchangeQuery (InputStream stream) { StructuredTextDocument document=null; try{ document = (StructuredTextDocument) StructuredDocumentFactory.newStructuredDocument( new MimeMediaType("text/xml"), stream); } catch(Exception e){e.printStackTrace();} Enumeration elements = document.getChildren(); elements = document.getChildren(); while (elements.hasMoreElements()) { Element el = (Element) elements.nextElement(); TextElement element = (TextElement) el; if(element.getName().equals("catalogName")) { catalogName = element.getTextValue(); continue; } if(element.getName().equals("catalogUrl")) { try{catalogUrl = new URL (element.getTextValue());} catch (Exception e){e.printStackTrace();} continue; } if(element.getName().equals("dateLastUpdate")) { long t=Long.valueOf(element.getTextValue()).longValue(); dateLastUpdate = new Date(t); continue; } } } /** * Method that returns the xml query document as String. * @return String rapresentation of the query. */ public String toString() { try { StringWriter out = new StringWriter(); StructuredTextDocument doc = (StructuredTextDocument) getDocument( new MimeMediaType("text/xml")); doc.sendToWriter(out); return out.toString(); } catch (Exception e) { e.printStackTrace(); return ""; } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -