11 | | import org.apache.http.HttpEntity; |
12 | | import org.apache.http.HttpResponse; |
13 | | import org.apache.http.NameValuePair; |
14 | | import org.apache.http.client.HttpClient; |
15 | | import org.apache.http.client.entity.UrlEncodedFormEntity; |
16 | | import org.apache.http.client.methods.HttpPost; |
17 | | //import org.apache.http.impl.client.HttpClientBuilder; |
18 | | import org.apache.http.message.BasicNameValuePair; |
19 | | import org.apache.http.util.EntityUtils; |
| 563 | |
| 564 | public LabelMap[] getLabels(String[] graphURIs, String[] resourceURIs, String language) throws Exception{ |
| 565 | if( resourceURIs == null || resourceURIs.length == 0 ){ |
| 566 | return new LabelMap[0]; |
| 567 | } |
| 568 | StringBuffer queryStr = new StringBuffer(); |
| 569 | queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n"); |
| 570 | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"); |
| 571 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"); |
| 572 | queryStr.append("SELECT DISTINCT ?res ?label \n"); |
| 573 | if (graphURIs != null) { |
| 574 | for (String graphURI : graphURIs) { |
| 575 | queryStr.append("FROM <"); |
| 576 | queryStr.append(graphURI); |
| 577 | queryStr.append(">\n"); |
| 578 | } |
| 579 | } |
| 580 | queryStr.append("WHERE{\n"); |
| 581 | queryStr.append(" ?res rdfs:label ?label.\n"); |
| 582 | queryStr.append(" FILTER(?res IN ("); |
| 583 | boolean f = false; |
| 584 | for( String resourceURI: resourceURIs ){ |
| 585 | if(f){ |
| 586 | queryStr.append(", "); |
| 587 | } |
| 588 | f = true; |
| 589 | queryStr.append("<"); |
| 590 | queryStr.append(resourceURI); |
| 591 | queryStr.append(">"); |
| 592 | } |
| 593 | queryStr.append("))\n"); |
| 594 | queryStr.append("}"); |
| 595 | |
| 596 | System.out.println(queryStr.toString()); |
| 597 | |
| 598 | Query query = QueryFactory.create(queryStr.toString()); |
| 599 | QueryExecution qexec = QueryExecutionFactory.sparqlService(endpointURI, |
| 600 | query); |
| 601 | |
| 602 | ResultSet results = qexec.execSelect(); |
| 603 | HashMap<String, LabelMap> lMap = new HashMap<String, LabelMap>(); |
| 604 | HashMap<String, InstanceLink> insLinkMap = new HashMap<String, InstanceLink>(); |
| 605 | for (; results.hasNext();) { |
| 606 | QuerySolution sol = results.next(); |
| 607 | String uri = sol.getResource("res").getURI(); |
| 608 | Literal literal = sol.getLiteral("label"); |
| 609 | if( literal != null ){ |
| 610 | String label = literal.getString(); |
| 611 | String lang = literal.getLanguage(); |
| 612 | if( language != null && language.equals(lang)){ |
| 613 | Label lbl = new Label(label, lang); |
| 614 | if( lMap.containsKey(uri)){ |
| 615 | LabelMap lm = lMap.get(uri); |
| 616 | lm.addLabel(lbl); |
| 617 | }else{ |
| 618 | LabelMap lm = new LabelMap(uri, new Label[]{lbl}); |
| 619 | lMap.put(uri, lm); |
| 620 | } |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | return lMap.values().toArray(new LabelMap[0]); |
| 625 | |
| 626 | |
| 627 | } |
| 628 | |