Clover coverage report - PMD - 3.7 Coverage timestamp: Wed May 31 2006 09:25:59 EDT FRAMES NO FRAMES |

file stats: | LOC: | 137 |  | Methods: | 14
| NCLOC: | 108 |  | Classes: | 4

Source file | Conditionals | Statements | Methods | TOTAL
SourceCode.java | 78.6% | 86.4% | 71.4% | 81.9% |

1 |  | /**
2 |  | * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3 |  | */
4 |  | package net.sourceforge.pmd.cpd;
5 |  |
6 |  | import net.sourceforge.pmd.PMD;
7 |  |
8 |  | import java.io.File;
9 |  | import java.io.FileReader;
10 |  | import java.io.LineNumberReader;
11 |  | import java.io.Reader;
12 |  | import java.io.StringReader;
13 |  | import java.lang.ref.SoftReference;
14 |  | import java.util.ArrayList;
15 |  | import java.util.List;
16 |  |
17 |  | public class SourceCode {
18 |  |
19 |  | public static abstract class CodeLoader {
20 |  | private SoftReference code;
21 |  |
22 | 20 | public List getCode() {
23 | 20 | List c = null;
24 | 20 | if (code != null) {
25 | 7 | c = (List) code.get();
26 |  | }
27 | 20 | if (c != null) {
28 | 7 | return c;
29 |  | }
30 | 13 | this.code = new SoftReference(load());
31 | 13 | return (List) code.get();
32 |  | }
33 |  |
34 |  | public abstract String getFileName();
35 |  |
36 |  | protected abstract Reader getReader() throws Exception;
37 |  |
38 | 13 | protected List load() {
39 | 13 | LineNumberReader lnr = null;
40 | 13 | try {
41 | 13 | lnr = new LineNumberReader(getReader());
42 | 13 | List lines = new ArrayList();
43 | 13 | String currentLine;
44 | ? | while ((currentLine = lnr.readLine()) != null) {
45 | 54 | lines.add(currentLine);
46 |  | }
47 | 13 | return lines;
48 |  | } catch (Exception e) {
49 | 0 | throw new RuntimeException("Problem while reading " + getFileName() + ":" + e.getMessage());
50 |  | } finally {
51 | 13 | try {
52 | 13 | if (lnr != null)
53 | 13 | lnr.close();
54 |  | } catch (Exception e) {
55 | 0 | throw new RuntimeException("Problem while reading " + getFileName() + ":" + e.getMessage());
56 |  | }
57 |  | }
58 |  | }
59 |  | }
60 |  |
61 |  | public static class FileCodeLoader extends CodeLoader {
62 |  | private File file;
63 |  |
64 | 0 | public FileCodeLoader(File file) {
65 | 0 | this.file = file;
66 |  | }
67 |  |
68 | 0 | public Reader getReader() throws Exception {
69 | 0 | return new FileReader(file);
70 |  | }
71 |  |
72 | 0 | public String getFileName() {
73 | 0 | return this.file.getAbsolutePath();
74 |  | }
75 |  | }
76 |  |
77 |  | public static class StringCodeLoader extends CodeLoader {
78 |  | public static final String DEFAULT_NAME = "CODE_LOADED_FROM_STRING";
79 |  |
80 |  | private String source_code;
81 |  |
82 |  | private String name;
83 |  |
84 | 10 | public StringCodeLoader(String code) {
85 | 10 | this(code, DEFAULT_NAME);
86 |  | }
87 |  |
88 | 13 | public StringCodeLoader(String code, String name) {
89 | 13 | this.source_code = code;
90 | 13 | this.name = name;
91 |  | }
92 |  |
93 | 13 | public Reader getReader() {
94 | 13 | return new StringReader(source_code);
95 |  | }
96 |  |
97 | 235 | public String getFileName() {
98 | 235 | return name;
99 |  | }
100 |  | }
101 |  |
102 |  | private CodeLoader cl;
103 |  |
104 | 13 | public SourceCode(CodeLoader cl) {
105 | 13 | this.cl = cl;
106 |  | }
107 |  |
108 | 0 | public List getCode() {
109 | 0 | return cl.getCode();
110 |  | }
111 |  |
112 | 13 | public StringBuffer getCodeBuffer() {
113 | 13 | StringBuffer sb = new StringBuffer();
114 | 13 | List lines = cl.getCode();
115 | 13 | for (int i = 0; i < lines.size(); i++) {
116 | 54 | sb.append((String) lines.get(i));
117 | 54 | sb.append(PMD.EOL);
118 |  | }
119 | 13 | return sb;
120 |  | }
121 |  |
122 | 7 | public String getSlice(int startLine, int endLine) {
123 | 7 | StringBuffer sb = new StringBuffer();
124 | 7 | List lines = cl.getCode();
125 | 7 | for (int i = startLine - 1; i < endLine && i < lines.size(); i++) {
126 | 9 | if (sb.length() != 0) {
127 | 2 | sb.append(PMD.EOL);
128 |  | }
129 | 9 | sb.append((String) lines.get(i));
130 |  | }
131 | 7 | return sb.toString();
132 |  | }
133 |  |
134 | 235 | public String getFileName() {
135 | 235 | return cl.getFileName();
136 |  | }
137 |  | }