Oficjalna API w
pozwala zadzwonić do lokalnego lub zdalnego sprawdzania przez W3C Markup Validator Web Service API od 2007 roku
ma jedno rozwiązanie klasy Java, używające Jersey i moxy-Jaxb do odczytywania w odpowiedzi SOAP.
jest to zależność Maven go używać:
<dependency>
<groupId>com.bitplan</groupId>
<artifactId>w3cValidator</artifactId>
<version>0.0.2</version>
</dependency>
Oto test Junit za próbę go:
/**
* 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
pokazuje jak uruchomić na walidatorze w3c w systemie Linux Ubuntu.
Jestem zaskoczony, że nie ma API dla Javy. Poza tym tak naprawdę nie chcę modyfikować mojego kodu źródłowego, aby dodać interfejsy API. Wszystko, co chcę zrobić, to zmienić plik konfiguracyjny w moim projekcie J2EE, aby móc go włączyć w czasie programowania i wyłączyć, gdy tego nie zrobię. potrzebuję tego. – newguy
zobacz rozwiązania Java w innych odpowiedziach –