📄 handler.java
字号:
/* * Created on 12-nov-2005 * * *//** * @author magowiz * */package catalog_search;import java.io.ByteArrayInputStream;import java.net.MalformedURLException;import java.net.URL;import gui.MainWindow;import java.util.Vector;import net.jxta.impl.protocol.ResolverResponse;import net.jxta.protocol.ResolverQueryMsg;import net.jxta.protocol.ResolverResponseMsg;import net.jxta.resolver.QueryHandler;import net.jxta.resolver.ResolverService;import java.util.regex.Pattern;import java.util.regex.Matcher;import org.w3c.dom.*; //Libreria di w3C per gestire DOMimport catalog_exchange.CatalogManager;public class Handler implements QueryHandler { private ResolverService resSvc; private Vector registeredListeners=new Vector(); private String handlerName; private CatalogManager catMan; private local_catalog_manager.LocalCatalogManager lcm; private Document [] doc; private MainWindow mw; private boolean lock; public Handler (ResolverService r,Vector rl,String hn, MainWindow m) { lock=false; resSvc=r; registeredListeners=rl; handlerName=hn; catMan=new CatalogManager(10); lcm=new local_catalog_manager.LocalCatalogManager(); mw=m; } public synchronized int processQuery(ResolverQueryMsg rqm) { boolean match=false; System.out.println("CatalogSearchQuery received"); CatalogSearchQuery csq= new CatalogSearchQuery(new ByteArrayInputStream(rqm.getQuery().getBytes())); int x=0; String [] c=null; String []tOp=null; String [] dOp=null; String [] uOp=null; String [] tOw=null; String [] dOw=null; String [] uOw=null; if((!csq.isOp()&&(!csq.isOw()))){ c=new String[csq.getCatN()]; int count=0; boolean ol=false; if(csq.getSearchCatalog(0)!=null){ for (int i=0;i<csq.getCatN();i++) { c[i]=csq.getSearchCatalog(i); if(c[i].equals("Local Peer Catalog")) count++; } if(count==c.length) { ol=true; } } if((c[0]!=null)&&(!ol)){ for (x=0;x<csq.getCatN();x++){ if(!c[x].equals("Local Peer Catalog")){ if(doc==null) { doc=new Document[1]; doc[x]=catMan.getCatalog(csq.getSearchCatalog(x)); c=new String[1]; c[x]=csq.getSearchCatalog(x); x++; } else{ Document [] d=new Document[x+1]; String [] b=new String[x+1]; for (int i=0;i<doc.length;i++) d[i]=doc[i]; for (int i=0;i<c.length;i++) b[i]=c[i]; d[x]=catMan.getCatalog(csq.getSearchCatalog(x)); b[x]=csq.getSearchCatalog(x); doc=d; c=b; x++; } } } String temp=null; String [] t=null; String [] d=null; String [] u=null; int j=0; for (x=0;x<doc.length;x++) { NodeList nl=doc[x].getElementsByTagName("title"); int begin=0; int end=nl.getLength(); int i=0; while((i<nl.getLength())){ //for(int i=0;i<nl.getLength();i++){ if (i>0) { String []b=new String [i+1]; for (int f=0;f<c.length;f++) b[f]=c[f]; b[i]=b[i-1]; c=b; } String par=null; String par2=null; String comodo2=null; NodeList nl2=null; Element el=null; Element el2=null; NodeList n=null; NodeList n2=null; if (csq.getSearchTitle()==null) { if(csq.getSearchDescription()==null) { return -1; } else { par=csq.getSearchDescription(); } } else { par=csq.getSearchTitle(); if (csq.getSearchDescription()!=null)par2=csq.getSearchDescription(); } el=(Element)nl.item(i); n=el.getChildNodes(); temp=n.item(0).getNodeValue(); nl2=doc[x].getElementsByTagName("description"); el2=(Element)nl2.item(i); n2=el2.getChildNodes(); if(n2.item(0)==null){ i++; continue; } comodo2=n2.item(0).getNodeValue(); Pattern p = null; Matcher m = null; Pattern p2 = null; Matcher m2 = null; if(par!=null && par2!=null) { p = Pattern.compile("(?ui)"+par); m = p.matcher(temp); match=m.find(); p2=Pattern.compile("(?ui)"+par2); m2=p2.matcher(comodo2); match=match && m2.find(); } else if (par!=null && par.equals(csq.getSearchTitle())){ p = Pattern.compile("(?ui)"+par); m = p.matcher(temp); match=m.find(); } else if (par!=null && par.equals(csq.getSearchDescription())){ p = Pattern.compile("(?ui)"+par); m = p.matcher(comodo2); match=m.find(); } if(match) { mw=csq.getMainWindow(); if (par==csq.getSearchTitle()) { if (t==null) { t=new String [1]; d=new String [1]; u=new String [1]; t[j]=temp; Node root=el.getParentNode(); NodeList nd=root.getChildNodes(); for(int k=0;k<nd.getLength();k++) { if(nd.item(k).getNodeName().equals("description")) { NodeList no=nd.item(k).getChildNodes(); d[j]=no.item(0).getNodeValue(); } if(nd.item(k).getNodeName().equals("url")) { NodeList no=nd.item(k).getChildNodes(); u[j]=no.item(0).getNodeValue(); } } } else { String [] t1=new String[j+1]; String [] d1=new String[j+1]; String [] u1=new String[j+1]; for (int k=0;k<t.length;k++) { t1[k]=t[k]; d1[k]=d[k]; u1[k]=u[k]; } t1[j]=temp; Node root=el.getParentNode(); NodeList nd=root.getChildNodes(); for(int k=0;k<nd.getLength();k++) { if(nd.item(k).getNodeName().equals("description")) { NodeList no=nd.item(k).getChildNodes(); d1[j]=no.item(0).getNodeValue(); } if(nd.item(k).getNodeName().equals("url")) { NodeList no=nd.item(k).getChildNodes(); u1[j]=no.item(0).getNodeValue(); } } t=t1; d=d1; u=u1; } j++; } else { if (t==null) { t=new String [1]; d=new String [1]; u=new String [1]; d[j]=comodo2; Node root=el.getParentNode(); NodeList nd=root.getChildNodes(); for(int k=0;k<nd.getLength();k++) { if(nd.item(k).getNodeName().equals("title")) { NodeList no=nd.item(k).getChildNodes(); t[j]=no.item(0).getNodeValue(); //System.out.println("t[j] "+t[j]); } if(nd.item(k).getNodeName().equals("url")) { NodeList no=nd.item(k).getChildNodes(); u[j]=no.item(0).getNodeValue(); } } } else { String [] t1=new String[j+1]; String [] d1=new String[j+1]; String [] u1=new String[j+1]; for (int k=0;k<t.length;k++) { t1[k]=t[k]; d1[k]=d[k]; u1[k]=u[k]; } d1[j]=comodo2; Node root=el.getParentNode(); NodeList nd=root.getChildNodes(); for(int k=0;k<nd.getLength();k++) { if(nd.item(k).getNodeName().equals("title")) { NodeList no=nd.item(k).getChildNodes(); t1[j]=no.item(0).getNodeValue(); //System.out.println("t[j] "+t[j]); } if(nd.item(k).getNodeName().equals("url")) { NodeList no=nd.item(k).getChildNodes(); u1[j]=no.item(0).getNodeValue(); } } t=t1; d=d1; u=u1; } j++; } } i++; } } if(t.length>0){ CatalogSearchResponse csr=new CatalogSearchResponse(t,d,u,c); ResolverResponse resp=new ResolverResponse(handlerName,null,rqm.getQueryId(),csr.toString()); resSvc.sendResponse(rqm.getSrc(),resp); return ResolverService.OK; } } } int jOp=0; int jOw=0; String comodo=null; if(csq.isOp()) { Document docOp=lcm.getDoc(); //p2p_system.Peer peer=mw.getP(); //ID iD=peer.getPeerId(); //String id=iD.toString(); String id="Other Peer Local Catalog"; c=new String[1]; c[0]=id; NodeList nl=docOp.getElementsByTagName("title"); int i=0; while((i<nl.getLength())){ String par=null; String par2=null; //for(int i=0;i<nl.getLength();i++){ if (i>0) { String []b=new String [i+1]; for (int f=0;f<c.length;f++) b[f]=c[f]; b[i]=b[i-1]; c=b; } if (csq.getSearchTitle()==null) { if(csq.getSearchDescription()==null) { return -1; } else { par=csq.getSearchDescription(); } } else { par=csq.getSearchTitle(); if (csq.getSearchDescription()!=null)par2=csq.getSearchDescription(); } NodeList n=null; NodeList nl2=null; NodeList n2=null; String comodo2=null; Element el2=null; Element el=null; el=(Element)nl.item(i); n=el.getChildNodes(); comodo=n.item(0).getNodeValue(); nl2=docOp.getElementsByTagName("description"); el2=(Element)nl2.item(i); n2=el2.getChildNodes(); comodo2=n2.item(0).getNodeValue(); if(n2.item(0)==null){ i++; continue; } Pattern p = null; Matcher m = null; Pattern p2 = null; Matcher m2 = null; if(par!=null && par2!=null) { p = Pattern.compile("(?ui)"+par); m = p.matcher(comodo); match=m.find(); p2=Pattern.compile("(?ui)"+par2); m2=p2.matcher(comodo2); match=match && m2.find(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -