Class: FxmlLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/jrubyfx-fxmlloader.rb

Constant Summary collapse

FX_NAMESPACE_VERSION =
"1"
@@fxml_jit_info =
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url = nil, ctrlr = nil, resourcs = nil, buildFactory = nil, charset = nil, loaders = nil) ⇒ FxmlLoader

Returns a new instance of FxmlLoader.



266
267
268
269
270
271
272
273
274
275
276
# File 'lib/jrubyfx-fxmlloader.rb', line 266

def initialize(url=nil, ctrlr=nil, resourcs=nil, buildFactory=nil, charset=nil, loaders=nil)
  @location = url
  @builderFactory = buildFactory || JavaFXBuilderFactory.new(JRuby.runtime.jruby_class_loader)
  @template = false
  @namespace = FXCollections.observableHashMap()
  self.controller = ctrlr
  @packages = []
  @classes = {}
  @root = nil
  @charset = charset || Charset.forName(FXL::DEFAULT_CHARSET_NAME)
end

Instance Attribute Details

#builderFactoryObject

Returns the value of attribute builderFactory.



261
262
263
# File 'lib/jrubyfx-fxmlloader.rb', line 261

def builderFactory
  @builderFactory
end

#charsetObject

Returns the value of attribute charset.



261
262
263
# File 'lib/jrubyfx-fxmlloader.rb', line 261

def charset
  @charset
end

#controllerObject

Returns the value of attribute controller.



263
264
265
# File 'lib/jrubyfx-fxmlloader.rb', line 263

def controller
  @controller
end

#controllerFactoryObject

Returns the value of attribute controllerFactory.



261
262
263
# File 'lib/jrubyfx-fxmlloader.rb', line 261

def controllerFactory
  @controllerFactory
end

#currentObject

Returns the value of attribute current.



261
262
263
# File 'lib/jrubyfx-fxmlloader.rb', line 261

def current
  @current
end

#locationObject

Returns the value of attribute location.



261
262
263
# File 'lib/jrubyfx-fxmlloader.rb', line 261

def location
  @location
end

#namespaceObject

Returns the value of attribute namespace.



261
262
263
# File 'lib/jrubyfx-fxmlloader.rb', line 261

def namespace
  @namespace
end

#rootObject

Returns the value of attribute root.



261
262
263
# File 'lib/jrubyfx-fxmlloader.rb', line 261

def root
  @root
end

#scriptEngineObject

Returns the value of attribute scriptEngine.



262
263
264
# File 'lib/jrubyfx-fxmlloader.rb', line 262

def scriptEngine
  @scriptEngine
end

#staticLoadObject

Returns the value of attribute staticLoad.



261
262
263
# File 'lib/jrubyfx-fxmlloader.rb', line 261

def staticLoad
  @staticLoad
end

#templateObject

Returns the value of attribute template.



261
262
263
# File 'lib/jrubyfx-fxmlloader.rb', line 261

def template
  @template
end

Instance Method Details

#clearImportsObject



384
385
386
387
# File 'lib/jrubyfx-fxmlloader.rb', line 384

def clearImports
  @packages.clear
  @classes.clear
end

#compareJFXVersions(rtVer, nsVer) ⇒ Object



679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
# File 'lib/jrubyfx-fxmlloader.rb', line 679

def compareJFXVersions(rtVer, nsVer)

  retVal = 0;

if (rtVer == nil || "" == (rtVer)			|| nsVer == nil || "" == (nsVer))
	return retVal;
  end

if (rtVer == (nsVer))
	return retVal;
  end

# version string can contain '-'
  dashIndex = rtVer.index("-");
  dashIndex = -1 unless dashIndex
if (dashIndex > 0)

	rtVer = rtVer[0, dashIndex]
  end

# or "_"
  underIndex = rtVer.index("_");
  underIndex = -1 unless underIndex
if (underIndex > 0)

	rtVer = rtVer[0, underIndex]
  end

# do not try to compare if the string is not valid version format
if (!rtVer.match(/^(\d+)(\.\d+)*$/)			|| !nsVer.match(/^(\d+)(\.\d+)*$/))
	return retVal;
  end

  nsVerTokenizer = StringTokenizer.new(nsVer, ".");
rtVerTokenizer = StringTokenizer.new(rtVer, ".");
nsDigit = 0
  rtDigit = 0;
rtVerEnd = false;

while (nsVerTokenizer.hasMoreTokens() && retVal == 0)
	nsDigit = nsVerTokenizer.nextToken().to_i
	if (rtVerTokenizer.hasMoreTokens())
		rtDigit = rtVerTokenizer.nextToken().to_i
		retVal = rtDigit - nsDigit;
	else
		rtVerEnd = true;
		break;
    end
  end

if (rtVerTokenizer.hasMoreTokens() && retVal == 0)
	rtDigit = rtVerTokenizer.nextToken().to_i
	if (rtDigit > 0)
		retVal = 1;
    end
  end

if (rtVerEnd)
	if (nsDigit > 0)
		retVal = -1;
	else
		while (nsVerTokenizer.hasMoreTokens())
			nsDigit = nsVerTokenizer.nextToken().to_i
			if (nsDigit > 0)
				retVal = -1;
				break;
        end
      end
    end
  end

return retVal;
end

#createElementObject



450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
# File 'lib/jrubyfx-fxmlloader.rb', line 450

def createElement()
prefix = @xmlStreamReader.getPrefix();
localName = @xmlStreamReader.getLocalName();

if !prefix
	i = localName.rindex('.')

	if localName[(i ? i : -1) + 1] == localName[(i ? i : -1) + 1].downcase
		name = localName[((i ? i : -1) + 1)..-1]

		if (i == nil)
			# This is an instance property
			if @loadListener
				@loadListener.beginPropertyElement(name, nil)
        end

			@current = PropertyElement.new(@current, @xmlStreamReader, @loadListener, self, name, nil)
		else
			# This is a static property
			sourceType = getType(localName[0, i]);
			if sourceType
				if @loadListener
					@loadListener.beginPropertyElement(name, sourceType);
          end

				@current = PropertyElement.new(@current, @xmlStreamReader, @loadListener, self,name, sourceType)
			elsif (@staticLoad)
				# The source type was not recognized
				if @loadListener
					@loadListener.beginUnknownStaticPropertyElement(localName);
          end

				@current = FXL::UnknownStaticPropertyElement.new
			else
				raise LoadException.new(localName + " is not a valid property.");
        end
      end
	else
		if (@current == nil && @root)
			raise LoadException.new("Root value already specified.");
      end

		type = getType(localName);
      prefixz = @xmlStreamReader.getLocation().getLineNumber().to_s + ": "
      numz = 1
      pppn = @current
      while pppn
        numz+=1
        pppn = pppn.parent
      end
      prefixz = (" " * numz) + prefixz

		if type
			if @loadListener
				@loadListener.beginInstanceDeclarationElement(type);
        end
			@current = InstanceDeclarationElement.new(@current, @xmlStreamReader, @loadListener, self, type)
		elsif (@staticLoad)
			# The type was not recognized
			if @loadListener
				@loadListener.beginUnknownTypeElement(localName);
        end

			@current = UnknownTypeElement.new(@current, @xmlStreamReader, @loadListener, self)
		else
        raise LoadException.new(localName + " is not a valid type.");
      end
    end
elsif prefix == FXL::FX_NAMESPACE_PREFIX
	if localName == FXL::INCLUDE_TAG
		if @loadListener
			@loadListener.beginIncludeElement()
      end
		@current = IncludeElement.new(@current, @xmlStreamReader, @loadListener, self)
	elsif localName == FXL::REFERENCE_TAG
		if @loadListener
			@loadListener.beginReferenceElement
      end

		@current = ReferenceElement.new(@current, @xmlStreamReader, @loadListener, self)
	elsif localName == FXL::COPY_TAG
		if @loadListener
        @loadListener.beginCopyElement();
      end

		@current = CopyElement.new(@current, @xmlStreamReader, @loadListener, self)
	elsif localName == FXL::ROOT_TAG
		if @loadListener
        @loadListener.beginRootElement();
      end

		@current = RootElement.new(@current, @xmlStreamReader, @loadListener, self)
	elsif localName == FXL::SCRIPT_TAG
		if @loadListener
        @loadListener.beginScriptElement();
      end

		@current = ScriptElement.new(@current, @xmlStreamReader, @loadListener, self)
	elsif localName == FXL::DEFINE_TAG
		if @loadListener
        @loadListener.beginDefineElement();
      end

		@current = DefineElement.new(@current, @xmlStreamReader, @loadListener, self)
	else
		raise LoadException.new(prefix + ":" + localName + " is not a valid element.");
    end
else
	raise LoadException.new("Unexpected namespace prefix: " + prefix + ".");
  end
end

#getScriptEngineManagerObject



657
658
659
660
661
662
663
664
# File 'lib/jrubyfx-fxmlloader.rb', line 657

def getScriptEngineManager()
unless @scriptEngineManager
	@scriptEngineManager =  Java.javax.script.ScriptEngineManager.new
	@scriptEngineManager.setBindings(SimpleBindings.new(@namespace))
  end

return @scriptEngineManager;
end

#getType(name) ⇒ Object



591
592
593
594
595
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
# File 'lib/jrubyfx-fxmlloader.rb', line 591

def getType(name)
	type = nil

	if name[0] == name[0].downcase
		# This is a fully-qualified class name
		begin
			type = loadType(name, false);
		rescue ClassNotFoundException => exception
			# No-op
		end
	else
		# This is an unqualified class name
		type = @classes[name];

		unless type
			# The class has not been loaded yet; look it up
			@packages.each do |packageName|
				begin
					type = loadTypeForPackage(packageName, name);
				rescue ClassNotFoundException => exception
					# No-op
				end
         break if type
			end
       unless type
         # check for ruby
         # TODO: this should require an import or something perhaps? need to think more about this?
         begin
					type = name.constantize_by(".")
				rescue
					# No-op
				end
       end
       @classes[name] = type if type
		end
	end

	return type;
end

#importClass(name) ⇒ Object



583
584
585
586
587
588
589
# File 'lib/jrubyfx-fxmlloader.rb', line 583

def importClass(name)
	begin
		loadType(name, true);
	rescue ClassNotFoundException => exception
		raise LoadException.new(exception);
	end
end

#importPackage(name) ⇒ Object



579
580
581
# File 'lib/jrubyfx-fxmlloader.rb', line 579

def importPackage(name)
	@packages << name
end

#load(jruby_exts = {jruby_ext: {}}) ⇒ Object

either :no_jit, or a number above 0 representing the number of times before jitting a fxml file



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/jrubyfx-fxmlloader.rb', line 288

def load(jruby_exts = {jruby_ext: {}})
  @jruby_ext = {jit: 0}.merge(jruby_exts[:jruby_ext])
  # TODO: actually open it properly
  unless jit_info = @@fxml_jit_info[file_path = @location.to_s]
    validate = true
    validate = @jruby_ext[:jit_validate] if @jruby_ext.has_key? :jit_validate
    jit_info = @@fxml_jit_info[file_path] = FxmlJitInfo.new(file_path, @jruby_ext[:jit], @jruby_ext[:jit_save_to], @jruby_ext[:jit_cache], validate, @jruby_ext[:jit_opts])
  end
  inputStream = @location.open_stream
  if @template
    @root = nil
  else
    clearImports
  end

  @namespace[FXL::LOCATION_KEY] = @location
  @namespace[FXL::RESOURCES_KEY] = @resources

  @script_engine = nil

difx = $DEBUG_IT_FXML_RB # TODO: hack to make this somewhat better at recursion
  # if we have it cached, use the jitted method
  if jit_info.compiled?
    begin
      return :dont_load if @jruby_ext[:dont_load]
		$DEBUG_IT_FXML_RB = jit_info.should_jit?
      res = jit_info.__build_via_jit(@controller, @namespace, @jruby_ext)
		$DEBUG_IT_FXML_RB = difx # restore
		return res
    rescue Exception, java.lang.Throwable
      puts "JIT compiled method for #{@location.to_s} FAILED with error:"
      puts $!
      puts $!.backtrace
      puts "Reverting to normal parsing..."
      jit_info.jit_settings = :no_jit
      jit_info.decompile
    end
  end

  $DEBUG_IT_FXML_RB = jit_info.should_jit?

  begin
    xmlInputFactory = XMLInputFactory.newFactory
    xmlInputFactory.setProperty("javax.xml.stream.isCoalescing", true)

	# Some stream readers incorrectly report an empty string as the prefix
	# for the default namespace; correct this as needed
	inputStreamReader = InputStreamReader.new(inputStream, @charset);
	@xmlStreamReader = SRDelegateClass.new(xmlInputFactory.createXMLStreamReader(inputStreamReader))
  rescue XMLStreamException => e
    raise LoadException.new(e)
  end

  # Parse the XML stream
begin
	while @xmlStreamReader.hasNext()
      event = @xmlStreamReader.next();
		case event
      when XMLStreamConstants::PROCESSING_INSTRUCTION
        processProcessingInstruction
      when XMLStreamConstants::COMMENT
        processComment
      when XMLStreamConstants::START_ELEMENT
        processStartElement
      when XMLStreamConstants::END_ELEMENT
        processEndElement
      when XMLStreamConstants::CHARACTERS
        processCharacters
      end
    end
rescue XMLStreamException => exception
	raise Exception.new(exception)
  end
  if @controller
    # TODO: initialize should be called here
    # Inject controller fields
    @controller.instance_variable_set("@" + FXL::LOCATION_KEY, @location)
    @controller.instance_variable_set("@" + FXL::RESOURCES_KEY, @resources)
  end
  if $DEBUG_IT_FXML_RB
    code = "#{rsem_out}\n#{rget @root}"
    jit_info.compile(code)
  end
  $RB_CMAPPER = {}
  $RB_IDMAPPER = {}
  $RB_MAPPER = {}
  $RB_PREFIX = ""
  $RB_NShowMAPPER = {}
  $last_elt = nil
  $RB_SCRIPT_ENGINE_MAPPINGS = {}

  @xmlStreamReader = nil
$DEBUG_IT_FXML_RB = difx # restore
  return @root
end

#loadType(name, cache) ⇒ Object



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/jrubyfx-fxmlloader.rb', line 631

def loadType(name, cache)
	i = name.index('.');
	n = name.length;
	while (i &&
         i < n &&
         name[i + 1] == name[i + 1].downcase)
		i = name.index('.', i + 1);
	end

	if (i == nil || i == n)
		raise ClassNotFoundException.new();
	end

	packageName = name[0, i];
	className = name[(i + 1)..-1];

	type = loadTypeForPackage(packageName, className);

	if (cache)
		@classes[className]  = type
	end

	return type;
end

#loadTypeForPackage(packageName, className = nil) ⇒ Object



666
667
668
669
670
671
672
673
674
675
676
677
678
# File 'lib/jrubyfx-fxmlloader.rb', line 666

def loadTypeForPackage(packageName, className=nil)
packageName = (packageName + "." + className.gsub('.', '$')) if className
  begin
    return JavaUtilities.get_proxy_class(packageName).java_class.to_java
  rescue NameError => ex
    # probably ruby class
    begin
      return packageName.constantize_by(".")
    rescue
      raise ClassNotFoundException.new(packageName) # nope, not our issue anymore
    end
  end
end

#processCharactersObject



572
573
574
575
576
577
# File 'lib/jrubyfx-fxmlloader.rb', line 572

def processCharacters()
	# Process the characters
	if (!@xmlStreamReader.isWhiteSpace())
		@current.processCharacters();
	end
end

#processCommentObject



431
432
433
# File 'lib/jrubyfx-fxmlloader.rb', line 431

def processComment
  @loadListener.readComment(@xmlStreamReader.text) if @loadListener
end

#processEndElementObject



562
563
564
565
566
567
568
569
570
# File 'lib/jrubyfx-fxmlloader.rb', line 562

def processEndElement()
	@current.processEndElement();
	if @loadListener
		@loadListener.endElement(@current.value);
	end

	# Move up the stack
	@current = @current.parent;
end

#processImportObject



417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/jrubyfx-fxmlloader.rb', line 417

def processImport
target = @xmlStreamReader.getPIData().strip

if @loadListener
	@loadListener.readImportProcessingInstruction(target)
  end

if target.end_with?(".*")
	importPackage(target[0,target.length - 2])
else
	importClass(target)
  end
end

#processLanguageObject



398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/jrubyfx-fxmlloader.rb', line 398

def processLanguage
if @scriptEngine
	raise LoadException.new("Page language already set.")
  end

language = @xmlStreamReader.getPIData()

if @loadListener
	@loadListener.readLanguageProcessingInstruction(language)
  end

unless staticLoad
	scriptEngineManager = getScriptEngineManager()
	@scriptEngine = scriptEngineManager.getEngineByName(language)
    rputs_script @scriptEngine, language
	@scriptEngine.setBindings(scriptEngineManager.getBindings(), ScriptContext.ENGINE_SCOPE)
  end
end

#processProcessingInstructionObject



389
390
391
392
393
394
395
396
# File 'lib/jrubyfx-fxmlloader.rb', line 389

def processProcessingInstruction
piTarget = @xmlStreamReader.getPITarget().strip
if piTarget == FXL::LANGUAGE_PROCESSING_INSTRUCTION
	processLanguage
elsif piTarget == FXL::IMPORT_PROCESSING_INSTRUCTION
	processImport
  end
end

#processStartElementObject



435
436
437
438
439
440
441
442
443
444
445
446
447
448
# File 'lib/jrubyfx-fxmlloader.rb', line 435

def processStartElement()
# Create the element
createElement();

# Process the start tag
@current.processStartElement();

# Set the root value
unless @root
	@root = @current.value;
    rputs @root, "__local_jruby_ext[:on_root_set].call(self) if __local_jruby_ext[:on_root_set]"
    @jruby_ext[:on_root_set].call(@root) if @jruby_ext[:on_root_set]
  end
end