/** * @author Hyacinthe MENIET * Created on 25 août 07 */ package net.dotmyself.restclient.yahoosearch; import java.io.ByteArrayInputStream; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; /** * Web services Client which calls the Yahoo! Term Extraction service. */ public class TermExtractionWSClient { /** * The main method. * @param args * The block of text that the terms will be extracted from. * @throws Exception */ public static void main(String[] args) throws Exception { if(args == null || args.length < 1) { throw new IllegalArgumentException("You must indicate the text you want to extract important terms."); } // Reads and parse the given XML with DOM DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder constructor = factory.newDocumentBuilder(); ByteArrayInputStream str = new ByteArrayInputStream(args[0].getBytes("ISO-8859-1")); Document document = constructor.parse(str); Element root = document.getDocumentElement(); String context = getTagValue("context",root); String query = getTagValue("query",root); // Displays results List terms = TermExtractionMapper.getTerms(context, query); for(String term : terms) { System.out.println(term); } } /** * Returns the text content of the given tag. * @param tag * The tag. * @param root * The root Element. * @return * The String or null if * no String could be found. */ private static String getTagValue(String tag, Element root) { NodeList listOfTags = root.getElementsByTagName(tag); if(listOfTags != null && listOfTags.getLength() > 0) { Node node = listOfTags.item(0); return node.getTextContent(); } return null; } }