001    // Copyright 2006, 2007, 2008, 2010 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    // http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry5.internal.services;
016    
017    import org.apache.tapestry5.ComponentResources;
018    import org.apache.tapestry5.internal.structure.ComponentPageElement;
019    import org.apache.tapestry5.internal.structure.Page;
020    import org.apache.tapestry5.ioc.internal.util.InternalUtils;
021    import org.apache.tapestry5.runtime.Component;
022    import org.apache.tapestry5.services.ComponentClassResolver;
023    import org.apache.tapestry5.services.ComponentSource;
024    import org.apache.tapestry5.services.RequestGlobals;
025    
026    @SuppressWarnings("all")
027    public class ComponentSourceImpl implements ComponentSource
028    {
029        private final RequestPageCache pageCache;
030    
031        private final ComponentClassResolver resolver;
032    
033        private final RequestGlobals globals;
034    
035        public ComponentSourceImpl(RequestPageCache pageCache, ComponentClassResolver resolver, RequestGlobals globals)
036        {
037            this.pageCache = pageCache;
038            this.resolver = resolver;
039            this.globals = globals;
040        }
041    
042        public Component getComponent(String completeId)
043        {
044            assert InternalUtils.isNonBlank(completeId);
045    
046            int colonx = completeId.indexOf(':');
047    
048            if (colonx < 0)
049            {
050                Page page = pageCache.get(completeId);
051    
052                return page.getRootComponent();
053            }
054    
055            String pageName = completeId.substring(0, colonx);
056    
057            Page page = pageCache.get(pageName);
058            String nestedId = completeId.substring(colonx + 1);
059            String mixinId = null;
060    
061            int dollarx = nestedId.indexOf("$");
062    
063            if (dollarx > 0)
064            {
065                mixinId = nestedId.substring(dollarx + 1);
066                nestedId = nestedId.substring(0, dollarx);
067            }
068    
069            ComponentPageElement element = page.getComponentElementByNestedId(nestedId);
070    
071            if (mixinId == null)
072                return element.getComponent();
073    
074            ComponentResources resources = element.getMixinResources(mixinId);
075    
076            return resources.getComponent();
077        }
078    
079        public Component getPage(String pageName)
080        {
081            assert pageName != null;
082    
083            Page page = pageCache.get(pageName);
084    
085            return page.getRootComponent();
086        }
087    
088        public Component getPage(Class pageClass)
089        {
090            assert pageClass != null;
091    
092            String pageName = resolver.resolvePageClassNameToPageName(pageClass.getName());
093    
094            return getPage(pageName);
095        }
096    
097        public Component getActivePage()
098        {
099            String pageName = globals.getActivePageName();
100    
101            return pageName == null ? null : getPage(pageName);
102        }
103    
104    }