You are here

public function EndNoteXMLParser::parse in Bibliography Module 7

Same name and namespace in other branches
  1. 6.2 modules/endnote/endnote_xml_parser.inc \EndNoteXMLParser::parse()
  2. 7.2 modules/endnote/endnote_xml_parser.inc \EndNoteXMLParser::parse()

File

modules/endnote/endnote_xml_parser.inc, line 34
endnote_xml_parser.inc

Class

EndNoteXMLParser

Code

public function parse($file, $terms, $batch, $session_id) {
  $this->terms = $terms;
  $this->batch_proc = $batch;
  $this->session_id = $session_id;
  $this->nids = array();
  $this->dups = array();
  $this->unmapped = array();
  if (!($fp = fopen($file->uri, "r"))) {
    drupal_set_message(t("could not open XML input"), 'error');
    return;
  }
  $data = fread($fp, 2048);
  if (strpos($data, 'record') !== FALSE && strpos($data, 'ref-type') !== FALSE) {
    $this->format = 'endnote8';
  }
  elseif (strpos($data, 'RECORD') !== FALSE && strpos($data, 'REFERENCE_TYPE') !== FALSE) {
    $this->format = 'endnote7';
  }
  if ($this->format) {
    $this->parser = drupal_xml_parser_create($data);
    xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, FALSE);
    xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, TRUE);
    xml_set_object($this->parser, $this);
    xml_set_element_handler($this->parser, $this->format . '_startElement', $this->format . '_endElement');
    xml_set_character_data_handler($this->parser, $this->format . '_characterData');
    xml_parse($this->parser, $data, feof($fp));
    while ($data = fread($fp, 2048)) {

      // $data = fread($fp, 2048);.
      set_time_limit(300);
      if (!xml_parse($this->parser, $data, feof($fp))) {
        drupal_set_message(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($this->parser)), xml_get_current_line_number($this->parser)), 'error');
      }
    }
    xml_parser_free($this->parser);
  }
  fclose($fp);
  if (!empty($this->unmapped)) {
    $ignored_tags = array_unique($this->unmapped);
    $message = t("The following elements were ignored because they do not map to any biblio fields:") . ' ';
    $message .= implode(', ', $ignored_tags);
    if (user_access('administer biblio')) {
      $message .= '. ' . t('Click !url if you wish to check the field mapping', array(
        '!url' => l(t('here'), 'admin/config/content/biblio/iomap/edit/' . $this->format),
      ));
    }
    drupal_set_message($message, 'warning');
  }
  return array(
    $this->nids,
    $this->dups,
  );
}