-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidate.java
More file actions
166 lines (138 loc) · 6.38 KB
/
Copy pathValidate.java
File metadata and controls
166 lines (138 loc) · 6.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package com.textparser.cli;
import com.textparser.JsonUtils;
import com.textparser.TextParser;
import com.textparser.TokenItem;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class Validate {
public static void main(String[] args) {
if (args.length < 2) {
System.out.println("Usage: Validate <definition_file> <directory_or_file>");
System.exit(1);
}
String definitionFile = args[0];
String targetPathStr = args[1];
try {
TextParser parser = TextParser.fromFile(definitionFile);
File targetPath = new File(targetPathStr);
List<String> defaultExts = new ArrayList<>();
if (parser.definition != null && parser.definition.tokens != null) {
defaultExts.add("cfm");
defaultExts.add("cfc");
}
recursiveParseDirectory(parser, definitionFile, targetPath, defaultExts);
} catch (Exception e) {
System.err.println("Error loading definition or executing validation: " + e.getMessage());
e.printStackTrace();
System.exit(1);
}
}
private static void recursiveParseDirectory(TextParser parser, String definitionFile, File targetPath, List<String> defaultExts) {
if (targetPath.isFile()) {
processFile(parser, definitionFile, targetPath);
return;
}
File[] files = targetPath.listFiles();
if (files == null) return;
for (File file : files) {
if (file.isFile()) {
String name = file.getName().toLowerCase();
boolean isTargetExt = name.endsWith(".py") || name.endsWith(".rs") || name.endsWith(".java");
for (String ext : defaultExts) {
if (name.endsWith("." + ext.toLowerCase())) {
isTargetExt = true;
break;
}
}
if (isTargetExt) {
processFile(parser, definitionFile, file);
}
} else if (file.isDirectory()) {
recursiveParseDirectory(parser, definitionFile, file, defaultExts);
}
}
}
private static void processFile(TextParser parser, String definitionFile, File file) {
System.out.print("Comparing " + file.getPath() + "...");
System.out.flush();
try {
byte[] bytes = Files.readAllBytes(file.toPath());
String text = new String(bytes, StandardCharsets.ISO_8859_1);
List<TokenItem> javaTree = parser.parse(text);
ProcessBuilder pb = new ProcessBuilder("bin/textparser", file.getPath(), "--definition", definitionFile, "--json");
Process proc = pb.start();
StringBuilder cJsonSb = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream(), StandardCharsets.UTF_8))) {
String line;
while ((line = reader.readLine()) != null) {
cJsonSb.append(line).append("\n");
}
}
int exitCode = proc.waitFor();
if (exitCode != 0) {
System.out.println(" bin/textparser failed with exit code " + exitCode);
System.exit(1);
}
Object cJsonObj = JsonUtils.parseJson(cJsonSb.toString());
@SuppressWarnings("unchecked")
List<Object> cTree = (List<Object>) cJsonObj;
if (!compareTrees(javaTree, cTree)) {
System.exit(1);
}
System.out.println(" done");
} catch (Exception e) {
System.out.println(" error: " + e.getMessage());
e.printStackTrace();
System.exit(1);
}
}
@SuppressWarnings("unchecked")
private static boolean compareTrees(List<TokenItem> javaTree, List<Object> cTree) {
if (javaTree.size() != cTree.size()) {
System.out.println("\nLength mismatch. Java: " + javaTree.size() + ", C: " + cTree.size());
return false;
}
for (int i = 0; i < javaTree.size(); i++) {
TokenItem javaNode = javaTree.get(i);
Map<String, Object> cNode = (Map<String, Object>) cTree.get(i);
String cId = cNode.get("id") != null ? cNode.get("id").toString() : "";
int cPos = cNode.get("position") != null ? ((Number) cNode.get("position")).intValue() : 0;
int cLen = cNode.get("length") != null ? ((Number) cNode.get("length")).intValue() : 0;
if (!javaNode.id.equals(cId)) {
System.out.println("\nID mismatch.");
System.out.println("ID - Java: " + javaNode.id + ", C: " + cId);
System.out.println("Position - Java: " + javaNode.position + ", C: " + cPos);
System.out.println("Length - Java: " + javaNode.length + ", C: " + cLen);
return false;
}
if (javaNode.position != cPos) {
System.out.println("\nPosition mismatch.");
System.out.println("ID - Java: " + javaNode.id + ", C: " + cId);
System.out.println("Position - Java: " + javaNode.position + ", C: " + cPos);
System.out.println("Length - Java: " + javaNode.length + ", C: " + cLen);
return false;
}
if (javaNode.length != cLen) {
System.out.println("\nLength mismatch. Java: " + javaNode.length + ", C: " + cLen);
System.out.println("ID - Java: " + javaNode.id + ", C: " + cId);
System.out.println("Position - Java: " + javaNode.position + ", C: " + cPos);
System.out.println("Length - Java: " + javaNode.length + ", C: " + cLen);
return false;
}
List<Object> cChildren = cNode.containsKey("children") && cNode.get("children") instanceof List<?> ? (List<Object>) cNode.get("children") : null;
if (cChildren != null && !cChildren.isEmpty()) {
if (!compareTrees(javaNode.children, cChildren)) {
return false;
}
}
}
return true;
}
}