import edu.uw.symblnet.URLSymbl;
import edu.uw.cline.BaseCommand;
import java.net.URL;
import java.net.MalformedURLException;

/**
 */
public class SearchCommand extends BaseCommand {
	
	// Here is the basic URL required to submit a search to google
	//
	private static String searchEngineString_ = 
		"http://www.google.com/search?q=";

	public SearchCommand() {
		super("search", -1, "word1 [word2 word3 ...]" +
			"\n- sends a search request to the google search engine");
	}

	public void execute(String args[]) {
		StringBuffer urlString = new StringBuffer(searchEngineString_);
		
		// iterate through the search words specified by the user
		// and construct a URL to submit to google
		//
		for (int i=0; i<args.length; i++) {
			if (i != 0) {
				// seperate words to search for with '+' character
				urlString.append("+");
			}
			urlString.append(args[i]);
		}

		try {
			URL url = new URL(urlString.toString());

			// Note - creating the URLSymbl will break apart the pieces of 
			// the URL and associate them together - the search words 
			// specified by the user will be automatically associated with 
			// the search URL
			//
			URLSymbl urlSymbl = URLSymbl.getOrCreate(url);
			urlSymbl.decode();
		} catch (MalformedURLException e) {
			System.err.println(
				"Could not create URL from '" + urlString + "'");
		}
	}
}
