You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CodeColorizers.java 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * SonarQube is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.batch.source;
  21. import com.google.common.collect.Lists;
  22. import java.io.BufferedReader;
  23. import java.io.File;
  24. import java.io.FileInputStream;
  25. import java.io.InputStreamReader;
  26. import java.io.Reader;
  27. import java.nio.charset.Charset;
  28. import java.util.HashMap;
  29. import java.util.List;
  30. import java.util.Map;
  31. import javax.annotation.CheckForNull;
  32. import org.apache.commons.io.input.BOMInputStream;
  33. import org.apache.commons.lang.StringUtils;
  34. import org.slf4j.Logger;
  35. import org.slf4j.LoggerFactory;
  36. import org.sonar.api.batch.BatchSide;
  37. import org.sonar.api.batch.sensor.highlighting.NewHighlighting;
  38. import org.sonar.api.web.CodeColorizerFormat;
  39. import org.sonar.colorizer.JavaTokenizers;
  40. import org.sonar.colorizer.Tokenizer;
  41. /**
  42. * Central point for sonar-colorizer extensions
  43. */
  44. @BatchSide
  45. public class CodeColorizers {
  46. private static final Logger LOG = LoggerFactory.getLogger(CodeColorizers.class);
  47. private final Map<String, CodeColorizerFormat> byLang;
  48. public CodeColorizers(List<CodeColorizerFormat> formats) {
  49. byLang = new HashMap<>();
  50. for (CodeColorizerFormat format : formats) {
  51. byLang.put(format.getLanguageKey(), format);
  52. }
  53. LOG.debug("Code colorizer, supported languages: " + StringUtils.join(byLang.keySet(), ","));
  54. }
  55. /**
  56. * Used when no plugin is defining some CodeColorizerFormat
  57. */
  58. public CodeColorizers() {
  59. this(Lists.<CodeColorizerFormat>newArrayList());
  60. }
  61. @CheckForNull
  62. public void toSyntaxHighlighting(File file, Charset charset, String language, NewHighlighting highlighting) {
  63. CodeColorizerFormat format = byLang.get(language);
  64. List<Tokenizer> tokenizers;
  65. if (format == null) {
  66. // Workaround for Java test code since Java plugin only provides highlighting for main source and no colorizer
  67. // TODO can be dropped when Java plugin embed its own CodeColorizerFormat of (better) provides highlighting for tests
  68. // See SONARJAVA-830
  69. if ("java".equals(language)) {
  70. tokenizers = JavaTokenizers.forHtml();
  71. } else {
  72. return;
  73. }
  74. } else {
  75. tokenizers = format.getTokenizers();
  76. }
  77. try (Reader reader = new BufferedReader(new InputStreamReader(new BOMInputStream(new FileInputStream(file)), charset))) {
  78. new HighlightingRenderer().render(reader, tokenizers, highlighting);
  79. } catch (Exception e) {
  80. LOG.warn("Unable to perform colorization of file " + file, e);
  81. }
  82. }
  83. }