@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); } }
/** * Returns a String representation of an ObjectNode. Object values do not have to be escaped. * * @param node Node * @throws GraphException * @return String */ protected String getNodeString(Node node) throws GraphException { // value to be returned String object = null; // determine type of subject node and create uri from it if (node != null) { try { if (node instanceof URIReference) { object = ((URIReference) node).getURI().toString(); } else if (node instanceof BlankNode) { object = new URI("#" + ((BlankNode) node).toString()).toString(); } else if (node instanceof Literal) { object = ((Literal) node).getLexicalForm(); } else { object = node.toString(); } } catch (URISyntaxException uriException) { throw new GraphException( "Could not get String for ObjectNode: " + node + ".", uriException); } } else { throw new GraphException( "Could not get String for ObjectNode: " + node + ". ObjectNode is null."); } return object; }
/** * Returns a URI that represents the Node. * * @param node the node representing the URI. * @throws GraphException * @return URI */ protected String getURI(Node node) throws GraphException { // value to be returned String uri = null; // determine type of subject node and create uri from it if (node != null) { try { if (node instanceof URIReference) { uri = ((URIReference) node).getURI().toString(); } else if (node instanceof BlankNode) { uri = new URI("#" + ((BlankNode) node).toString()).toString(); } else { uri = node.toString(); } } catch (URISyntaxException uriException) { throw new GraphException("Could not get URI for Node: " + node + ".", uriException); } } else { throw new GraphException("Could not get URI for Node: " + node + ". Node is null."); } // return the URI with any namespaces replaced with prefixes return this.replaceNamespace(uri); }
/** * 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); }
/** * Determines if the object is a Literal or Resource and returns an iTQL representation. * * <p>Throws an Exception if the ObjectNode is a BlankNode * * @param node ObjectNode * @return String */ protected String asString(Node node) { if (node == null) { throw new IllegalArgumentException("Node is null"); } // determine type and convert to String if (node instanceof URIReference) { return " <" + ((URIReference) node).getURI() + ">"; } else if (node instanceof Literal) { return " '" + escape(((Literal) node).getLexicalForm()) + "'"; } else { // probably a BlankNode throw new IllegalArgumentException("Unsupported Node type: " + node.getClass().getName()); } }
/** * Resolve a constraint against an RDF/XML document. * * <p>Resolution is by filtration of a URL stream, and thus very slow. */ public Resolution resolve(Constraint constraint) throws QueryException { if (logger.isDebugEnabled()) { logger.debug("Resolve " + constraint); } // Validate "constraint" parameter if (constraint == null) { throw new IllegalArgumentException("Null \"constraint\" parameter"); } if (!(constraint.getModel() instanceof LocalNode)) { if (logger.isDebugEnabled()) logger.debug("Ignoring solutions for " + constraint); return new EmptyResolution(constraint, false); } if (!(constraint.getElement(1) instanceof LocalNode) || !(constraint.getElement(2) instanceof LocalNode)) { throw new QueryException( "Prefix resolver can only be used for fixed prefixes: " + constraint); } try { long property = ((LocalNode) constraint.getElement(1)).getValue(); LocalNode object = (LocalNode) constraint.getElement(2); Node prefixNode = resolverSession.globalize(object.getValue()); // check the constraint for consistency if ((property != mulgaraPrefix && property != mulgaraStringPrefix) || !(prefixNode instanceof Literal || prefixNode instanceof URIReference)) { logger.debug("property = " + property + ", mulgaraPrefix = " + mulgaraPrefix); logger.debug("element(2): " + prefixNode + " [" + prefixNode.getClass().getName() + "]"); throw new QueryException( "Prefix resolver can only be used for prefix constraints: " + constraint); } if (logger.isDebugEnabled()) { logger.debug( "Evaluating " + constraint.getElement(0) + " has prefix " + constraint.getElement(2)); } ConstraintElement node = constraint.getElement(0); assert node != null; Tuples tuples; if (node instanceof Variable) { // convert the prefix into a string pool object SPObjectFactory spoFact = SPObjectFactoryImpl.getInstance(); SPObject startPrefixObj = getStartObject(spoFact, prefixNode, property); SPObject endPrefixObj = getEndObject(spoFact, prefixNode, property); // get the extents of the prefix from the string pool tuples = resolverSession.findStringPoolRange(startPrefixObj, true, endPrefixObj, false); assert tuples != null; // rename variables away from subject, predicate and object tuples.renameVariables(constraint); long resultSize; try { // Get the size of the final result. resultSize = tuples.getRowCount(); } catch (TuplesException e) { throw new QueryException("Unable to build result", e); } if (logger.isDebugEnabled()) { try { logger.debug( "tuples size = " + tuples.getRowCount() + " (should be " + resultSize + ")"); } catch (TuplesException e) { logger.debug("Error getting the length of the tuples object"); } } return new TuplesWrapperResolution(tuples, constraint); } else { // if (node instanceof Variable) // node must therefore be an instanceof LocalNode // we can shortcut the process here assert node instanceof LocalNode; LocalNode n = (LocalNode) node; // get the node out of the string pool SPObject spo = resolverSession.findStringPoolObject(n.getValue()); // check that the node exists if (spo == null) { tuples = TuplesOperations.empty(); } else { // see if the node starts with the required prefix String prefix; // extract the string from the literal if (prefixNode instanceof Literal) { prefix = ((Literal) prefixNode).getLexicalForm(); } else { prefix = ((URIReference) prefixNode).getURI().toString(); } if (spo.getLexicalForm().startsWith(prefix)) { tuples = TuplesOperations.unconstrained(); } else { tuples = TuplesOperations.empty(); } } } // convert the tuples to a resolution return new TuplesWrapperResolution(tuples, constraint); } catch (GlobalizeException ge) { throw new QueryException("Couldn't convert internal data into a string", ge); } catch (StringPoolException e) { throw new QueryException("Couldn't query constraint", e); } }