001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020
021 /**
022 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
023 */
024 package org.sonar.duplications.cpd;
025
026 import net.sourceforge.pmd.cpd.*;
027 import net.sourceforge.pmd.util.FileFinder;
028
029 import java.io.File;
030 import java.io.FileNotFoundException;
031 import java.io.IOException;
032 import java.util.HashMap;
033 import java.util.Iterator;
034 import java.util.List;
035 import java.util.Map;
036
037 /**
038 * @deprecated since 2.14, will be removed soon, and in any case should not be used for unit tests in Sonar plugins:
039 * instead of using this class for tests, you should test only your implementation of {@link Tokenizer}
040 */
041 @Deprecated
042 public class CPD {
043
044 private Map<String, SourceCode> source = new HashMap<String, SourceCode>();
045 private CPDListener listener = new CPDNullListener();
046 private Tokens tokens = new Tokens();
047 private int minimumTileSize;
048 private MatchAlgorithm matchAlgorithm;
049 private Language language;
050 private boolean loadSourceCodeSlices = true;
051 private String encoding = System.getProperty("file.encoding");
052
053 public CPD(int minimumTileSize, Language language) {
054 TokenEntry.clearImages(); // workaround for bug 1947823
055 this.minimumTileSize = minimumTileSize;
056 this.language = language;
057 }
058
059 public void setCpdListener(CPDListener cpdListener) {
060 this.listener = cpdListener;
061 }
062
063 public void setEncoding(String encoding) {
064 this.encoding = encoding;
065 }
066
067 public void setLoadSourceCodeSlices(boolean loadSourceCodeSlices) {
068 this.loadSourceCodeSlices = loadSourceCodeSlices;
069 }
070
071 public void go() {
072 TokenEntry.clearImages();
073 matchAlgorithm = new MatchAlgorithm(source, tokens, minimumTileSize, listener);
074 matchAlgorithm.setLoadSourceCodeSlices(loadSourceCodeSlices);
075 matchAlgorithm.findMatches();
076 }
077
078 public Iterator<Match> getMatches() {
079 return matchAlgorithm.matches();
080 }
081
082 public void add(File file) throws IOException {
083 add(1, file);
084 }
085
086 public void addAllInDirectory(String dir) throws IOException {
087 addDirectory(dir, false);
088 }
089
090 public void addRecursively(String dir) throws IOException {
091 addDirectory(dir, true);
092 }
093
094 public void add(List<File> files) throws IOException {
095 for (File f : files) {
096 add(files.size(), f);
097 }
098 }
099
100 private void addDirectory(String dir, boolean recurse) throws IOException {
101 if (!(new File(dir)).exists()) {
102 throw new FileNotFoundException("Couldn't find directory " + dir);
103 }
104 FileFinder finder = new FileFinder();
105 add(finder.findFilesFrom(dir, language.getFileFilter(), recurse));
106 }
107
108 private void add(int fileCount, File file) throws IOException {
109 if (!file.getCanonicalPath().equals(new File(file.getAbsolutePath()).getCanonicalPath())) {
110 System.out.println("Skipping " + file + " since it appears to be a symlink");
111 return;
112 }
113
114 listener.addedFile(fileCount, file);
115 SourceCode sourceCode = new SourceCode(new FileCodeLoaderWithoutCache(file, encoding));
116 language.getTokenizer().tokenize(sourceCode, tokens);
117 source.put(sourceCode.getFileName(), sourceCode);
118 }
119
120 }