module LANSA::XML

Constants

DEFAULT_XML_DECL
DEFAULT_XML_ENCODING
DEFAULT_XML_STANDALONE
DEFAULT_XML_VERSION
LOCATION_ATTRIBUTE_NAME
XML_CONTENT_TYPE

Public Class Methods

transform(*args, trafo: nil, input: nil, output: nil, content_type: nil, direction: nil, parameters: nil, &block) click to toggle source
# File xml/transformer.rb, line 629
  def self.transform *args, trafo: nil, input: nil, output: nil, content_type: nil, direction: nil, parameters: nil, &block

    trafo0, input0, output0, content_type0 = *args

    trafo ||= trafo0
    input ||= input0 || $input || $stdin
    output ||= output0 || $output || $stdout
    content_type ||= content_type0 || $content_type || XML_CONTENT_TYPE

    direction ||= $direction || ''
    parameters ||= $parameters || {}

    writer = Writer.new(output, true, '  ')
    writer.xmldecl(*DEFAULT_XML_DECL) if content_type.start_with?(XML_CONTENT_TYPE)

    if trafo.nil? and block.nil?
      IO.copy_stream input, output
      return
    end

    transformer = Transformer.new(writer, content_type, parameters)
    if trafo
      transformer.instance_eval trafo
    elsif block
      transformer.instance_eval &block
    end

    # Delegate to a standard transformer as last fallback.
    # Note that the 'delegate' method will only create a delegate transformer,
    # if no delegate for the same class exists in the transformer or any of it's delegates!
    # This way we do not create 'multiple' transformers.
    case direction.downcase
    when 'inbound'
      require 'lansa/xml/pim-inbound-transformer'
      transformer.send(:delegate, PIM::XML::PimInboundTransformer)
    when 'outbound'
      # FIXME: Add standard outbound transformer!
#      require 'lansa/xml/pim-outbound-transformer'
#      transformer.send(:delegate, PIM::XML::PimOutboundTransformer)
    else
      PIM.log_warn "No 'direction' specified in call to 'transform'"
    end

    delegator = TransformerDelegator.new(transformer)
    delegator.start

    splitter = Splitter.new(delegator)

    parser = Nokogiri::XML::SAX::Parser.new(splitter)
    parser.parse(input) do |parser_context|

      # Enable 'recovery' mode, i.e. simply skip unparsable XML
      parser_context.recovery = true

      # Provide context to current transformer to have access to current line and column
      delegator.parser_context = parser_context

    end

    delegator.end

    return parameters

  ensure
    output.flush if output.respond_to?:flush
  end