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.

SourceCode.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program 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. * This program 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. /**
  21. * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
  22. */
  23. package net.sourceforge.pmd.cpd;
  24. import java.io.File;
  25. import java.io.FileInputStream;
  26. import java.io.InputStreamReader;
  27. import java.io.LineNumberReader;
  28. import java.io.Reader;
  29. import java.io.StringReader;
  30. import java.lang.ref.SoftReference;
  31. import java.util.ArrayList;
  32. import java.util.List;
  33. /**
  34. * <p>Not intended to be instantiated by clients.</p>
  35. *
  36. * @since 2.2
  37. * @deprecated since 5.5
  38. */
  39. @Deprecated
  40. public class SourceCode {
  41. public static final String EOL = System.getProperty("line.separator", "\n");
  42. public abstract static class CodeLoader {
  43. private SoftReference<List<String>> code;
  44. public List<String> getCode() {
  45. List<String> c = null;
  46. if (code != null) {
  47. c = code.get();
  48. }
  49. if (c != null) {
  50. return c;
  51. }
  52. this.code = new SoftReference<>(load());
  53. return code.get();
  54. }
  55. public abstract String getFileName();
  56. protected abstract Reader getReader() throws Exception;
  57. protected List<String> load() {
  58. try (LineNumberReader lnr = new LineNumberReader(getReader())) {
  59. List<String> lines = new ArrayList<>();
  60. String currentLine;
  61. while ((currentLine = lnr.readLine()) != null) {
  62. lines.add(currentLine);
  63. }
  64. return lines;
  65. } catch (Exception e) {
  66. throw new IllegalStateException("Problem while reading " + getFileName() + ":" + e.getMessage(), e);
  67. }
  68. }
  69. }
  70. public static class FileCodeLoader extends CodeLoader {
  71. private File file;
  72. private String encoding;
  73. public FileCodeLoader(File file, String encoding) {
  74. this.file = file;
  75. this.encoding = encoding;
  76. }
  77. @Override
  78. public Reader getReader() throws Exception {
  79. return new InputStreamReader(new FileInputStream(file), encoding);
  80. }
  81. @Override
  82. public String getFileName() {
  83. return this.file.getAbsolutePath();
  84. }
  85. }
  86. public static class StringCodeLoader extends CodeLoader {
  87. public static final String DEFAULT_NAME = "CODE_LOADED_FROM_STRING";
  88. private String sourceCode;
  89. private String name;
  90. public StringCodeLoader(String code) {
  91. this(code, DEFAULT_NAME);
  92. }
  93. public StringCodeLoader(String code, String name) {
  94. this.sourceCode = code;
  95. this.name = name;
  96. }
  97. @Override
  98. public Reader getReader() {
  99. return new StringReader(sourceCode);
  100. }
  101. @Override
  102. public String getFileName() {
  103. return name;
  104. }
  105. }
  106. private CodeLoader cl;
  107. public SourceCode(CodeLoader cl) {
  108. this.cl = cl;
  109. }
  110. public List<String> getCode() {
  111. return cl.getCode();
  112. }
  113. public StringBuffer getCodeBuffer() {
  114. StringBuffer sb = new StringBuffer();
  115. List<String> lines = cl.getCode();
  116. for (String line : lines) {
  117. sb.append(line);
  118. sb.append(EOL);
  119. }
  120. return sb;
  121. }
  122. public String getSlice(int startLine, int endLine) {
  123. StringBuffer sb = new StringBuffer();
  124. List lines = cl.getCode();
  125. for (int i = (startLine == 0 ? startLine : (startLine - 1)); i < endLine && i < lines.size(); i++) {
  126. if (sb.length() != 0) {
  127. sb.append(EOL);
  128. }
  129. sb.append((String) lines.get(i));
  130. }
  131. return sb.toString();
  132. }
  133. /**
  134. * Within Sonar Ecosystem - absolute path to file containing code,
  135. * whereas in fact existence of such file not guaranteed - see {@link StringCodeLoader}.
  136. */
  137. public String getFileName() {
  138. return cl.getFileName();
  139. }
  140. }