Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  // Copyright 2010 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.transform;
 
 import java.util.List;
 
Implements the org.apache.tapestry5.annotations.Import annotation, both at the class and at the method level.

Since:
5.2.0
 
 public class ImportWorker implements ComponentClassTransformWorker
 {
     private final JavaScriptSupport javascriptSupport;
 
     private final SymbolSource symbolSource;
 
     private final AssetSource assetSource;
 
     private final Worker<AssetimportLibrary = new Worker<Asset>()
     {
         public void work(Asset asset)
         {
             .importJavaScriptLibrary(asset);
         }
     };
 
     private final Worker<AssetimportStylesheet = new Worker<Asset>()
     {
         public void work(Asset asset)
         {
             .importStylesheet(asset);
         };
     };
 
     public ImportWorker(JavaScriptSupport javascriptSupportSymbolSource symbolSourceAssetSource assetSource)
     {
         this. = javascriptSupport;
         this. = symbolSource;
         this. = assetSource;
     }
 
     public void transform(ClassTransformation transformationMutableComponentModel model)
     {
         processClassAnnotationAtSetupRenderPhase(transformationmodel);
 
         for (TransformMethod m : transformation.matchMethodsWithAnnotation(Import.class))
         {
             decorateMethod(transformationmodelm);
         }
     }
 
     private void processClassAnnotationAtSetupRenderPhase(ClassTransformation transformation,
             MutableComponentModel model)
     {
         Import annotation = transformation.getAnnotation(Import.class);
 
         if (annotation == null)
             return;
 
         TransformMethod setupRender = transformation.getOrCreateMethod(.);
 
         decorateMethod(transformationmodelsetupRenderannotation);
 
         model.addRenderPhase(SetupRender.class);
     }
 
     private void decorateMethod(ClassTransformation transformationMutableComponentModel modelTransformMethod method)
     {
         Import annotation = method.getAnnotation(Import.class);
 
        decorateMethod(transformationmodelmethodannotation);
    }
    private void decorateMethod(ClassTransformation transformationMutableComponentModel model,
            TransformMethod methodImport annotation)
    {
        importStacks(methodannotation.stack());
        importLibraries(transformationmodelmethodannotation.library());
        importStylesheets(transformationmodelmethodannotation.stylesheet());
    }
    private void importStacks(TransformMethod methodString[] stacks)
    {
        if (stacks.length != 0)
            method.addAdvice(createImportStackAdvice(stacks));
    }
    private ComponentMethodAdvice createImportStackAdvice(final String[] stacks)
    {
        return new ComponentMethodAdvice()
        {
            public void advise(ComponentMethodInvocation invocation)
            {
                for (String stack : stacks)
                {
                    .importStack(stack);
                    invocation.proceed();
                }
            }
        };
    }
    private void importLibraries(ClassTransformation transformationMutableComponentModel model,
            TransformMethod methodString[] paths)
    {
        decorateMethodWithOperation(transformationmodelmethodpaths);
    }
    private void importStylesheets(ClassTransformation transformationMutableComponentModel model,
            TransformMethod methodString[] paths)
    {
        decorateMethodWithOperation(transformationmodelmethodpaths);
    }
    private void decorateMethodWithOperation(ClassTransformation transformationMutableComponentModel model,
            TransformMethod methodString[] pathsWorker<Assetoperation)
    {
        if (paths.length == 0)
            return;
        String[] expandedPaths = expandPaths(paths);
        FieldAccess access = createFieldForAssets(transformation);
        storeLocalizedAssetsAtPageLoad(transformationmodel.getBaseResource(), expandedPathsaccess);
        addMethodAssetOperationAdvice(methodaccessoperation);
    }
    private String[] expandPaths(String[] paths)
    {
        String[] result = new String[paths.length];
        for (int i = 0; i < paths.lengthi++)
            result[i] = .expandSymbols(paths[i]);
        return result;
    }
    private FieldAccess createFieldForAssets(ClassTransformation transformation)
    {
        TransformField field = transformation.createField(.List.class.getName(), "includedAssets");
        return field.getAccess();
    }
    private void storeLocalizedAssetsAtPageLoad(ClassTransformation transformationfinal Resource baseResource,
            final String[] expandedPathsfinal FieldAccess access)
    {
        ComponentMethodAdvice advice = new ComponentMethodAdvice()
        {
            public void advise(ComponentMethodInvocation invocation)
            {
                invocation.proceed();
                ComponentResources resources = invocation.getComponentResources();
                List<Assetassets = convertPathsToAssets(baseResourceresources.getLocale(), expandedPaths);
                access.write(invocation.getInstance(), assets);
            }
        };
    }
    private List<AssetconvertPathsToAssets(final Resource baseResourcefinal Locale localeString[] assetPaths)
    {
        return F.flow(assetPaths).map(new Mapper<StringAsset>()
        {
            public Asset map(String assetPath)
            {
                return .getAsset(baseResourceassetPathlocale);
            }
        }).toList();
    }
    private void addMethodAssetOperationAdvice(TransformMethod methodfinal FieldAccess access,
            final Worker<Assetoperation)
    {
        {
            public void invoke(Component instance)
            {
                List<Assetassets = (List<Asset>) access.read(instance);
                F.flow(assets).each(operation);
            }
        };
        method.addOperationBefore(advice);
    }
New to GrepCode? Check out our FAQ X