<commit_before># frozen_string_literal: true

require 'libis/ingester'
require_relative 'base/xml_parser'

require 'fileutils'

module Libis
  module Ingester

    class AcpParser < Libis::Ingester::Task
      attr_accessor :element_stack, :element_path

      taskgroup :collector
      description 'Parses an extracted ACP file and collects IE from that'

      help <<-STR.align_left
        This collector needs an extracted ACP file. The given XML file - normally the file in the root of the exported
        ACP file - will be parsed and for each <abs:isadStuk> entry an IE will be created.
      STR

      parameter acp_dir: nil,
                description: 'The folder where the ACP (Alfresco Content Package) export file was extracted.'

      parameter item_types: [Libis::Ingester::Run], frozen: true

      protected

      # @param [Libis::Ingester::Run] item
      def process(item)
        unless Dir.exist?(parameter(:acp_dir))
          raise Libis::WorkflowAbort, "ACP directory '#{parameter(:acp_dir)}' cannot not be found."
        end

        xml_files = Dir.glob(File.join(parameter(:acp_dir), '*.xml'))
        unless xml_files.count == 1
          raise Libis::WorkflowAbort, "ACP directory should contain only 1 XML file."
        end

        xml_file = xml_files.first
        @element_stack = []
        @element_path = []
        ie = null
        Libis::Ingester::Base::XmlParser.new(xml_file) do |function, *args|
          case (function)
          when :start_element
            element = args[0]
            element_stack.push(element)
            attrs = args[1].reduce({}) {|r, x| r[x[0]] = x[1]; r}
            case element
            when /abs:isad(Archief|Domein|Subdomein|Rubriek|Reeks|Serie|Groep|Dossier)/
              element_path.push(child_name(attrs))
            when 'abs:isadStuk'
              ie = {}
              ie[:name] = child_name(attrs)
              ie[:path] = element_path.join('/')
            when 'cm:content'
              if 'cm:contains' == element_stack[-2]
                ie ||= {}
                ie[:name] = child_name(attrs)
                ie[:path] = element_path.join('/')<commit_msg>AcpParser: typo fix<commit_after># frozen_string_literal: true

require 'libis/ingester'
require_relative 'base/xml_parser'

require 'fileutils'

module Libis
  module Ingester

    class AcpParser < Libis::Ingester::Task
      attr_accessor :element_stack, :element_path

      taskgroup :collector
      description 'Parses an extracted ACP file and collects IE from that'

      help <<-STR.align_left
        This collector needs an extracted ACP file. The given XML file - normally the file in the root of the exported
        ACP file - will be parsed and for each <abs:isadStuk> entry an IE will be created.
      STR

      parameter acp_dir: nil,
                description: 'The folder where the ACP (Alfresco Content Package) export file was extracted.'

      parameter item_types: [Libis::Ingester::Run], frozen: true

      protected

      # @param [Libis::Ingester::Run] item
      def process(item)
        unless Dir.exist?(parameter(:acp_dir))
          raise Libis::WorkflowAbort, "ACP directory '#{parameter(:acp_dir)}' cannot not be found."
        end

        xml_files = Dir.glob(File.join(parameter(:acp_dir), '*.xml'))
        unless xml_files.count == 1
          raise Libis::WorkflowAbort, "ACP directory should contain only 1 XML file."
        end

        xml_file = xml_files.first
        @element_stack = []
        @element_path = []
        ie = nil
        Libis::Ingester::Base::XmlParser.new(xml_file) do |function, *args|
          case (function)
          when :start_element
            element = args[0]
            element_stack.push(element)
            attrs = args[1].reduce({}) {|r, x| r[x[0]] = x[1]; r}
            case element
            when /abs:isad(Archief|Domein|Subdomein|Rubriek|Reeks|Serie|Groep|Dossier)/
              element_path.push(child_name(attrs))
            when 'abs:isadStuk'
              ie = {}
              ie[:name] = child_name(attrs)
              ie[:path] = element_path.join('/')
            when 'cm:content'
              if 'cm:contains' == element_stack[-2]
                ie ||= {}
                ie[:name] = child_name(attrs)
                ie[:path] = element_path.join('/')
{'fraction_non_alphanumeric': 0.10716755012675731, 'fraction_numerical': 0.0032265498962894676, 'mean_word_length': 2.3054074638233053, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, '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': '4996692', 'n_tokens_mistral': 1319, 'n_tokens_neox': 1256, 'n_words': 347}