/** {@inheritDoc} */ protected void addLiteral(Literal literal) throws IOException { s.append(i(4)).append("<literal"); if (literal.getLanguage() != null) s.append(" xml:lang=\"").append(literal.getLanguage()).append("\""); else if (literal.getDatatype() != null) s.append(" datatype=\"").append(literal.getDatatype().toString()).append("\""); s.append(">").append(StringUtil.quoteAV(literal.getLexicalForm())).append("</literal>"); }
@Override public int write(TupleIterator iter) throws TrippiException { try { m_out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); doEntities(); m_out.println("<sparql xmlns=\"http://www.w3.org/2001/sw/DataAccess/rf1/result\">"); String[] names = iter.names(); m_out.println(" <head>"); for (int i = 0; i < names.length; i++) { m_out.println(" <variable name=\"" + names[i] + "\"/>"); } m_out.println(" </head>"); m_out.println(" <results>"); int count = 0; while (iter.hasNext()) { m_out.println(" <result>"); Map<String, Node> result = iter.next(); for (int i = 0; i < names.length; i++) { m_out.print(" <" + names[i]); Node n = result.get(names[i]); if (n == null) { m_out.println(" bound=\"false\"/>"); } else if (n instanceof URIReference) { String uriString = ((URIReference) n).getURI().toString(); m_out.println(" uri=\"" + getURI(uriString) + "\"/>"); } else if (n instanceof BlankNode) { String id = "blank" + n.hashCode(); m_out.println(" bnodeid=\"" + id + "\"/>"); } else if (n instanceof Literal) { Literal lit = (Literal) n; URI dType = lit.getDatatypeURI(); if (dType != null) { m_out.print(" datatype=\"" + getURI(dType.toString()) + "\""); } String lang = lit.getLanguage(); if (lang != null) { m_out.print(" xml:lang=\"" + lang + "\""); } m_out.println(">" + enc(lit.getLexicalForm()) + "</" + names[i] + ">"); } else { throw new TrippiException("Unrecognized node type: " + n.getClass().getName()); } } m_out.println(" </result>"); m_out.flush(); count++; } m_out.println(" </results>"); m_out.println("</sparql>"); m_out.flush(); iter.close(); return count; } catch (IOException e) { throw new TrippiException("Error writing", e); } }
/** * Convert a Mulgara Value to a Jena graph node. * * @param obj The Mulgara value to convert. * @return A new Jena graph node. */ static Node o2n(org.jrdf.graph.Node obj) { if (obj == null) return Node.ANY; // testing for org.jrdf.graph.URIReference if (obj.isURIReference()) { URIReference uRef = (URIReference) obj; if (skolemizedBlankNodes) { String x = uRef.getURI().toString(); if (x.startsWith(bNodeScheme)) { x = x.substring(bNodeScheme.length()); Node n = Node.createAnon(new AnonId(x)); return n; } } return Node.createURI(uRef.getURI().toString()); } // testing for org.jrdf.graph.Literal if (obj.isLiteral()) { Literal literal = (Literal) obj; if (literal.getDatatypeURI() != null) { RDFDatatype type = TypeMapper.getInstance().getSafeTypeByName(literal.getDatatypeURI().toString()); return Node.createLiteral(literal.getLexicalForm(), null, type); } return Node.createLiteral(literal.getLexicalForm(), literal.getLanguage(), null); } if (obj.isBlankNode()) { BlankNodeImpl b = (BlankNodeImpl) obj; // check if this was a Jena-allocated node Node jenaNode = valuesToNodes.get(b); // if not known, then create a Jena node from the Mulgara ID return jenaNode != null ? jenaNode : Node.createAnon(new AnonId(b.getID())); } throw new RuntimeException("Can't convert to Jena Node : " + obj); }