01 /*
02 * Copyright 2008-2015 the original author or authors.
03 *
04 * Licensed under the Apache License, Version 2.0 (the "License");
05 * you may not use this file except in compliance with the License.
06 * You may obtain a copy of the License at
07 *
08 * http://www.apache.org/licenses/LICENSE-2.0
09 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package griffon.builder.pivot.factory
17
18 import org.apache.pivot.beans.BXMLSerializer
19
20 import java.lang.reflect.Field
21
22 import static griffon.builder.pivot.PivotUtils.setBeanProperty
23
24 /**
25 * @author Andres Almiray
26 */
27 class BxmlFactory extends AbstractFactory {
28 Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes) throws InstantiationException, IllegalAccessException {
29 if (value instanceof GString) value = value.toString()
30 if (!value) {
31 if (attributes.containsKey('src')) {
32 value = attributes.remove('src').toString()
33 } else {
34 throw new IllegalArgumentException("In $name you must define a value for src: or a node value that points to a bxml resource.")
35 }
36 }
37 if (!value.startsWith('/')) value = '/' + value
38
39 def bxml = new BXMLSerializer()
40 def root = bxml.readObject(builder.app, value)
41 def rootId = attributes['id'] ? attributes['id'] + '_' : ''
42
43 Field namedObjectsField = BXMLSerializer.class.getDeclaredField('namedObjects')
44 namedObjectsField.setAccessible(true)
45 def pivotMap = namedObjectsField.get(bxml)
46 pivotMap.each { id ->
47 builder.setVariable(rootId + id, pivotMap.get(id))
48 }
49
50 return root
51 }
52
53 boolean onHandleNodeAttributes(FactoryBuilderSupport builder, Object node, Map attributes) {
54 attributes.each { property, value ->
55 setBeanProperty(property, value, node)
56 }
57
58 return false
59 }
60 }
|