2010-06-23 64 views
5

匹配我有一個生成使用Velocity模板和Java HTML頁面的項目。但大部分頁面不符合W3C標準。我如何驗證這些HTML頁面並獲取日誌,告訴我在哪些頁面上出現什麼錯誤/警告?如何驗證HTML W3C標準

然後我可以手動修復錯誤。我試過JTidyFilter,但這對我不起作用。

回答

0

廣泛的研究和一點點代碼黑客後,我設法在我的項目中使用JTidyFilter,現在正在粉墨登場。 JTidyFilter在JTidyServlet中,這是JTidy在五年前編寫的一個子項目。最近他們已經更新了代碼以符合Java 5編譯器。我下載了它們的代碼,升級了一些依賴關係,最重要的是,改變了處理過濾器的JTidyFilter類中的一些行,並最終在我的項目中很好地工作。

還有在重新格式化HTML一些問題,因爲我能看到一個兩個錯誤,當我使用Firefox的HTML驗證插件,但在其他大多數頁面通過驗證。

2

,還可以從W3C的一個實驗性的API來幫助自動驗證。他們懇請您扼殺請求,並提供有關在本地服務器上設置驗證器的說明。這絕對是更多的工作,但是如果您生成了大量的HTML頁面,那麼也可以使驗證自動化。

http://validator.w3.org/docs/api.html

+1

我很驚訝沒有Java的API。除此之外,我並不想修改我的源代碼來添加API。我想要做的只是在我的J2EE項目中更改配置文件,以便在開發時打開它,當我不打開時關閉它。需要它。 – newguy 2010-06-23 23:22:33

+0

在其他答案中參見Java解決方案 – 2014-09-23 15:50:12

0

官方的API允許自2007年以來

通過標記驗證的Web服務API調用本地或遠程W3C檢查

擁有一個使用Jersey和moxy-Jaxb讀取SOAP響應的Java類解決方案。

這是行家依賴於使用它:

<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驗證器上。