package com.alchemyapi.test; import com.alchemyapi.api.AlchemyAPI; import org.xml.sax.SAXException; import org.w3c.dom.Document; import java.io.*; import javax.xml.parsers.ParserConfigurationException; import javax.xml.xpath.XPathExpressionException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; class ConstraintQueryTest { public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { // Create an AlchemyAPI object. AlchemyAPI alchemyObj = AlchemyAPI.GetInstanceFromFile("api_key.txt"); // Extract first link from an URL. Document doc = alchemyObj.URLGetConstraintQuery("http://microformats.org/wiki/hcard", "1st link"); System.out.println(getStringFromDocument(doc)); // Extract first link from a HTML. String htmlDoc = getFileContents("data/example.html"); doc = alchemyObj.HTMLGetConstraintQuery(htmlDoc, "http://www.test.com/", "1st link"); System.out.println(getStringFromDocument(doc)); } // utility function private static String getFileContents(String filename) throws IOException, FileNotFoundException { File file = new File(filename); StringBuilder contents = new StringBuilder(); BufferedReader input = new BufferedReader(new FileReader(file)); try { String line = null; while ((line = input.readLine()) != null) { contents.append(line); contents.append(System.getProperty("line.separator")); } } finally { input.close(); } return contents.toString(); } // utility method private static String getStringFromDocument(Document doc) { try { DOMSource domSource = new DOMSource(doc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.transform(domSource, result); return writer.toString(); } catch (TransformerException ex) { ex.printStackTrace(); return null; } } }