All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.royale.compiler.internal.css.CSSManager Maven / Gradle / Ivy

/*
 *
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You 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.royale.compiler.internal.css;

import static org.apache.royale.compiler.internal.css.semantics.CSSSemanticAnalyzer.*;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.commons.io.FilenameUtils;

import org.apache.royale.compiler.config.Configuration;
import org.apache.royale.compiler.css.ICSSDocument;
import org.apache.royale.compiler.css.ICSSManager;
import org.apache.royale.compiler.css.ICSSRule;
import org.apache.royale.compiler.definitions.IClassDefinition;
import org.apache.royale.compiler.definitions.IDefinition;
import org.apache.royale.compiler.filespecs.IFileSpecification;
import org.apache.royale.compiler.internal.caches.CSSDocumentCache;
import org.apache.royale.compiler.internal.caches.CacheStoreKeyBase;
import org.apache.royale.compiler.internal.css.codegen.CSSCompilationSession;
import org.apache.royale.compiler.internal.projects.RoyaleProject;
import org.apache.royale.compiler.internal.scopes.ASProjectScope;
import org.apache.royale.compiler.internal.units.EmbedCompilationUnit;
import org.apache.royale.compiler.problems.CSSUnresolvedClassReferenceProblem;
import org.apache.royale.compiler.problems.ICompilerProblem;
import org.apache.royale.compiler.units.ICompilationUnit;
import org.apache.royale.swc.ISWC;
import org.apache.royale.swc.ISWCFileEntry;
import org.apache.royale.swc.ISWCManager;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;

/**
 * Project-level CSS manager.
 */
public class CSSManager implements ICSSManager
{

    /**
     * Find all the dependency class definitions and compilation units
     * introduced by the given CSS rules. This method is basically an
     * aggregation of {@link CSSSemanticAnalyzer#resolveDependencies()}.
     * 
     * @param session CSS compilation session that stores resolved embed
     * compilation units.
     * @param royaleProject Flex project.
     * @param cssRules Resolve class dependencies in these CSS rules.
     * @param problems Problem collection.
     */
    static void getClassDefinitionDependencies(
            final CSSCompilationSession session,
            final RoyaleProject royaleProject,
            final ImmutableSet cssRules,
            final Set classReferences,
            final Set embedCompilationUnits,
            final Collection problems)
    {
        for (final ICSSRule matchedRule : cssRules)
        {
            resolveDependencies(
                    session.resolvedEmbedProperties,
                    matchedRule,
                    royaleProject,
                    classReferences,
                    embedCompilationUnits,
                    problems);
        }
    }

    /**
     * Find compilation units for the given definition set. A definition might
     * not have a compilation unit, but a compiler problem must already be
     * logged for it in {@code problems} before this method is called.
     * 
     * @param royaleProject Flex project.
     * @param classDefinitions Class definitions.
     * @param problems Problems collection.
     * @return A set of compilation unit for the given class definitions.
     */
    static ImmutableSet getCompilationUnitsForDefinitions(
            final RoyaleProject royaleProject,
            final Set classDefinitions,
            final Collection problems)
    {
        final ImmutableSet.Builder builder = new ImmutableSet.Builder();
        for (final IClassDefinition classDefinition : classDefinitions)
        {
            final ASProjectScope scope = royaleProject.getScope();
            final ICompilationUnit compilationUnit = scope.getCompilationUnitForDefinition(classDefinition);
            if (compilationUnit != null)
                builder.add(compilationUnit);
            else
                assert problemCreatedForUnresolvedClassReference(problems, classDefinition) : "Can't find compilation unit for class '" + classDefinition.getQualifiedName() + "'. Expected a 'CSSUnresolvedClassReference'.";
        }
        return builder.build();
    }

    /**
     * Check that a {@link CSSUnresolvedClassReferenceProblem} problem is created for
     * the given class definition.
     * 
     * @param problems Problems collection.
     * @param classDefinition {@link IClassDefinition} definition of the unresolved class.
     * @return True if there's a problem for the unresolved class definition.
     */
    static boolean problemCreatedForUnresolvedClassReference(
            final Collection problems,
            final IClassDefinition classDefinition)
    {
        for (final ICompilerProblem problem : problems)
        {
            if (problem instanceof CSSUnresolvedClassReferenceProblem)
            {
                final CSSUnresolvedClassReferenceProblem unresolved = (CSSUnresolvedClassReferenceProblem)problem;
                if (unresolved.qname.equals(classDefinition.getQualifiedName()))
                    return true;
            }
        }
        return false;
    }

    /** Owner project. */
    private final RoyaleProject royaleProject;

    /**
     * Initialize a CSS manager.
     * 
     * @param royaleProject Owner project.
     */
    public CSSManager(final RoyaleProject royaleProject)
    {
        assert royaleProject != null;
        this.royaleProject = royaleProject;
    }

    @Override
    public Collection getCSSFromStyleModules()
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Collection getCSSFromSWCDefaultStyle()
    {
        final Collection result = new ArrayList();
        for (final ISWC swc : royaleProject.getLibraries())
        {
            final ICSSDocument css = getDefaultCSS(swc.getSWCFile());
            if (css != null)
                result.add(css);
        }
        return result;
    }

    @Override
    public ICSSDocument getDefaultCSS(final File swcFile)
    {
        final ISWCManager swcManager = royaleProject.getWorkspace().getSWCManager();
        final CSSDocumentCache cache = (CSSDocumentCache)swcManager.getCSSDocumentCache();
        final ISWC swc = swcManager.get(swcFile);
        return cache.getDefaultsCSS(swc, royaleProject.getCompatibilityVersion());
    }
    
    @Override
    public Collection getDependentCompilationUnitsFromCSS(
            final CSSCompilationSession session,
            final ICSSDocument cssDocument,
            final Collection definitions,
            final Collection problems)
    {
        assert cssDocument != null : "Expected CSS document to get CSS dependencies.";
        assert definitions != null : "Expected a set of definitions to activate CSS rules.";
        assert problems != null : "Expected problems collection. Do not ignore problems.";

        final ImmutableSet qnames = buildQNameToDefinitionMap(getClassDefinitionSet(definitions)).keySet();

        // IFilter (ICSSRule[], IClassDefinition[]) -> (ICSSRule[] applies to IClassDefinition[])
        final ImmutableSet matchedRules = getMatchedRules(session, royaleProject, cssDocument, qnames, problems);

        // Find IClassDefinition(ClassReference) and CompilationUnit(Embed) from the matched ICSSRule[] 
        final Set classReferences = new HashSet();
        final Set embedCompilationUnits = new HashSet();
        getClassDefinitionDependencies(
                session,
                royaleProject,
                matchedRules,
                classReferences, // output
                embedCompilationUnits, // output
                problems);

        // Find ICompilationUnit[] for IClassDefinition(ClassReference)[]
        final ImmutableSet classReferenceCompilationUnits =
                getCompilationUnitsForDefinitions(royaleProject, classReferences, problems);

        // Only "activated" rules are included in code generation.
        for (final ICSSRule rule : matchedRules)
        {
            session.activatedRules.add(rule);
        }

        // Compilation units from ClassReference() and Embed().
        return new ImmutableSet.Builder()
                .addAll(classReferenceCompilationUnits)
                .addAll(embedCompilationUnits)
                .build();
    }

    @Override
    public Collection getCSSFromThemes(final Collection problems)
    {
        final ImmutableList.Builder builder = new ImmutableList.Builder();
        final ISWCManager swcManager = royaleProject.getWorkspace().getSWCManager();
        final CSSDocumentCache cssCache = (CSSDocumentCache)swcManager.getCSSDocumentCache();
        for (final IFileSpecification themeFile : royaleProject.getThemeFiles())
        {
            try
            {
                final ICSSDocument css;
                final String extension = FilenameUtils.getExtension(themeFile.getPath());
                if ("swc".equalsIgnoreCase(extension))
                {
                    final ISWC swc = swcManager.get(new File(themeFile.getPath()));
                    css = cssCache.getDefaultsCSS(
                                swc,
                                royaleProject.getCompatibilityVersion());
                }
                else if ("css".equalsIgnoreCase(extension))
                {
                    final CacheStoreKeyBase key = CSSDocumentCache.createKey(themeFile.getPath());
                    css = cssCache.get(key);
                }
                else
                {
                    continue;
                }

                // Ignore theme file without a defaults CSS.
                if (css != null && css != CSSDocumentCache.EMPTY_CSS_DOCUMENT)
                    builder.add(css);
                
                if ("swc".equalsIgnoreCase(extension))
                {
                    final ISWC swc = swcManager.get(new File(themeFile.getPath()));
                    // add other css files.
                    Map files = swc.getFiles();
                    Set fileNames = files.keySet();
                    for (String fileName : fileNames)
                    {
                    	String suffix = FilenameUtils.getExtension(fileName);
                    	if ("css".equalsIgnoreCase(suffix) && !fileName.contains("default"))
                    	{
                            final CacheStoreKeyBase key = CSSDocumentCache.createKey(swc, fileName);
                            final ICSSDocument extracss = cssCache.get(key);
                            builder.add(extracss);
                    	}
                    }
                }
            }
            catch (CSSDocumentCache.ProblemParsingCSSRuntimeException cssError)
            {
                problems.addAll(cssError.cssParserProblems);
            }

        }
        return builder.build();
    }

    @Override
    public boolean isFlex3CSS()
    {
        final Integer compatibilityVersion = royaleProject.getCompatibilityVersion();
        if (compatibilityVersion == null)
            return false;
        else if (compatibilityVersion > Configuration.MXML_VERSION_3_0)
            return false;
        else
            return true;
    }

    @Override
    public ICSSDocument getCSS(String cssFilename)
    {
        final CSSDocumentCache cache = (CSSDocumentCache)royaleProject.getWorkspace().getSWCManager().getCSSDocumentCache();
        final CacheStoreKeyBase key = CSSDocumentCache.createKey(cssFilename);
        final ICSSDocument css = cache.get(key);
        
        if(CSSDocumentCache.EMPTY_CSS_DOCUMENT == css)
            return null;
        else
            return css;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy