Advertisement
Guest User

main.java

a guest
Jan 31st, 2011
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.32 KB | None | 0 0
  1. package it.cybion.dbpedia.textsearch;
  2.  
  3. import java.awt.List;
  4. import java.io.ByteArrayInputStream;
  5. import java.io.File;
  6. import java.io.InputStream;
  7. import java.io.UnsupportedEncodingException;
  8. import java.net.URI;
  9. import java.net.URISyntaxException;
  10. import java.util.LinkedList;
  11.  
  12. import it.cybion.dbpedia.textsearch.rest.response.ArrayOfResult;
  13. import it.cybion.dbpedia.textsearch.rest.response.Result;
  14. import it.cybion.dbpedia.textsearch.rest.response.DBpediaClass;
  15. import it.cybion.dbpedia.textsearch.rest.response.DBpediaCategory;
  16.  
  17. import javax.ws.rs.core.MediaType;
  18. import javax.ws.rs.core.MultivaluedMap;
  19. import javax.xml.bind.JAXBContext;
  20. import javax.xml.bind.JAXBElement;
  21. import javax.xml.bind.JAXBException;
  22. import javax.xml.bind.Marshaller;
  23. import javax.xml.bind.UnmarshalException;
  24. import javax.xml.bind.Unmarshaller;
  25. import javax.xml.validation.SchemaFactory;
  26.  
  27. import org.xml.sax.SAXException;
  28.  
  29. import com.sun.jersey.api.client.Client;
  30. import com.sun.jersey.api.client.UniformInterfaceException;
  31. import com.sun.jersey.api.client.WebResource;
  32. import com.sun.jersey.core.util.MultivaluedMapImpl;
  33.  
  34. /**
  35.  * Hello world!
  36.  *
  37.  */
  38. public class App {
  39.     public static void main(String[] args) {
  40.         System.out.println("Hello dbpedia lookup!");
  41.  
  42.         Client client = Client.create();
  43.  
  44.         /* dbpedia keyword search */
  45.         WebResource webResource = client
  46.                 .resource("http://lookup.dbpedia.org/api/search.asmx/KeywordSearch");
  47.  
  48.         // QueryClass=&MaxHits=5&QueryString=berl
  49.         MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
  50.         // queryParams.add("QueryClass", "");
  51.         queryParams.add("MaxHits", "5");
  52.         queryParams.add("QueryString", "galway");
  53.  
  54.         System.out.println("uri "
  55.                 + webResource.queryParams(queryParams).toString());
  56.  
  57.         JAXBContext ctx;
  58.         try {
  59.             ctx = JAXBContext.newInstance(ArrayOfResult.class);
  60.             Marshaller marshaller = ctx.createMarshaller();
  61.             marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);        
  62.            
  63.             ArrayOfResult arrayResult = new ArrayOfResult();
  64.             LinkedList<Result> results = new LinkedList<Result>();
  65.             Result result = new Result();
  66.             result.setDescription("desc");
  67.             result.setLabel("label");
  68.             try {
  69.                 result.setUri(new URI("http://www.cybion.it/"));
  70.             } catch (URISyntaxException e) {
  71.                 // TODO Auto-generated catch block
  72.                 e.printStackTrace();
  73.             }
  74.             result.setRefCount(12);
  75.            
  76.             LinkedList<DBpediaClass> classes = new LinkedList<DBpediaClass>();
  77.             LinkedList<DBpediaCategory> categories = new LinkedList<DBpediaCategory>();
  78.             DBpediaClass dbClass = new DBpediaClass();
  79.             DBpediaCategory dbPediaCategory = new DBpediaCategory();
  80.             dbClass.setLabel("labelClass");
  81.             dbPediaCategory.setLabel("labelCategory");
  82.             try {
  83.                 dbClass.setUri(new URI("http://www.dbpedia.cls"));
  84.                 dbPediaCategory.setUri(new URI("http://www.dbpedia.cat"));
  85.             } catch (URISyntaxException e) {
  86.                 // TODO Auto-generated catch block
  87.                 e.printStackTrace();
  88.             }
  89.             classes.add(dbClass);
  90.             categories.add(dbPediaCategory);
  91.            
  92.             result.setClasses(classes);
  93.             result.setCategories(categories);
  94.             results.add(result);
  95.             arrayResult.setResults(results);
  96.            
  97. //          marshaller.marshal(arrayResult, System.out);
  98.         } catch (JAXBException e) {
  99.             // TODO Auto-generated catch block
  100.             e.printStackTrace();
  101.         }
  102.        
  103.         /* first request in string, it works: */
  104.         String resultsString = webResource.queryParams(queryParams)
  105.         .accept(MediaType.APPLICATION_XML).get(String.class);
  106.  
  107.         InputStream is = null;
  108.  
  109.         try {
  110.             is = new ByteArrayInputStream(resultsString.getBytes("UTF-8"));
  111.         } catch (UnsupportedEncodingException e) {
  112.             e.printStackTrace();
  113.         }
  114.  
  115.         ArrayOfResult arrayOfResult = webResource.queryParams(queryParams)
  116.                 .accept(MediaType.APPLICATION_XML).get(ArrayOfResult.class);
  117.  
  118. //      System.out.println(arrayOfResult.toString());
  119.                
  120.         JAXBContext context;
  121.  
  122.         try {
  123.             context = JAXBContext.newInstance(ArrayOfResult.class);
  124.             Unmarshaller unmarshaller = context.createUnmarshaller();
  125.             unmarshaller.setEventHandler(new JAXBEventCollector());        
  126.             ArrayOfResult otherArrayResult = null;
  127.             otherArrayResult = (ArrayOfResult) unmarshaller.unmarshal(is);
  128.            
  129.             System.out.println(otherArrayResult.toString());
  130.         } catch (JAXBException e) {
  131.             // TODO Auto-generated catch block
  132.             e.printStackTrace();
  133.         }
  134.  
  135.     }
  136. }
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153. /////////////////////////////////////////
  154.  
  155.  
  156. package it.cybion.dbpedia.textsearch.rest.response;
  157.  
  158.  
  159. import java.util.List;
  160.  
  161. import javax.xml.bind.annotation.XmlAccessType;
  162. import javax.xml.bind.annotation.XmlAccessorType;
  163. import javax.xml.bind.annotation.XmlAttribute;
  164. import javax.xml.bind.annotation.XmlElement;
  165. import javax.xml.bind.annotation.XmlRootElement;
  166.  
  167. @XmlRootElement(name = "ArrayOfResult",
  168.                 namespace = "http://lookup.dbpedia.org/"
  169.     )
  170. @XmlAccessorType(XmlAccessType.FIELD)
  171. public class ArrayOfResult {
  172.  
  173.     @XmlAttribute
  174.     private String xmlns;
  175.    
  176.     @XmlElement(name = "Result")
  177.     private List<Result> results;
  178.    
  179.     public ArrayOfResult() {}
  180.  
  181.     public List<Result> getResults() {
  182.         return results;
  183.     }
  184.  
  185.     public void setResults(List<Result> results) {
  186.         this.results = results;
  187.     }
  188.  
  189.     @Override
  190.     public String toString() {
  191.         return "ArrayOfResult [xmlns=" + xmlns + ", results=" + results + "]";
  192.     }
  193.  
  194. }
  195.  
  196.  
  197.  
  198. /////////////////////////////
  199.  
  200.  
  201.  
  202.  
  203. package it.cybion.dbpedia.textsearch.rest.response;
  204.  
  205. import java.net.URI;
  206. import java.util.List;
  207.  
  208. import javax.xml.bind.annotation.XmlAccessType;
  209. import javax.xml.bind.annotation.XmlAccessorType;
  210. import javax.xml.bind.annotation.XmlElement;
  211. import javax.xml.bind.annotation.XmlElementWrapper;
  212. import javax.xml.bind.annotation.XmlRootElement;
  213.  
  214. @XmlRootElement(name = "Result")
  215. @XmlAccessorType(XmlAccessType.FIELD)
  216. public class Result {
  217.    
  218.     @XmlElement(name = "Label")
  219.     private String label;
  220.     @XmlElement(name = "URI")
  221.     private URI uri;
  222.     @XmlElement(name = "Description")
  223.     private String description;
  224.     @XmlElementWrapper(name = "Classes")
  225.     @XmlElement(name = "Class")
  226.     private List<DBpediaClass> classes;
  227.     @XmlElementWrapper(name = "Categories")
  228.     @XmlElement(name = "Category")
  229.     private List<DBpediaCategory>categories;
  230.    
  231.     //TODO: missing templates, redirects
  232. //  private List<Template> templates;
  233.    
  234.     @XmlElement(name = "Refcount")
  235.     private int refCount;
  236.    
  237.     public Result() {}
  238.    
  239.     public String getLabel() {
  240.         return label;
  241.     }
  242.     public void setLabel(String label) {
  243.         this.label = label;
  244.     }
  245.     public URI getUri() {
  246.         return uri;
  247.     }
  248.     public void setUri(URI uri) {
  249.         this.uri = uri;
  250.     }
  251.     public String getDescription() {
  252.         return description;
  253.     }
  254.     public void setDescription(String description) {
  255.         this.description = description;
  256.     }
  257.     public List<DBpediaClass> getClasses() {
  258.         return classes;
  259.     }
  260.     public void setClasses(List<DBpediaClass> classes) {
  261.         this.classes = classes;
  262.     }
  263.     public List<DBpediaCategory> getCategories() {
  264.         return categories;
  265.     }
  266.     public void setCategories(List<DBpediaCategory> categories) {
  267.         this.categories = categories;
  268.     }
  269.     public int getRefCount() {
  270.         return refCount;
  271.     }
  272.     public void setRefCount(int refCount) {
  273.         this.refCount = refCount;
  274.     }
  275.     @Override
  276.     public String toString() {
  277.         return "Result [label=" + label + ", uri=" + uri + ", description="
  278.                 + description + ", classes=" + classes + ", categories="
  279.                 + categories + ", refCount=" + refCount + "]";
  280.     }
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement