You are here

class NodeExportXmlDecoder in Node export 7.3

Same name and namespace in other branches
  1. 6.3 modules/node_export_xml/node_export_xml.module \NodeExportXmlDecoder

Class for parsing Node export XML.

Hierarchy

Expanded class hierarchy of NodeExportXmlDecoder

File

formats/xml.inc, line 110
The Node export XML format handler.

View source
class NodeExportXmlDecoder {
  var $stack;
  var $output;
  function decode($code_string) {
    $parser = xml_parser_create();
    xml_set_element_handler($parser, array(
      &$this,
      'start_handler',
    ), array(
      &$this,
      'end_handler',
    ));
    xml_set_character_data_handler($parser, array(
      &$this,
      'data_handler',
    ));
    $this->stack = array(
      array(
        'name' => 'node_export',
        'attributes' => array(),
        'children' => array(),
        'data' => '',
      ),
    );
    if (!xml_parse($parser, $code_string)) {
      $errors[] = "Node export XML import was unsuccessful, error details follow.  No nodes imported.";
      $line = xml_get_current_line_number($parser);
      $column = xml_get_current_column_number($parser);
      $error = xml_error_string(xml_get_error_code($parser));
      $errors[] = "Line " . $line . ", Column " . $column . ": " . $error;
      $lines = explode("\n", $code_string, $line + 1);
      $errors[] = "<pre>" . htmlspecialchars($lines[$line - 1]) . "</pre>";
      xml_parser_free($parser);
      return array(
        'success' => FALSE,
        'output' => $errors,
      );
    }
    xml_parser_free($parser);
    $tmp = $this
      ->build($this->stack[0]);
    if (count($tmp) == 1) {
      $this->output = array_pop($tmp);
    }
    else {
      $this->output = array();
    }
    unset($this->stack);
    return $this->output;
  }
  function build($stack) {
    $result = array();
    if (count($stack['children']) > 0) {
      $keycount = array();
      foreach ($stack['children'] as $child) {
        $keycount[] = $child['name'];
      }
      if (count(array_unique($keycount)) != count($keycount)) {

        // Enumerated array.
        $children = array();
        foreach ($stack['children'] as $child) {
          $children[] = $this
            ->build($child);
        }
      }
      else {

        // Associative array.
        $children = array();
        foreach ($stack['children'] as $child) {
          if (!empty($stack['attributes']['_NUMERIC_KEYS'])) {
            $child['name'] = intval(substr($child['name'], 1));
          }
          $children[$child['name']] = $this
            ->build($child);
        }
      }
      $result = array_merge($result, $children);
    }
    if (count($result) == 0) {

      // An atomic value.
      $return = trim($stack['data']);
      if (isset($stack['attributes']['TYPE'])) {
        if ($stack['attributes']['TYPE'] == 'boolean') {
          return trim($stack['data']) == 'TRUE' ? TRUE : FALSE;
        }
        elseif ($stack['attributes']['TYPE'] == 'NULL') {
          return NULL;
        }
      }
      elseif (!empty($stack['attributes']['_NUMERIC_KEYS'])) {
        return array();
      }
      return htmlspecialchars_decode(trim($stack['data']));
    }
    else {

      // An array or object.
      if (isset($stack['attributes']['CLASS'])) {
        $object = new $stack['attributes']['CLASS']();
        foreach ($result as $k => $v) {
          $object->{$k} = $v;
        }
        $result = $object;
      }
      return $result;
    }
  }
  function start_handler($parser, $name, $attributes = array()) {
    $token = array();
    $token['name'] = strtolower($name);
    $token['attributes'] = $attributes;
    $token['data'] = '';
    $token['children'] = array();
    $this->stack[] = $token;
  }
  function end_handler($parser, $name, $attributes = array()) {
    $token = array_pop($this->stack);
    $this->stack[count($this->stack) - 1]['children'][] = $token;
  }
  function data_handler($parser, $data) {
    $this->stack[count($this->stack) - 1]['data'] .= $data;
  }

}

Members