QuerySolution,保持"< >"

时间:2015-11-30 09:29:30

标签: java sparql jena

我目前坚持创建Fuseki Triple Store浏览器的项目。我需要可视化TripleStore中的所有数据并使应用程序可浏览。唯一的问题是QuerySolution省略了"< >"在三重商店里。

如果我使用ResultSetFormatter.asText(ResultSet),则返回此信息:

-------------------------------------------------------------------------------------------------------------------------------------
| subject                    | predicate                                         | object                                           |
=====================================================================================================================================
| <urn:animals:data>         | <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> | <http://www.w3.org/1999/02/22-rdf-syntax-ns#Seq> |
| <urn:animals:data>         | <http://www.w3.org/1999/02/22-rdf-syntax-ns#_1>   | <urn:animals:lion>                               |
| <urn:animals:data>         | <http://www.w3.org/1999/02/22-rdf-syntax-ns#_2>   | <urn:animals:tarantula>                          |
| <urn:animals:data>         | <http://www.w3.org/1999/02/22-rdf-syntax-ns#_3>   | <urn:animals:hippopotamus>                       |
-------------------------------------------------------------------------------------------------------------------------------------

请注意,某些数据包含较小/较大的符号&#34;&lt;&#34;和&#34;&gt;&#34;。一旦我尝试解析ResultSet中的数据,它就会删除这些符号,以便数据如下所示:

-------------------------------------------------------------------------------------------------------------------------------
| subject                  | predicate                                       | object                                         |
===============================================================================================================================
| urn:animals:data         | http://www.w3.org/1999/02/22-rdf-syntax-ns#type | http://www.w3.org/1999/02/22-rdf-syntax-ns#Seq |
| urn:animals:data         | http://www.w3.org/1999/02/22-rdf-syntax-ns#_1   | urn:animals:lion                               |
| urn:animals:data         | http://www.w3.org/1999/02/22-rdf-syntax-ns#_2   | urn:animals:tarantula                          |
| urn:animals:data         | http://www.w3.org/1999/02/22-rdf-syntax-ns#_3   | urn:animals:hippopotamus                       |

如您所见,数据不包含&#34;&lt;&#34;和&#34;&gt;&#34;迹象。

这是我解析ResultSet中数据的方法:

while (rs.hasNext()) {
            // Moves onto the next result
            QuerySolution sol = rs.next();
            // Return the value of the named variable in this binding.
            // A return of null indicates that the variable is not present in
            // this solution        
            RDFNode object = sol.get("object");
            RDFNode predicate = sol.get("predicate");
            RDFNode subject = sol.get("subject");


            // Fill the table with the data
            DefaultTableModel modelTable = (DefaultTableModel) this.getModel();
            modelTable.addRow(new Object[] { subject, predicate, object });
        }

很难解释这个问题,但有没有办法保持&#34;&lt; &GT;&#34;解析数据后的标志?

1 个答案:

答案 0 :(得分:3)

'&lt;&gt;'格式化程序使用格式化程序来指示该值是URI而不是字符串:因此"http://example.com/"是文字文本值,而<http://example.com/>是URI。

你可以自己做同样的事情:

RDFNode node; // subject, predicate, or object    

if (node.isURIResource()) {
    return "<" + node.asResource().getURI() + ">";
} else {
    ...
}

但使用FmtUtils更容易:

String nodeAsString = FmtUtils.stringForRDFNode(subject); // or predicate, or object

您需要做的是在呈现表格单元格时调用该代码:当前表格正在使用Object::toString()

概括地说,所需的步骤是:

modelTable.setDefaultRenderer(RDFNode.class, new MyRDFNodeRenderer());

然后查看http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#renderer有关如何创建简单渲染器的信息。请注意,value将是RDFNode

static class MyRDFNodeRenderer extends DefaultTableCellRenderer {

    public MyRDFNodeRenderer() { super(); }

    public void setValue(Object value) {
        setText((value == null) ? "" : FmtUtils.stringForRDFNode((RDFNode) value));
    }
}