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

com.google.errorprone.BugPatternIndexWriter Maven / Gradle / Ivy

There is a newer version: 2.37.0
Show newest version
/*
 * Copyright 2014 Google Inc. All Rights Reserved.
 *
 * 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.errorprone;

import static com.google.common.collect.Multimaps.index;

import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Ordering;
import com.google.errorprone.DocGenTool.Target;

import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;
import com.github.mustachejava.MustacheFactory;

import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

/**
 * @author [email protected] (Alex Eagle)
 */
public class BugPatternIndexWriter {

  void dump(Collection patterns, Writer w, Target target) throws IOException {

    Map>> data = new TreeMap<>(Ordering.natural().reverse());

    ListMultimap index =
        index(
            patterns,
            new Function() {
              @Override
              public String apply(BugPatternInstance input) {
                return (input.maturity.description + " : " + input.severity).replace("_", "\\_");
              }
            });

    for (Entry> entry : index.asMap().entrySet()) {
      data.put(
          entry.getKey(),
          FluentIterable.from(entry.getValue())
              .transform(
                  new Function>() {
                    @Override
                    public Map apply(BugPatternInstance input) {
                      return ImmutableMap.of("name", input.name, "summary", input.summary);
                    }
                  })
              .toSortedList(
                  new Ordering>() {
                    @Override
                    public int compare(Map left, Map right) {
                      return left.get("name").compareTo(right.get("name"));
                    }
                  }));
    }

    Map templateData = new HashMap<>();

    List> entryData = new ArrayList<>();
    for (Entry>> entry : data.entrySet()) {
      entryData.add(ImmutableMap.of("category", entry.getKey(), "checks", entry.getValue()));
    }
    templateData.put("bugpatterns", entryData);

    if (target == Target.EXTERNAL) {
      Map frontmatterData =
          ImmutableMap.builder()
              .put("title", "Bug Patterns")
              .put("layout", "master")
              .build();
      DumperOptions options = new DumperOptions();
      options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
      Yaml yaml = new Yaml(options);
      Writer yamlWriter = new StringWriter();
      yamlWriter.write("---\n");
      yaml.dump(frontmatterData, yamlWriter);
      yamlWriter.write("---\n");
      templateData.put("frontmatter", yamlWriter.toString());

      MustacheFactory mf = new DefaultMustacheFactory();
      Mustache mustache =
          mf.compile("com/google/errorprone/resources/bugpatterns_external.mustache");
      mustache.execute(w, templateData);
    } else {
      MustacheFactory mf = new DefaultMustacheFactory();
      Mustache mustache =
          mf.compile("com/google/errorprone/resources/bugpatterns_internal.mustache");
      mustache.execute(w, templateData);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy