Example #1
0
  /**
   * Map a Jena graph node to a Mulgara value.
   *
   * @param x The Jena Node to convert.
   * @param session A session to use for blank node persistence.
   * @return A Mulgara Value.
   * @throws URISyntaxException When creating a URIReference that refers to an invalid URI.
   */
  static Value n2v(Node x, Session session) throws URISyntaxException {
    if (x.isURI()) return new URIReferenceImpl(new URI(x.getURI()));

    if (x.isLiteral()) {
      // The return types are Mulgara LiteralImpl
      if (x.getLiteralDatatypeURI() != null) {
        return new LiteralImpl(x.getLiteralLexicalForm(), new URI(x.getLiteralDatatypeURI()));
      }
      if (x.getLiteralLanguage() != null) {
        return new LiteralImpl(x.getLiteralLexicalForm(), x.getLiteralLanguage());
      }
      return new LiteralImpl(x.getLiteralLexicalForm());
    }

    if (x.isBlank()) {
      // is this a previously encountered Jena-allocated node?
      Value bn = nodesToValues.get(x);
      if (bn != null) return bn;

      // May be a Mulgara-allocated bNode (and so we we have seen before)
      String blankLabel = x.getBlankNodeLabel();
      if (blankLabel.startsWith(LABEL)) {
        long id = Long.parseLong(blankLabel.substring(LABEL_LEN));
        return new BlankNodeImpl(BlankNodeImpl.counterToNode(id));
      }

      // It's not - it's a Jena one.
      if (skolemizedBlankNodes) {
        String skol = bNodeScheme + x.getBlankNodeLabel();
        return new URIReferenceImpl(new URI(skol));
      }

      // Not a Mulgara-allocated bNode.  Create a new mapping.
      BlankNodeImpl v = new BlankNodeImpl();

      nodesToValues.put(x, v);
      valuesToNodes.put(v.getNodeId(), x);
      return v;
    }

    throw new RuntimeException("Can't convert from Jena node : " + x);
  }
Example #2
0
  /**
   * 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);
  }