Beispiel #1
0
  // returns list of statements
  protected static void replaceMultSPO(
      Statement st, NodeFactory f, Map o2n, Collection result, RDFNode toReplace, int position)
      throws ModelException {

    Collection replacements;

    if (toReplace instanceof Statement) {

      List l = new ArrayList();
      replaceMult((Statement) toReplace, f, o2n, l);

      if (l.size() == 1 && toReplace == l.get(0)) {
        result.add(st);
        return; // keep the same
      } else replacements = l;

    } else {

      Object ro = o2n.get(toReplace);

      if (ro instanceof Collection) replacements = (Collection) ro;
      else if (ro != null) {

        replacements = new ArrayList();
        replacements.add(ro);

      } else { // no replacement needed

        result.add(st); // keep the same statement
        return;
      }
    }

    for (Iterator it = replacements.iterator(); it.hasNext(); ) {

      Statement rs = null;
      Object rr = it.next();

      switch (position) {
        case 0:
          rs = f.createStatement((Resource) rr, st.predicate(), st.object());
          break;
        case 1:
          rs = f.createStatement(st.subject(), (Resource) rr, st.object());
          break;
        case 2:
          rs = f.createStatement(st.subject(), st.predicate(), (RDFNode) rr);
          break;
      }
      result.add(rs);
    }
  }
Beispiel #2
0
  public static Statement replaceNamespace(
      Statement st, String o, String n, NodeFactory f, Map o2n, Set resourcesToIgnore)
      throws ModelException {

    boolean replaced = false;
    Resource subj = st.subject();
    Resource pred = st.predicate();
    RDFNode obj = st.object();

    if (obj instanceof Resource
        && !(obj instanceof Statement)
        && o.equals(((Resource) obj).getNamespace())
        && (resourcesToIgnore == null || !resourcesToIgnore.contains(obj))) {

      replaced = true;
      Resource r = f.createResource(n, ((Resource) obj).getLocalName());
      if (o2n != null) o2n.put(obj, r);
      obj = r;
    }

    if (o.equals(subj.getNamespace())
        && (resourcesToIgnore == null || !resourcesToIgnore.contains(subj))) {

      replaced = true;
      Resource r = f.createResource(n, subj.getLocalName());
      if (o2n != null) o2n.put(subj, r);
      subj = r;
    }

    if (o.equals(pred.getNamespace())
        && (resourcesToIgnore == null || !resourcesToIgnore.contains(pred))) {

      replaced = true;
      Resource r = f.createResource(n, pred.getLocalName());
      if (o2n != null) o2n.put(pred, r);
      pred = r;
    }
    return replaced ? f.createStatement(subj, pred, obj) : st;
  }
Beispiel #3
0
  public static Statement replaceResources(Statement st, NodeFactory f, Map o2n)
      throws ModelException {

    boolean replaced = false;
    Resource subj = st.subject();
    Resource pred = st.predicate();
    RDFNode obj = st.object();

    Object n = null;

    if (obj instanceof Statement) {

      n = obj;
      obj = replaceResources((Statement) obj, f, o2n);
      replaced = n != obj;

    } else if ((n = o2n.get(obj)) != null) {
      replaced = true;
      obj = (RDFNode) n;
    }

    if (subj instanceof Statement) {

      n = subj;
      subj = replaceResources((Statement) subj, f, o2n);
      replaced = n != subj;
    }
    if ((n = o2n.get(subj)) != null) {
      replaced = true;
      subj = (Resource) n;
    }

    if ((n = o2n.get(pred)) != null) {
      replaced = true;
      pred = (Resource) n;
    }
    return replaced ? f.createStatement(subj, pred, obj) : st;
  }
Beispiel #4
0
  /**
   * @return a new model in which all occurrences of the old resources are replaced by the new ones.
   *     Returns number replacements done.
   */
  public static int replaceResource(Model m, Resource oldR, Resource newR) throws ModelException {

    Map map = new HashMap();
    map.put(oldR, newR);
    return replaceResources(m, map);
  }