我有一个使用Velocity模板和Java生成HTML页面的项目。但是大多数页面都不符合W3C标准。如何验证这些HTML页面并获取日志,告诉我哪些页面上有哪些错误/警告?
然后我可以手动修复错误。我尝试过JTidyFilter,但这对我不起作用。
答案 0 :(得分:5)
答案 1 :(得分:2)
W3C还提供了一个实验性API,可帮助自动验证。他们恳请您限制请求,并提供有关在本地服务器上设置验证器的说明。这肯定是更多的工作,但如果你生成了很多HTML页面,那么自动化验证可能也是有意义的。
答案 2 :(得分:0)
经过广泛的研究和一些代码破解,我已经设法在我的项目中使用了JTidyFilter,现在它工作得非常好。 JTidyFilter位于JTidyServlet中,这是大约五年前写的JTidy子项目。最近他们更新了代码以符合Java 5编译器。我下载了他们的代码,升级了一些依赖项,最重要的是,更改了处理过滤器的JTidyFilter类中的一些行,最后让它在我的项目中很好地工作。
重新格式化HTML时仍然存在一些问题,因为我在使用Firefox HTML验证插件时会看到一两个错误,但大多数页面都会通过验证。
答案 3 :(得分:0)
的官方API
允许自2007年以来通过Markup Validator Web Service API调用本地或远程W3C Checker。
有一个使用Jersey和moxy-Jaxb的Java类解决方案来读取SOAP响应。
这是使用它的maven依赖:
<dependency>
<groupId>com.bitplan</groupId>
<artifactId>w3cValidator</artifactId>
<version>0.0.2</version>
</dependency>
这是试用它的Junit测试:
/**
* 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
显示了如何在Ubuntu Linux系统上运行w3c验证器。