Class: MasterView::TemplateProcessing::SAXParserListener

Inherits:
Object
  • Object
show all
Includes:
DirectiveHelpers, ParserHelpers, REXML::SAX2Listener
Defined in:
lib/masterview/parser.rb

Overview

SAX parser event handler. Handles the low-level events from the SAX parser processing an XML document and passes the element tag and content data off to a MasterViewer::TemplateProcessing::Renderer for template processing.

Constant Summary

Constants included from DirectiveHelpers

DirectiveHelpers::CRLF, DirectiveHelpers::ERB_CONTENT_END, DirectiveHelpers::ERB_CONTENT_START, DirectiveHelpers::ERB_EVAL_END, DirectiveHelpers::ERB_EVAL_START

Instance Method Summary collapse

Methods included from ParserHelpers

#parse_eval_into_array, #parse_eval_into_hash

Methods included from DirectiveHelpers

#delete_last_in_parent, #find_last_in_parent, #find_string_val_in_string_hash, #parse, #quote, #quote_if, #render_partial_name_to_file_name

Constructor Details

#initialize(options = {}) ⇒ SAXParserListener

Returns a new instance of SAXParserListener.



485
486
487
# File 'lib/masterview/parser.rb', line 485

def initialize( options = {} )
  @renderer = Renderer.new(options)
end

Instance Method Details

#attlistdecl(element, pairs, contents) ⇒ Object

If a doctype includes an ATTLIST declaration, it will cause this method to be called. The content is the declaration itself, unparsed. EG, <!ATTLIST el attr CDATA #REQUIRED> will come to this method as “el attr CDATA #REQUIRED”. This is the same for all of the .*decl methods.



529
530
531
# File 'lib/masterview/parser.rb', line 529

def attlistdecl(element, pairs, contents)
  #todo
end

#cdata(content) ⇒ Object



579
580
581
# File 'lib/masterview/parser.rb', line 579

def cdata(content)
  @renderer.append_content(:cdata, content)
end

#characters(text) ⇒ Object



571
572
573
# File 'lib/masterview/parser.rb', line 571

def characters(text)
  @renderer.append_content(:characters, text)
end

#comment(comment) ⇒ Object



575
576
577
# File 'lib/masterview/parser.rb', line 575

def comment(comment)
  @renderer.append_content(:comment, comment)
end

#doctype(name, pub_sys, long_name, uri) ⇒ Object

Handles a doctype declaration. Any attributes of the doctype which are not supplied will be nil. # EG, <!DOCTYPE me PUBLIC “foo” “bar”>



509
510
511
512
513
514
515
516
517
518
# File 'lib/masterview/parser.rb', line 509

def doctype(name, pub_sys, long_name, uri)
  xml_doctype = []
  xml_doctype << '<!DOCTYPE'
  xml_doctype << name
  xml_doctype << pub_sys unless pub_sys.nil?
  xml_doctype << %Q["#{long_name}"] unless long_name.nil?
  xml_doctype << %Q["#{uri}"] unless uri.nil?
  doctype_str = xml_doctype.join(' ')+'>'
  @renderer.append_to_prolog( doctype_str )
end

#elementdecl(content) ⇒ Object

<!ELEMENT …>



534
535
536
# File 'lib/masterview/parser.rb', line 534

def elementdecl content
  #todo
end

#end_documentObject



587
588
589
# File 'lib/masterview/parser.rb', line 587

def end_document
  #todo
end

#end_element(uri, localname, qname) ⇒ Object



583
584
585
# File 'lib/masterview/parser.rb', line 583

def end_element(uri, localname, qname)
  @renderer.pop_tag
end

#entitydecl(content) ⇒ Object

<!ENTITY …> The argument passed to this method is an array of the entity declaration. It can be in a number of formats, but in general it returns (example, result):

<!ENTITY % YN '"Yes"'>
["%", "YN", "'\"Yes\"'", "\""]
<!ENTITY % YN 'Yes'>
["%", "YN", "'Yes'", "s"]
<!ENTITY WhatHeSaid "He said %YN;">
["WhatHeSaid", "\"He said %YN;\"", "YN"]
<!ENTITY open-hatch SYSTEM "http://www.textuality.com/boilerplate/OpenHatch.xml">
["open-hatch", "SYSTEM", "\"http://www.textuality.com/boilerplate/OpenHatch.xml\""]
<!ENTITY open-hatch PUBLIC "-//Textuality//TEXT Standard open-hatch boilerplate//EN" "http://www.textuality.com/boilerplate/OpenHatch.xml">
["open-hatch", "PUBLIC", "\"-//Textuality//TEXT Standard open-hatch boilerplate//EN\"", "\"http://www.textuality.com/boilerplate/OpenHatch.xml\""]
<!ENTITY hatch-pic SYSTEM "../grafix/OpenHatch.gif" NDATA gif>
["hatch-pic", "SYSTEM", "\"../grafix/OpenHatch.gif\"", "\n\t\t\t\t\t\t\tNDATA gif", "gif"]


554
555
556
# File 'lib/masterview/parser.rb', line 554

def entitydecl content
  #todo
end

#generate_replace(value) ⇒ Object



598
599
600
# File 'lib/masterview/parser.rb', line 598

def generate_replace(value)
  @renderer.append_raw ERB_CONTENT_START+value+ERB_CONTENT_END
end

#handle_gen_partial(attributes) ⇒ Object

handle a mv:gen_partial attribute, which calls generate and outputs a token it takes an optional :dir => ‘foo/bar’ which is prepended to partial path, otherwise it just uses what is in partial. This creates a generate attribute value which will be used later. Parameters value = attribute value for gen_partial attributes = all remaining attributes hash



609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
# File 'lib/masterview/parser.rb', line 609

def handle_gen_partial(attributes)
  value = @renderer.keyword_expander.resolveAttrAndDelete(attributes, @renderer.mv_gen_partial_attr)
  if value
    prepend_dir = find_string_val_in_string_hash(value, :dir) #only used for masterview
    partial = find_string_val_in_string_hash(value, :partial)
    return if partial.nil?
    path = render_partial_name_to_file_name(partial, @renderer.default_extension)
    path = File.join(prepend_dir, path) if prepend_dir
    generate_attribute = attributes[@renderer.mv_generate_attr] || '' # check if we need to add to existing generate
    generate_attribute = path + (generate_attribute.blank? ? '' : ', '+generate_attribute)
    attributes[@renderer.mv_generate_attr] = generate_attribute
    page_locals = @renderer.keyword_expander.resolveAttrAndDelete(attributes, "mvpg:locals") #todo make this dynamic
    value = (value.trim.empty?) ? page_locals : "#{value}, :locals => {#{page_locals}}" if page_locals #todo need to handle trailing }
    @renderer.append_raw( ERB_CONTENT_START+'render( '+value+' )'+ERB_CONTENT_END )
  end
end

#notationdecl(content) ⇒ Object

<!NOTATION …>



559
560
561
# File 'lib/masterview/parser.rb', line 559

def notationdecl content
  puts 'in notationdecl'
end

#processing_instruction(target, data) ⇒ Object



520
521
522
# File 'lib/masterview/parser.rb', line 520

def processing_instruction target, data
  #todo
end

#push_levels(attributes) ⇒ Object



626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
# File 'lib/masterview/parser.rb', line 626

def push_levels(attributes)
  handle_gen_partial(attributes)

  gen_replace = @renderer.keyword_expander.resolveAttrAndDelete(attributes, @renderer.mv_gen_replace_attr) #get and delete from map
  generate_replace( gen_replace ) unless gen_replace.nil?

  gen = @renderer.keyword_expander.resolveAttrAndDelete(attributes, @renderer.mv_generate_attr) #get and delete from map
  if gen
    omit_comment = @renderer.options[:omit_comment] || OmitGeneratedComments || File.extname(gen) != MasterView::IOMgr.erb.default_extension
    attributes[@renderer.mv_insert_generated_comment_attr] = @renderer.template_full_pathname.to_s unless omit_comment #add the comment directive, so it will be written to each gen'd file
    render_level = nil
    gen_values = parse_eval_into_hash(gen, :normal)

    #Log.debug { 'generate_hash='+gen_values.inspect }

    gen_values.each do |key,value|
      mode_type = key.to_sym
      arr_values = (value.is_a?(Enumerable)) ? value : [value] #if not enumerable add it to array
      value.each do |path|
        path.strip!
        #Log.debug { ('pushing mode='+mode_type.to_s+' path='+path).indent(2*@renderer.render_levels.size) }
        render_level ||= RenderLevel.new
        render_level.push RenderMode.new(path, mode_type)
      end
    end
    @renderer.push_level(render_level) unless render_level.nil?
  end

end

#start_documentObject



499
500
501
# File 'lib/masterview/parser.rb', line 499

def start_document
  #todo
end

#start_element(uri, localname, qname, attributes) ⇒ Object



563
564
565
566
567
568
569
# File 'lib/masterview/parser.rb', line 563

def start_element(uri, localname, qname, attributes)
  unescape_attributes!(attributes)
  ci_attributes = CaseInsensitiveHash.new
  ci_attributes.replace(attributes) #populate from original hash
  push_levels(ci_attributes)
  @renderer.push_tag(qname, ci_attributes)
end

#unescape_attributes!(attributes) ⇒ Object



591
592
593
594
595
596
# File 'lib/masterview/parser.rb', line 591

def unescape_attributes!(attributes)
  attributes.each do |name, value|
    value.replace CGI::unescapeHTML(value)
    value.gsub!('&apos;', '\'')
  end
end

#xmldecl(version, encoding, standalone) ⇒ Object



489
490
491
492
493
494
495
496
497
# File 'lib/masterview/parser.rb', line 489

def xmldecl(version, encoding, standalone)
  xmldecl = []
  xmldecl << '<?xml'
  xmldecl << %Q[version="#{version}"] unless version.nil?
  xmldecl << %Q[encoding="#{encoding}"] unless encoding.nil?
  xmldecl << %Q[standalone="#{standalone}"] unless standalone.nil?
  xmldecl << '?>'
  @renderer.append_to_prolog( xmldecl.join(' ') )
end