UberBuilderRegistration.groovy
001 /*
002  * Copyright 2007-2013 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, Map attributes, Object value) {
040         if (builder) {
041             // need to turn off proxy to get at class durring lookup
042             def continuationData = builder.getContinuationData()
043             boolean needToPopContext = false;
044             try {
045                 builder.restoreFromContinuationData( [
046                         "proxyBuilder": builder,
047                         "contexts": builder.contexts
048                         ] )
049                 if (builder.context == null) {
050                     builder.pushContext();
051                 }
052                 String localName = name
053                 if (prefixString && name.startsWith(prefixString)) {
054                     localName = name.substring(prefixString.length())
055                 }
056                 localName = builder.getName(localName)
057                 return builder.resolveFactory(localName, attributes, value)
058             finally {
059                 if (needToPopContext) {
060                     builder.popContext()
061                 }
062                 builder.restoreFromContinuationData(continuationData)
063             }
064         }
065         if (factory) {
066             if (name == prefixString) {
067                 return factory
068             }
069         }
070         return null
071     }
072 
073     Closure nominateExplicitMethod(String name) {
074         if (builder) {
075             // need to turn off proxy to get at class durring lookup
076             def oldProxy = builder.proxyBuilder
077             try {
078                 builder.proxyBuilder = builder
079                 String localName = name
080                 if (prefixString && name.startsWith(prefixString)) {
081                     localName = name.substring(prefixString.length())
082                 }
083                 localName = builder.getName(localName)
084                 if (builder.getLocalExplicitMethods().containsKey(localName)) {
085                     return builder.getLocalExplicitMethods()[localName]
086                 }
087             finally {
088                 builder.proxyBuilder = oldProxy
089             }
090         }
091         return null
092     }
093 
094     Closure[] nominateExplicitProperty(String name) {
095         if (builder) {
096             // need to turn off proxy to get at class durring lookup
097             def oldProxy = builder.proxyBuilder
098             try {
099                 builder.proxyBuilder = builder
100                 String localName = name
101                 if (prefixString && name.startsWith(prefixString)) {
102                     localName = name.substring(prefixString.length())
103                 }
104                 localName = builder.getName(localName)
105                 if (builder.explicitProperties.containsKey(localName)) {
106                     return builder.explicitProperties[localName]
107                 }
108             finally {
109                 builder.proxyBuilder = oldProxy
110             }
111         }
112         return EMPTY_CLOSURE_ARRAY
113     }
114 
115     String toString() {
116         return "UberBuilderRegistration{ factory '$factory' builder '$builder' prefix '$prefixString' }"
117     }
118 }