001: /**
002: * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
003: */package net.sourceforge.pmd.cpd;
004:
005: import net.sourceforge.pmd.PMD;
006:
007: import java.io.File;
008: import java.io.LineNumberReader;
009: import java.io.Reader;
010: import java.io.StringReader;
011: import java.io.FileInputStream;
012: import java.io.InputStreamReader;
013: import java.lang.ref.SoftReference;
014: import java.util.ArrayList;
015: import java.util.List;
016:
017: public class SourceCode {
018:
019: public static abstract class CodeLoader {
020: private SoftReference<List<String>> code;
021:
022: public List<String> getCode() {
023: List<String> c = null;
024: if (code != null) {
025: c = code.get();
026: }
027: if (c != null) {
028: return c;
029: }
030: this .code = new SoftReference<List<String>>(load());
031: return code.get();
032: }
033:
034: public abstract String getFileName();
035:
036: protected abstract Reader getReader() throws Exception;
037:
038: protected List<String> load() {
039: LineNumberReader lnr = null;
040: try {
041: lnr = new LineNumberReader(getReader());
042: List<String> lines = new ArrayList<String>();
043: String currentLine;
044: while ((currentLine = lnr.readLine()) != null) {
045: lines.add(currentLine);
046: }
047: return lines;
048: } catch (Exception e) {
049: e.printStackTrace();
050: throw new RuntimeException("Problem while reading "
051: + getFileName() + ":" + e.getMessage());
052: } finally {
053: try {
054: if (lnr != null)
055: lnr.close();
056: } catch (Exception e) {
057: throw new RuntimeException("Problem while reading "
058: + getFileName() + ":" + e.getMessage());
059: }
060: }
061: }
062: }
063:
064: public static class FileCodeLoader extends CodeLoader {
065: private File file;
066: private String encoding;
067:
068: public FileCodeLoader(File file, String encoding) {
069: this .file = file;
070: this .encoding = encoding;
071: }
072:
073: public Reader getReader() throws Exception {
074: return new InputStreamReader(new FileInputStream(file),
075: encoding);
076: }
077:
078: public String getFileName() {
079: return this .file.getAbsolutePath();
080: }
081: }
082:
083: public static class StringCodeLoader extends CodeLoader {
084: public static final String DEFAULT_NAME = "CODE_LOADED_FROM_STRING";
085:
086: private String source_code;
087:
088: private String name;
089:
090: public StringCodeLoader(String code) {
091: this (code, DEFAULT_NAME);
092: }
093:
094: public StringCodeLoader(String code, String name) {
095: this .source_code = code;
096: this .name = name;
097: }
098:
099: public Reader getReader() {
100: return new StringReader(source_code);
101: }
102:
103: public String getFileName() {
104: return name;
105: }
106: }
107:
108: private CodeLoader cl;
109:
110: public SourceCode(CodeLoader cl) {
111: this .cl = cl;
112: }
113:
114: public List<String> getCode() {
115: return cl.getCode();
116: }
117:
118: public StringBuffer getCodeBuffer() {
119: StringBuffer sb = new StringBuffer();
120: List<String> lines = cl.getCode();
121: for (String line : lines) {
122: sb.append(line);
123: sb.append(PMD.EOL);
124: }
125: return sb;
126: }
127:
128: public String getSlice(int startLine, int endLine) {
129: StringBuffer sb = new StringBuffer();
130: List lines = cl.getCode();
131: for (int i = startLine - 1; i < endLine && i < lines.size(); i++) {
132: if (sb.length() != 0) {
133: sb.append(PMD.EOL);
134: }
135: sb.append((String) lines.get(i));
136: }
137: return sb.toString();
138: }
139:
140: public String getFileName() {
141: return cl.getFileName();
142: }
143: }
|