Class: ScriptElement

Inherits:
Element show all
Defined in:
lib/fxmlloader/real_elts.rb

Overview

Element representing a script block

Instance Attribute Summary

Attributes inherited from Element

#current, #eventHandlerAttributes, #instancePropertyAttributes, #lineNumber, #loadListener, #parent, #parentLoader, #staticPropertyAttributes, #staticPropertyElements, #value, #valueAdapter, #xmlStreamReader

Instance Method Summary collapse

Methods inherited from Element

#add, #addEventHandler, #applyProperty, #callz, #getProperties, #getValueAdapter, #isBidirectionalBindingExpression, #isBindingExpression, #isTyped, #populateArrayFromString, #populateListFromString, #processEventHandlerAttributes, #processInstancePropertyAttributes, #processPropertyAttribute, #processValue3, #resolvePrefixedValue, #set, #staticLoad, #updateValue, #warnDeprecatedEscapeSequence

Constructor Details

#initialize(current, xmlStreamReader, loadListener, parentLoader) ⇒ ScriptElement

Returns a new instance of ScriptElement.


586
587
588
589
590
# File 'lib/fxmlloader/real_elts.rb', line 586

def initialize(current, xmlStreamReader, loadListener, parentLoader)
  super
  @source = nil;
  @charset = parentLoader.charset;
end

Instance Method Details

#isCollectionObject


592
593
594
# File 'lib/fxmlloader/real_elts.rb', line 592

def isCollection()
  return false;
end

#processAttribute(prefix, localName, value) ⇒ Object


675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
# File 'lib/fxmlloader/real_elts.rb', line 675

def processAttribute(prefix, localName, value)
  if (prefix == nil        && localName == (FXL::SCRIPT_SOURCE_ATTRIBUTE))
    if (loadListener != nil)
      loadListener.readInternalAttribute(localName, value);
    end

    @source = value;
  elsif (localName == (FXL::SCRIPT_CHARSET_ATTRIBUTE))
    if (loadListener != nil)
      loadListener.readInternalAttribute(localName, value);
    end

    @charset = Charset.forName(value);
  else
    raise LoadException.new(prefix == nil ? localName : prefix + ":" + localName        + " is not a valid attribute.");
  end
end

#processCharactersObject


664
665
666
667
668
669
670
671
672
673
# File 'lib/fxmlloader/real_elts.rb', line 664

def processCharacters()
  if (@source != nil)
    raise LoadException.new("Script source already specified.");
  end

  if (parentLoader.scriptEngine == nil && !parentLoader.staticLoad)
    raise LoadException.new("Page language not specified.");
  end
  updateValue(xmlStreamReader.getText());
end

#processEndElementObject


651
652
653
654
655
656
657
658
659
660
661
662
# File 'lib/fxmlloader/real_elts.rb', line 651

def processEndElement()
  super();
  if (value != nil && !parentLoader.staticLoad)
    # Evaluate the script
    begin
      rputs nil, "__local_sem_lang_inst_#{rget_sem(parentLoader.scriptEngine)}.eval(#{value.to_s.inspect})"
      parentLoader.scriptEngine.eval( value.to_s);
    rescue ScriptException => exception
      STDERR.puts (exception.getMessage());
    end
  end
end

#processStartElementObject


596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
# File 'lib/fxmlloader/real_elts.rb', line 596

def processStartElement()
  super();

  if (@source != nil && !staticLoad)
    i = @source.rindex(".");
    if (i == nil)
      raise ("Cannot determine type of script \""											+ @source + "\".");
    end
    extension = @source[(i + 1)..-1];
    scriptEngine = nil
    #TODO: use JRUBY stuff
    oldLoader = Thread.currentThread().getContextClassLoader();
    begin
      Thread.currentThread().setContextClassLoader(classLoader);
      scriptEngineManager = getScriptEngineManager();
      scriptEngine = scriptEngineManager.getEngineByExtension(extension);
    ensure
      Thread.currentThread().setContextClassLoader(oldLoader);
    end

    if (scriptEngine == nil)
      raise ("Unable to locate scripting engine for"											+ " extension " + extension + ".");
    end

    scriptEngine.setBindings(scriptEngineManager.getBindings(), ScriptContext.ENGINE_SCOPE);

    begin
      location = nil
      if (@source[0] == '/')
        location = classLoader.getResource(@source[(1)..-1]);
      else
        if (parentLoader.location == nil)
          raise LoadException.new("Base location is undefined.");
        end

        location = URL.new(parentLoader.location, @source);
      end

      InputStreamReader scriptReader = nil;
      begin
        scriptReader = InputStreamReader.new(location.openStream(), @charset);
        scriptEngine.eval(scriptReader);
      rescue ScriptException => exception
        exception.printStackTrace();
      ensure
        if (scriptReader != nil)
          scriptReader.close();
        end
      end
    rescue IOException => exception
      raise LoadException.new(exception);
    end
  end
end