refactory: extract util package

This commit is contained in:
Gang ZHANG 2020-06-11 20:34:52 +08:00
parent 2ca9e1d9c9
commit ae52007222
44 changed files with 47 additions and 683 deletions

View File

@ -81,6 +81,12 @@
</build>
<dependencies>
<dependency>
<groupId>cn.emergentdesign.se</groupId>
<artifactId>utils</artifactId>
<version>0.0.1</version>
</dependency>
<dependency>
<groupId>eclipse-photon</groupId>
<artifactId>org.eclipse.core.resources</artifactId>

View File

@ -36,20 +36,20 @@ import depends.extractor.LangProcessorRegistration;
import depends.extractor.UnsolvedBindings;
import depends.format.DependencyDumper;
import depends.format.detail.UnsolvedSymbolDumper;
import depends.format.path.DotPathFilenameWritter;
import depends.format.path.EmptyFilenameWritter;
import depends.format.path.FilenameWritter;
import depends.format.path.UnixPathFilenameWritter;
import depends.format.path.WindowsPathFilenameWritter;
import multilang.depends.util.file.path.DotPathFilenameWritter;
import multilang.depends.util.file.path.EmptyFilenameWritter;
import multilang.depends.util.file.path.FilenameWritter;
import multilang.depends.util.file.path.UnixPathFilenameWritter;
import multilang.depends.util.file.path.WindowsPathFilenameWritter;
import depends.generator.DependencyGenerator;
import depends.generator.FileDependencyGenerator;
import depends.generator.FunctionDependencyGenerator;
import depends.matrix.core.DependencyMatrix;
import depends.matrix.transform.MatrixLevelReducer;
import depends.matrix.transform.strip.LeadingNameStripper;
import depends.util.FileUtil;
import depends.util.FolderCollector;
import depends.util.TemporaryFile;
import multilang.depends.util.file.strip.LeadingNameStripper;
import multilang.depends.util.file.FileUtil;
import multilang.depends.util.file.FolderCollector;
import multilang.depends.util.file.TemporaryFile;
import edu.emory.mathcs.backport.java.util.Arrays;
import net.sf.ehcache.CacheManager;
import picocli.CommandLine;

View File

@ -40,7 +40,7 @@ import org.slf4j.LoggerFactory;
import depends.entity.repo.EntityRepo;
import depends.relations.Inferer;
import depends.util.TemporaryFile;
import multilang.depends.util.file.TemporaryFile;
/**
* ContainerEntity for example file, class, method, etc. they could contain

View File

@ -46,8 +46,8 @@ import depends.matrix.core.DependencyMatrix;
import depends.matrix.transform.OrderedMatrixGenerator;
import depends.relations.ImportLookupStrategy;
import depends.relations.Inferer;
import depends.util.FileTraversal;
import depends.util.FileUtil;
import multilang.depends.util.file.FileTraversal;
import multilang.depends.util.file.FileUtil;
abstract public class AbstractLangProcessor {
/**

View File

@ -11,7 +11,7 @@ import java.util.Map;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroDefinition;
import depends.entity.repo.EntityRepo;
import depends.util.TemporaryFile;
import multilang.depends.util.file.TemporaryFile;
public class MacroFileRepo extends MacroRepo{
private EntityRepo entityRepo;

View File

@ -1,19 +1,9 @@
package depends.extractor.cpp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroDefinition;
import org.eclipse.cdt.core.dom.ast.IMacroBinding;
import org.eclipse.cdt.core.parser.IScanner;
import org.eclipse.cdt.core.parser.NullLogService;
import org.eclipse.cdt.core.parser.ParserMode;
import org.eclipse.cdt.internal.core.dom.parser.AbstractGNUSourceCodeParser;
import org.eclipse.cdt.internal.core.dom.parser.cpp.GNUCPPSourceParser;
import depends.extractor.cpp.cdt.GPPParserExtensionConfigurationExtension;
import depends.util.FileUtil;
public class MacroMemoryRepo extends MacroRepo{

View File

@ -13,7 +13,7 @@ import org.eclipse.cdt.internal.core.dom.parser.AbstractGNUSourceCodeParser;
import org.eclipse.cdt.internal.core.dom.parser.cpp.GNUCPPSourceParser;
import depends.extractor.cpp.cdt.GPPParserExtensionConfigurationExtension;
import depends.util.FileUtil;
import multilang.depends.util.file.FileUtil;
public abstract class MacroRepo {
private Map<String, String> defaultMacroMap = new HashMap<>();

View File

@ -36,7 +36,7 @@ import depends.entity.repo.EntityRepo;
import depends.extractor.cpp.CppFileParser;
import depends.extractor.cpp.MacroRepo;
import depends.relations.Inferer;
import depends.util.FileUtil;
import multilang.depends.util.file.FileUtil;
public class CdtCppFileParser extends CppFileParser {

View File

@ -30,15 +30,14 @@ import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import org.codehaus.plexus.util.StringUtils;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorStatement;
import org.eclipse.cdt.internal.core.parser.scanner.ScannerUtility;
import depends.util.FileTraversal;
import depends.util.FileTraversal.IFileVisitor;
import depends.util.FileUtil;
import multilang.depends.util.file.FileTraversal;
import multilang.depends.util.file.FileTraversal.IFileVisitor;
import multilang.depends.util.file.FileUtil;
public class PreprocessorHandler {
private List<String> includePaths;

View File

@ -28,7 +28,7 @@ import java.io.File;
import java.util.List;
import depends.entity.GenericName;
import depends.util.FileUtil;
import multilang.depends.util.file.FileUtil;
public class PomCoords {
public PomCoords() {

View File

@ -40,7 +40,7 @@ import depends.extractor.FileParser;
import depends.extractor.xml.XMLLexer;
import depends.extractor.xml.XMLParser;
import depends.relations.Inferer;
import depends.util.FileUtil;
import multilang.depends.util.file.FileUtil;
public class PomFileParser implements FileParser {

View File

@ -27,7 +27,7 @@ package depends.extractor.pom;
import java.io.File;
import java.util.List;
import depends.util.FileUtil;
import multilang.depends.util.file.FileUtil;
public class PomLocator {

View File

@ -7,7 +7,6 @@ import java.util.List;
import org.antlr.v4.runtime.ParserRuleContext;
import depends.entity.AliasEntity;
import depends.entity.ContainerEntity;
import depends.entity.DecoratedEntity;
import depends.entity.Entity;
@ -43,7 +42,7 @@ import depends.extractor.python.PythonParserBaseListener;
import depends.extractor.ruby.IncludedFileLocator;
import depends.importtypes.FileImport;
import depends.relations.Inferer;
import depends.util.FileUtil;
import multilang.depends.util.file.FileUtil;
public class PythonCodeListener extends PythonParserBaseListener{
private PythonHandlerContext context;

View File

@ -27,7 +27,7 @@ package depends.extractor.ruby;
import java.io.File;
import java.util.List;
import depends.util.FileUtil;
import multilang.depends.util.file.FileUtil;
public class IncludedFileLocator {
private List<String> includesPath;

View File

@ -24,7 +24,6 @@ SOFTWARE.
package depends.extractor.ruby;
import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.ExecutorService;
@ -36,7 +35,7 @@ import depends.extractor.HandlerContext;
import depends.extractor.ParserCreator;
import depends.importtypes.FileImport;
import depends.relations.Inferer;
import depends.util.FileUtil;
import multilang.depends.util.file.FileUtil;
public class RubyHandlerContext extends HandlerContext {

View File

@ -31,7 +31,6 @@ import java.util.concurrent.ExecutorService;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.jrubyparser.CompatVersion;
import org.jrubyparser.NodeVisitor;
import org.jrubyparser.Parser;
import org.jrubyparser.ast.Node;
import org.jrubyparser.parser.ParserConfiguration;
@ -43,7 +42,7 @@ import depends.extractor.FileParser;
import depends.extractor.ParserCreator;
import depends.extractor.ruby.IncludedFileLocator;
import depends.relations.Inferer;
import depends.util.FileUtil;
import multilang.depends.util.file.FileUtil;
public class JRubyFileParser implements FileParser {
private String fileFullPath;
private EntityRepo entityRepo;

View File

@ -36,7 +36,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import depends.extractor.UnsolvedBindings;
import depends.matrix.transform.strip.LeadingNameStripper;
import multilang.depends.util.file.strip.LeadingNameStripper;
public class UnsolvedSymbolDumper{
private Set<UnsolvedBindings> unsolved;

View File

@ -1,43 +0,0 @@
/*
MIT License
Copyright (c) 2018-2019 Gang ZHANG
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package depends.format.path;
public class DotPathFilenameWritter implements FilenameWritter {
@Override
public String reWrite(String originalPath) {
String[] segments = originalPath.split("[/\\\\]");
StringBuilder sb = new StringBuilder();
for(String segment:segments) {
if (sb.length()>0)
sb.append(".");
sb.append(segment.replace(".", "_"));
}
if (sb.toString().startsWith("_")) {
return sb.toString().substring(1);
}
return sb.toString();
}
}

View File

@ -1,32 +0,0 @@
/*
MIT License
Copyright (c) 2018-2019 Gang ZHANG
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package depends.format.path;
public class EmptyFilenameWritter implements FilenameWritter {
@Override
public String reWrite(String originalPath) {
return originalPath;
}
}

View File

@ -1,30 +0,0 @@
/*
MIT License
Copyright (c) 2018-2019 Gang ZHANG
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package depends.format.path;
public interface FilenameWritter {
String reWrite(String originalPath);
}

View File

@ -1,8 +0,0 @@
package depends.format.path;
public class UnixPathFilenameWritter implements FilenameWritter{
@Override
public String reWrite(String originalPath) {
return originalPath.replaceAll("\\\\", "/");
}
}

View File

@ -1,8 +0,0 @@
package depends.format.path;
public class WindowsPathFilenameWritter implements FilenameWritter{
@Override
public String reWrite(String originalPath) {
return originalPath.replaceAll("/","\\\\");
}
}

View File

@ -29,12 +29,12 @@ import java.util.List;
import depends.entity.Entity;
import depends.entity.PackageNamePrefixRemover;
import depends.entity.repo.EntityRepo;
import depends.format.path.EmptyFilenameWritter;
import depends.format.path.FilenameWritter;
import multilang.depends.util.file.path.EmptyFilenameWritter;
import multilang.depends.util.file.path.FilenameWritter;
import depends.matrix.core.DependencyDetail;
import depends.matrix.core.DependencyMatrix;
import depends.matrix.transform.strip.EmptyLeadingNameStripper;
import depends.matrix.transform.strip.ILeadingNameStrippper;
import multilang.depends.util.file.strip.EmptyLeadingNameStripper;
import multilang.depends.util.file.strip.ILeadingNameStrippper;
public abstract class DependencyGenerator {
public abstract DependencyMatrix build(EntityRepo entityRepo,List<String> typeFilter);

View File

@ -29,7 +29,7 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import depends.format.path.FilenameWritter;
import multilang.depends.util.file.path.FilenameWritter;
public class DependencyMatrix {
private HashMap<String, DependencyPair> dependencyPairs = new HashMap<>();

View File

@ -1,31 +0,0 @@
/*
MIT License
Copyright (c) 2018-2019 Gang ZHANG
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package depends.matrix.transform.strip;
public class EmptyLeadingNameStripper implements ILeadingNameStrippper {
@Override
public String stripFilename(String path) {
return path;
}
}

View File

@ -1,30 +0,0 @@
/*
MIT License
Copyright (c) 2018-2019 Gang ZHANG
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package depends.matrix.transform.strip;
public interface ILeadingNameStrippper {
String stripFilename(String path);
}

View File

@ -1,58 +0,0 @@
/*
MIT License
Copyright (c) 2018-2019 Gang ZHANG
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package depends.matrix.transform.strip;
import java.io.File;
import depends.util.FileUtil;
public class LeadingNameStripper implements ILeadingNameStrippper {
String leadingSrcPath;
private String[] additionalStripPaths;
private boolean isStripLeadingPath;
public LeadingNameStripper(boolean isStripLeadingPath, String leadingSrcPath, String[] additionalStripPaths) {
this.isStripLeadingPath = isStripLeadingPath;
this.leadingSrcPath = leadingSrcPath;
this.additionalStripPaths = additionalStripPaths;
}
@Override
public String stripFilename(String path) {
for (String p:additionalStripPaths) {
String prefix = p;
if (isStripLeadingPath)
p = leadingSrcPath + File.separator + p;
prefix = FileUtil.uniqFilePath(prefix);
if (path.startsWith(prefix)) {
return path.substring(prefix.length());
}
}
if (path.startsWith(leadingSrcPath) && isStripLeadingPath) {
path = path.substring(leadingSrcPath.length());
}
return path;
}
}

View File

@ -1,103 +0,0 @@
/*
MIT License
Copyright (c) 2018-2019 Gang ZHANG
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package depends.util;
import java.io.File;
import java.util.ArrayList;
/**
* Recursively visit every file in the given root path using the
* extended IFileVisitor
*
*/
public class FileTraversal {
/**
* The visitor interface
* Detail operation should be implemented here
*/
public interface IFileVisitor {
void visit(File file);
}
IFileVisitor visitor;
private ArrayList<String> extensionFilters = new ArrayList<>();
boolean shouldVisitDirectory = false;
boolean shouldVisitFile = true;
public FileTraversal(IFileVisitor visitor){
this.visitor = visitor;
}
public FileTraversal(IFileVisitor visitor,boolean shouldVisitDirectory,boolean shouldVisitFile){
this.visitor = visitor;
this.shouldVisitDirectory = shouldVisitDirectory;
this.shouldVisitFile = shouldVisitFile;
}
public void travers(String path) {
File dir = new File(path);
travers(dir);
}
public void travers(File root) {
File[] files = root.listFiles();
if (files == null)
return;
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
travers(files[i]);
if (shouldVisitDirectory) {
invokeVisitor(files[i]);
}
} else {
if (shouldVisitFile) {
invokeVisitor( files[i]);
}
}
}
}
private void invokeVisitor(File f) {
if (extensionFilters.size()==0) {
visitor.visit(f);
}else {
for (String ext:extensionFilters) {
if (f.getAbsolutePath().toLowerCase().endsWith(ext.toLowerCase())) {
visitor.visit(f);
}
}
}
}
public FileTraversal extensionFilter(String ext) {
this.extensionFilters.add(ext.toLowerCase());
return this;
}
public void extensionFilter(String[] fileSuffixes) {
for (String fileSuffix:fileSuffixes){
extensionFilter(fileSuffix);
}
}
}

View File

@ -1,80 +0,0 @@
/*
MIT License
Copyright (c) 2018-2019 Gang ZHANG
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package depends.util;
import java.io.File;
import java.io.IOException;
import java.util.Stack;
public class FileUtil {
public static String uniqFilePath(String filePath) {
try {
File f = new File(filePath);
filePath = f.getCanonicalPath();
} catch (IOException e) {
}
return filePath;
}
public static boolean existFile(String path) {
return new File(path).exists();
}
public static boolean isDirectory(String path) {
return new File(path).isDirectory();
}
public static String getLocatedDir(String filepath) {
File file = new File(filepath);
if (!file.exists()) return null;
return file.getParent();
}
public static String getShortFileName(String path) {
return new File(path).getName();
}
public static String uniformPath(String path) {
String[] paths = path.split("[/|\\\\]");
StringBuilder sb = new StringBuilder();
Stack<String> pathStack = new Stack<>();
for (int i=0;i<paths.length;i++) {
String s = paths[i];
if (s.equals(".")) continue;
if (s.equals("..") && !pathStack.empty()) {
pathStack.pop();
continue;
}
pathStack.push(s);
}
for (int i=0;i<pathStack.size();i++) {
sb.append(pathStack.get(i));
if (i<pathStack.size()-1)
sb.append(File.separator);
}
return sb.toString();
}
}

View File

@ -1,57 +0,0 @@
/*
MIT License
Copyright (c) 2018-2019 Gang ZHANG
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package depends.util;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import depends.util.FileTraversal.IFileVisitor;
public class FolderCollector implements IFileVisitor {
List<String> folders;
FileTraversal fileTransversal ;
public FolderCollector() {
fileTransversal = new FileTraversal(this,true,false);
}
public List<String> getFolders(String path){
folders = new ArrayList<>();
folders.add(path);
fileTransversal.travers(path);
return folders;
}
@Override
public void visit(File file) {
if (file.isDirectory())
try {
folders.add(file.getCanonicalPath());
} catch (IOException e) {
e.printStackTrace(); //should never happen
}
}
}

View File

@ -1,57 +0,0 @@
package depends.util;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import depends.util.FileTraversal.IFileVisitor;
public class TemporaryFile {
Path tempDirWithPrefix;
private static TemporaryFile _inst = null;
public static void reset() {
_inst=null;
}
public static TemporaryFile getInstance() {
if (_inst==null)
_inst = new TemporaryFile();
return _inst;
}
private TemporaryFile() {
try {
tempDirWithPrefix = Files.createTempDirectory("depends.tmp");
} catch (IOException e) {
e.printStackTrace();
}
}
public String exprPath(Integer id) {
return tempDirWithPrefix.toAbsolutePath().toFile() + File.separator + id + ".expr";
}
public String macroPath(Integer fileId) {
return tempDirWithPrefix.toAbsolutePath().toFile() + File.separator + fileId + ".macros";
}
public void delete() {
if (tempDirWithPrefix==null) return;
IFileVisitor visitor = new IFileVisitor() {
@Override
public void visit(File file) {
try {
Files.deleteIfExists(file.toPath());
} catch (IOException e) {
}
}
};
FileTraversal t = new FileTraversal(visitor,true,true);
t.travers(tempDirWithPrefix.toAbsolutePath().toFile());
try {
Files.deleteIfExists(tempDirWithPrefix);
} catch (IOException e) {
}
}
}

View File

@ -9,7 +9,7 @@ import depends.extractor.ParserTest;
import depends.extractor.cpp.cdt.CdtCppFileParser;
import depends.extractor.cpp.cdt.PreprocessorHandler;
import depends.relations.Inferer;
import depends.util.TemporaryFile;
import multilang.depends.util.file.TemporaryFile;
public abstract class CppParserTest extends ParserTest{
protected EntityRepo repo;

View File

@ -6,14 +6,12 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Test;
import depends.deptypes.DependencyType;
import depends.entity.AliasEntity;
import depends.extractor.cpp.CppFileParser;
import depends.util.FileUtil;
import multilang.depends.util.file.FileUtil;
public class IncludeRelationTest extends CppParserTest{
@Before

View File

@ -3,11 +3,8 @@ package depends.extractor.java;
import depends.entity.repo.EntityRepo;
import depends.entity.repo.InMemoryEntityRepo;
import depends.extractor.ParserTest;
import depends.extractor.java.JavaBuiltInType;
import depends.extractor.java.JavaFileParser;
import depends.extractor.java.JavaImportLookupStrategy;
import depends.relations.Inferer;
import depends.util.TemporaryFile;
import multilang.depends.util.file.TemporaryFile;
public abstract class JavaParserTest extends ParserTest{
protected EntityRepo entityRepo ;

View File

@ -6,7 +6,7 @@ import java.util.List;
import depends.entity.repo.EntityRepo;
import depends.extractor.ParserTest;
import depends.relations.Inferer;
import depends.util.TemporaryFile;
import multilang.depends.util.file.TemporaryFile;
public abstract class MavenParserTest extends ParserTest{

View File

@ -11,7 +11,7 @@ import depends.entity.FileEntity;
import depends.entity.FunctionEntity;
import depends.entity.MultiDeclareEntities;
import depends.extractor.python.union.PythonFileParser;
import depends.util.FileUtil;
import multilang.depends.util.file.FileUtil;
public class PythonImportTest extends PythonParserTest {
@Before

View File

@ -10,8 +10,8 @@ import depends.extractor.ParserTest;
import depends.extractor.python.union.PythonFileParser;
import depends.extractor.python.union.PythonProcessor;
import depends.relations.Inferer;
import depends.util.FileUtil;
import depends.util.TemporaryFile;
import multilang.depends.util.file.FileUtil;
import multilang.depends.util.file.TemporaryFile;
public abstract class PythonParserTest extends ParserTest implements ParserCreator {

View File

@ -12,7 +12,7 @@ import depends.extractor.ParserCreator;
import depends.extractor.ParserTest;
import depends.extractor.ruby.jruby.JRubyFileParser;
import depends.relations.Inferer;
import depends.util.TemporaryFile;
import multilang.depends.util.file.TemporaryFile;
public abstract class RubyParserTest extends ParserTest implements ParserCreator{
protected EntityRepo entityRepo ;
protected Inferer inferer ;

View File

@ -10,7 +10,7 @@ import org.junit.Test;
import depends.entity.ContainerEntity;
import depends.entity.VarEntity;
import depends.extractor.FileParser;
import depends.util.FileUtil;
import multilang.depends.util.file.FileUtil;
public class RubySingletonMethodTest extends RubyParserTest {
@Before

View File

@ -10,7 +10,7 @@ import org.junit.Test;
import depends.entity.ContainerEntity;
import depends.entity.VarEntity;
import depends.extractor.FileParser;
import depends.util.FileUtil;
import multilang.depends.util.file.FileUtil;
public class RubyVarInvocationRecordTest extends RubyParserTest {
@Before

View File

@ -1,19 +0,0 @@
package depends.format.path;
import static org.junit.Assert.*;
import org.junit.Test;
public class DotPathFilenameWritterTest {
@Test
public void testRewriteFilename() {
DotPathFilenameWritter r = new DotPathFilenameWritter();
assertEquals("abc.123_cpp",r.reWrite("/abc/123.cpp"));
assertEquals(".abc.123_cpp",r.reWrite("./abc/123.cpp"));
assertEquals(".abc.123_1_cpp",r.reWrite("./abc/123.1.cpp"));
assertEquals("abc.123_cpp",r.reWrite("\\abc\\123.cpp"));
assertEquals("a_b_c.123_cpp",r.reWrite("\\a.b.c\\123.cpp"));
}
}

View File

@ -1,17 +0,0 @@
package depends.format.path;
import static org.junit.Assert.*;
import org.junit.Test;
public class UnixPathFilenameWritterTest {
@Test
public void testRewriteFilename() {
UnixPathFilenameWritter r = new UnixPathFilenameWritter();
assertEquals("/abc/123.cpp",r.reWrite("/abc/123.cpp"));
assertEquals("/abc/123.cpp",r.reWrite("\\abc\\123.cpp"));
assertEquals("abc/123.cpp",r.reWrite("abc/123.cpp"));
}
}

View File

@ -1,16 +0,0 @@
package depends.format.path;
import static org.junit.Assert.*;
import org.junit.Test;
public class WindowsPathFilenameWritterTest {
@Test
public void testRewriteFilename() {
WindowsPathFilenameWritter r = new WindowsPathFilenameWritter();
assertEquals("\\abc\\123.cpp",r.reWrite("/abc/123.cpp"));
assertEquals("\\abc\\123.cpp",r.reWrite("\\abc\\123.cpp"));
}
}

View File

@ -1,34 +0,0 @@
package depends.util;
import static org.junit.Assert.*;
import org.junit.Test;
public class FileUtilTest {
@Test
public void test_uniform_path_1() {
String r = FileUtil.uniformPath("/home/user/abc/a.cpp");
assertEquals("/home/user/abc/a.cpp",r);
}
@Test
public void test_uniform_path_1w() {
String r = FileUtil.uniformPath("C:\\home\\user\\abc\\a.cpp");
assertEquals("C:/home/user/abc/a.cpp",r);
}
@Test
public void test_uniform_path_2() {
String r = FileUtil.uniformPath("/home/./user/abc/./a.cpp");
assertEquals("/home/user/abc/a.cpp",r);
}
@Test
public void test_uniform_path_3() {
String r = FileUtil.uniformPath("/home/./user/abc/../a.cpp");
assertEquals("/home/user/a.cpp",r);
}
}