1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
/*
* Sonar, open source software quality management tool.
* Copyright (C) 2008-2012 SonarSource
* mailto:contact AT sonarsource DOT com
*
* Sonar is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* Sonar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.cpd;
import java.io.*;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;
/**
* <p>Not intended to be instantiated by clients.</p>
*
* @since 2.2
*/
public class SourceCode {
public static final String EOL = System.getProperty("line.separator", "\n");
public static abstract class CodeLoader {
private SoftReference<List<String>> code;
public List<String> getCode() {
List<String> c = null;
if (code != null) {
c = code.get();
}
if (c != null) {
return c;
}
this.code = new SoftReference<List<String>>(load());
return code.get();
}
public abstract String getFileName();
protected abstract Reader getReader() throws Exception;
protected List<String> load() {
LineNumberReader lnr = null;
try {
lnr = new LineNumberReader(getReader());
List<String> lines = new ArrayList<String>();
String currentLine;
while ((currentLine = lnr.readLine()) != null) {
lines.add(currentLine);
}
return lines;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Problem while reading " + getFileName() + ":" + e.getMessage());
} finally {
try {
if (lnr != null)
lnr.close();
} catch (Exception e) {
throw new RuntimeException("Problem while reading " + getFileName() + ":" + e.getMessage());
}
}
}
}
public static class FileCodeLoader extends CodeLoader {
private File file;
private String encoding;
public FileCodeLoader(File file, String encoding) {
this.file = file;
this.encoding = encoding;
}
public Reader getReader() throws Exception {
return new InputStreamReader(new FileInputStream(file), encoding);
}
public String getFileName() {
return this.file.getAbsolutePath();
}
}
public static class StringCodeLoader extends CodeLoader {
public static final String DEFAULT_NAME = "CODE_LOADED_FROM_STRING";
private String source_code;
private String name;
public StringCodeLoader(String code) {
this(code, DEFAULT_NAME);
}
public StringCodeLoader(String code, String name) {
this.source_code = code;
this.name = name;
}
public Reader getReader() {
return new StringReader(source_code);
}
public String getFileName() {
return name;
}
}
private CodeLoader cl;
public SourceCode(CodeLoader cl) {
this.cl = cl;
}
public List<String> getCode() {
return cl.getCode();
}
public StringBuffer getCodeBuffer() {
StringBuffer sb = new StringBuffer();
List<String> lines = cl.getCode();
for (String line : lines) {
sb.append(line);
sb.append(EOL);
}
return sb;
}
public String getSlice(int startLine, int endLine) {
StringBuffer sb = new StringBuffer();
List lines = cl.getCode();
for (int i = (startLine == 0 ? startLine : startLine - 1); i < endLine && i < lines.size(); i++) {
if (sb.length() != 0) {
sb.append(EOL);
}
sb.append((String) lines.get(i));
}
return sb.toString();
}
/**
* Within Sonar Ecosystem - absolute path to file containing code,
* whereas in fact existence of such file not guaranteed - see {@link StringCodeLoader}.
*/
public String getFileName() {
return cl.getFileName();
}
}
|