Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package it.cybion.dbpedia.textsearch;
- import java.awt.List;
- import java.io.ByteArrayInputStream;
- import java.io.File;
- import java.io.InputStream;
- import java.io.UnsupportedEncodingException;
- import java.net.URI;
- import java.net.URISyntaxException;
- import java.util.LinkedList;
- import it.cybion.dbpedia.textsearch.rest.response.ArrayOfResult;
- import it.cybion.dbpedia.textsearch.rest.response.Result;
- import it.cybion.dbpedia.textsearch.rest.response.DBpediaClass;
- import it.cybion.dbpedia.textsearch.rest.response.DBpediaCategory;
- import javax.ws.rs.core.MediaType;
- import javax.ws.rs.core.MultivaluedMap;
- import javax.xml.bind.JAXBContext;
- import javax.xml.bind.JAXBElement;
- import javax.xml.bind.JAXBException;
- import javax.xml.bind.Marshaller;
- import javax.xml.bind.UnmarshalException;
- import javax.xml.bind.Unmarshaller;
- import javax.xml.validation.SchemaFactory;
- import org.xml.sax.SAXException;
- import com.sun.jersey.api.client.Client;
- import com.sun.jersey.api.client.UniformInterfaceException;
- import com.sun.jersey.api.client.WebResource;
- import com.sun.jersey.core.util.MultivaluedMapImpl;
- /**
- * Hello world!
- *
- */
- public class App {
- public static void main(String[] args) {
- System.out.println("Hello dbpedia lookup!");
- Client client = Client.create();
- /* dbpedia keyword search */
- WebResource webResource = client
- .resource("http://lookup.dbpedia.org/api/search.asmx/KeywordSearch");
- // QueryClass=&MaxHits=5&QueryString=berl
- MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
- // queryParams.add("QueryClass", "");
- queryParams.add("MaxHits", "5");
- queryParams.add("QueryString", "galway");
- System.out.println("uri "
- + webResource.queryParams(queryParams).toString());
- JAXBContext ctx;
- try {
- ctx = JAXBContext.newInstance(ArrayOfResult.class);
- Marshaller marshaller = ctx.createMarshaller();
- marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
- ArrayOfResult arrayResult = new ArrayOfResult();
- LinkedList<Result> results = new LinkedList<Result>();
- Result result = new Result();
- result.setDescription("desc");
- result.setLabel("label");
- try {
- result.setUri(new URI("http://www.cybion.it/"));
- } catch (URISyntaxException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- result.setRefCount(12);
- LinkedList<DBpediaClass> classes = new LinkedList<DBpediaClass>();
- LinkedList<DBpediaCategory> categories = new LinkedList<DBpediaCategory>();
- DBpediaClass dbClass = new DBpediaClass();
- DBpediaCategory dbPediaCategory = new DBpediaCategory();
- dbClass.setLabel("labelClass");
- dbPediaCategory.setLabel("labelCategory");
- try {
- dbClass.setUri(new URI("http://www.dbpedia.cls"));
- dbPediaCategory.setUri(new URI("http://www.dbpedia.cat"));
- } catch (URISyntaxException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- classes.add(dbClass);
- categories.add(dbPediaCategory);
- result.setClasses(classes);
- result.setCategories(categories);
- results.add(result);
- arrayResult.setResults(results);
- // marshaller.marshal(arrayResult, System.out);
- } catch (JAXBException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- /* first request in string, it works: */
- String resultsString = webResource.queryParams(queryParams)
- .accept(MediaType.APPLICATION_XML).get(String.class);
- InputStream is = null;
- try {
- is = new ByteArrayInputStream(resultsString.getBytes("UTF-8"));
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- ArrayOfResult arrayOfResult = webResource.queryParams(queryParams)
- .accept(MediaType.APPLICATION_XML).get(ArrayOfResult.class);
- // System.out.println(arrayOfResult.toString());
- JAXBContext context;
- try {
- context = JAXBContext.newInstance(ArrayOfResult.class);
- Unmarshaller unmarshaller = context.createUnmarshaller();
- unmarshaller.setEventHandler(new JAXBEventCollector());
- ArrayOfResult otherArrayResult = null;
- otherArrayResult = (ArrayOfResult) unmarshaller.unmarshal(is);
- System.out.println(otherArrayResult.toString());
- } catch (JAXBException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- /////////////////////////////////////////
- package it.cybion.dbpedia.textsearch.rest.response;
- import java.util.List;
- import javax.xml.bind.annotation.XmlAccessType;
- import javax.xml.bind.annotation.XmlAccessorType;
- import javax.xml.bind.annotation.XmlAttribute;
- import javax.xml.bind.annotation.XmlElement;
- import javax.xml.bind.annotation.XmlRootElement;
- @XmlRootElement(name = "ArrayOfResult",
- namespace = "http://lookup.dbpedia.org/"
- )
- @XmlAccessorType(XmlAccessType.FIELD)
- public class ArrayOfResult {
- @XmlAttribute
- private String xmlns;
- @XmlElement(name = "Result")
- private List<Result> results;
- public ArrayOfResult() {}
- public List<Result> getResults() {
- return results;
- }
- public void setResults(List<Result> results) {
- this.results = results;
- }
- @Override
- public String toString() {
- return "ArrayOfResult [xmlns=" + xmlns + ", results=" + results + "]";
- }
- }
- /////////////////////////////
- package it.cybion.dbpedia.textsearch.rest.response;
- import java.net.URI;
- import java.util.List;
- import javax.xml.bind.annotation.XmlAccessType;
- import javax.xml.bind.annotation.XmlAccessorType;
- import javax.xml.bind.annotation.XmlElement;
- import javax.xml.bind.annotation.XmlElementWrapper;
- import javax.xml.bind.annotation.XmlRootElement;
- @XmlRootElement(name = "Result")
- @XmlAccessorType(XmlAccessType.FIELD)
- public class Result {
- @XmlElement(name = "Label")
- private String label;
- @XmlElement(name = "URI")
- private URI uri;
- @XmlElement(name = "Description")
- private String description;
- @XmlElementWrapper(name = "Classes")
- @XmlElement(name = "Class")
- private List<DBpediaClass> classes;
- @XmlElementWrapper(name = "Categories")
- @XmlElement(name = "Category")
- private List<DBpediaCategory>categories;
- //TODO: missing templates, redirects
- // private List<Template> templates;
- @XmlElement(name = "Refcount")
- private int refCount;
- public Result() {}
- public String getLabel() {
- return label;
- }
- public void setLabel(String label) {
- this.label = label;
- }
- public URI getUri() {
- return uri;
- }
- public void setUri(URI uri) {
- this.uri = uri;
- }
- public String getDescription() {
- return description;
- }
- public void setDescription(String description) {
- this.description = description;
- }
- public List<DBpediaClass> getClasses() {
- return classes;
- }
- public void setClasses(List<DBpediaClass> classes) {
- this.classes = classes;
- }
- public List<DBpediaCategory> getCategories() {
- return categories;
- }
- public void setCategories(List<DBpediaCategory> categories) {
- this.categories = categories;
- }
- public int getRefCount() {
- return refCount;
- }
- public void setRefCount(int refCount) {
- this.refCount = refCount;
- }
- @Override
- public String toString() {
- return "Result [label=" + label + ", uri=" + uri + ", description="
- + description + ", classes=" + classes + ", categories="
- + categories + ", refCount=" + refCount + "]";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement