/usr/share/doc/libhttpunit-java-doc/examples/NearWords.java is in libhttpunit-java-doc 1.7+dfsg-10.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | import com.meterware.httpunit.*;
import java.io.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
/**
* This class demonstrates using httpunit to use the functionality of a web set from a command
* line. To use it, specify a single word with one or more characters replaced by '?'. The
* program will use the Merriam-Webster web site to find all words which match the pattern.
*
* Note: this program is not robust, but should work is used properly.
**/
public class NearWords {
public static void main( String[] params ) {
try {
if (params.length < 1) {
System.out.println( "Usage: java NearWords [pattern]" );
System.out.println( "where [pattern] may contain '?' to match any character" );
System.out.println( "");
System.out.println( "will demonstrate usage with the pattern 'test' now ...");
String[] defaultParams={"test"};
params=defaultParams;
}
WordSeeker seeker = new WordSeeker();
PrintStream err = new PrintStream( new FileOutputStream( "null.txt" ) );
System.setErr( err );
String[] words = seeker.getWordsMatching( params[0] );
for (int i=0; i < words.length; i++) {
System.out.println( (i+1) + ". " + words[i] );
}
} catch (Exception e) {
System.err.println( "Exception: " + e );
}
}
}
/**
* subclass to seek words from the Merriam-Webster Online search
* as of 2008-05-02
*
*/
class WordSeeker {
public WordSeeker() {
try {
HttpUnitOptions.setScriptingEnabled(false);
String url="http://www.m-w.com/";
System.out.println("visiting "+url);
WebRequest request = new GetMethodWebRequest( url );
response = conversation.getResponse( request );
} catch (Exception e) {
throw new RuntimeException( "Error retrieving form: " + e );
}
}
public String[] getWordsMatching( String pattern ) throws SAXException, IOException, java.net.MalformedURLException {
System.out.println("getting Words matching '"+pattern+"'");
WebForm lookupForm = response.getFormWithID("search_form");
WebRequest request = lookupForm.getRequest();
request.setParameter( "va", pattern );
request.setParameter( "book", "Dictionary" );
response = conversation.getResponse( request );
return getOptionsFromResponse();
}
private WebConversation conversation = new WebConversation();
private WebResponse response;
private String[] getOptionsFromResponse() throws SAXException {
String[] words;
WebForm[] forms = response.getForms();
for (int i=0; i < forms.length; i++) {
Element form = (Element) forms[i].getDOMSubtree();
NodeList nl = form.getElementsByTagName( "option" );
if (nl.getLength() == 0) continue;
words = new String[ nl.getLength() ];
for (int j = 0; j < nl.getLength(); j++) {
words[j] = nl.item(j).getFirstChild().getNodeValue();
}
return words;
}
return new String[0];
}
}
|