1 /*
2 * Copyright (c) 2002-2025 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 */
32 public interface HTMLParserListener {
33
34 /**
35 * Simple implementation of {@link HTMLParserListener} logging the received warnings
36 * and errors in the "org.htmlunit.html.HTMLParserListener" log.<br>
37 * Errors are logged at the error level and warnings at the warning level.
38 */
39 HTMLParserListener LOG_REPORTER = new SimpleHTMLParserListener();
40
41 /**
42 * Called when the HTML parser reports an error.
43 * @param message the description of the problem
44 * @param url the URL of the document in which the problem occurs
45 * @param html the content of the snippet in which the problem occurs
46 * @param line the line of the problem
47 * @param column the column of the problem
48 * @param key the key identifying the "type" of problem
49 */
50 void error(String message, URL url, String html,
51 int line, int column, String key);
52
53 /**
54 * Called when the HTML parser reports a warning.
55 * @param message the description of the problem
56 * @param url the URL of the document in which the problem occurs
57 * @param html the content of the snippet in which the problem occurs
58 * @param line the line of the problem
59 * @param column the column of the problem
60 * @param key the key identifying the "type" of problem
61 */
62 void warning(String message, URL url, String html,
63 int line, int column, String key);
64 }
65
66 /**
67 * Simple implementation of {@link HTMLParserListener} logging the received warnings
68 * and errors in the "org.htmlunit.html.HTMLParserListener" log.<br/>
69 * Errors are logged at the error level and warnings at the warning level.
70 */
71 class SimpleHTMLParserListener implements HTMLParserListener {
72
73 private static final Log LOG = LogFactory.getLog(SimpleHTMLParserListener.class);
74
75 @Override
76 public void error(final String message, final URL url, final String html,
77 final int line, final int column, final String key) {
78 LOG.error(format(message, url, html, line, column));
79 }
80
81 @Override
82 public void warning(final String message, final URL url, final String html,
83 final int line, final int column, final String key) {
84 if (LOG.isWarnEnabled()) {
85 LOG.warn(format(message, url, html, line, column));
86 }
87 }
88
89 private static String format(final String message, final URL url, final String html,
90 final int line, final int column) {
91 final StringBuilder builder = new StringBuilder(message);
92 builder.append(" (")
93 .append(url.toExternalForm())
94 .append(' ')
95 .append(line)
96 .append(':')
97 .append(column);
98 if (null != html) {
99 builder.append(" htmlSnippet: '")
100 .append(html)
101 .append('\'');
102 }
103 builder.append(')');
104 return builder.toString();
105 }
106 }