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

com.google.gwt.resources.css.ExtractClassNamesVisitor Maven / Gradle / Ivy

/*
 * Copyright 2009 Google Inc.
 * 
 * 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 com.google.gwt.resources.css;

import com.google.gwt.core.ext.typeinfo.JClassType;
import com.google.gwt.resources.css.ast.Context;
import com.google.gwt.resources.css.ast.CssExternalSelectors;
import com.google.gwt.resources.css.ast.CssSelector;
import com.google.gwt.resources.css.ast.CssStylesheet;
import com.google.gwt.resources.css.ast.CssVisitor;
import com.google.gwt.resources.rg.CssResourceGenerator;

import java.util.HashSet;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.regex.Matcher;

/**
 * Collect all CSS class names in a stylesheet.
 */
public class ExtractClassNamesVisitor extends CssVisitor {
  /**
   * Extract all CSS class names in the provided stylesheet.
   */
  public static Set exec(CssStylesheet sheet) {
    return exec(sheet, new JClassType[0]);
  }

  /**
   * Extract all CSS class names in the provided stylesheet, modulo those
   * imported from another context.
   */
  public static Set exec(CssStylesheet sheet, JClassType... imports) {
    SortedSet ignoredPrefixes = new TreeSet();

    for (JClassType clazz : imports) {
      String prefix = CssResourceGenerator.getImportPrefix(clazz);
      ignoredPrefixes.add(prefix);
    }

    ExtractClassNamesVisitor v = new ExtractClassNamesVisitor(ignoredPrefixes);
    v.accept(sheet);
    return v.found;
  }

  private final Set found = new HashSet();
  private final SortedSet ignoredPrefixes;

  /**
   * Package-protected for testing.
   */
  ExtractClassNamesVisitor(SortedSet ignoredPrefixes) {
    this.ignoredPrefixes = ignoredPrefixes;
  }

  @Override
  public void endVisit(CssExternalSelectors x, Context ctx) {
    addAll(x.getClasses());
  }

  @Override
  public void endVisit(CssSelector x, Context ctx) {
    Matcher m = CssSelector.CLASS_SELECTOR_PATTERN.matcher(x.getSelector());
    while (m.find()) {
      add(m.group(1));
    }
  }

  /**
   * Package-protected for testing.
   */
  Set getFoundClasses() {
    return found;
  }

  private void add(String selector) {
    SortedSet headSet = ignoredPrefixes.headSet(selector);
    if (headSet.isEmpty() || !selector.startsWith(headSet.last())) {
      found.add(selector);
    }
  }

  private void addAll(Iterable selectors) {
    for (String selector : selectors) {
      add(selector);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy