class LANSA::XML::Transformer

Attributes

content_type[R]
error_messages[R]
intercept_depth[R]
output[R]
parameters[R]
split_element[R]
split_location[R]

Public Class Methods

new(writer, content_type = XML_CONTENT_TYPE, parameters = {}) click to toggle source
# File xml/transformer.rb, line 138
def initialize writer, content_type = XML_CONTENT_TYPE, parameters = {}

  @writer = writer
  @output = DocumentBuilder.new(writer, self)
  @content_type = content_type
  @parameters = parameters

  @matching = {}
  @context = {}
  @error_messages = []
  @sax2dom = SAX2DOM.new(false)

  @intercept_depth = -1

  @delegates = []
  init_delegates(*self.class.all_delegate_classes)

end

Protected Class Methods

add_match(matching, *names, &block) click to toggle source
# File xml/transformer.rb, line 445
def self.add_match matching, *names, &block
  names.flatten.each do |name|
    name = name.to_s
    raise "Element #{name} does already have a matching rule" if matching.has_key?(name)
    matching[name] = block
  end
end
all_delegate_classes() click to toggle source
# File xml/transformer.rb, line 455
def all_delegate_classes
  if @all_delegate_classes.nil?
    @all_delegate_classes = []
    @all_delegate_classes += @class_delegate_classes if @class_delegate_classes
    @all_delegate_classes += superclass.all_delegate_classes if superclass < LANSA::XML::Transformer
  end
  @all_delegate_classes
end
all_matching() click to toggle source
# File xml/transformer.rb, line 482
def all_matching
  if @all_matching.nil?
    @all_matching = {}
    @all_matching.merge! superclass.all_matching if superclass < LANSA::XML::Transformer
    @all_matching.merge! @class_matching if @class_matching
  end
  @all_matching
end
all_split() click to toggle source
# File xml/transformer.rb, line 473
def all_split
  if @all_split.nil?
    @all_split = []
    @all_split << @class_split if @class_split
    @all_split += superclass.all_split if superclass < LANSA::XML::Transformer
  end
  @all_split
end
all_start() click to toggle source
# File xml/transformer.rb, line 464
def all_start
  if @all_start.nil?
    @all_start = []
    @all_start << @class_start if @class_start
    @all_start += superclass.all_start if superclass < LANSA::XML::Transformer
  end
  @all_start
end
exit_on_error(flag = nil) click to toggle source
# File xml/transformer.rb, line 491
def exit_on_error flag = nil
  @exit_on_error = flag unless flag.nil?
  return @exit_on_error == true
end
exit_on_warning(flag = nil) click to toggle source
# File xml/transformer.rb, line 496
def exit_on_warning flag = nil
  @exit_on_warning = flag unless flag.nil?
  return @exit_on_warning == true
end
inherited(subclass) click to toggle source
# File xml/transformer.rb, line 503
def inherited subclass
  subclass.initialize_class self
end
initialize_class(parent = nil) click to toggle source
# File xml/transformer.rb, line 507
def initialize_class parent = nil
  @class_delegate_classes = []
  @class_start = nil
  @class_split = nil
  @class_matching = {}
  @exit_on_error = parent.exit_on_error unless parent.nil?
  @exit_on_warning = parent.exit_on_warning unless parent.nil?
end

Public Instance Methods

apply(elem)
Alias for: default_apply
cdata_handler(node)
column() click to toggle source
# File xml/transformer.rb, line 247
def column
  @parser_context.column
end
comment_handler(node)
default_apply(elem) click to toggle source
# File xml/transformer.rb, line 308
def default_apply elem
  elem.each do |e|
    handle e
  end
end
Also aliased as: apply
default_cdata_handler(node) click to toggle source
# File xml/transformer.rb, line 377
def default_cdata_handler node
  @output.cdata node.content
end
Also aliased as: cdata_handler
default_comment_handler(node) click to toggle source
# File xml/transformer.rb, line 387
def default_comment_handler node
  @output.comment node.content
end
Also aliased as: comment_handler
default_document_handler(node) click to toggle source
# File xml/transformer.rb, line 362
def default_document_handler node
  apply node.children
end
Also aliased as: document_handler
default_element_handler(node) click to toggle source
# File xml/transformer.rb, line 367
def default_element_handler node
  @output.element node.name do
    node.attributes.each do |n, a|
      @output.attribute n, a.value
    end
    apply node.children
  end
end
Also aliased as: element_handler
default_end_split(*args) click to toggle source
# File xml/transformer.rb, line 276
def default_end_split *args

  document = @sax2dom.document
  handle(document) unless document.nil?

  @split_element = nil
  @split_location = nil
  @sax2dom.clear

end
Also aliased as: end_split
default_find_delegate(*args) click to toggle source
# File xml/transformer.rb, line 197
def default_find_delegate *args

  # We only check THIS transformer, if no delegates were specified.
  # Otherwise, we ONLY check the delegates, thus the delegates always
  # have precedence!

  # If no delegates exist, check current transformer.
  # This case happens if a transformer was defined in a "legacy" way,
  # i.e. inside the call to "Transformer.transform"!
  if @delegates.empty?
    return self if LANSA::XML::Utils.call_object_method(self, :start?, *args)
  else

    @delegates.each do |delegate|
      found_delegate = LANSA::XML::Utils.call_object_method(delegate, :find_delegate, *args)
      return found_delegate unless found_delegate.nil?
    end

  end

  return nil
end
Also aliased as: find_delegate
default_handle(node) click to toggle source
# File xml/transformer.rb, line 335
def default_handle node
  if is_document?(node)
    document_handler node
  elsif is_element?(node)
    handler = handler(node.name)
    if handler
      self.on_match node if self.respond_to? :on_match
      begin
        self.instance_exec node, &handler
      rescue Exception => e
        raise e
      end
    else
      element_handler node
    end
  elsif is_cdata?(node)
    cdata_handler node
  elsif is_text?(node)
    text_handler node
  elsif is_comment?(node)
    comment_handler node
  else
    raise "Unknown node type: #{node.class}"
  end
end
Also aliased as: handle
default_handler(name) click to toggle source
# File xml/transformer.rb, line 303
def default_handler name
  @matching[name] || self.class.all_matching[name]
end
Also aliased as: handler
default_intercept_end_element(name, *args) click to toggle source
# File xml/transformer.rb, line 401
def default_intercept_end_element name, *args
  @current_element_name = nil
  @intercept_depth -= 1
end
Also aliased as: intercept_end_element
default_intercept_error(msg) click to toggle source
# File xml/transformer.rb, line 407
def default_intercept_error msg
  msg = error_msg(msg.chomp)
  @error_messages << msg
  raise msg if exit_on_error
  PIM.log_error msg
end
Also aliased as: intercept_error
default_intercept_start_element(name, *args) click to toggle source
# File xml/transformer.rb, line 395
def default_intercept_start_element name, *args
  @current_element_name = name
  @intercept_depth += 1
end
Also aliased as: intercept_start_element
default_intercept_warning(msg) click to toggle source
# File xml/transformer.rb, line 415
def default_intercept_warning msg
  msg = warning_msg(msg.chomp)
  @error_messages << msg
  raise msg if exit_on_warning or exit_on_error
  PIM.log_warn msg
end
Also aliased as: intercept_warning
default_match(*names, &block) click to toggle source
# File xml/transformer.rb, line 298
def default_match *names, &block
  Transformer.add_match @matching, *names, &block
end
Also aliased as: match
default_output_location_attribute(location = nil) click to toggle source
# File xml/transformer.rb, line 292
def default_output_location_attribute location = nil
  location = split_location_msg
  @output.attribute LOCATION_ATTRIBUTE_NAME, location
end
Also aliased as: output_location_attribute
default_split?(element) click to toggle source
# File xml/transformer.rb, line 251
def default_split? element

  # Get all 'split?' blocks and return 'true' if non are defined.
  # In that case, the transformer might have a legacy implementation
  # and is unaware that a 'split?' block could have been defined
  all_split = self.class.all_split
  return true if all_split.empty?

  all_split.each do |split|
    return true if self.instance_exec(element, &split)
  end

  return false;
end
Also aliased as: split?
default_start?(*args) click to toggle source
# File xml/transformer.rb, line 221
def default_start? *args

  # Get all 'start?' blocks and return 'true' if non are defined.
  # In that case, the transformer might have a legacy implementation
  # and is unaware that a 'start?' block could have been defined
  all_start = self.class.all_start
  return true if all_start.empty?

  all_start.each do |start|
    return true if self.instance_exec(*args, &start)
  end

  return false
end
Also aliased as: start?
default_start_split(element) click to toggle source
# File xml/transformer.rb, line 267
def default_start_split element
  @split_element = element
  @split_location = "#{line}:#{column}"
  @sax2dom.clear
  return @sax2dom
end
Also aliased as: start_split, split!
default_text_handler(node) click to toggle source
# File xml/transformer.rb, line 382
def default_text_handler node
  @output.text node.content
end
Also aliased as: text_handler
document_handler(node)
element_handler(node)
end() click to toggle source
# File xml/transformer.rb, line 240
def end
end
end_split(*args)
Alias for: default_end_split
error_msg(msg) click to toggle source
# File xml/transformer.rb, line 423
def error_msg msg
  "Error #{location_msg}: #{msg}"
end
exit_on_error(flag = nil) click to toggle source
# File xml/transformer.rb, line 435
def exit_on_error flag = nil
  self.class.exit_on_error(flag)
end
exit_on_warning(flag = nil) click to toggle source
# File xml/transformer.rb, line 439
def exit_on_warning flag = nil
  self.class.exit_on_warning(flag)
end
find_delegate(*args)
handle(node)
Alias for: default_handle
handler(name)
Alias for: default_handler
intercept_end_element(name, *args)
intercept_error(msg)
intercept_start_element(name, *args)
intercept_warning(msg)
is_cdata?(node) click to toggle source
# File xml/transformer.rb, line 323
def is_cdata? node
  return node.kind_of?(Nokogiri::XML::CDATA)
end
is_comment?(node) click to toggle source
# File xml/transformer.rb, line 331
def is_comment? node
  return node.kind_of?(Nokogiri::XML::Comment)
end
is_document?(node) click to toggle source
# File xml/transformer.rb, line 315
def is_document? node
  return node.kind_of?(Nokogiri::XML::Document)
end
is_element?(node) click to toggle source
# File xml/transformer.rb, line 319
def is_element? node
  return node.kind_of?(Nokogiri::XML::Element)
end
is_text?(node) click to toggle source
# File xml/transformer.rb, line 327
def is_text? node
  return node.kind_of?(Nokogiri::XML::Text)
end
line() click to toggle source
# File xml/transformer.rb, line 243
def line
  @parser_context.line
end
location_msg() click to toggle source
# File xml/transformer.rb, line 431
def location_msg
  msg = (@current_element_name ? "parsing element '#{@current_element_name}' at" : "in") + " line #{line}, column #{column}"
end
match(*names, &block)
Alias for: default_match
output_location_attribute(location = nil)
parser_context=(parser_context) click to toggle source
# File xml/transformer.rb, line 157
def parser_context= parser_context
  @parser_context = parser_context
  @delegates.each do |delegate|
    delegate.parser_context = parser_context
  end
end
split!(element)
Alias for: default_start_split
split?(element)
Alias for: default_split?
split_location_msg() click to toggle source
# File xml/transformer.rb, line 288
def split_location_msg
  "#{split_element.name}:#{split_location}" if split_element and split_location
end
start() click to toggle source
# File xml/transformer.rb, line 237
def start
end
start?(*args)
Alias for: default_start?
start_split(element)
Alias for: default_start_split
text_handler(node)
validate_element(name, attrs) click to toggle source
# File xml/transformer.rb, line 392
def validate_element name, attrs
end
warning_msg(msg) click to toggle source
# File xml/transformer.rb, line 427
def warning_msg msg
  "Warning #{location_msg}: #{msg}"
end

Protected Instance Methods

has_delegate_for_class?(delegate_class) click to toggle source
# File xml/transformer.rb, line 172
def has_delegate_for_class? delegate_class
  return false if @delegates.nil?
  return true if @delegates.any? { |d| d.class == delegate_class }
  @delegates.each do |delegate|
    return true if delegate.has_delegate_for_class?(delegate_class)
  end
  return false
end