1 /*
2 * Copyright (c) 2002-2026 Gargoyle Software Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 * https://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 package org.htmlunit.html.parser;
16
17 import java.net.URL;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22 /**
23 * Listener for messages from the HTML parser. <br>
24 * The classification of problems as warnings or errors is the one of the HTML parser
25 * used by HtmlUnit. The line and column may indicate the position of the problem detected
26 * by the parser. This is only an indication and in some cases the position where
27 * the problem has to be solved is located lines before.
28 * In some cases (when parsing a html snippet), the html content is also available.
29 *
30 * @author Marc Guillemot
31 * @author Ronald Brill
32 */
33 public interface HTMLParserListener {
34
35 /**
36 * Simple implementation of {@link HTMLParserListener} logging the received warnings
37 * and errors in the "org.htmlunit.html.HTMLParserListener" log.<br>
38 * Errors are logged at the error level and warnings at the warning level.
39 */
40 HTMLParserListener LOG_REPORTER = new SimpleHTMLParserListener();
41
42 /**
43 * Called when the HTML parser reports an error.
44 * @param message the description of the problem
45 * @param url the URL of the document in which the problem occurs
46 * @param html the content of the snippet in which the problem occurs
47 * @param line the line of the problem
48 * @param column the column of the problem
49 * @param key the key identifying the "type" of problem
50 */
51 void error(String message, URL url, String html,
52 int line, int column, String key);
53
54 /**
55 * Called when the HTML parser reports a warning.
56 * @param message the description of the problem
57 * @param url the URL of the document in which the problem occurs
58 * @param html the content of the snippet in which the problem occurs
59 * @param line the line of the problem
60 * @param column the column of the problem
61 * @param key the key identifying the "type" of problem
62 */
63 void warning(String message, URL url, String html,
64 int line, int column, String key);
65 }
66
67 /**
68 * Simple implementation of {@link HTMLParserListener} logging the received warnings
69 * and errors in the "org.htmlunit.html.HTMLParserListener" log.<br/>
70 * Errors are logged at the error level and warnings at the warning level.
71 */
72 class SimpleHTMLParserListener implements HTMLParserListener {
73
74 private static final Log LOG = LogFactory.getLog(SimpleHTMLParserListener.class);
75
76 @Override
77 public void error(final String message, final URL url, final String html,
78 final int line, final int column, final String key) {
79 LOG.error(format(message, url, html, line, column));
80 }
81
82 @Override
83 public void warning(final String message, final URL url, final String html,
84 final int line, final int column, final String key) {
85 if (LOG.isWarnEnabled()) {
86 LOG.warn(format(message, url, html, line, column));
87 }
88 }
89
90 private static String format(final String message, final URL url, final String html,
91 final int line, final int column) {
92 final StringBuilder builder = new StringBuilder(message);
93 builder.append(" (")
94 .append(url.toExternalForm())
95 .append(' ')
96 .append(line)
97 .append(':')
98 .append(column);
99 if (null != html) {
100 builder.append(" htmlSnippet: '")
101 .append(html)
102 .append('\'');
103 }
104 builder.append(')');
105 return builder.toString();
106 }
107 }