/* * Copyright 2004-2008 the Seasar Foundation and the Others. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, * either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ package examples.teeda.web.htmlvalidator; import java.io.IOException; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.methods.PostMethod; import org.seasar.teeda.extension.annotation.validator.Required; /** * @author koichik * */ public class HtmlValidatorPage { protected static final Pattern[] patterns = new Pattern[] { Pattern.compile("xmlns:te\\s*=\\s*\"[^\"]+\"", Pattern.MULTILINE), Pattern.compile("xmlns:te\\s*=\\s*'[^']+'", Pattern.MULTILINE), Pattern.compile("te:\\w+\\s*=\"[^\"]+\"", Pattern.MULTILINE), Pattern.compile("te:\\w+\\s*='[^']+'", Pattern.MULTILINE) }; protected static final Pattern pattern = Pattern.compile( "(.*)", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE | Pattern.DOTALL); @Required public String text; public String result; public void doSubmit() throws Exception { String html = getHtmlVocabulary(text); result = validate(html); } protected static String getHtmlVocabulary(String text) { StringBuilder buf = new StringBuilder(text); for (Pattern pattern : patterns) { Matcher m = pattern.matcher(text); while (m.find()) { int start = m.start(); int end = m.end(); for (int i = start; i < end; ++i) { buf.setCharAt(i, ' '); } } } return new String(buf); } protected static String validate(String html) throws HttpException, IOException { PostMethod method = new PostMethod("http://validator.w3.org/check"); method.setParameter("fragment", html); method.setParameter("doctype", "Inline"); HttpClient httpclient = new HttpClient(); try { httpclient.executeMethod(method); String result = method.getResponseBodyAsString(); Matcher m = pattern.matcher(result); if (m.find()) { return m.group(1); } } finally { method.releaseConnection(); } return null; } public static void main(String[] args) throws Exception { System.out.println(validate("")); } }