KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > pmd > cpd > SourceCode


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