You are here

function ARC_sparql_parser::substitute_strings in Taxonomy import/export via XML 6.2

Same name and namespace in other branches
  1. 5.2 arc/ARC_sparql_parser.php \ARC_sparql_parser::substitute_strings()
  2. 5 arc/ARC_sparql_parser.php \ARC_sparql_parser::substitute_strings()
  3. 6 arc/ARC_sparql_parser.php \ARC_sparql_parser::substitute_strings()
1 call to ARC_sparql_parser::substitute_strings()
ARC_sparql_parser::parse in arc/ARC_sparql_parser.php

File

arc/ARC_sparql_parser.php, line 275

Class

ARC_sparql_parser

Code

function substitute_strings($val = "") {
  $result = "";
  $delims = array(
    "d1" => '"""',
    "d2" => "'''",
    "d3" => "'",
    "d4" => '"',
    "d5" => '`',
    "d6" => "#",
  );
  $brs = array(
    "b1" => "\r\n",
    "b2" => "\r",
    "bn3" => "\n",
  );
  $cur_pos = 0;
  $val_length = strlen($val);
  $prefix = "_string_";
  $subs_count = 0;
  while ($cur_pos < $val_length) {

    /* find next string or comment start */
    $next_delim_pos = $val_length;
    $next_delim_name = "";
    foreach ($delims as $cur_delim_name => $cur_delim_code) {
      $cur_next_delim_pos = strpos($val, $cur_delim_code, $cur_pos);
      if ($cur_next_delim_pos !== false && $cur_next_delim_pos < $next_delim_pos) {
        $next_delim_pos = $cur_next_delim_pos;
        $next_delim_name = $cur_delim_name;
      }
    }
    if ($next_delim_name) {
      if ($next_delim_name === "d6") {

        /* comment */

        /* find next linebreak */
        $next_br_pos = $val_length;
        $next_br_name = "";
        foreach ($brs as $cur_br_name => $cur_br_code) {
          $cur_next_br_pos = strpos($val, $cur_br_code, $next_delim_pos);
          if ($cur_next_br_pos !== false && $cur_next_br_pos < $next_br_pos) {
            $next_br_pos = $cur_next_br_pos;
            $next_br_name = $cur_br_name;
          }
        }
        $result .= substr($val, $cur_pos, $next_delim_pos - $cur_pos);

        /* up to comment start */
        $cur_pos = $next_br_pos;
        $this->logs[] = "removed comment '" . substr($val, $next_delim_pos, $next_br_pos - $next_delim_pos) . "' in substitute_strings()";
      }
      else {

        /* literal start */

        /* find literal end */
        $next_delim_code = $delims[$next_delim_name];
        $next_end_pos = strpos($val, $next_delim_code, $next_delim_pos + strlen($next_delim_code));
        while (($cur_prev_char = substr($val, $next_end_pos - 1, 1)) && $cur_prev_char == "\\") {
          $next_end_pos = strpos($val, $next_delim_code, $next_end_pos + 1);
          if (!$next_end_pos || $next_end_pos == $val_length - 1) {
            $this->errors[] = "unterminated literal in substitute_strings()";
            $next_end_pos = $val_length;
            break;
          }
        }
        if ($next_end_pos) {
          $result .= substr($val, $cur_pos, $next_delim_pos - $cur_pos);
          $str_val = substr($val, $next_delim_pos + strlen($next_delim_code), $next_end_pos - (strlen($next_delim_code) + $next_delim_pos));

          /* expand iris */
          if (preg_match_all("/\\|(_iri_.+)\\|/U", $str_val, $matches)) {
            $iri_subs = $matches[1];
            foreach ($iri_subs as $cur_subs) {
              if ($iri = $this->iri_placeholders[$cur_subs]) {
                $str_val = str_replace("|" . $cur_subs . "|", "<" . $iri . ">", $str_val);
              }
            }
          }

          //$this->logs[]="substituted '".$str_val."' with ".$prefix.$subs_count;
          $this->str_placeholders[$prefix . $subs_count] = array(
            "delim_code" => $next_delim_code,
            "val" => $str_val,
          );
          $result .= $prefix . $subs_count;
          $cur_pos = $next_end_pos + strlen($next_delim_code);
          $subs_count++;
        }
        else {
          $this->errors[] = "unterminated literal in substitute_strings()";
          $result .= substr($val, $cur_pos);
          $cur_pos = $val_length;
        }
      }
    }
    else {
      $result .= substr($val, $cur_pos);
      $cur_pos = $val_length;
    }
  }
  return $result;
}