ltd-graph-builder/src/main/java/grafos/Transformador.java

87 lines
2.4 KiB
Java
Raw Normal View History

2019-03-26 20:11:14 +01:00
package grafos;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.comments.Comment;
import com.github.javaparser.ast.visitor.VoidVisitor;
import java.io.File;
import java.io.FileNotFoundException;
2019-03-26 20:11:14 +01:00
public class Transformador {
public static void main(String[] args) throws FileNotFoundException {
File file = new File(args[0]);
if (file.isDirectory())
analyzeDir(file);
else
analyzeFile(file);
}
public static void analyzeDir(File dir) throws FileNotFoundException {
for (File f : dir.listFiles()) {
if (f.isDirectory())
analyzeDir(f);
else if (f.getName().endsWith(".java"))
analyzeFile(f);
}
}
public static void analyzeFile(File file) throws FileNotFoundException {
2019-03-26 20:11:14 +01:00
// Ruta del fichero con el programa que vamos a transformar
String ruta = file.getPath().substring(0, file.getPath().lastIndexOf(".java"));
2019-03-26 20:11:14 +01:00
// Abrimos el fichero original (un ".java")
File original = new File(ruta + ".java");
2019-03-26 20:11:14 +01:00
// Parseamos el fichero original. Se crea una unidad de compilación (un AST).
CompilationUnit cu = JavaParser.parse(original);
2019-03-26 20:11:14 +01:00
quitarComentarios(cu);
2019-03-26 20:11:14 +01:00
// Recorremos el AST
2019-03-26 22:25:52 +01:00
CFG graph = new CFG();
VoidVisitor<CFG> visitador = new Visitador();
visitador.visit(cu, graph);
2019-03-26 22:25:52 +01:00
// Imprimimos el CFG del program
StringBuilder builder = new StringBuilder();
for (String s : graph.toStringList()) {
builder.append(s);
builder.append(";");
System.out.println("ARCO: " + s);
}
String dotInfo = builder.toString();
2019-03-26 20:11:14 +01:00
// Generamos un PDF con el CFG del programa
System.out.print("\nGenerando PDF...");
GraphViz gv = new GraphViz();
gv.addln(gv.start_graph());
gv.add(dotInfo);
gv.addln(gv.end_graph());
String type = "pdf"; // String type = "gif";
// gv.increaseDpi();
gv.decreaseDpi();
gv.decreaseDpi();
gv.decreaseDpi();
gv.decreaseDpi();
File destino_CFG = new File(ruta + "_CFG." + type);
gv.writeGraphToFile(gv.getGraph(gv.getDotSource(), type), destino_CFG);
System.out.println(" PDF generado!");
2019-03-26 20:11:14 +01:00
}
// Elimina todos los comentarios de un nodo y sus hijos
private static void quitarComentarios(Node node) {
2019-03-26 20:11:14 +01:00
node.removeComment();
for (Comment comment : node.getOrphanComments()) {
2019-03-26 20:11:14 +01:00
node.removeOrphanComment(comment);
}
// Do something with the node
for (Node child : node.getChildNodes()) {
quitarComentarios(child);
}
2019-03-26 20:11:14 +01:00
}
2019-03-26 20:11:14 +01:00
}