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