Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  // Copyright 2009 The Apache Software Foundation
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  // http://www.apache.org/licenses/LICENSE-2.0
  //
  // Unless required by applicable law or agreed to in writing, software
 // distributed under the License is distributed on an "AS IS" BASIS,
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
 package org.apache.tapestry5.internal.pageload;
 
 
 import java.util.List;
 import java.util.Map;
 
 {
     private final ComponentAssemblerSource assemblerSource;
 
 
 
     private final Instantiator instantiator;
 
     private final Locale locale;
 
     private final ComponentPageElementResources resources;
 
     private final List<PageAssemblyActionactions = CollectionFactory.newList();
 
     private final IdAllocator allocator = new IdAllocator();
 
     private final OperationTracker tracker;
 
 
 
     public ComponentAssemblerImpl(ComponentAssemblerSource assemblerSource,
             ComponentInstantiatorSource instantiatorSourceComponentClassResolver componentClassResolver,
             Instantiator instantiatorComponentPageElementResources resourcesLocale localeOperationTracker tracker)
     {
         this. = assemblerSource;
         this. = instantiatorSource;
         this. = componentClassResolver;
         this. = instantiator;
         this. = resources;
         this. = locale;
         this. = tracker;
     }
 
     public ComponentPageElement assembleRootComponent(final Page page)
     {
         return .invoke("Assembling root component for page " + page.getName(),
                 new Invokable<ComponentPageElement>()
                 {
                     public ComponentPageElement invoke()
                     {
                         return performAssembleRootComponent(page);
                     }
                 });
     }
 
     {
         PageAssembly pageAssembly = new PageAssembly(page);
 
         try
         {
             ComponentPageElement newElement = new ComponentPageElementImpl(pageAssembly.page);
 
             pageAssembly.componentName.push(new ComponentName(pageAssembly.page.getName()));
 
             pageAssembly.page.addLifecycleListener(newElement);
 
             addRootComponentMixins(newElement);
            pushNewElement(pageAssemblynewElement);
            runActions(pageAssembly);
            popNewElement(pageAssembly);
            // Execute the deferred actions in reverse order to how they were added. This makes
            // sense, as (currently) all deferred actions are related to inheriting informal parameters;
            // those are added deepest component to shallowed (root) component, but should be executed
            // in the opposite order to ensure that chained inherited parameters resolve correctly.
            int count = pageAssembly.deferred.size();
            for (int i = count - 1; i >= 0; i--)
            {
                PageAssemblyAction action = pageAssembly.deferred.get(i);
                action.execute(pageAssembly);
            }
            return pageAssembly.createdElement.peek();
        }
        catch (RuntimeException ex)
        {
            throw new RuntimeException(PageloadMessages.exceptionAssemblingRootComponent(pageAssembly.page.getName(),
                    InternalUtils.toMessage(ex)), ex);
        }
    }
    private void addRootComponentMixins(ComponentPageElement element)
    {
        for (String className : .getModel().getMixinClassNames())
        {
            Instantiator mixinInstantiator = .getInstantiator(className);
            ComponentModel model = .getModel();
            element.addMixin(InternalUtils.lastTerm(className), mixinInstantiatormodel.getOrderForMixin(className));
        }
    }
    public void assembleEmbeddedComponent(final PageAssembly pageAssembly,
            final EmbeddedComponentAssembler embeddedAssemblerfinal String embeddedIdfinal String elementName,
            final Location location)
    {
        ComponentName containerName = pageAssembly.componentName.peek();
        final ComponentName embeddedName = containerName.child(embeddedId.toLowerCase());
        final String componentClassName = .getModel().getComponentClassName();
        String description = String.format("Assembling component %s (%s)"embeddedName.completeIdcomponentClassName);
        .run(descriptionnew Runnable()
        {
            public void run()
            {
                ComponentPageElement container = pageAssembly.activeElement.peek();
                try
                {
                    pageAssembly.componentName.push(embeddedName);
                    ComponentPageElement newElement = container.newChild(embeddedIdembeddedName.nestedId,
                            embeddedName.completeIdelementNamelocation);
                    pageAssembly.page.addLifecycleListener(newElement);
                    pushNewElement(pageAssemblynewElement);
                    embeddedAssembler.addMixinsToElement(newElement);
                    runActions(pageAssembly);
                    popNewElement(pageAssembly);
                    pageAssembly.componentName.pop();
                }
                catch (RuntimeException ex)
                {
                    throw new TapestryException(PageloadMessages.exceptionAssemblingEmbeddedComponent(embeddedId,
                            componentClassNamecontainer.getCompleteId(), InternalUtils.toMessage(ex)), locationex);
                }
            }
        });
    }
    private void pushNewElement(PageAssembly pageAssemblyfinal ComponentPageElement componentElement)
    {
        // This gets popped after all actions have executed.
        pageAssembly.activeElement.push(componentElement);
        // The container pops this one.
        pageAssembly.createdElement.push(componentElement);
        BodyPageElement shunt = new BodyPageElement()
        {
            public void addToBody(RenderCommand element)
            {
                componentElement.addToTemplate(element);
            }
        };
        pageAssembly.bodyElement.push(shunt);
    }
    private void popNewElement(PageAssembly pageAssembly)
    {
        pageAssembly.bodyElement.pop();
        pageAssembly.activeElement.pop();
        // But the component itself stays on the createdElement stack!
    }
    private void runActions(PageAssembly pageAssembly)
    {
        for (PageAssemblyAction action : )
            action.execute(pageAssembly);
    }
    public ComponentModel getModel()
    {
        return .getModel();
    }
    public void add(PageAssemblyAction action)
    {
        .add(action);
    }
    public void validateEmbeddedIds(Map<StringLocationcomponentIdsResource templateResource)
    {
        Map<StringBooleanembeddedIds = CollectionFactory.newCaseInsensitiveMap();
        for (String id : getModel().getEmbeddedComponentIds())
            embeddedIds.put(idtrue);
        for (String id : componentIds.keySet())
        {
            .allocateId(id);
            embeddedIds.remove(id);
        }
        if (!embeddedIds.isEmpty())
        {
            String className = getModel().getComponentClassName();
            throw new RuntimeException(PageloadMessages.embeddedComponentsNotInTemplate(InternalUtils
                    .joinSorted(embeddedIds.keySet()), className, InternalUtils.lastTerm(className), templateResource));
        }
    }
    public String generateEmbeddedId(String componentType)
    {
        // Component types may be in folders; strip off the folder part for starters.
        int slashx = componentType.lastIndexOf("/");
        String baseId = componentType.substring(slashx + 1).toLowerCase();
        // The idAllocator is pre-loaded with all the component ids from the template, so even
        // if the lower-case type matches the id of an existing component, there won't be a name
        // collision.
        return .allocateId(baseId);
    }
    public EmbeddedComponentAssembler createEmbeddedAssembler(String embeddedIdString componentClassName,
            EmbeddedComponentModel embeddedModelString mixinsLocation location)
    {
        try
        {
            if (InternalUtils.isBlank(componentClassName)) { throw new TapestryException(PageloadMessages
                    .missingComponentType(), locationnull); }
                    componentClassNameembeddedModelmixins,
                    location);
            if ( == null)
                 = CollectionFactory.newMap();
            .put(embeddedIdembedded);
            if (embeddedModel != null)
            {
                for (String publishedParameterName : embeddedModel.getPublishedParameters())
                {
                    if ( == null)
                         = CollectionFactory.newCaseInsensitiveMap();
                    String existingEmbeddedId = .get(publishedParameterName);
                    if (existingEmbeddedId != null) { throw new TapestryException(PageloadMessages
                            .parameterAlreadyPublished(publishedParameterNameembeddedId.getModel()
                                    .getComponentClassName(), existingEmbeddedId), locationnull); }
                    .put(publishedParameterNameembeddedId);
                }
            }
            return embedded;
        }
        catch (Exception ex)
        {
            throw new TapestryException(PageloadMessages.failureCreatingEmbeddedComponent(embeddedId
                    .getModel().getComponentClassName(), InternalUtils.toMessage(ex)), locationex);
        }
    }
    public ParameterBinder getBinder(final String parameterName)
    {
        final String embeddedId = InternalUtils.get(parameterName);
        if (embeddedId == null)
            return null;
        final EmbeddedComponentAssembler embededdedComponentAssembler = .get(embeddedId);
        final ComponentAssembler embeddedAssembler = embededdedComponentAssembler.getComponentAssembler();
        final ParameterBinder embeddedBinder = embeddedAssembler.getBinder(parameterName);
        // The complex case: a re-publish! Yes you can go deep here if you don't
        // value your sanity!
        if (embeddedBinder != null) { return new ParameterBinder()
        {
            public void bind(ComponentPageElement elementBinding binding)
            {
                ComponentPageElement subelement = element.getEmbeddedElement(embeddedId);
                embeddedBinder.bind(subelementbinding);
            }
            public String getDefaultBindingPrefix(String metaDefault)
            {
                return embeddedBinder.getDefaultBindingPrefix(metaDefault);
            }
        }; }
        final ParameterBinder innerBinder = embededdedComponentAssembler.createParameterBinder(parameterName);
        if (innerBinder == null)
        {
            String message = PageloadMessages.publishedParameterNonexistant(parameterName.getModel()
                    .getComponentClassName(), embeddedId);
            throw new TapestryException(messageembededdedComponentAssembler.getLocation(), null);
        }
        // The simple case, publishing a parameter of a subcomponent as if it were a parameter
        // of this component.
        return new ParameterBinder()
        {
            public void bind(ComponentPageElement elementBinding binding)
            {
                ComponentPageElement subelement = element.getEmbeddedElement(embeddedId);
                innerBinder.bind(subelementbinding);
            }
            public String getDefaultBindingPrefix(String metaDefault)
            {
                return innerBinder.getDefaultBindingPrefix(metaDefault);
            }
        };
    }
    public Locale getLocale()
    {
        return ;
    }
    @Override
    public String toString()
    {
        return String.format("ComponentAssembler[%s]".getModel().getComponentClassName());
    }
New to GrepCode? Check out our FAQ X