Exemple #1
0
  /**
   * @param g Where to assemble the description.
   * @param tripleStore The KB instance to be described.
   * @param serviceURI The SPARQL service end point.
   * @param aDefaultDataset The data set identifier that will be used on the description (the
   *     bigdata namespace of the dataset is obtained from the <i>tripleStore</i>).
   */
  public VoID(
      final Graph g,
      final AbstractTripleStore tripleStore,
      final String[] serviceURI,
      final Resource aDataset) {

    if (g == null) throw new IllegalArgumentException();

    if (tripleStore == null) throw new IllegalArgumentException();

    if (serviceURI == null) throw new IllegalArgumentException();

    if (serviceURI.length == 0) throw new IllegalArgumentException();

    for (String s : serviceURI) if (s == null) throw new IllegalArgumentException();

    if (aDataset == null) throw new IllegalArgumentException();

    this.g = g;

    this.tripleStore = tripleStore;

    this.serviceURI = serviceURI;

    this.f = g.getValueFactory();

    this.aDataset = aDataset;

    this.aDefaultGraph = f.createBNode("defaultGraph");
  }
Exemple #2
0
  /**
   * Implementation using the json.org API.
   *
   * @param json The RDF/JSON string to be parsed and converted into a Sesame Graph.
   * @return A Sesame Graph if successful, otherwise null.
   */
  public static Graph rdfJsonToGraph(String json) {
    Graph result = new GraphImpl();
    ValueFactory vf = result.getValueFactory();

    try {
      JSONObject input = new JSONObject(json);
      Iterator<String> subjects = input.keys();
      while (subjects.hasNext()) {
        String subjStr = subjects.next();
        Resource subject = null;
        subject =
            subjStr.startsWith("_:") ? vf.createBNode(subjStr.substring(2)) : vf.createURI(subjStr);
        JSONObject pObj = input.getJSONObject(subjStr);
        Iterator<String> predicates = pObj.keys();
        while (predicates.hasNext()) {
          String predStr = predicates.next();
          URI predicate = vf.createURI(predStr);
          JSONArray predArr = pObj.getJSONArray(predStr);
          for (int i = 0; i < predArr.length(); i++) {
            Value object = null;
            JSONObject obj = predArr.getJSONObject(i);
            if (!obj.has("value")) {
              continue;
            }
            String value = obj.getString("value");
            if (!obj.has("type")) {
              continue;
            }
            String type = obj.getString("type");
            String lang = null;
            if (obj.has("lang")) {
              lang = obj.getString("lang");
            }
            String datatype = null;
            if (obj.has("datatype")) {
              datatype = obj.getString("datatype");
            }
            if ("literal".equals(type)) {
              if (lang != null) {
                object = vf.createLiteral(value, lang);
              } else if (datatype != null) {
                object = vf.createLiteral(value, vf.createURI(datatype));
              } else {
                object = vf.createLiteral(value);
              }
            } else if ("bnode".equals(type)) {
              object = vf.createBNode(value.substring(2));
            } else if ("uri".equals(type)) {
              object = vf.createURI(value);
            }

            if (obj.has("graphs")) {
              JSONArray a = obj.getJSONArray("graphs");
              // System.out.println("a.length() = " + a.length());
              for (int j = 0; j < a.length(); j++) {
                // Note: any nulls here will result in statements in the default context.
                String s = a.getString(j);
                Resource context = s.equals("null") ? null : vf.createURI(s);
                // System.out.println("context = " + context);
                result.add(subject, predicate, object, context);
              }
            } else {
              result.add(subject, predicate, object);
            }
          }
        }
      }
    } catch (JSONException e) {
      log.error(e.getMessage(), e);
      return null;
    }

    return result;
  }