-
Notifications
You must be signed in to change notification settings - Fork 0
/
Validator.java
70 lines (66 loc) · 2.56 KB
/
Validator.java
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
import java.io.*;
import java.net.*;
import java.net.http.*;
import java.net.http.HttpResponse.*;
import java.net.http.HttpClient.*;
import java.time.Duration;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
public class Validator {
static public String validate(String fileName) throws Exception {
HttpClient client = HttpClient.newBuilder()
.version(Version.HTTP_1_1)
.followRedirects(Redirect.NORMAL)
.connectTimeout(Duration.ofSeconds(20))
.build();
String json = new String(new FileInputStream(fileName).readFully());
var content = new StringContent(json, Encoding.UTF8, "application/json");
var result = client.PostAsync(url, content).Result;
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https:/coderextreme.net/X3DJSONLD/src/main/html/validator.html"))
.build();
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
System.out.println(response.statusCode());
// fix up the HTML
String buffer = response.body()
.replaceAll("async([ />])", "async='true'$1")
.replaceAll("defer([ />])", "defer='true'$1")
.replaceAll("(<meta[^<>]*)/>", "$1>")
.replaceAll("(<meta[^>]*>)", "$1</meta>")
.replaceAll("(<link[^<>]*)/>", "$1>")
.replaceAll("(<link[^>]*>)", "$1</link>")
.replaceAll("(<img[^<>]*)/>", "$1>")
.replaceAll("(<img[^>]*>)", "$1</img>")
.replaceAll("&(amp;){0}", "&$1")
.replaceAll("&amp;", "&")
.replaceAll("<hr>", "<hr/>")
.replaceAll("</body>", "</div></body>")
;
// System.out.println(buffer);
System.out.println("json="+json);
String out = null;
try {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmlDocument = builder.parse(new InputSource(new StringReader(buffer)));
XPath xPath = XPathFactory.newInstance().newXPath();
String expression = "/html/body/text()";
out = xPath.compile(expression).evaluate(xmlDocument, XPathConstants.STRING).toString().trim();
} catch (Exception e) {
out = "Unvalidated "+json;
}
System.out.println(out);
return out;
}
static public void main(String args[]) throws Exception {
for (int a = 0; a < args.length; a++) {
Validator.validate(args[a]);
}
}
}