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

📄 metadatasearchdemo.java

📁 jxta-cms-src-2.4.zip 是官网上的比较稳定的CMS源代码。目前是最稳定的高版本。
💻 JAVA
字号:
/* * Copyright (c) 2001 Sun Microsystems, Inc.  All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in *    the documentation and/or other materials provided with the *    distribution. * * 3. The end-user documentation included with the redistribution, *    if any, must include the following acknowledgment: *       "This product includes software developed by the *       Sun Microsystems, Inc. for Project JXTA." *    Alternately, this acknowledgment may appear in the software itself, *    if and wherever such third-party acknowledgments normally appear. * * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" must *    not be used to endorse or promote products derived from this *    software without prior written permission. For written *    permission, please contact Project JXTA at http://www.jxta.org. * * 5. Products derived from this software may not be called "JXTA", *    nor may "JXTA" appear in their name, without prior written *    permission of Sun. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED.  IN NO EVENT SHALL SUN MICROSYSTEMS OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * *==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of Project JXTA.  For more * information on Project JXTA, please see * <http://www.jxta.org/>. * * This license is based on the BSD license adopted by the Apache Foundation. * * $Id: MetadataSearchDemo.java,v 1.2 2002/04/10 15:30:40 rmjohnson Exp $ * */package net.jxta.test;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.lang.reflect.InvocationTargetException;import java.io.File;import java.io.InputStream;import java.io.IOException;import java.util.Vector;import net.jxta.document.MimeMediaType;import net.jxta.document.Advertisement;import net.jxta.peergroup.PeerGroup;import net.jxta.peergroup.PeerGroupFactory;import net.jxta.exception.PeerGroupException;import net.jxta.impl.peergroup.Platform;import net.jxta.impl.peergroup.GenericPeerGroup;import net.jxta.share.*;import net.jxta.share.client.*;import net.jxta.share.metadata.*;/** * A simple applications that demonstrates how to search for content that is  * annotated with certain metadata tags. * * @see MetadataShareDemo * @see SearchDemo * @see net.jxta.share.metadata.ContentMetadata * @see net.jxta.share.client.ListContentRequest *  * @version $Revision: 1.2 $ */public class MetadataSearchDemo {    private PeerGroup netPeerGroup  = null;        static public void main(String args[]) {	//start MetadataSearchDemo	new MetadataSearchDemo();    }        public MetadataSearchDemo() {	startJxta();		SearchWindow window = new SearchWindow();	window.setVisible(true);    }        /**     * initializes NetPeerGroup and the CMS     */    private void startJxta() {	try {	    // create, and Start the default jxta NetPeerGroup	    netPeerGroup = PeerGroupFactory.newNetPeerGroup();	    	    //uncomment the following line if you want to start the app defined	    // the NetPeerGroup Advertisement (by default it's the shell)	    // in this case we want use jxta directly.	    	    // netPeerGroup.startApp(null);				} catch (PeerGroupException e) {	    // could not instanciate the group, print the stack and exit	    System.out.println("fatal error : group creation failure");	    e.printStackTrace();	    System.exit(-1);	}    }        /**     * SearchWindow serves as the GUI for MetadataSearchDemo     */    public class SearchWindow extends Frame implements ActionListener {		Button searchButton;	Button viewButton;	List resultList;		MetadataQuery descQuery;	MetadataQuery keywdQuery;		//A ListContentRequest is needed to query other peers for	//ContentAdvertisements	ListContentRequest request = null;		//an array is needed to store ContentAdvertisements returned by the	//ListContentRequest	ContentAdvertisement[] results = null;		//this vector will store the ContentAdvertisements from results that	// were found to have metadata that matches the query	Vector matches = new Vector();		/**	 * Initializes & arranges the window and its components.	 */	public SearchWindow() {	    super("Metadata Search Demo");	    setSize(450, 250);	    addWindowListener(new WindowMonitor());	    	    Panel toolbar = new Panel();	    toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));	    	    searchButton = new Button("Search");	    searchButton.addActionListener(this);	    toolbar.add(searchButton);	    	    viewButton = new Button("View Advertisement");	    viewButton.addActionListener(this);	    toolbar.add(viewButton);	    	    add(toolbar, BorderLayout.NORTH);	    	    resultList = new java.awt.List();	    add(resultList, BorderLayout.CENTER);	}		public void actionPerformed(ActionEvent e) {	    System.out.println(e.getActionCommand());	    	    //handle the event caused by the "Search" button being clicked	    if (e.getSource().equals(searchButton)) {		if (request != null) {		    request.cancel();		}				//prompt the user for a search string		String searchString = JOptionPane		    .showInputDialog(this, "Enter a string to search for:");				if(searchString == null) return;				//see the source of net.jxta.share.metadata.Keywords and		//net.jxta.share.metadata.Description to see		//how these MetadataQuery objects work				//this will generate a MetadataQuery object for querying a		//Description object		descQuery = Description.newQuery(searchString);				//generate a MetadataQuery for querying a Keywords object		keywdQuery = Keywords.newQuery(searchString);				//Initialize a ListContentRequest.  Note that an empty search		//string is passed in, causing every peer in netPeerGroup to		//return advertisements for all of the content they are sharing		request = new MyListRequest(netPeerGroup, "", this);				//send the list request and wait for results to be sent back		request.activateRequest();					    }else if (e.getSource().equals(viewButton)) {		//handle the event caused by the "View Advertisement" button		// being clicked.				//figure out which content advertisement is selected, then		//display it in an AdvertisementViewer		int selectedIndex = resultList.getSelectedIndex();		if(selectedIndex != -1) {		    ContentAdvertisement selAd =			(ContentAdvertisement)matches.elementAt(selectedIndex);		    if(selAd != null)			new AdvertisementViewer(selAd);		}	    }	}	/**	 * This method filters through advertisements returned by other peers	 * and then displays the matches in the list.	 */	protected void updateResults(ContentAdvertisement[] results) {	    this.results = results;	    //erase all of the old results	    resultList.removeAll();	    matches = new Vector(results.length);	    	    ContentMetadata[] melems;	    	    for (int i=0; i<results.length; i++) {		//retrieve the metadata in a ContentAdvertisement		melems = results[i].getMetadata();				//check if the metadata matches the search criteria using a		// MetadataQuery of the appropriate type.				if(melems != null) {		    for(int j= 0; j < melems.length; j++) {			try {			    if(melems[j] instanceof Description) {				//add results with a description				//that contains the search string,				if(descQuery.queryMetadata(melems[j]) > 0) {				    resultList.add(results[i].getName());				    matches.addElement(results[i]);				    break;				}							    }else if(melems[j] instanceof Keywords) {				// or a keyword identical to the search string.				if(keywdQuery.queryMetadata(melems[j]) > 0) {				    resultList.add(results[i].getName());				    matches.addElement(results[i]);				    break;				}			    }			}catch(IllegalArgumentException iae) {			    //the ContentMetadata object passed into			    // queryMetadata() wasn't formatted properly			    System.out.println("malformed metadata");			}		    }		}	    }	}    }        /**     * An implementation of ListContentRequest that will automatically update     * a SearchWindow as ContentAdvertisements are returned.     *      * @see ListContentRequest     * @see CachedListContentRequest     */    class MyListRequest extends ListContentRequest {	SearchWindow searchWindow = null;	/**	 * Initialize a list request that will be propagated throughout a given	 * peer group.  Any ContentAdvertisement for which the string returned	 * by getName() or getDescription() contains inSubStr	 *  (case insensitive) is sent back in a list response. However, the	 * list request isn't sent until activateRequest() is called.	 * 	 * @see net.jxta.share.client.ListContentRequest	 * @see net.jxta.share.client.ListContentRequest#ListContentRequest(net.jxta.peergroup.PeerGroup, java.lang.String)	 */	public MyListRequest(PeerGroup group, String inSubStr			     ,SearchWindow searchWindow) {	    super(group, inSubStr);	    this.searchWindow = searchWindow;	}		/**	 * This function is called each time more results are received.	 */	public void notifyMoreResults() {	    if (searchWindow != null) {		//note: getResults() returns all of the ContentAdvertisements		//received so far, not just the ones that were in the last list		//response.		searchWindow.updateResults(getResults());	    }	}    }        class WindowMonitor extends WindowAdapter {	public void windowClosing(WindowEvent e) {	    Window w = e.getWindow();	    w.setVisible(false);	    w.dispose();	    System.exit(0);	}    }}

⌨️ 快捷键说明

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