Class: Element

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Element.


10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/fxmlloader/elts.rb', line 10

def initialize(current, xmlStreamReader, loadListener, parentLoader)
  @parent = current;
  @lineNumber = xmlStreamReader.getLocation().getLineNumber();
  @current = current;
  @xmlStreamReader = xmlStreamReader
  @loadListener = loadListener
  @value = nil
  @valueAdapter = nil
  @eventHandlerAttributes = []
  @instancePropertyAttributes = []
  @staticPropertyAttributes = []
  @staticPropertyElements = []
  @parentLoader = parentLoader
end

Instance Attribute Details

#currentObject

Returns the value of attribute current


8
9
10
# File 'lib/fxmlloader/elts.rb', line 8

def current
  @current
end

#eventHandlerAttributesObject

Returns the value of attribute eventHandlerAttributes


9
10
11
# File 'lib/fxmlloader/elts.rb', line 9

def eventHandlerAttributes
  @eventHandlerAttributes
end

#instancePropertyAttributesObject

Returns the value of attribute instancePropertyAttributes


9
10
11
# File 'lib/fxmlloader/elts.rb', line 9

def instancePropertyAttributes
  @instancePropertyAttributes
end

#lineNumberObject

Returns the value of attribute lineNumber


8
9
10
# File 'lib/fxmlloader/elts.rb', line 8

def lineNumber
  @lineNumber
end

#loadListenerObject

Returns the value of attribute loadListener


8
9
10
# File 'lib/fxmlloader/elts.rb', line 8

def loadListener
  @loadListener
end

#parentObject

Returns the value of attribute parent


8
9
10
# File 'lib/fxmlloader/elts.rb', line 8

def parent
  @parent
end

#parentLoaderObject

Returns the value of attribute parentLoader


8
9
10
# File 'lib/fxmlloader/elts.rb', line 8

def parentLoader
  @parentLoader
end

#staticPropertyAttributesObject

Returns the value of attribute staticPropertyAttributes


9
10
11
# File 'lib/fxmlloader/elts.rb', line 9

def staticPropertyAttributes
  @staticPropertyAttributes
end

#staticPropertyElementsObject

Returns the value of attribute staticPropertyElements


9
10
11
# File 'lib/fxmlloader/elts.rb', line 9

def staticPropertyElements
  @staticPropertyElements
end

#valueObject

Returns the value of attribute value


9
10
11
# File 'lib/fxmlloader/elts.rb', line 9

def value
  @value
end

#valueAdapterObject

Returns the value of attribute valueAdapter


9
10
11
# File 'lib/fxmlloader/elts.rb', line 9

def valueAdapter
  @valueAdapter
end

#xmlStreamReaderObject

Returns the value of attribute xmlStreamReader


8
9
10
# File 'lib/fxmlloader/elts.rb', line 8

def xmlStreamReader
  @xmlStreamReader
end

Instance Method Details

#add(element, prop_name = nil, rputs_elt = nil) ⇒ Object


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/fxmlloader/elts.rb', line 55

def add(element, prop_name=nil, rputs_elt=nil)
  # If value is a list, add element to it; otherwise, get the value
  # of the default property, which is assumed to be a list and add
  # to that (coerce to the appropriate type)
  if (@value.kind_of?(Enumerable) || @value.java_kind_of?(java.util.List))
    if prop_name == nil
      rputs value, "add(#{rget(element)||element.inspect})"
    else
      rputs @parent.value, "get#{prop_name[0].upcase}#{prop_name[1..-1]}.add(#{rputs_elt || rget(element)||element.inspect})"
    end
    value.to_java
  else
    type = value.java_class
    defaultProperty = type.annotation(DefaultProperty.java_class);
    defaultPropertyName = defaultProperty.to_java.value();

    # Get the list value
    list =  getProperties[defaultPropertyName]

    # Coerce the element to the list item type
    if (!java.util.Map.java_class.assignable_from?(type))
      listType = @valueAdapter.getGenericType(defaultPropertyName);
      element = RubyWrapperBeanAdapter.coerce(element, RubyWrapperBeanAdapter.getListItemType(listType));
    end
    rputs @value, "get#{defaultPropertyName[0].upcase}#{defaultPropertyName[1..-1]}.add(#{rget(element)||element.inspect})"
    list = list.to_java if list.class == Java::JavaObject
    list
  end.add(element)
end

#addEventHandler(attribute, eventHandler) ⇒ Object


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

def addEventHandler(attribute, eventHandler)
  if (attribute.name.end_with?(FXL::CHANGE_EVENT_HANDLER_SUFFIX))
    i = FXL::EVENT_HANDLER_PREFIX.length();
    j = attribute.name.length() - FXL::CHANGE_EVENT_HANDLER_SUFFIX.length();
    if (i == j)
      if (@value.is_a? ObservableList)
        list =  @value;
        list.addListener(ObservableListChangeAdapter.new(list, eventHandler));
      elsif (@value.is_a? ObservableMap)
        map = @value;
        map.addListener(ObservableMapChangeAdapter.new(map, eventHandler));
      else
        raise LoadException.new("Invalid event source.");
      end
    else
      key = attribute.name[i].downcase + attribute.name[i + 1, j]
      propertyModel = getValueAdapter().getPropertyModel(key);
      if (propertyModel == nil)
        raise LoadException.new(@value.getClass().getName() + " does not define" + " a property model for \"" + key + "\".");
      end

      propertyModel.addListener(PropertyChangeAdapter.new(@value,  eventHandler));
    end
  else
    getValueAdapter[attribute.name] =  eventHandler
  end
end

#applyProperty(name, sourceType, value) ⇒ Object


412
413
414
415
416
417
418
# File 'lib/fxmlloader/elts.rb', line 412

def applyProperty(name,  sourceType, value)
  if (sourceType == nil)
    getProperties[name] = value
  else
    RubyWrapperBeanAdapter.put3(@value, sourceType, name, value);
  end
end

#callzObject


27
28
29
30
31
32
33
34
35
# File 'lib/fxmlloader/elts.rb', line 27

def callz
  numz = 1
  pppn = @parent
  while pppn
    numz+=1
    pppn = pppn.parent
  end
  (" " * numz) + @lineNumber.to_s + ": "
end

#getPropertiesObject


116
117
118
# File 'lib/fxmlloader/elts.rb', line 116

def getProperties()
  return (isTyped()) ? getValueAdapter() : @value;
end

#getValueAdapterObject


109
110
111
112
113
114
# File 'lib/fxmlloader/elts.rb', line 109

def getValueAdapter()
  if (@valueAdapter == nil)
    @valueAdapter = RubyWrapperBeanAdapter.for(@value)
  end
  return @valueAdapter;
end

#isBidirectionalBindingExpression(aValue) ⇒ Object


240
241
242
# File 'lib/fxmlloader/elts.rb', line 240

def isBidirectionalBindingExpression(aValue)
  return aValue.start_with?(FXL::BI_DIRECTIONAL_BINDING_PREFIX);
end

#isBindingExpression(aValue) ⇒ Object


235
236
237
238
# File 'lib/fxmlloader/elts.rb', line 235

def isBindingExpression(aValue)
  # TODO: BINDING_EXPRESSION_PREFIX == ${
  aValue.start_with?("${") && aValue.end_with?(FXL::BINDING_EXPRESSION_SUFFIX);
end

#isCollectionObject


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fxmlloader/elts.rb', line 37

def isCollection()
  # Return true if value is a list, or if the value's type defines
  # a default property that is a list
  collection = false
  if (@value.kind_of?(Enumerable) || @value.java_kind_of?(Java.java.util.List))
    collection = true;
  else
    defaultProperty = @value.java_class.annotation(DefaultProperty.java_class);

    if (defaultProperty != nil)
      collection = getProperties()[defaultProperty.value].java_kind_of?(Java.java.util.List)
    else
      collection = false;
    end
  end
  return collection;
end

#isTypedObject


105
106
107
# File 'lib/fxmlloader/elts.rb', line 105

def isTyped()
  !(@value.java_kind_of? Java::java.util.Map or @value.is_a? Hash  )
end

#populateArrayFromString(type, stringValue) ⇒ Object

TODO: fix this udp to use java arrays


359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/fxmlloader/elts.rb', line 359

def populateArrayFromString( type, stringValue)

  propertyValue = nil;
  # Split the string and set the values as an array
  componentType = type.getComponentType();

  if (stringValue.length > 0)
    values = stringValue.split(FXL::ARRAY_COMPONENT_DELIMITER);
    propertyValue = Array.newInstance(componentType, values.length);
    values.length.times do |i|
      Array.set(propertyValue, i,
        RubyWrapperBeanAdapter.coerce(resolvePrefixedValue(values[i].strip),
          type.getComponentType()));
    end
  else
    propertyValue = Array.newInstance(componentType, 0);
  end
  return propertyValue;
end

#populateListFromString(valueAdapter, listPropertyName, stringValue) ⇒ Object

TODO: check the types


386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/fxmlloader/elts.rb', line 386

def populateListFromString( valueAdapter, listPropertyName,stringValue)
  # Split the string and add the values to the list
  list =  valueAdapter[listPropertyName].to_java
  listType = valueAdapter.getGenericType(listPropertyName);
  itemType =  RubyWrapperBeanAdapter.getGenericListItemType(listType);

  if (itemType.is_a? ParameterizedType)
    itemType = ( itemType).getRawType();
  end

  if (stringValue.length() > 0)
    values = stringValue.split(FXL::ARRAY_COMPONENT_DELIMITER)

    for  aValue in values
      aValue = aValue.strip
      list.add(
        RubyWrapperBeanAdapter.coerce(resolvePrefixedValue(aValue),
          itemType));
    end
  end
end

#processAttribute(prefix, localName, value) ⇒ Object


151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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
# File 'lib/fxmlloader/elts.rb', line 151

def processAttribute(prefix, localName, value)
  if (prefix == nil)
    # Add the attribute to the appropriate list
    if (localName.start_with?(FXL::EVENT_HANDLER_PREFIX))
      if (@loadListener != nil)
        @loadListener.readEventHandlerAttribute(localName, value);
      end
      eventHandlerAttributes << (Attribute.new(localName, nil, value));
    else
      i = localName.rindex('.');

      if (i == nil)
        # The attribute represents an instance property
        if (@loadListener != nil)
          @loadListener.readPropertyAttribute(localName, nil, value);
        end

        instancePropertyAttributes << (Attribute.new(localName, nil, value));
      else
        # The attribute represents a static property
        name = localName[(i + 1)..-1];
        sourceType = parentLoader.getType(localName[0, i]);

        if (sourceType != nil)
          if (@loadListener != nil)
            @loadListener.readPropertyAttribute(name, sourceType, value);
          end

          @staticPropertyAttributes << (Attribute.new(name, sourceType, value));
        elsif (staticLoad)
          if (@loadListener != nil)
            @loadListener.readUnknownStaticPropertyAttribute(localName, value);
          end
        else
          raise LoadException.new(localName + " is not a valid attribute.");
        end
      end

    end
  else
    raise LoadException.new(prefix + ":" + localName +
        " is not a valid attribute.");
  end
end

#processCharactersObject

Raises:


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

def processCharacters()
  raise LoadException.new("Unexpected characters in input stream.");
end

#processEndElementObject


135
136
137
# File 'lib/fxmlloader/elts.rb', line 135

def processEndElement()
  # No-op
end

#processEventHandlerAttributesObject


420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/fxmlloader/elts.rb', line 420

def processEventHandlerAttributes()
  if (@eventHandlerAttributes.length > 0 && !parentLoader.staticLoad)
    for attribute in @eventHandlerAttributes
      eventHandler = nil;
      attrValue = attribute.value;

      if (attrValue.start_with?(FXL::CONTROLLER_METHOD_PREFIX))
        attrValue = attrValue[FXL::CONTROLLER_METHOD_PREFIX.length..-1]
        if (!attrValue.start_with?(FXL::CONTROLLER_METHOD_PREFIX))
          if (attrValue.length() == 0)
            raise LoadException.new("Missing controller method.");
          end
          if (parentLoader.controller == nil)
            dputs "eek"
            raise LoadException.new("No controller specified. ");
          end

          #            method = parentLoader.controller.method(attrValue)
          #
          #            if (method == nil)
          #              raise LoadException.new("Controller method \"" + attrValue + "\" not found.");
          #            end
          eventHandler = EventHandlerWrapper.new(parentLoader.controller, attrValue)
        end

      elsif (attrValue.start_with?(FXL::EXPRESSION_PREFIX))
        attrValue = attrValue[FXL::EXPRESSION_PREFIX.length..-1]
        if (attrValue.length() == 0)
          raise LoadException.new("Missing expression reference.");
        end
        expression = Expression.get(@namespace, KeyPath.parse(attrValue));
        if (expression.is_a? EventHandler)
          eventHandler = expression;
        end

      end
      if (eventHandler == nil)
        if (attrValue.length() == 0 || parentLoader.scriptEngine == nil)
          raise LoadException.new("Error resolving " + attribute.name + "='" + attribute.value +
              "', either the event handler is not in the Namespace or there is an error in the script.");
        end

        eventHandler = ScriptEventHandler.new(attrValue, parentLoader.scriptEngine);
      end
      # Add the handler
      if (eventHandler != nil)

        addEventHandler(attribute, eventHandler);
      end
    end
  end
end

#processInstancePropertyAttributesObject


143
144
145
146
147
148
149
# File 'lib/fxmlloader/elts.rb', line 143

def processInstancePropertyAttributes()
  if (@instancePropertyAttributes.length > 0)
    for attribute in @instancePropertyAttributes
      processPropertyAttribute(attribute);
    end
  end
end

#processPropertyAttribute(attribute) ⇒ Object


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

def processPropertyAttribute(attribute)
  value = attribute.value;
  if (isBindingExpression(value))
    # Resolve the expression

    if (attribute.sourceType != nil)
      raise LoadException.new("Cannot bind to static property.");
    end

    if (!isTyped())
      raise LoadException.new("Cannot bind to untyped object.");
    end

    # TODO We may want to identify binding properties in processAttribute()
    # and apply them after build() has been called
    if (@value.is_a? Builder)
      raise LoadException.new("Cannot bind to builder property.");
    end

    value = value[2..-2]    # TODO: BINDING_EXPRESSION_PREFIX == ${
    #value.length() - 1];
    # TODO: this only works for 7, not 8
    expression = Expression.valueOf(value);
    # Create the binding
    targetAdapter = RubyWrapperBeanAdapter.new(@value);
    propertyModel = targetAdapter.getPropertyModel(attribute.name).to_java
    type = targetAdapter.getType(attribute.name);
    if (propertyModel.is_a? Property)
      rputs @value, "#{attribute.name}Property.bind(RRExpressionValue.new(__local_namespace, Java::org.jruby.jfx8.Expression.valueOf(#{value.inspect}), Java::#{type.name.gsub(/[\$\.]/, "::")}.java_class))"
      #expression.value_property.addListener(JRExpressionTargetMapping.new(expression, getProperties(), Expression.split(value)));
      ( propertyModel).bind(RRExpressionValue.new(parentLoader.namespace, expression, type));
    end
  elsif (isBidirectionalBindingExpression(value))
    raise UnsupportedOperationException.new("This feature is not currently enabled.");
  else
    processValue3(attribute.sourceType, attribute.name, value);
  end
end

#processStartElementObject


120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/fxmlloader/elts.rb', line 120

def processStartElement()
  n = @xmlStreamReader.getAttributeCount()
  n.times do |i|
    prefix = @xmlStreamReader.getAttributePrefix(i);
    localName = @xmlStreamReader.getAttributeLocalName(i);
    value = @xmlStreamReader.getAttributeValue(i);

    if (@loadListener && prefix && prefix == (FXL::FX_NAMESPACE_PREFIX))
      @loadListener.readInternalAttribute(prefix + ":" + localName, value);
    end

    processAttribute(prefix, localName, value);
  end
end

#processValue3(sourceType, propertyName, aValue) ⇒ Object


244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/fxmlloader/elts.rb', line 244

def processValue3( sourceType,  propertyName,  aValue)


  processed = false;
  #process list or array first
  if (sourceType == nil && isTyped())
    lvalueAdapter = getValueAdapter();
    type = lvalueAdapter.getType(propertyName);

    if (type == nil)
      dputs "Processing values3 fails on: "
      dp sourceType, propertyName, aValue
      dp lvalueAdapter
      dp caller
      raise("Property \"" + propertyName          + "\" does not exist" + " or is read-only.");
    end
    if (List.java_class.assignable_from?(type) && lvalueAdapter.read_only?(propertyName))
      populateListFromString(lvalueAdapter, propertyName, aValue);
      processed = true;
    elsif false #TODO: fix type.ruby_class.ancestors.include? Enumerable
      applyProperty(propertyName, sourceType, populateArrayFromString(type, aValue));
      processed = true;
    end
  end
  if (!processed)
    applyProperty(propertyName, sourceType, resolvePrefixedValue(aValue));
    processed = true;
  end
  return processed;
end

#resolvePrefixedValue(aValue) ⇒ Object

Resolves value prefixed with RELATIVE_PATH_PREFIX and RESOURCE_KEY_PREFIX.


279
280
281
282
283
284
285
286
287
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
# File 'lib/fxmlloader/elts.rb', line 279

def resolvePrefixedValue(aValue)
  if (aValue.start_with?(FXL::ESCAPE_PREFIX))
    aValue = aValue[FXL::ESCAPE_PREFIX.length..-1]

    if (aValue.length == 0 || !(aValue.start_with?(FXL::ESCAPE_PREFIX) ||
            aValue.start_with?(FXL::RELATIVE_PATH_PREFIX) ||
            aValue.start_with?(FXL::RESOURCE_KEY_PREFIX) ||
            aValue.start_with?(FXL::EXPRESSION_PREFIX) ||
            aValue.start_with?(FXL::BI_DIRECTIONAL_BINDING_PREFIX)))
      raise LoadException.new("Invalid escape sequence.");
    end
    return aValue;
  elsif (aValue.start_with?(FXL::RELATIVE_PATH_PREFIX))
    aValue = aValue[FXL::RELATIVE_PATH_PREFIX.length..-1]
    if (aValue.length == 0)
      raise LoadException.new("Missing relative path.");
    end
    if (aValue.start_with?(FXL::RELATIVE_PATH_PREFIX))
      # The prefix was escaped
      warnDeprecatedEscapeSequence(RELATIVE_PATH_PREFIX);
      return aValue;
    else
      begin
        if $JRUBYFX_AOT_COMPILING
          return RelativeFXMLString.new(aValue, URL.new(parentLoader.location, aValue).to_s)
        else
          return (aValue[0] == '/') ? classLoader.getResource(aValue[1..-1]).to_s : URL.new(parentLoader.location, aValue).to_s
        end
      rescue MalformedURLException => e
        dp e
        dputs "#{parentLoader.location} + /+ #{aValue}"
        raise "whoops"
      end
    end
  elsif (aValue.start_with?(FXL::RESOURCE_KEY_PREFIX))
    aValue = aValue[FXL::RESOURCE_KEY_PREFIX.length..-1]
    if (aValue.length() == 0)
      raise LoadException.new("Missing resource key.");
    end
    if (aValue.start_with?(FXL::RESOURCE_KEY_PREFIX))
      # The prefix was escaped
      warnDeprecatedEscapeSequence(FXL::RESOURCE_KEY_PREFIX);
      return aValue;
    else
      # Resolve the resource value
      if (@resources == nil)
        raise LoadException.new("No resources specified.");
      end
      if (!@resources.has_key?(aValue))
        raise LoadException.new("Resource \"" + aValue + "\" not found.");
      end
      return @resources.getString(aValue);
    end
  elsif (aValue.start_with?(FXL::EXPRESSION_PREFIX))
    aValue = aValue[FXL::EXPRESSION_PREFIX.length..-1]
    if (aValue.length() == 0)
      raise LoadException.new("Missing expression.");
    end
    if (aValue.start_with?(FXL::EXPRESSION_PREFIX))
      # The prefix was escaped
      warnDeprecatedEscapeSequence(FXL::EXPRESSION_PREFIX);
      return aValue;
    elsif (aValue == (FXL::NULL_KEYWORD))
      # The attribute value is nil
      return nil;
    end
    # remove all nils, them add one in at the end so [0] returns nil if empty
    q = (KeyPath.parse(aValue).map{|i|parentLoader.namespace[i]} - [nil] + [nil])[0]
    return q
  end
  return aValue;
end

#set(value) ⇒ Object


85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/fxmlloader/elts.rb', line 85

def set(value)
  unless @value
    raise LoadException.new("Cannot set value on this element.");
  end

  # Apply value to this element's properties
  type = @value.java_class;
  defaultProperty = type.getAnnotation(DefaultProperty.java_class);
  if (defaultProperty == nil)
    raise LoadException.new("Element does not define a default property.");
  end

  getProperties[defaultProperty.value] = value
end

#staticLoadObject


24
25
26
# File 'lib/fxmlloader/elts.rb', line 24

def staticLoad
  @parentLoader.staticLoad
end

#updateValue(value) ⇒ Object


100
101
102
103
# File 'lib/fxmlloader/elts.rb', line 100

def updateValue(value)
  @value = value;
  @valueAdapter = nil;
end

#warnDeprecatedEscapeSequence(prefix) ⇒ Object


408
409
410
# File 'lib/fxmlloader/elts.rb', line 408

def warnDeprecatedEscapeSequence(prefix)
  puts(prefix + prefix + " is a deprecated escape sequence. "       + "Please use \\" + prefix + " instead.");
end