UberBuilderRegistration.groovy
001 /*
002  * Copyright 2007-2012 the original author or authors.
003  *
004  * Licensed under the Apache License, Version 2.0 (the "License")
005  * you may not use this file except in compliance with the License.
006  * You may obtain a copy of the License at
007  *
008  *     http://www.apache.org/licenses/LICENSE-2.0
009  *
010  * Unless required by applicable law or agreed to in writing, software
011  * distributed under the License is distributed on an "AS IS" BASIS,
012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013  * See the License for the specific language governing permissions and
014  * limitations under the License.
015  */
016 package org.codehaus.griffon.runtime.builder
017 
018 /**
019  @author Danno Ferrin
020  @author Andres Almiray
021  */
022 class UberBuilderRegistration {
023     private static final Closure[] EMPTY_CLOSURE_ARRAY = new Closure[0]
024 
025     Factory factory
026     FactoryBuilderSupport builder
027     String prefixString
028 
029     UberBuilderRegistration(String prefixString, FactoryBuilderSupport builder) {
030         this.@prefixString = prefixString
031         this.@builder = builder
032     }
033 
034     UberBuilderRegistration(String prefixString, Factory factory) {
035         this.@prefixString = prefixString
036         this.@factory = factory
037     }
038 
039     Factory nominateFactory(String name) {
040         if (builder) {
041             // need to turn off proxy to get at class durring lookup
042             def oldProxy = builder.proxyBuilder
043             try {
044                 builder.proxyBuilder = builder
045                 String localName = name
046                 if (prefixString && name.startsWith(prefixString)) {
047                     localName = name.substring(prefixString.length())
048                 }
049                 localName = builder.getName(localName)
050                 if (builder.factories.containsKey(localName)) {
051                     return builder.factories[localName]
052                 }
053             finally {
054                 builder.proxyBuilder = oldProxy
055             }
056         }
057         if (factory) {
058             if (name == prefixString) {
059                 return factory
060             }
061         }
062         return null
063     }
064 
065     Closure nominateExplicitMethod(String name) {
066         if (builder) {
067             // need to turn off proxy to get at class durring lookup
068             def oldProxy = builder.proxyBuilder
069             try {
070                 builder.proxyBuilder = builder
071                 String localName = name
072                 if (prefixString && name.startsWith(prefixString)) {
073                     localName = name.substring(prefixString.length())
074                 }
075                 localName = builder.getName(localName)
076                 if (builder.getLocalExplicitMethods().containsKey(localName)) {
077                     return builder.getLocalExplicitMethods()[localName]
078                 }
079             finally {
080                 builder.proxyBuilder = oldProxy
081             }
082         }
083         return null
084     }
085 
086     Closure[] nominateExplicitProperty(String name) {
087         if (builder) {
088             // need to turn off proxy to get at class durring lookup
089             def oldProxy = builder.proxyBuilder
090             try {
091                 builder.proxyBuilder = builder
092                 String localName = name
093                 if (prefixString && name.startsWith(prefixString)) {
094                     localName = name.substring(prefixString.length())
095                 }
096                 localName = builder.getName(localName)
097                 if (builder.explicitProperties.containsKey(localName)) {
098                     return builder.explicitProperties[localName]
099                 }
100             finally {
101                 builder.proxyBuilder = oldProxy
102             }
103         }
104         return EMPTY_CLOSURE_ARRAY
105     }
106 
107     String toString() {
108         return "UberBuilderRegistration{ factory '$factory' builder '$builder' prefix '$prefixString' }"
109     }
110 }