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

📄 catalogexchangeresponse.java

📁 基于jxta的文件共享和聊天系统源代码,下载下来分析吧
💻 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;import org.apache.xerces.dom.DocumentImpl;import org.w3c.dom.NodeList;/** * Object that implements a Catalog Exchange Response, a XML JXTA Message that * will be sent by a peer to reply to a catalog exchange query.  * */public class CatalogExchangeResponse {	private org.w3c.dom.Document catalog;/**The catalog which is transported by response*/	private Date dateDownload;/**The download date of the catalog*/	private Date dateLastUpdate;/**The date of last update of the catalog*/	private URL catalogUrl;/**The catalog URL*/	private String catalogName;	/**catalog name, identifies the catalog*/	private String got;/**status of the response, it tells if the peer has got 	the catalog and if it's older or newer*/			/**	 * Returns the catalog transported by the response 	 * 	 * */		public org.w3c.dom.Document  getCatalog()	{	 return catalog;	}				/**	 * Returns the download date of the transported catalog 	 * 	 * */	public long getDateDownload()	{	 return dateDownload.getTime(); 		}		/**	 * Returns the last update date of the transported catalog 	 * 	 * */	public long getDateLastUpdate()	{	 return dateLastUpdate.getTime(); 		}		/**	 * Returns the URL of the transported catalog 	 * 	 * */	public URL getCatalogUrl()	{	 return catalogUrl;	}		/**	 * Returns the name of the transported catalog 	 * 	 * */	public String getCatalogName()	{	 return catalogName;	}			/**	 * Returns the got status of the transported catalog 	 * can be false, newer, older,equal,true	 * 	 * */	public String getGot()	{	 return got;		}			/**	 * Used in case which the peer send the catalog (newer,true)	 * @param g got value, represents the status of the responsed catalog	 * @param cn name of the catalog to send	 * @param curl Url of the catalog	 * @param dd download date of the catalog 	 * @param dlu date of last update	 * @param cat catalog to send	 * */	public CatalogExchangeResponse(String g,String cn,URL curl,long dd,long dlu,org.w3c.dom.Document  cat)	{		got=g;		catalogName=cn;		catalogUrl=curl;		dateDownload=new Date(dd);		dateLastUpdate=new Date(dlu);		catalog=cat;	}			/**	 * Used in case which the query peer and the response peer has the same catalog 	 * or in case which the sender has got a older version(equal,older)	 * @param g got value, represents the status of the responsed catalog	 * @param cn name of the catalog to send	 * @param curl Url of the catalog	 * @param dlu date of last update	 * 	 * */	public CatalogExchangeResponse(String g,String cn,URL curl,long dlu)	{		got=g;		catalogName=cn;		catalogUrl=curl;		dateDownload=null;		dateLastUpdate=new Date(dlu);		catalog=null;	}				/**	 * Used in case which the peer hasn't the catalog (false)	 * @param g got value, represents the status of the responsed catalog	 * @param cn name of the catalog to send	 * @param curl Url of the catalog	 * 	 * */	public CatalogExchangeResponse(String g,String cn,URL curl)	{		got=g;		catalogName=cn;		catalogUrl=curl;		dateDownload=null;		dateLastUpdate=null;		catalog=null;	}			/**     * Method that creates and return the xml document which represents     * the response.     * @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:ExampleResponse");	      Element element;	      	      element = document.createElement("got",got);	      document.appendChild(element);	      element = document.createElement("catalogName",catalogName);	      document.appendChild(element);	  	  element=document.createElement("catalogUrl",catalogUrl.toString());	      document.appendChild(element);		      if(dateDownload!=null)	      {element=document.createElement("dateDownload",String.valueOf(dateDownload.getTime()));	  	  document.appendChild(element);}	      if(dateLastUpdate!=null){	      	element=document.createElement("dateLastUpdate",String.valueOf(dateLastUpdate.getTime()));	  	  document.appendChild(element);}	  	  if(catalog!=null)	  	  	{	  	  		element = document.createElement("catalog");	  	  		document.appendChild(element);	  	  		domToJxtaDocElements(catalog,document,element);	  	  	}	      return document;	  }		/**     * Needed for the peer who receive the response to easily extract     * info from response.     * @param stream is the response xml document received     */	public CatalogExchangeResponse (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;	         }	         if(element.getName().equals("dateDownload"))	         {	         	 long t=Long.valueOf(element.getTextValue()).longValue();	         	 dateDownload = new Date(t);	             continue;	         }	        	         if(element.getName().equals("got"))	         {	         	got=element.getTextValue();	         	continue;	         }	         if(element.getName().equals("catalog"))	         {	         	catalog=jxtaDocToDom(element);	         }	         	     } 	     	 }	 	/**     * Extracts the catalog from response to a xml dom tree     * @param c element that contains the catalog     */	private org.w3c.dom.Document jxtaDocToDom(Element c)	{		org.w3c.dom.Document doc=null;		doc= new DocumentImpl();		org.w3c.dom.Element root = doc.createElement("catalog");		doc.appendChild(root);				Enumeration elements=c.getChildren();		while(elements.hasMoreElements())		{			Element el=(Element)elements.nextElement();			TextElement element = (TextElement) el;			if(element.getName().equals("university"))			{				org.w3c.dom.Element un= doc.createElement("university");				org.w3c.dom.Node n1=doc.createTextNode(element.getTextValue());				un.appendChild(n1);				root.appendChild(un);							}			else if(element.getName().equals("faculty"))			{				org.w3c.dom.Element fa= doc.createElement("faculty");				org.w3c.dom.Node n2=doc.createTextNode(element.getTextValue());				fa.appendChild(n2);				root.appendChild(fa);							}			else if (element.getName().equals("download"))			{				org.w3c.dom.Element ed=doc.createElement("download");				root.appendChild(ed);				catalogItem(element,doc,ed);			}						else			{				org.w3c.dom.Element ed=doc.createElement(element.getName());				org.w3c.dom.Node n=null;				if(element.getTextValue()!=null) n=doc.createTextNode(element.getTextValue());				else n=doc.createTextNode(" ");				ed.appendChild(n);				root.appendChild(ed);							}					}		System.out.println(" jxta to dom "+doc.toString());		return doc;			}		/**     * Extracts the catalog item to a xml dom tree     * @param ci catalog item element in jxta response     * @param  r xml document     * @param e xml dom root element      */	private void catalogItem(Element ci, org.w3c.dom.Document r, org.w3c.dom.Element e )	{		Enumeration elements=ci.getChildren();		while(elements.hasMoreElements()){			Element el=(Element)elements.nextElement();			TextElement element = (TextElement) el;				org.w3c.dom.Element ed=r.createElement(element.getName());			org.w3c.dom.Node n=null;			if (element.getTextValue()!=null)n=r.createTextNode(element.getTextValue());			else n=r.createTextNode(" ");			ed.appendChild(n);			e.appendChild(ed);						}	}			/**     * Inserts the catalog from a xml dom tree to jxta response     * @param c catalog in xml dom format     * @param r xml response document     * @param rc element of jxta response that contains catalogs     */	private void domToJxtaDocElements(org.w3c.dom.Document c,StructuredDocument r, Element rc)	{		//NodeList nl3=c.getElementsByTagName("catalog");				NodeList nl1=c.getElementsByTagName("university");		Element e1=r.createElement("university",nl1.item(0).getFirstChild().getNodeValue());		rc.appendChild(e1);		NodeList nl2=c.getElementsByTagName("faculty");		Element e2=r.createElement("faculty",nl2.item(0).getFirstChild().getNodeValue());		rc.appendChild(e2);		NodeList nl=c.getElementsByTagName("download");		Element e=null;		for(int i=0; i<nl.getLength();i++)		{			e=r.createElement("download");			rc.appendChild(e);			catalogItem((org.w3c.dom.Element) nl.item(i),r,e);		}		System.out.println(" dom to jxta "+r.toString());	}		/**     * Extracts the catalog item to a xml dom tree     * @param ci catalog item element in dom xml tree     * @param  r jxta response document     * @param e jxta response root element      */	private void catalogItem(org.w3c.dom.Element ci, StructuredDocument r,Element e)	{		NodeList nl=ci.getChildNodes();		for (int i=0;i<nl.getLength();i++)		{			if(nl.item(i).getNodeType()==org.w3c.dom.Node.ELEMENT_NODE)			{				org.w3c.dom.Element el= (org.w3c.dom.Element) nl.item(i);				Element itemEl=null;				if(el.getFirstChild()!=null)					itemEl=r.createElement(el.getNodeName(),el.getFirstChild().getNodeValue());				else					itemEl=r.createElement(el.getNodeName()," ");			 	e.appendChild(itemEl);			}		}	}		/**     * Method that returns the xml response document as String.     * @return String rapresentation of the response.     */	 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 + -