API chính thức tại
phép để gọi một địa phương hoặc từ xa W3C Checker qua Markup Validator Dịch vụ Web API kể từ 2007.
có một giải pháp Java Class duy nhất sử dụng Jersey và moxy-Jaxb để đọc trong phản hồi SOAP.
đây là phụ thuộc maven để sử dụng nó:
<dependency>
<groupId>com.bitplan</groupId>
<artifactId>w3cValidator</artifactId>
<version>0.0.2</version>
</dependency>
Đây là một thử nghiệm Junit cho cố gắng nó:
/**
* the URL of the official W3C Markup Validation service
* if you'd like to run the tests against your own installation you might want to modify this
*/
public static final String url="http://validator.w3.org/check";
/**
* test the w3cValidator interface with some html code
* @throws Exception
*/
@Test
public void testW3CValidator() throws Exception {
String preamble="<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n" +
" \"http://www.w3.org/TR/html4/loose.dtd\">\n"+
"<html>\n"+
" <head>\n"+
" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n"+
" <title>test</title>\n"+
" </head>\n"+
" <body>\n";
String footer=" </body>\n"+
"</html>\n";
String[] htmls = {
preamble+
" <div>\n"+
footer,
"<!DOCTYPE html><html><head><title>test W3CChecker</title></head><body><div></body></html>"
};
int[] expectedErrs={1,2};
int[] expectedWarnings={1,2};
int index=0;
System.out.println("Testing "+htmls.length+" html messages via "+url);
for (String html : htmls) {
W3CValidator checkResult = W3CValidator.check(url, html);
List<ValidationError> errlist = checkResult.body.response.errors.errorlist;
List<ValidationWarning> warnlist = checkResult.body.response.warnings.warninglist;
Object first = errlist.get(0);
assertTrue("if first is a string, than moxy is not activated",first instanceof ValidationError);
//System.out.println(first.getClass().getName());
//System.out.println(first);
System.out.println("Validation result for test "+(index+1)+":");
for (ValidationError err:errlist) {
System.out.println("\t"+err.toString());
}
for (ValidationWarning warn:warnlist) {
System.out.println("\t"+warn.toString());
}
System.out.println();
assertTrue(errlist.size()>=expectedErrs[index]);
assertTrue(warnlist.size()>=expectedWarnings[index]);
index++;
}
} // testW3CValidator
cho thấy làm thế nào để chạy của bạn trên trình xác thực w3c trên hệ thống Ubuntu Linux.
Nguồn
2014-09-22 11:00:00
Tôi ngạc nhiên vì không có API cho Java. Ngoài ra, tôi không thực sự muốn sửa đổi mã nguồn của mình để thêm các API. Tất cả những gì tôi muốn làm là thay đổi tệp cấu hình trong dự án J2EE của mình để tôi có thể bật nó vào lúc phát triển và tắt nó đi khi tôi không làm. cần nó. – newguy
xem các giải pháp Java trong các câu trả lời khác –