<commit_before><?php
/**
 * DBSteward XML definition file parsing
 *
 * @package DBSteward
 * @license http://www.opensource.org/licenses/bsd-license.php Simplified BSD License
 * @author <NAME> <<EMAIL>>
 */

class xml_parser {

  private function __construct() {
    // this class is not to be instanced
    // use the static methods only
  }

  /**
   * Composite a list of XML files into one dbsteward definition
   * @NOTICE: only 'base' XML files, those listed in the $files list should contain includeFile entries
   *
   * @param  string    $output_prefix
   * @param  array     $files array list of files to composite
   *
   * @return string    XML files contents, composited
   */
  public static function xml_composite($output_prefix, $files, &$composite_file) {
    $file_list = '';
    $composite = new SimpleXMLElement('<dbsteward></dbsteward>');

    for ($i = 0; $i < count($files); $i++) {
      $file_name = $files[$i];
      $file_list .= $file_name . ' ';
      dbsteward::console_line(1, "Loading XML " . realpath($file_name) . "..");
      $xml_contents = file_get_contents($file_name);
      if ($xml_contents === FALSE) {
        throw new exception("Failed to load XML from disk: " . $file_name);
      }
      $doc = simplexml_load_string($xml_contents);
      if ($doc === FALSE) {
        throw new Exception("failed to simplexml_load_string() contents of " . $file_name);
      }
      $doc = xml_parser::expand_tabrow_data($doc);
      $doc = xml_parser::sql_format_convert($doc);
      $xml_contents = $doc->saveXML();

      // only validate the first composite in the chain before composite has been completed
      if ($i == 0) {
        self::validate_xml($xml_contents);
      }

      dbsteward::console_line(1, "Compositing XML File " . $file_name);
      
      // if doc defines the database element
      // composite it first, to adhere to the DTD
      $doc_clone = NULL;
      if ( isset($doc->database) ) {
        $doc_clone = clone $doc;
        $doc_clone_child_nodes = $doc_clone->children();
        foreach ($doc_clone_child_nodes AS $doc_clone_child_node) {
          $doc_clone_child_names[] = $doc_clone_child_node->getName();
        }
        foreach ($doc_clone_child_names AS $doc_clone_child_name) {
          if ( strcasecmp($doc_clone_child_name, 'inlineAssembly') == 0 ) {
            // need to keep this definition at the top for DTD compliance
            // this is for MSSQL assembly support
          }
          else if ( strcasecmp($doc_clone_child_name, 'database') == 0 ) {
            // include database element and it's children first, to maintain adherence to the DTD
          }
          else {
            unset($doc_clone->{$doc_clone_child_name});
          }
        }
        // these element trees no longer need overlaid from $doc
        unset($doc->inlineAssembly);
        unset($doc->database);

        // if not defined in the composite yet
        if ( !isset($composite->database) ) {
          //add database node to the composite so it will be in the right natural order position
          $composite->addChild('database');
        }
      }

      // do doc includes first, to allow definer to overwrite included file values at the same level
      while (isset($doc->includeFile)) {
        foreach ($doc->includeFile AS $includeFile) {
          $include_file_name = (string)($includeFile['name']);
          // if include_file_name does not appear to be absolute, make it relative to its parent
          if (substr($include_file_name, 0, 1) != '/') {
            $include_file_name = dirname($file_name) . '/' . $include_file_name;
          }
          dbsteward::console_line(1, "Compositing XML includeFile " . $include_file_name);
          $include_doc = simplexml_load_file($include_file_name);
          if ($include_doc === FALSE) {<commit_msg>fix for mssql10 inline assembly creation<commit_after><?php
/**
 * DBSteward XML definition file parsing
 *
 * @package DBSteward
 * @license http://www.opensource.org/licenses/bsd-license.php Simplified BSD License
 * @author <NAME> <<EMAIL>>
 */

class xml_parser {

  private function __construct() {
    // this class is not to be instanced
    // use the static methods only
  }

  /**
   * Composite a list of XML files into one dbsteward definition
   * @NOTICE: only 'base' XML files, those listed in the $files list should contain includeFile entries
   *
   * @param  string    $output_prefix
   * @param  array     $files array list of files to composite
   *
   * @return string    XML files contents, composited
   */
  public static function xml_composite($output_prefix, $files, &$composite_file) {
    $file_list = '';
    $composite = new SimpleXMLElement('<dbsteward></dbsteward>');

    for ($i = 0; $i < count($files); $i++) {
      $file_name = $files[$i];
      $file_list .= $file_name . ' ';
      dbsteward::console_line(1, "Loading XML " . realpath($file_name) . "..");
      $xml_contents = file_get_contents($file_name);
      if ($xml_contents === FALSE) {
        throw new exception("Failed to load XML from disk: " . $file_name);
      }
      $doc = simplexml_load_string($xml_contents);
      if ($doc === FALSE) {
        throw new Exception("failed to simplexml_load_string() contents of " . $file_name);
      }
      $doc = xml_parser::expand_tabrow_data($doc);
      $doc = xml_parser::sql_format_convert($doc);
      $xml_contents = $doc->saveXML();

      // only validate the first composite in the chain before composite has been completed
      if ($i == 0) {
        self::validate_xml($xml_contents);
      }

      dbsteward::console_line(1, "Compositing XML File " . $file_name);
      
      // if doc defines the database element
      // composite it first, to adhere to the DTD
      $doc_clone = NULL;
      if ( isset($doc->database) ) {
        $doc_clone = clone $doc;
        $doc_clone_child_nodes = $doc_clone->children();
        foreach ($doc_clone_child_nodes AS $doc_clone_child_node) {
          $doc_clone_child_names[] = $doc_clone_child_node->getName();
        }
        foreach ($doc_clone_child_names AS $doc_clone_child_name) {
          if ( strcasecmp($doc_clone_child_name, 'inlineAssembly') == 0 ) {
            // need to keep this definition at the top for DTD compliance
            // this is for MSSQL assembly support
          }
          else if ( strcasecmp($doc_clone_child_name, 'database') == 0 ) {
            // include database element and it's children first, to maintain adherence to the DTD
          }
          else {
            unset($doc_clone->{$doc_clone_child_name});
          }
        }
        // these elements do not need mainline overlaid from $doc
        unset($doc->inlineAssembly);
        unset($doc->database);
      }
      
      // if doc_clone is defined, $doc->database was found, add it after inlineAssembly entries
      // and if there were inlineAssembly elements included,
      // composite these inlineAssembly elements in very first
      if ( $doc_clone ) {
        if ( isset($doc_clone->inlineAssembly) ) {
          $doc_clone_ila = clone $doc_clone;
          unset($doc_clone_ila->database);
          self::xml_composite_children($composite, $doc_clone_ila);
        }
        
        // if not defined in the composite yet
        if ( !isset($composite->database) ) {
          //add database node to the composite so it will be in the right natural order position
          $composite->addChild('database');
        }
      }

      // do doc includes first, to allow definer to overwrite included file values at the same level
      while (isset($doc->includeFile)) {
        foreach ($doc->includeFile AS $includeFile) {
          $include_file_name = (string)($includeFile['name']);
          // if include_file_name does not appear to be absolute, make it relative to its parent
          if (substr($include_file_name, 0, 1) != '/') {
            $include_file_name = dirname($file_name) . '/' . $include_file_name;
          }
          dbsteward::console_line(1, "Compositing XML includeFile " . $include_file_name);
          $include_doc = simplexml_load_file($include_file_name);
          if ($include_doc === FALSE) {
{'fraction_non_alphanumeric': 0.11386861313868613, 'fraction_numerical': 0.0024330900243309003, 'mean_word_length': 2.596237970253718, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 27}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2733881', 'n_tokens_mistral': 2381, 'n_tokens_neox': 2360, 'n_words': 683}