예제 #1
0
    public Vertex addVertex(final Value value) {
      Vertex v = graph.addVertex(null);

      if (value instanceof URI) {
        v.setProperty(KIND, URI);
        v.setProperty(VALUE, value.stringValue());
      } else if (value instanceof Literal) {
        Literal l = (Literal) value;
        v.setProperty(KIND, LITERAL);
        v.setProperty(VALUE, l.getLabel());
        if (null != l.getDatatype()) {
          v.setProperty(TYPE, l.getDatatype().stringValue());
        }
        if (null != l.getLanguage()) {
          v.setProperty(LANG, l.getLanguage());
        }
      } else if (value instanceof BNode) {
        BNode b = (BNode) value;
        v.setProperty(KIND, BNODE);
        v.setProperty(VALUE, b.getID());
      } else {
        throw new IllegalStateException("value of unexpected type: " + value);
      }

      return v;
    }
예제 #2
0
 @Override
 public IV get(final IBindingSet bs) {
   URI datatype = null;
   String lang = null;
   boolean allSame = true;
   final StringBuilder sb = new StringBuilder();
   for (int i = 0; i < arity(); i++) {
     @SuppressWarnings("rawtypes")
     final IV v = getAndCheckLiteral(i, bs);
     String label = null;
     if (allSame) {
       final Literal lit = asLiteral(v);
       label = lit.getLabel();
       if (lit.getDatatype() != null) {
         if (lang != null) {
           allSame = false;
         } else if (datatype == null) {
           if (i == 0) {
             datatype = lit.getDatatype();
           } else {
             allSame = false;
           }
         } else if (!datatype.equals(lit.getDatatype())) {
           allSame = false;
         }
       } else if (lit.getLanguage() != null) {
         if (datatype != null) {
           allSame = false;
         } else if (lang == null) {
           if (i == 0) {
             lang = lit.getLanguage();
           } else {
             allSame = false;
           }
         } else if (!lang.equals(lit.getLanguage())) {
           allSame = false;
         }
       } else {
         allSame = false;
       }
     } else {
       label = literalLabel(v);
     }
     sb.append(label);
   }
   if (allSame) {
     if (datatype != null) {
       return super.asIV(getValueFactory().createLiteral(sb.toString(), datatype), bs);
     } else if (lang != null) {
       return super.asIV(getValueFactory().createLiteral(sb.toString(), lang), bs);
     }
   }
   return super.asIV(getValueFactory().createLiteral(sb.toString()), bs);
 }
예제 #3
0
    public String literalToNative(final Literal literal) {
      URI datatype = literal.getDatatype();

      if (null == datatype) {
        String language = literal.getLanguage();

        if (null == language) {
          return GraphSail.PLAIN_LITERAL_PREFIX + GraphSail.SEPARATOR + literal.getLabel();
        } else {
          return GraphSail.LANGUAGE_TAG_LITERAL_PREFIX
              + GraphSail.SEPARATOR
              + language
              + GraphSail.SEPARATOR
              + literal.getLabel();
        }
      } else {
        // FIXME
        return ""
            + GraphSail.TYPED_LITERAL_PREFIX
            + GraphSail.SEPARATOR
            + datatype
            + GraphSail.SEPARATOR
            + literal.getLabel();
      }
    }
예제 #4
0
  private Pair toPair(Edge e, Graph graph) {
    URI predicate = e.getPredicate();
    Value object = e.getObject();
    String value = null;
    if (object instanceof Literal) {
      Literal literal = (Literal) object;
      String language = literal.getLanguage();
      URI type = literal.getDatatype();
      if (type.equals(XMLSchema.STRING)) {
        type = null;
      }
      StringBuilder builder = new StringBuilder();
      builder.append('"');
      builder.append(literal.getLabel());
      builder.append('"');
      if (language != null) {
        builder.append('@');
        builder.append(language);
      } else if (type != null) {
        builder.append('^');
        builder.append(type.stringValue());
      }
      value = builder.toString();
    } else if (object instanceof URI) {
      value = object.stringValue();
    } else {
      Resource id = (Resource) object;
      Vertex v = graph.getVertex(id);
      value = createHash(predicate, v);
    }

    return new Pair(predicate, value);
  }
예제 #5
0
  /**
   * Creates an NativeLiteral that is equal to the supplied literal. This method returns the
   * supplied literal itself if it is already a NativeLiteral that has been created by this
   * ValueStore, which prevents unnecessary object creations.
   *
   * @return A NativeLiteral for the specified literal.
   */
  public NativeLiteral getNativeLiteral(Literal l) {
    if (isOwnValue(l)) {
      return (NativeLiteral) l;
    }

    if (Literals.isLanguageLiteral(l)) {
      return new NativeLiteral(revision, l.getLabel(), l.getLanguage().get());
    } else {
      NativeIRI datatype = getNativeURI(l.getDatatype());
      return new NativeLiteral(revision, l.getLabel(), datatype);
    }
  }
예제 #6
0
 private Value copy(Value value) {
   if (value instanceof IRI) {
     return createIRI(value.stringValue());
   } else if (value instanceof Literal) {
     Literal lit = (Literal) value;
     if (Literals.isLanguageLiteral(lit)) {
       return createLiteral(value.stringValue(), lit.getLanguage().orElse(null));
     } else {
       return createLiteral(value.stringValue(), lit.getDatatype());
     }
   } else {
     return createBNode(value.stringValue());
   }
 }
  private Literal getOptionalObjectLiteral(
      Model model, Resource subject, URI predicate, String lang) {
    Set<Value> objects = GraphUtil.getObjects(model, subject, predicate);

    Literal result = null;

    for (Value nextValue : objects) {
      if (nextValue instanceof Literal) {
        final Literal literal = (Literal) nextValue;
        if (result == null || (lang != null && lang.equals(literal.getLanguage()))) {
          result = literal;
        }
      }
    }
    return result;
  }
예제 #8
0
  @Override
  public IV get(final IBindingSet bs) throws SparqlTypeErrorException {

    final IV iv = getAndCheckLiteral(0, bs);

    final IV datatype = getAndCheckBound(1, bs);

    if (!datatype.isURI()) throw new SparqlTypeErrorException();

    final BigdataURI dt = (BigdataURI) asValue(datatype);

    final Literal lit = asLiteral(iv);

    if (lit.getDatatype() != null || lit.getLanguage() != null) {
      throw new SparqlTypeErrorException();
    }

    final String label = lit.getLabel();

    final BigdataLiteral str = getValueFactory().createLiteral(label, dt);

    return super.asIV(str, bs);
  }
예제 #9
0
 private Object encodeLiteral(final Literal literal) {
   final URI datatype = literal.getDatatype();
   if (datatype == null || datatype.equals(XMLSchema.STRING)) {
     final String language = literal.getLanguage();
     if (language == null) {
       return literal.getLabel();
     } else {
       return SerializerAvro.newGenericRecord(Schemas.STRING_LANG, literal.getLabel(), language);
     }
   } else if (datatype.equals(XMLSchema.BOOLEAN)) {
     return literal.booleanValue();
   } else if (datatype.equals(XMLSchema.LONG)) {
     return literal.longValue();
   } else if (datatype.equals(XMLSchema.INT)) {
     return literal.intValue();
   } else if (datatype.equals(XMLSchema.DOUBLE)) {
     return literal.doubleValue();
   } else if (datatype.equals(XMLSchema.FLOAT)) {
     return literal.floatValue();
   } else if (datatype.equals(XMLSchema.SHORT)) {
     return SerializerAvro.newGenericRecord(Schemas.SHORT, literal.intValue());
   } else if (datatype.equals(XMLSchema.BYTE)) {
     return SerializerAvro.newGenericRecord(Schemas.BYTE, literal.intValue());
   } else if (datatype.equals(XMLSchema.INTEGER)) {
     return SerializerAvro.newGenericRecord(Schemas.BIGINTEGER, literal.stringValue());
   } else if (datatype.equals(XMLSchema.DECIMAL)) {
     return SerializerAvro.newGenericRecord(Schemas.BIGDECIMAL, literal.stringValue());
   } else if (datatype.equals(XMLSchema.DATETIME)) {
     final XMLGregorianCalendar calendar = literal.calendarValue();
     return SerializerAvro.newGenericRecord(
         Schemas.CALENDAR,
         calendar.getTimezone(),
         calendar.toGregorianCalendar().getTimeInMillis());
   }
   throw new IllegalArgumentException("Unsupported literal: " + literal);
 }
예제 #10
0
 /**
  * Implementation using the org.json API.
  *
  * @param graph A Sesame Graph.
  * @return An RDF/JSON string if successful, otherwise null.
  */
 public static String graphToRdfJson(Graph graph) {
   JSONObject result = new JSONObject();
   try {
     Set<Resource> subjects = new HashSet<Resource>();
     for (Statement s1 : graph) {
       subjects.add(s1.getSubject());
     }
     for (Resource subject : subjects) {
       JSONObject predicateObj = new JSONObject();
       Set<URI> predicates = new HashSet<URI>();
       Iterator<Statement> s2 = graph.match(subject, null, null);
       while (s2.hasNext()) {
         predicates.add(s2.next().getPredicate());
       }
       for (URI predicate : predicates) {
         JSONArray valueArray = new JSONArray();
         Iterator<Statement> stmnts = graph.match(subject, predicate, null);
         Set<Value> objects = new HashSet<Value>();
         while (stmnts.hasNext()) {
           objects.add(stmnts.next().getObject());
         }
         for (Value object : objects) {
           Iterator<Statement> stmnts2 = graph.match(subject, predicate, object);
           JSONArray contexts = new JSONArray();
           int i = 0;
           boolean nonDefaultContext = false;
           while (stmnts2.hasNext()) {
             Resource context = stmnts2.next().getContext();
             contexts.put(i, null == context ? null : context.toString());
             if (null != context) {
               nonDefaultContext = true;
             }
             i++;
           }
           JSONObject valueObj = new JSONObject();
           valueObj.put("value", object.stringValue());
           if (object instanceof Literal) {
             valueObj.put("type", "literal");
             Literal l = (Literal) object;
             if (l.getLanguage() != null) {
               valueObj.put("lang", l.getLanguage());
             } else if (l.getDatatype() != null) {
               valueObj.put("datatype", l.getDatatype().stringValue());
             }
           } else if (object instanceof BNode) {
             valueObj.put("type", "bnode");
           } else if (object instanceof URI) {
             valueObj.put("type", "uri");
           }
           if (nonDefaultContext) {
             valueObj.put("graphs", contexts);
           }
           valueArray.put(valueObj);
         }
         predicateObj.put(predicate.stringValue(), valueArray);
       }
       result.put(subject.stringValue(), predicateObj);
     }
     return result.toString(2);
   } catch (JSONException e) {
     log.error(e.getMessage(), e);
   }
   return null;
 }
예제 #11
0
  public HashMap<String, Properties> generateResourceBundle(String baseName)
      throws GenerationException {
    // be sure to have at least the default URI constant settings
    if (uriGeneration == null) {
      uriGeneration = GenerationSetting.createDefault(caseFormat, "", "");
    }
    Pattern pattern = Pattern.compile(Pattern.quote(getPrefix()) + "(.+)");
    HashMap<String, URI> splitUris = new HashMap<>();
    for (Resource nextSubject : model.subjects()) {
      if (nextSubject instanceof URI) {
        Matcher matcher = pattern.matcher(nextSubject.stringValue());
        if (matcher.find()) {
          String k = matcher.group(1);
          splitUris.put(k, (URI) nextSubject);
        }
      }
    }

    List<String> keys = new ArrayList<>();
    keys.addAll(splitUris.keySet());
    Collections.sort(keys, String.CASE_INSENSITIVE_ORDER);

    HashMap<String, Properties> bundles = new HashMap<>();
    // Default we have for sure
    bundles.put(baseName, new Properties());
    for (String key : keys) {
      final URI resource = splitUris.get(key);
      //
      String nextKey = cleanKey(doCaseFormatting(key, uriGeneration.getCaseFormat()));

      for (URI p : LABEL_PROPERTIES) {
        for (Value v : GraphUtil.getObjects(model, resource, p)) {
          if (v instanceof Literal) {
            final Literal lit = (Literal) v;
            final String lang = lit.getLanguage();
            final Properties bundle;
            if (lang == null) {
              bundle = bundles.get(baseName);
            } else if (bundles.containsKey(baseName + "_" + lang)) {
              bundle = bundles.get(baseName + "_" + lang);
            } else {
              bundle = new Properties();
              bundles.put(baseName + "_" + lang, bundle);
            }

            if (!bundle.containsKey(nextKey + ".label")) {
              bundle.put(nextKey + ".label", lit.getLabel().replaceAll("\\s+", " "));
            }
          }
        }
      }

      for (URI p : COMMENT_PROPERTIES) {
        for (Value v : GraphUtil.getObjects(model, resource, p)) {
          if (v instanceof Literal) {
            final Literal lit = (Literal) v;
            final String lang = lit.getLanguage();
            final Properties bundle;
            if (lang == null) {
              bundle = bundles.get(baseName);
            } else if (bundles.containsKey(baseName + "_" + lang)) {
              bundle = bundles.get(baseName + "_" + lang);
            } else {
              bundle = new Properties();
              bundles.put(baseName + "_" + lang, bundle);
            }

            if (!bundle.containsKey(nextKey + ".comment")) {
              bundle.put(nextKey + ".comment", lit.getLabel().replaceAll("\\s+", " "));
            }
          }
        }
      }
    }

    if (getPreferredLanguage() != null) {
      log.debug("completing default Bundle with preferred language {}", getPreferredLanguage());
      final Properties defaultBundle = bundles.get(baseName);
      final Properties prefBundle = bundles.get(baseName + "_" + getPreferredLanguage());
      if (prefBundle != null) {
        for (Entry<Object, Object> key : prefBundle.entrySet()) {
          String nextKey = (String) key.getKey();
          if (!defaultBundle.containsKey(nextKey)) {
            log.trace("copying {} from {} to default Bundle", nextKey, getPreferredLanguage());
            defaultBundle.setProperty(nextKey, (String) key.getValue());
          }
        }
      } else {
        log.warn("No Bundle data found for preferred language {}", getPreferredLanguage());
      }
    }
    return bundles;
  }
예제 #12
0
 private byte[] literal2legacy(Literal literal) throws IOException {
   IRI dt = literal.getDatatype();
   if (XMLSchema.STRING.equals(dt) || RDF.LANGSTRING.equals(dt))
     return literal2data(literal.getLabel(), literal.getLanguage(), null, false);
   return literal2data(literal.getLabel(), literal.getLanguage(), dt, false);
 }
예제 #13
0
 private byte[] literal2data(Literal literal, boolean create) throws IOException {
   return literal2data(literal.getLabel(), literal.getLanguage(), literal.getDatatype(), create);
 }