Class: FxmlLoader

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

Constant Summary collapse

FX_NAMESPACE_VERSION =
"1"

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.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/jrubyfx-fxmlloader.rb', line 141

def initialize(url=nil, ctrlr=nil, resourcs=nil, buildFactory=nil, charset=nil, loaders=nil)
  @location = url
  @builderFactory = buildFactory || JavaFXBuilderFactory.new
  @template = false
  if resourcs
    dputs "WHOA WHOAT!!!! resources"
    dp resourcs
  end
  if loaders
    dputs "WHOA WHOAT!!!! loaders"
    dp loaders
  end
  @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.



138
139
140
# File 'lib/jrubyfx-fxmlloader.rb', line 138

def builderFactory
  @builderFactory
end

#controllerObject

Returns the value of attribute controller.



139
140
141
# File 'lib/jrubyfx-fxmlloader.rb', line 139

def controller
  @controller
end

#controllerFactoryObject

Returns the value of attribute controllerFactory.



138
139
140
# File 'lib/jrubyfx-fxmlloader.rb', line 138

def controllerFactory
  @controllerFactory
end

#currentObject

Returns the value of attribute current.



138
139
140
# File 'lib/jrubyfx-fxmlloader.rb', line 138

def current
  @current
end

#locationObject

Returns the value of attribute location.



138
139
140
# File 'lib/jrubyfx-fxmlloader.rb', line 138

def location
  @location
end

#namespaceObject

Returns the value of attribute namespace.



138
139
140
# File 'lib/jrubyfx-fxmlloader.rb', line 138

def namespace
  @namespace
end

#rootObject

Returns the value of attribute root.



138
139
140
# File 'lib/jrubyfx-fxmlloader.rb', line 138

def root
  @root
end

#staticLoadObject

Returns the value of attribute staticLoad.



138
139
140
# File 'lib/jrubyfx-fxmlloader.rb', line 138

def staticLoad
  @staticLoad
end

#templateObject

Returns the value of attribute template.



138
139
140
# File 'lib/jrubyfx-fxmlloader.rb', line 138

def template
  @template
end

Instance Method Details

#clearImportsObject



237
238
239
240
# File 'lib/jrubyfx-fxmlloader.rb', line 237

def clearImports
  @packages.clear
  @classes.clear
end

#compareJFXVersions(rtVer, nsVer) ⇒ Object



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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
# File 'lib/jrubyfx-fxmlloader.rb', line 603

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

#constantize(camel_cased_word) ⇒ Object

steal handy methods from activesupport Tries to find a constant with the name specified in the argument string.

‘Module’.constantize # => Module ‘Test::Unit’.constantize # => Test::Unit

The name is assumed to be the one of a top-level constant, no matter whether it starts with “::” or not. No lexical context is taken into account:

C = ‘outside’ module M C = ‘inside’ C # => ‘inside’ ‘C’.constantize # => ‘outside’, same as ::C end

NameError is raised when the name is not in CamelCase or the constant is unknown.



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
# File 'lib/jrubyfx-fxmlloader.rb', line 466

def constantize(camel_cased_word)
  names = camel_cased_word.split('.')
  names.shift if names.empty? || names.first.empty?

  names.inject(Object) do |constant, name|
    if constant == Object
      constant.const_get(name)
    else
      candidate = constant.const_get(name)
      next candidate if constant.const_defined?(name, false)
      next candidate unless Object.const_defined?(name)

      # Go down the ancestors to check it it's owned
      # directly before we reach Object or the end of ancestors.
      constant = constant.ancestors.inject do |const, ancestor|
        break const if ancestor == Object
        break ancestor if ancestor.const_defined?(name, false)
        const
      end

      # owner is in Object, so raise
      constant.const_get(name, false)
    end
  end
end

#createElementObject



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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/jrubyfx-fxmlloader.rb', line 300

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
      dputs "#{prefixz}Creating new stuff"
      dprint prefixz
      dp localName
      dprint prefixz
      dp type

		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



589
590
591
592
593
594
595
596
# File 'lib/jrubyfx-fxmlloader.rb', line 589

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

return @scriptEngineManager;
end

#getType(name) ⇒ Object



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
561
# File 'lib/jrubyfx-fxmlloader.rb', line 523

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 = constantize(name)
				rescue 
					# No-op
				end
       end
       @classes[name] = type if type
		end
	end

	return type;
end

#importClass(name) ⇒ Object



439
440
441
442
443
444
445
# File 'lib/jrubyfx-fxmlloader.rb', line 439

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

#importPackage(name) ⇒ Object



435
436
437
# File 'lib/jrubyfx-fxmlloader.rb', line 435

def importPackage(name)
	@packages << name
end

#loadObject



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/jrubyfx-fxmlloader.rb', line 170

def load()
  dp "This is the namespace", @namespace
  # TODO: actually open it properly
  inputStream = @location.open_stream
  if @template
    @root = nil
  else
    clearImports
  end

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

  @script_engine = nil

  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()
      dputs "......"
      event = @xmlStreamReader.next();
      dputs "#{event} aout happened, dude"
		case event
      when XMLStreamConstants::PROCESSING_INSTRUCTION
        dputs "processing instr"
        processProcessingInstruction
      when XMLStreamConstants::COMMENT
        dputs "processing comment"
        processComment
      when XMLStreamConstants::START_ELEMENT
        dputs "processing start"
        processStartElement
      when XMLStreamConstants::END_ELEMENT
        dputs "processing end"
        processEndElement
      when XMLStreamConstants::CHARACTERS
        dputs "processing chars"
        processCharacters
      end
    end
rescue XMLStreamException => exception
	raise Exception.new(exception)
  end
  dputs "Saving stuff!!!!s"
  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


  @xmlStreamReader = nil
  return @root
end

#loadType(name, cache) ⇒ Object



563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
# File 'lib/jrubyfx-fxmlloader.rb', line 563

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



598
599
600
601
602
# File 'lib/jrubyfx-fxmlloader.rb', line 598

def loadTypeForPackage(packageName, className=nil)
packageName = (packageName + "." + className.gsub('.', '$')) if className
  #TODO: fix for ruby stuff
return Java.java.lang.Class::forName(packageName, true, FXL::default_class_loader);
end

#processCharactersObject



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

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

#processCommentObject



283
284
285
# File 'lib/jrubyfx-fxmlloader.rb', line 283

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

#processEndElementObject



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

def processEndElement()
   dputs "ending!!!!!!!!"
	@current.processEndElement();
	if @loadListener
		@loadListener.endElement(@current.value);
	end

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

#processImportObject



269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/jrubyfx-fxmlloader.rb', line 269

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



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/jrubyfx-fxmlloader.rb', line 251

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)
	scriptEngine.setBindings(scriptEngineManager.getBindings(), ScriptContext.ENGINE_SCOPE)
  end
end

#processProcessingInstructionObject



242
243
244
245
246
247
248
249
# File 'lib/jrubyfx-fxmlloader.rb', line 242

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

#processStartElementObject



287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/jrubyfx-fxmlloader.rb', line 287

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

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

# Set the root value
unless @root
	@root = @current.value;
  end
end

#safe_constantize(camel_cased_word) ⇒ Object

Tries to find a constant with the name specified in the argument string.

‘Module’.safe_constantize # => Module ‘Test::Unit’.safe_constantize # => Test::Unit

The name is assumed to be the one of a top-level constant, no matter whether it starts with “::” or not. No lexical context is taken into account:

C = ‘outside’ module M C = ‘inside’ C # => ‘inside’ ‘C’.safe_constantize # => ‘outside’, same as ::C end

nil is returned when the name is not in CamelCase or the constant (or part of it) is unknown.

‘blargle’.safe_constantize # => nil ‘UnknownModule’.safe_constantize # => nil ‘UnknownModule::Foo::Bar’.safe_constantize # => nil



514
515
516
517
518
519
520
521
# File 'lib/jrubyfx-fxmlloader.rb', line 514

def safe_constantize(camel_cased_word)
  constantize(camel_cased_word)
rescue NameError => e
  raise unless e.message =~ /(uninitialized constant|wrong constant name) #{const_regexp(camel_cased_word)}$/ ||
    e.name.to_s == camel_cased_word.to_s
rescue ArgumentError => e
  raise unless e.message =~ /not missing constant #{const_regexp(camel_cased_word)}\!$/
end