Example #1
0
  /**
   * The template value of the term map for a given logical table row is determined as follows.
   *
   * @param stringTemplate
   * @param dbValues
   * @param dbTypes
   * @return
   * @throws R2RMLDataError
   * @throws SQLException
   * @throws UnsupportedEncodingException
   * @throws MalformedURLException
   */
  public static String extractColumnValueFromStringTemplate(
      String stringTemplate, Map<ColumnIdentifier, byte[]> dbValues, ResultSetMetaData dbTypes)
      throws R2RMLDataError, SQLException, UnsupportedEncodingException {
    // Let result be the template string

    String result = stringTemplate;
    if (dbValues == null) return null;
    for (ColumnIdentifier dbValue : dbValues.keySet()) {
      // For each pair of unescaped curly braces in result: if value is
      // NULL, then return NULL
      if (dbValues.get(dbValue) == null) return null;
    }
    for (ColumnIdentifier column : dbValues.keySet()) {
      // Extract db value
      byte[] byteValue = dbValues.get(column);
      // Extract RDF Natural form
      SQLType sqlType = column.getSqlType();
      // Apply cast to string to the SQL data value
      String value;
      if (sqlType != null) {
        XSDType xsdType = SQLToXMLS.getEquivalentType(sqlType);
        value = XSDLexicalTransformation.extractNaturalRDFFormFrom(xsdType, byteValue);
      } else {
        value = new String(byteValue, "UTF-8");
      }
      result = column.replaceAll(result, getIRISafeVersion(value));
      // Test backslashes result =
      /*
       * result.replaceAll("\\{\\\"" + column + "\\\"\\}",
       * getIRISafeVersion(result)); result = result.replaceAll("\\{\\'" +
       * column + "\\'\\}", getIRISafeVersion(result)); result =
       * result.replaceAll("\\{\\`" + column + "\\`\\}",
       * getIRISafeVersion(result));
       */

    }
    // Curly braces that do not enclose column names MUST be
    // escaped by a backslash character (“\”).
    result = result.replaceAll("\\\\\\{", "{");
    result = result.replaceAll("\\\\\\}", "}");

    return result;
  }